Skip to content

Commit 5ed7a94

Browse files
committed
Auto merge of #124738 - notriddle:notriddle/search-form-js, r=GuillaumeGomez
rustdoc: dedup search form HTML This change constructs the search form HTML using JavaScript, instead of plain HTML. It uses a custom element because - the [parser]'s insert algorithm runs the connected callback synchronously, so we won't get layout jank - it requires very little HTML, so it's a real win in size [parser]: https://html.spec.whatwg.org/multipage/parsing.html#create-an-element-for-the-token This shrinks the standard library by about 60MiB, by my test. There should be no visible changes. Just use less disk space.
2 parents 60a7c19 + 9262816 commit 5ed7a94

File tree

5 files changed

+50
-26
lines changed

5 files changed

+50
-26
lines changed

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

+43
Original file line numberDiff line numberDiff line change
@@ -239,3 +239,46 @@ window.addEventListener("pageshow", ev => {
239239
setTimeout(updateSidebarWidth, 0);
240240
}
241241
});
242+
243+
// Custom elements are used to insert some JS-dependent features into Rustdoc,
244+
// because the [parser] runs the connected callback
245+
// synchronously. It needs to be added synchronously so that nothing below it
246+
// becomes visible until after it's done. Otherwise, you get layout jank.
247+
//
248+
// That's also why this is in storage.js and not main.js.
249+
//
250+
// [parser]: https://html.spec.whatwg.org/multipage/parsing.html
251+
class RustdocSearchElement extends HTMLElement {
252+
constructor() {
253+
super();
254+
}
255+
connectedCallback() {
256+
const rootPath = getVar("root-path");
257+
const currentCrate = getVar("current-crate");
258+
this.innerHTML = `<nav class="sub">
259+
<form class="search-form">
260+
<span></span> <!-- This empty span is a hacky fix for Safari - See #93184 -->
261+
<div id="sidebar-button" tabindex="-1">
262+
<a href="${rootPath}${currentCrate}/all.html" title="show sidebar"></a>
263+
</div>
264+
<input
265+
class="search-input"
266+
name="search"
267+
aria-label="Run search in the documentation"
268+
autocomplete="off"
269+
spellcheck="false"
270+
placeholder="Type ‘S’ or ‘/’ to search, ‘?’ for more options…"
271+
type="search">
272+
<div id="help-button" tabindex="-1">
273+
<a href="${rootPath}help.html" title="help">?</a>
274+
</div>
275+
<div id="settings-menu" tabindex="-1">
276+
<a href="${rootPath}settings.html" title="settings">
277+
Settings
278+
</a>
279+
</div>
280+
</form>
281+
</nav>`;
282+
}
283+
}
284+
window.customElements.define("rustdoc-search", RustdocSearchElement);

src/librustdoc/html/templates/page.html

+3-24
Original file line numberDiff line numberDiff line change
@@ -117,30 +117,9 @@ <h2>Files</h2> {# #}
117117
<div class="sidebar-resizer"></div> {# #}
118118
<main> {# #}
119119
{% if page.css_class != "src" %}<div class="width-limiter">{% endif %}
120-
<nav class="sub"> {# #}
121-
<form class="search-form"> {# #}
122-
<span></span> {# This empty span is a hacky fix for Safari - See #93184 #}
123-
<div id="sidebar-button" tabindex="-1"> {# #}
124-
<a href="{{page.root_path|safe}}{{layout.krate|safe}}/all.html" title="show sidebar"></a> {# #}
125-
</div> {# #}
126-
<input {#+ #}
127-
class="search-input" {#+ #}
128-
name="search" {#+ #}
129-
aria-label="Run search in the documentation" {#+ #}
130-
autocomplete="off" {#+ #}
131-
spellcheck="false" {#+ #}
132-
placeholder="Type ‘S’ or ‘/’ to search, ‘?’ for more options…" {#+ #}
133-
type="search"> {# #}
134-
<div id="help-button" tabindex="-1"> {# #}
135-
<a href="{{page.root_path|safe}}help.html" title="help">?</a> {# #}
136-
</div> {# #}
137-
<div id="settings-menu" tabindex="-1"> {# #}
138-
<a href="{{page.root_path|safe}}settings.html" title="settings"> {# #}
139-
Settings {# #}
140-
</a> {# #}
141-
</div> {# #}
142-
</form> {# #}
143-
</nav> {# #}
120+
{# defined in storage.js to avoid duplicating complex UI across every page #}
121+
{# and because the search form only works if JS is enabled anyway #}
122+
<rustdoc-search></rustdoc-search> {# #}
144123
<section id="main-content" class="content">{{ content|safe }}</section> {# #}
145124
{% if page.css_class != "src" %}</div>{% endif %}
146125
</main> {# #}

src/tools/html-checker/main.rs

+2
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@ fn check_html_file(file: &Path) -> usize {
2929
.arg("-quiet")
3030
.arg("--mute-id") // this option is useful in case we want to mute more warnings
3131
.arg("yes")
32+
.arg("--new-blocklevel-tags")
33+
.arg("rustdoc-search") // custom elements
3234
.arg("--mute")
3335
.arg(&to_mute_s)
3436
.arg(file);

tests/rustdoc-gui/javascript-disabled.goml

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ javascript: false
44

55
go-to: "file://" + |DOC_PATH| + "/test_docs/struct.Foo.html"
66
show-text: true
7-
assert-css: (".sub", {"display": "none"})
7+
assert-false: ".sub"
88

99
// Even though JS is disabled, we should still have themes applied. Links are never black-colored
1010
// if styles are applied so we check that they are not.

tests/rustdoc-gui/sidebar-source-code-display.goml

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ javascript: false
44
go-to: "file://" + |DOC_PATH| + "/src/test_docs/lib.rs.html"
55
// Since the javascript is disabled, there shouldn't be a toggle.
66
wait-for-css: (".sidebar", {"display": "none"})
7-
assert-css: ("#sidebar-button", {"display": "none"})
7+
assert-false: "#sidebar-button"
88

99
// Let's retry with javascript enabled.
1010
javascript: true

0 commit comments

Comments
 (0)