Skip to content

Commit 765fe32

Browse files
committed
Clean up rustdoc JS nits
1 parent 8f24039 commit 765fe32

File tree

2 files changed

+106
-97
lines changed

2 files changed

+106
-97
lines changed

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1266,7 +1266,7 @@ function preLoadCss(cssUrl) {
12661266
}
12671267

12681268
window.rustdocConfigureTooltip = e => {
1269-
e.addEventListener("click", ev => {
1269+
e.onclick = ev => {
12701270
ev.preventDefault();
12711271
ev.stopPropagation();
12721272
e.TOOLTIP_FORCE_VISIBLE = e.TOOLTIP_FORCE_VISIBLE ? false : true;
@@ -1279,7 +1279,7 @@ function preLoadCss(cssUrl) {
12791279
window.CURRENT_TOOLTIP_ELEMENT.onblur = tooltipBlurHandler;
12801280
}
12811281
return false;
1282-
});
1282+
};
12831283
e.onpointerenter = ev => {
12841284
// If this is a synthetic touch event, ignore it. A click event will be along shortly.
12851285
if (ev.pointerType !== "mouse") {

src/librustdoc/html/static/js/search.js

Lines changed: 104 additions & 95 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,20 @@ function takeFromArray(array, count) {
2424
return result;
2525
}
2626

27+
function onEachBtwn(arr, func, funcBtwn) {
28+
let i = 0;
29+
for (const value of arr) {
30+
if (i !== 0) {
31+
funcBtwn(value, i);
32+
}
33+
if (func(value, i)) {
34+
return true;
35+
}
36+
i += 1;
37+
}
38+
return false;
39+
}
40+
2741
(function() {
2842
// This mapping table should match the discriminants of
2943
// `rustdoc::formats::item_type::ItemType` type in Rust.
@@ -1575,6 +1589,49 @@ function initSearch(rawSearchIndex) {
15751589
pushText({name: ")", highlighted: false}, result);
15761590
}
15771591
}
1592+
/**
1593+
* Write a primitive type with special syntax, like `!` or `[T]`.
1594+
* Returns `false` if the supplied type isn't special.
1595+
*
1596+
* @param {FunctionType} fnType
1597+
* @param {[string]} result
1598+
*/
1599+
function writeSpecialPrimitive(fnType, result) {
1600+
if (fnType.id === typeNameIdOfArray || fnType.id === typeNameIdOfSlice ||
1601+
fnType.id === typeNameIdOfTuple || fnType.id === typeNameIdOfUnit) {
1602+
const [ob, sb] =
1603+
fnType.id === typeNameIdOfArray || fnType.id === typeNameIdOfSlice ?
1604+
["[", "]"] :
1605+
["(", ")"];
1606+
pushText({ name: ob, highlighted: fnType.highlighted }, result);
1607+
onEachBtwn(
1608+
fnType.generics,
1609+
nested => writeFn(nested, result),
1610+
() => pushText({ name: ", ", highlighted: false }, result),
1611+
);
1612+
pushText({ name: sb, highlighted: fnType.highlighted }, result);
1613+
return true;
1614+
} else if (fnType.id === typeNameIdOfReference) {
1615+
pushText({ name: "&", highlighted: fnType.highlighted }, result);
1616+
let prevHighlighted = false;
1617+
onEachBtwn(
1618+
fnType.generics,
1619+
value => {
1620+
prevHighlighted = value.highlighted;
1621+
writeFn(value, result);
1622+
},
1623+
value => pushText({
1624+
name: " ",
1625+
highlighted: prevHighlighted && value.highlighted,
1626+
}, result),
1627+
);
1628+
return true;
1629+
} else if (fnType.id === typeNameIdOfFn) {
1630+
writeHof(fnType, result);
1631+
return true;
1632+
}
1633+
return false;
1634+
}
15781635
/**
15791636
* Write a type. This function checks for special types,
15801637
* like slices, with their own formatting. It also handles
@@ -1602,55 +1659,17 @@ function initSearch(rawSearchIndex) {
16021659
highlighted: !!fnType.highlighted,
16031660
}, result);
16041661
const where = [];
1605-
let first = true;
1606-
for (const nested of fnType.generics) {
1607-
if (first) {
1608-
first = false;
1609-
} else {
1610-
pushText({ name: " + ", highlighted: false }, where);
1611-
}
1612-
writeFn(nested, where);
1613-
}
1662+
onEachBtwn(
1663+
fnType.generics,
1664+
nested => writeFn(nested, where),
1665+
() => pushText({ name: " + ", highlighted: false }, where),
1666+
);
16141667
if (where.length > 0) {
16151668
whereClause.set(fnParamNames[-1 - fnType.id], where);
16161669
}
16171670
} else {
16181671
if (fnType.ty === TY_PRIMITIVE) {
1619-
if (fnType.id === typeNameIdOfArray || fnType.id === typeNameIdOfSlice ||
1620-
fnType.id === typeNameIdOfTuple || fnType.id === typeNameIdOfUnit) {
1621-
const [ob, sb] =
1622-
fnType.id === typeNameIdOfArray || fnType.id === typeNameIdOfSlice ?
1623-
["[", "]"] :
1624-
["(", ")"];
1625-
pushText({ name: ob, highlighted: fnType.highlighted }, result);
1626-
let needsComma = false;
1627-
for (const value of fnType.generics) {
1628-
if (needsComma) {
1629-
pushText({ name: ", ", highlighted: false }, result);
1630-
}
1631-
needsComma = true;
1632-
writeFn(value, result);
1633-
}
1634-
pushText({ name: sb, highlighted: fnType.highlighted }, result);
1635-
return;
1636-
} else if (fnType.id === typeNameIdOfReference) {
1637-
pushText({ name: "&", highlighted: fnType.highlighted }, result);
1638-
let needsSpace = false;
1639-
let prevHighlighted = false;
1640-
for (const value of fnType.generics) {
1641-
if (needsSpace) {
1642-
pushText({
1643-
name: " ",
1644-
highlighted: prevHighlighted && value.highlighted,
1645-
}, result);
1646-
}
1647-
needsSpace = true;
1648-
prevHighlighted = value.highlighted;
1649-
writeFn(value, result);
1650-
}
1651-
return;
1652-
} else if (fnType.id === typeNameIdOfFn) {
1653-
writeHof(fnType, result);
1672+
if (writeSpecialPrimitive(fnType, result)) {
16541673
return;
16551674
}
16561675
} else if (fnType.ty === TY_TRAIT &&
@@ -1661,66 +1680,55 @@ function initSearch(rawSearchIndex) {
16611680
return;
16621681
}
16631682
pushText(fnType, result);
1664-
if (fnType.bindings.length > 0 || fnType.generics.length > 0) {
1683+
if (fnType.bindings.size > 0 || fnType.generics.length > 0) {
16651684
pushText({ name: "<", highlighted: false }, result);
16661685
}
1667-
let needsComma = false;
1668-
if (fnType.bindings.length > 0) {
1669-
for (const [key, values] of fnType.bindings) {
1670-
if (needsComma) {
1671-
pushText({ name: ", ", highlighted: false }, result);
1672-
}
1673-
needsComma = true;
1674-
writeFn(key, result);
1675-
pushText({
1676-
name: values.length > 1 ? "=(" : "=",
1677-
highlighted: false,
1678-
}, result);
1679-
let needsPlus = false;
1680-
for (const value of values) {
1681-
if (needsPlus) {
1682-
pushText({ name: " + ", highlighted: false }, result);
1686+
if (fnType.bindings.size > 0) {
1687+
onEachBtwn(
1688+
fnType.bindings,
1689+
([key, values]) => {
1690+
pushText(key, result);
1691+
pushText({
1692+
name: values.length > 1 ? "=(" : "=",
1693+
highlighted: false,
1694+
}, result);
1695+
onEachBtwn(
1696+
values,
1697+
value => writeFn(value, result),
1698+
() => pushText({ name: " + ", highlighted: false }, result),
1699+
);
1700+
if (values.length > 1) {
1701+
pushText({ name: ")", highlighted: false }, result);
16831702
}
1684-
needsPlus = true;
1685-
writeFn(value, result);
1686-
}
1687-
if (values.length > 1) {
1688-
pushText({ name: ")", highlighted: false }, result);
1689-
}
1690-
}
1703+
},
1704+
() => pushText({ name: ", ", highlighted: false }, result),
1705+
);
16911706
}
1692-
if (fnType.generics.length > 0) {
1693-
for (const value of fnType.generics) {
1694-
if (needsComma) {
1695-
pushText({ name: ", ", highlighted: false }, result);
1696-
}
1697-
needsComma = true;
1698-
writeFn(value, result);
1699-
}
1707+
if (fnType.bindings.size > 0 && fnType.generics.length > 0) {
1708+
pushText({ name: ", ", highlighted: false }, result);
17001709
}
1701-
if (fnType.bindings.length > 0 || fnType.generics.length > 0) {
1710+
onEachBtwn(
1711+
fnType.generics,
1712+
value => writeFn(value, result),
1713+
() => pushText({ name: ", ", highlighted: false }, result),
1714+
);
1715+
if (fnType.bindings.size > 0 || fnType.generics.length > 0) {
17021716
pushText({ name: ">", highlighted: false }, result);
17031717
}
17041718
}
17051719
}
17061720
const type = [];
1707-
let needsComma = false;
1708-
for (const fnType of fnInputs) {
1709-
if (needsComma) {
1710-
pushText({ name: ", ", highlighted: false }, type);
1711-
}
1712-
needsComma = true;
1713-
writeFn(fnType, type);
1714-
}
1721+
onEachBtwn(
1722+
fnInputs,
1723+
fnType => writeFn(fnType, type),
1724+
() => pushText({ name: ", ", highlighted: false }, type),
1725+
);
17151726
pushText({ name: " -> ", highlighted: false }, type);
1716-
needsComma = false;
1717-
for (const fnType of fnOutput) {
1718-
if (needsComma) {
1719-
pushText({ name: ", ", highlighted: false }, type);
1720-
}
1721-
needsComma = true;
1722-
writeFn(fnType, type);
1723-
}
1727+
onEachBtwn(
1728+
fnOutput,
1729+
fnType => writeFn(fnType, type),
1730+
() => pushText({ name: ", ", highlighted: false }, type),
1731+
);
17241732

17251733
return {type, mappedNames, whereClause};
17261734
}
@@ -3074,7 +3082,9 @@ function initSearch(rawSearchIndex) {
30743082
async function addTab(array, query, display) {
30753083
const extraClass = display ? " active" : "";
30763084

3077-
let output = document.createElement("ul");
3085+
const output = document.createElement(
3086+
array.length === 0 && query.error === null ? "div" : "ul",
3087+
);
30783088
if (array.length > 0) {
30793089
output.className = "search-results " + extraClass;
30803090

@@ -3193,7 +3203,6 @@ ${item.displayPath}<span class="${type}">${name}</span>\
31933203
}
31943204
});
31953205
} else if (query.error === null) {
3196-
output = document.createElement("div");
31973206
output.className = "search-failed" + extraClass;
31983207
output.innerHTML = "No results :(<br/>" +
31993208
"Try on <a href=\"https://duckduckgo.com/?q=" +

0 commit comments

Comments
 (0)