Skip to content

Commit 596e9dd

Browse files
committed
typecheck window.CURRENT_TOOLTIP_ELEMENT
1 parent 6c22ef5 commit 596e9dd

File tree

3 files changed

+4
-32
lines changed

3 files changed

+4
-32
lines changed

src/librustdoc/html/static/js/main.js

Lines changed: 2 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1275,13 +1275,11 @@ function preLoadCss(cssUrl) {
12751275
}
12761276

12771277
window.addEventListener("resize", () => {
1278-
// @ts-expect-error
12791278
if (window.CURRENT_TOOLTIP_ELEMENT) {
12801279
// As a workaround to the behavior of `contains: layout` used in doc togglers,
12811280
// tooltip popovers are positioned using javascript.
12821281
//
12831282
// This means when the window is resized, we need to redo the layout.
1284-
// @ts-expect-error
12851283
const base = window.CURRENT_TOOLTIP_ELEMENT.TOOLTIP_BASE;
12861284
const force_visible = base.TOOLTIP_FORCE_VISIBLE;
12871285
hideTooltip(false);
@@ -1337,14 +1335,12 @@ function preLoadCss(cssUrl) {
13371335
}
13381336
// Make this function idempotent. If the tooltip is already shown, avoid doing extra work
13391337
// and leave it alone.
1340-
// @ts-expect-error
13411338
if (window.CURRENT_TOOLTIP_ELEMENT && window.CURRENT_TOOLTIP_ELEMENT.TOOLTIP_BASE === e) {
1342-
// @ts-expect-error
13431339
clearTooltipHoverTimeout(window.CURRENT_TOOLTIP_ELEMENT);
13441340
return;
13451341
}
13461342
window.hideAllModals(false);
1347-
const wrapper = document.createElement("div");
1343+
const wrapper = Object.assign(document.createElement("div"), {TOOLTIP_BASE: e});
13481344
if (notable_ty) {
13491345
wrapper.innerHTML = "<div class=\"content\">" +
13501346
// @ts-expect-error
@@ -1390,11 +1386,7 @@ function preLoadCss(cssUrl) {
13901386
);
13911387
}
13921388
wrapper.style.visibility = "";
1393-
// @ts-expect-error
13941389
window.CURRENT_TOOLTIP_ELEMENT = wrapper;
1395-
// @ts-expect-error
1396-
window.CURRENT_TOOLTIP_ELEMENT.TOOLTIP_BASE = e;
1397-
// @ts-expect-error
13981390
clearTooltipHoverTimeout(window.CURRENT_TOOLTIP_ELEMENT);
13991391
wrapper.onpointerenter = ev => {
14001392
// If this is a synthetic touch event, ignore it. A click event will be along shortly.
@@ -1429,19 +1421,15 @@ function preLoadCss(cssUrl) {
14291421
*/
14301422
function setTooltipHoverTimeout(element, show) {
14311423
clearTooltipHoverTimeout(element);
1432-
// @ts-expect-error
14331424
if (!show && !window.CURRENT_TOOLTIP_ELEMENT) {
14341425
// To "hide" an already hidden element, just cancel its timeout.
14351426
return;
14361427
}
1437-
// @ts-expect-error
14381428
if (show && window.CURRENT_TOOLTIP_ELEMENT) {
14391429
// To "show" an already visible element, just cancel its timeout.
14401430
return;
14411431
}
1442-
// @ts-expect-error
14431432
if (window.CURRENT_TOOLTIP_ELEMENT &&
1444-
// @ts-expect-error
14451433
window.CURRENT_TOOLTIP_ELEMENT.TOOLTIP_BASE !== element) {
14461434
// Don't do anything if another tooltip is already visible.
14471435
return;
@@ -1464,7 +1452,6 @@ function preLoadCss(cssUrl) {
14641452
*/
14651453
function clearTooltipHoverTimeout(element) {
14661454
if (element.TOOLTIP_HOVER_TIMEOUT !== undefined) {
1467-
// @ts-expect-error
14681455
removeClass(window.CURRENT_TOOLTIP_ELEMENT, "fade-out");
14691456
clearTimeout(element.TOOLTIP_HOVER_TIMEOUT);
14701457
delete element.TOOLTIP_HOVER_TIMEOUT;
@@ -1473,15 +1460,10 @@ function preLoadCss(cssUrl) {
14731460

14741461
// @ts-expect-error
14751462
function tooltipBlurHandler(event) {
1476-
// @ts-expect-error
14771463
if (window.CURRENT_TOOLTIP_ELEMENT &&
1478-
// @ts-expect-error
14791464
!window.CURRENT_TOOLTIP_ELEMENT.contains(document.activeElement) &&
1480-
// @ts-expect-error
14811465
!window.CURRENT_TOOLTIP_ELEMENT.contains(event.relatedTarget) &&
1482-
// @ts-expect-error
14831466
!window.CURRENT_TOOLTIP_ELEMENT.TOOLTIP_BASE.contains(document.activeElement) &&
1484-
// @ts-expect-error
14851467
!window.CURRENT_TOOLTIP_ELEMENT.TOOLTIP_BASE.contains(event.relatedTarget)
14861468
) {
14871469
// Work around a difference in the focus behaviour between Firefox, Chrome, and Safari.
@@ -1503,30 +1485,22 @@ function preLoadCss(cssUrl) {
15031485
* If set to `false`, leave keyboard focus alone.
15041486
*/
15051487
function hideTooltip(focus) {
1506-
// @ts-expect-error
15071488
if (window.CURRENT_TOOLTIP_ELEMENT) {
1508-
// @ts-expect-error
15091489
if (window.CURRENT_TOOLTIP_ELEMENT.TOOLTIP_BASE.TOOLTIP_FORCE_VISIBLE) {
15101490
if (focus) {
1511-
// @ts-expect-error
15121491
window.CURRENT_TOOLTIP_ELEMENT.TOOLTIP_BASE.focus();
15131492
}
1514-
// @ts-expect-error
15151493
window.CURRENT_TOOLTIP_ELEMENT.TOOLTIP_BASE.TOOLTIP_FORCE_VISIBLE = false;
15161494
}
1517-
// @ts-expect-error
15181495
document.body.removeChild(window.CURRENT_TOOLTIP_ELEMENT);
1519-
// @ts-expect-error
15201496
clearTooltipHoverTimeout(window.CURRENT_TOOLTIP_ELEMENT);
1521-
// @ts-expect-error
1522-
window.CURRENT_TOOLTIP_ELEMENT = null;
1497+
window.CURRENT_TOOLTIP_ELEMENT = undefined;
15231498
}
15241499
}
15251500

15261501
onEachLazy(document.getElementsByClassName("tooltip"), e => {
15271502
e.onclick = () => {
15281503
e.TOOLTIP_FORCE_VISIBLE = e.TOOLTIP_FORCE_VISIBLE ? false : true;
1529-
// @ts-expect-error
15301504
if (window.CURRENT_TOOLTIP_ELEMENT && !e.TOOLTIP_FORCE_VISIBLE) {
15311505
hideTooltip(true);
15321506
} else {
@@ -1562,9 +1536,7 @@ function preLoadCss(cssUrl) {
15621536
if (ev.pointerType !== "mouse") {
15631537
return;
15641538
}
1565-
// @ts-expect-error
15661539
if (!e.TOOLTIP_FORCE_VISIBLE && window.CURRENT_TOOLTIP_ELEMENT &&
1567-
// @ts-expect-error
15681540
!window.CURRENT_TOOLTIP_ELEMENT.contains(ev.relatedTarget)) {
15691541
// Tooltip pointer leave gesture:
15701542
//
@@ -1597,7 +1569,6 @@ function preLoadCss(cssUrl) {
15971569
// * https://www.nngroup.com/articles/tooltip-guidelines/
15981570
// * https://bjk5.com/post/44698559168/breaking-down-amazons-mega-dropdown
15991571
setTooltipHoverTimeout(e, false);
1600-
// @ts-expect-error
16011572
addClass(window.CURRENT_TOOLTIP_ELEMENT, "fade-out");
16021573
}
16031574
};

src/librustdoc/html/static/js/rustdoc.d.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ declare global {
3030
SIDEBAR_ITEMS?: { [key: string]: string[] };
3131
/** Notable trait data */
3232
NOTABLE_TRAITS?: { [key: string]: string };
33+
CURRENT_TOOLTIP_ELEMENT?: HTMLElement & { TOOLTIP_BASE: HTMLElement };
3334
/** Used by the popover tooltip code. */
3435
RUSTDOC_TOOLTIP_HOVER_MS: number;
3536
/** Used by the popover tooltip code. */

src/librustdoc/html/static/js/storage.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ function addClass(elem, className) {
117117
* Remove a class from a DOM Element. If `elem` is null,
118118
* does nothing. This function is idempotent.
119119
*
120-
* @param {Element|null} elem
120+
* @param {Element|null|undefined} elem
121121
* @param {string} className
122122
*/
123123
// eslint-disable-next-line no-unused-vars

0 commit comments

Comments
 (0)