Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

rustdoc: dedup search form HTML #124738

Merged
merged 2 commits into from
May 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 43 additions & 0 deletions src/librustdoc/html/static/js/storage.js
Original file line number Diff line number Diff line change
Expand Up @@ -239,3 +239,46 @@ window.addEventListener("pageshow", ev => {
setTimeout(updateSidebarWidth, 0);
}
});

// Custom elements are used to insert some JS-dependent features into Rustdoc,
// because the [parser] runs the connected callback
// synchronously. It needs to be added synchronously so that nothing below it
// becomes visible until after it's done. Otherwise, you get layout jank.
//
// That's also why this is in storage.js and not main.js.
//
// [parser]: https://html.spec.whatwg.org/multipage/parsing.html
class RustdocSearchElement extends HTMLElement {
constructor() {
super();
}
connectedCallback() {
const rootPath = getVar("root-path");
const currentCrate = getVar("current-crate");
this.innerHTML = `<nav class="sub">
<form class="search-form">
<span></span> <!-- This empty span is a hacky fix for Safari - See #93184 -->
<div id="sidebar-button" tabindex="-1">
<a href="${rootPath}${currentCrate}/all.html" title="show sidebar"></a>
</div>
<input
class="search-input"
name="search"
aria-label="Run search in the documentation"
autocomplete="off"
spellcheck="false"
placeholder="Type ‘S’ or ‘/’ to search, ‘?’ for more options…"
type="search">
<div id="help-button" tabindex="-1">
<a href="${rootPath}help.html" title="help">?</a>
</div>
<div id="settings-menu" tabindex="-1">
<a href="${rootPath}settings.html" title="settings">
Settings
</a>
</div>
</form>
</nav>`;
}
}
window.customElements.define("rustdoc-search", RustdocSearchElement);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Didn't know you could do that. Pretty nice!

27 changes: 3 additions & 24 deletions src/librustdoc/html/templates/page.html
Original file line number Diff line number Diff line change
Expand Up @@ -117,30 +117,9 @@ <h2>Files</h2> {# #}
<div class="sidebar-resizer"></div> {# #}
<main> {# #}
{% if page.css_class != "src" %}<div class="width-limiter">{% endif %}
<nav class="sub"> {# #}
<form class="search-form"> {# #}
<span></span> {# This empty span is a hacky fix for Safari - See #93184 #}
<div id="sidebar-button" tabindex="-1"> {# #}
<a href="{{page.root_path|safe}}{{layout.krate|safe}}/all.html" title="show sidebar"></a> {# #}
</div> {# #}
<input {#+ #}
class="search-input" {#+ #}
name="search" {#+ #}
aria-label="Run search in the documentation" {#+ #}
autocomplete="off" {#+ #}
spellcheck="false" {#+ #}
placeholder="Type ‘S’ or ‘/’ to search, ‘?’ for more options…" {#+ #}
type="search"> {# #}
<div id="help-button" tabindex="-1"> {# #}
<a href="{{page.root_path|safe}}help.html" title="help">?</a> {# #}
</div> {# #}
<div id="settings-menu" tabindex="-1"> {# #}
<a href="{{page.root_path|safe}}settings.html" title="settings"> {# #}
Settings {# #}
</a> {# #}
</div> {# #}
</form> {# #}
</nav> {# #}
{# defined in storage.js to avoid duplicating complex UI across every page #}
{# and because the search form only works if JS is enabled anyway #}
<rustdoc-search></rustdoc-search> {# #}
<section id="main-content" class="content">{{ content|safe }}</section> {# #}
{% if page.css_class != "src" %}</div>{% endif %}
</main> {# #}
Expand Down
2 changes: 2 additions & 0 deletions src/tools/html-checker/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ fn check_html_file(file: &Path) -> usize {
.arg("-quiet")
.arg("--mute-id") // this option is useful in case we want to mute more warnings
.arg("yes")
.arg("--new-blocklevel-tags")
.arg("rustdoc-search") // custom elements
.arg("--mute")
.arg(&to_mute_s)
.arg(file);
Expand Down
2 changes: 1 addition & 1 deletion tests/rustdoc-gui/javascript-disabled.goml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ javascript: false

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

// Even though JS is disabled, we should still have themes applied. Links are never black-colored
// if styles are applied so we check that they are not.
Expand Down
2 changes: 1 addition & 1 deletion tests/rustdoc-gui/sidebar-source-code-display.goml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ javascript: false
go-to: "file://" + |DOC_PATH| + "/src/test_docs/lib.rs.html"
// Since the javascript is disabled, there shouldn't be a toggle.
wait-for-css: (".sidebar", {"display": "none"})
assert-css: ("#sidebar-button", {"display": "none"})
assert-false: "#sidebar-button"

// Let's retry with javascript enabled.
javascript: true
Expand Down
Loading