Skip to content

Commit dfec2bb

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

File tree

3 files changed

+7
-32
lines changed

3 files changed

+7
-32
lines changed

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

Lines changed: 5 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,15 @@ 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+
// use Object.assign to make sure the object has the correct type
1344+
// with all of the correct fields before it is assigned to a variable,
1345+
// as typescript has no way to change the type of a variable once it is initialized.
1346+
const wrapper = Object.assign(document.createElement("div"), {TOOLTIP_BASE: e});
13481347
if (notable_ty) {
13491348
wrapper.innerHTML = "<div class=\"content\">" +
13501349
// @ts-expect-error
@@ -1390,11 +1389,7 @@ function preLoadCss(cssUrl) {
13901389
);
13911390
}
13921391
wrapper.style.visibility = "";
1393-
// @ts-expect-error
13941392
window.CURRENT_TOOLTIP_ELEMENT = wrapper;
1395-
// @ts-expect-error
1396-
window.CURRENT_TOOLTIP_ELEMENT.TOOLTIP_BASE = e;
1397-
// @ts-expect-error
13981393
clearTooltipHoverTimeout(window.CURRENT_TOOLTIP_ELEMENT);
13991394
wrapper.onpointerenter = ev => {
14001395
// If this is a synthetic touch event, ignore it. A click event will be along shortly.
@@ -1429,19 +1424,15 @@ function preLoadCss(cssUrl) {
14291424
*/
14301425
function setTooltipHoverTimeout(element, show) {
14311426
clearTooltipHoverTimeout(element);
1432-
// @ts-expect-error
14331427
if (!show && !window.CURRENT_TOOLTIP_ELEMENT) {
14341428
// To "hide" an already hidden element, just cancel its timeout.
14351429
return;
14361430
}
1437-
// @ts-expect-error
14381431
if (show && window.CURRENT_TOOLTIP_ELEMENT) {
14391432
// To "show" an already visible element, just cancel its timeout.
14401433
return;
14411434
}
1442-
// @ts-expect-error
14431435
if (window.CURRENT_TOOLTIP_ELEMENT &&
1444-
// @ts-expect-error
14451436
window.CURRENT_TOOLTIP_ELEMENT.TOOLTIP_BASE !== element) {
14461437
// Don't do anything if another tooltip is already visible.
14471438
return;
@@ -1464,7 +1455,6 @@ function preLoadCss(cssUrl) {
14641455
*/
14651456
function clearTooltipHoverTimeout(element) {
14661457
if (element.TOOLTIP_HOVER_TIMEOUT !== undefined) {
1467-
// @ts-expect-error
14681458
removeClass(window.CURRENT_TOOLTIP_ELEMENT, "fade-out");
14691459
clearTimeout(element.TOOLTIP_HOVER_TIMEOUT);
14701460
delete element.TOOLTIP_HOVER_TIMEOUT;
@@ -1473,15 +1463,10 @@ function preLoadCss(cssUrl) {
14731463

14741464
// @ts-expect-error
14751465
function tooltipBlurHandler(event) {
1476-
// @ts-expect-error
14771466
if (window.CURRENT_TOOLTIP_ELEMENT &&
1478-
// @ts-expect-error
14791467
!window.CURRENT_TOOLTIP_ELEMENT.contains(document.activeElement) &&
1480-
// @ts-expect-error
14811468
!window.CURRENT_TOOLTIP_ELEMENT.contains(event.relatedTarget) &&
1482-
// @ts-expect-error
14831469
!window.CURRENT_TOOLTIP_ELEMENT.TOOLTIP_BASE.contains(document.activeElement) &&
1484-
// @ts-expect-error
14851470
!window.CURRENT_TOOLTIP_ELEMENT.TOOLTIP_BASE.contains(event.relatedTarget)
14861471
) {
14871472
// Work around a difference in the focus behaviour between Firefox, Chrome, and Safari.
@@ -1503,30 +1488,22 @@ function preLoadCss(cssUrl) {
15031488
* If set to `false`, leave keyboard focus alone.
15041489
*/
15051490
function hideTooltip(focus) {
1506-
// @ts-expect-error
15071491
if (window.CURRENT_TOOLTIP_ELEMENT) {
1508-
// @ts-expect-error
15091492
if (window.CURRENT_TOOLTIP_ELEMENT.TOOLTIP_BASE.TOOLTIP_FORCE_VISIBLE) {
15101493
if (focus) {
1511-
// @ts-expect-error
15121494
window.CURRENT_TOOLTIP_ELEMENT.TOOLTIP_BASE.focus();
15131495
}
1514-
// @ts-expect-error
15151496
window.CURRENT_TOOLTIP_ELEMENT.TOOLTIP_BASE.TOOLTIP_FORCE_VISIBLE = false;
15161497
}
1517-
// @ts-expect-error
15181498
document.body.removeChild(window.CURRENT_TOOLTIP_ELEMENT);
1519-
// @ts-expect-error
15201499
clearTooltipHoverTimeout(window.CURRENT_TOOLTIP_ELEMENT);
1521-
// @ts-expect-error
1522-
window.CURRENT_TOOLTIP_ELEMENT = null;
1500+
window.CURRENT_TOOLTIP_ELEMENT = undefined;
15231501
}
15241502
}
15251503

15261504
onEachLazy(document.getElementsByClassName("tooltip"), e => {
15271505
e.onclick = () => {
15281506
e.TOOLTIP_FORCE_VISIBLE = e.TOOLTIP_FORCE_VISIBLE ? false : true;
1529-
// @ts-expect-error
15301507
if (window.CURRENT_TOOLTIP_ELEMENT && !e.TOOLTIP_FORCE_VISIBLE) {
15311508
hideTooltip(true);
15321509
} else {
@@ -1562,9 +1539,7 @@ function preLoadCss(cssUrl) {
15621539
if (ev.pointerType !== "mouse") {
15631540
return;
15641541
}
1565-
// @ts-expect-error
15661542
if (!e.TOOLTIP_FORCE_VISIBLE && window.CURRENT_TOOLTIP_ELEMENT &&
1567-
// @ts-expect-error
15681543
!window.CURRENT_TOOLTIP_ELEMENT.contains(ev.relatedTarget)) {
15691544
// Tooltip pointer leave gesture:
15701545
//
@@ -1597,7 +1572,6 @@ function preLoadCss(cssUrl) {
15971572
// * https://www.nngroup.com/articles/tooltip-guidelines/
15981573
// * https://bjk5.com/post/44698559168/breaking-down-amazons-mega-dropdown
15991574
setTooltipHoverTimeout(e, false);
1600-
// @ts-expect-error
16011575
addClass(window.CURRENT_TOOLTIP_ELEMENT, "fade-out");
16021576
}
16031577
};

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)