Skip to content

Better result dom generation #85540

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

Merged
merged 3 commits into from
May 29, 2021
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
65 changes: 49 additions & 16 deletions src/librustdoc/html/static/search.js
Original file line number Diff line number Diff line change
Expand Up @@ -968,11 +968,11 @@ window.initSearch = function(rawSearchIndex) {
extraClass = " active";
}

var output = "";
var output = document.createElement("div");
var duplicates = {};
var length = 0;
if (array.length > 0) {
output = "<div class=\"search-results " + extraClass + "\">";
output.className = "search-results " + extraClass;

array.forEach(function(item) {
if (item.is_alias !== true) {
Expand All @@ -994,19 +994,46 @@ window.initSearch = function(rawSearchIndex) {
extra = " <i>(keyword)</i>";
}

output += "<a class=\"result-" + type + "\" href=\"" + item.href + "\">" +
"<div><div class=\"result-name\">" +
(item.is_alias === true ?
("<span class=\"alias\"><b>" + item.alias + " </b></span><span " +
"class=\"grey\"><i>&nbsp;- see&nbsp;</i></span>") : "") +
item.displayPath + "<span class=\"" + type + "\">" +
name + extra + "</span></div><div class=\"desc\">" +
"<span>" + item.desc +
"&nbsp;</span></div></div></a>";
var link = document.createElement("a");
link.className = "result-" + type;
link.href = item.href;

var wrapper = document.createElement("div");
var resultName = document.createElement("div");
resultName.className = "result-name";

if (item.is_alias) {
var alias = document.createElement("span");
alias.className = "alias";

var bold = document.createElement("b");
bold.innerText = item.alias;
alias.appendChild(bold);

alias.insertAdjacentHTML(
"beforeend",
"<span class=\"grey\"><i>&nbsp;- see&nbsp;</i></span>");

resultName.appendChild(alias);
}
resultName.insertAdjacentHTML(
"beforeend",
item.displayPath + "<span class=\"" + type + "\">" + name + extra + "</span>");
wrapper.appendChild(resultName);

var description = document.createElement("div");
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
var description = document.createElement("div");
var description = document.createElement("div");
description.className = "result-description";

Adding this would simplify the CSS in the mobile variant

Copy link
Member Author

Choose a reason for hiding this comment

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

Let's do it in another PR, this one is big enough, I prefer style changes to be put on their own.

description.className = "desc";
var spanDesc = document.createElement("span");
spanDesc.innerText = item.desc + "\u00A0";

description.appendChild(spanDesc);
wrapper.appendChild(description);
link.appendChild(wrapper);
output.appendChild(link);
});
output += "</div>";
} else {
output = "<div class=\"search-failed\"" + extraClass + ">No results :(<br/>" +
output.className = "search-failed" + extraClass;
output.innerHTML = "No results :(<br/>" +
"Try on <a href=\"https://duckduckgo.com/?q=" +
encodeURIComponent("rust " + query.query) +
"\">DuckDuckGo</a>?<br/><br/>" +
Expand All @@ -1018,7 +1045,7 @@ window.initSearch = function(rawSearchIndex) {
"href=\"https://doc.rust-lang.org/book/index.html\">Rust Book</a> for " +
"introductions to language features and the language itself.</li><li><a " +
"href=\"https://docs.rs\">Docs.rs</a> for documentation of crates released on" +
" <a href=\"https://crates.io/\">crates.io</a>.</li></ul></div>";
" <a href=\"https://crates.io/\">crates.io</a>.</li></ul>";
}
return [output, length];
}
Expand Down Expand Up @@ -1078,10 +1105,16 @@ window.initSearch = function(rawSearchIndex) {
makeTabHeader(0, "In Names", ret_others[1]) +
makeTabHeader(1, "In Parameters", ret_in_args[1]) +
makeTabHeader(2, "In Return Types", ret_returned[1]) +
"</div><div id=\"results\">" +
ret_others[0] + ret_in_args[0] + ret_returned[0] + "</div>";
"</div>";

var resultsElem = document.createElement("div");
resultsElem.id = "results";
resultsElem.appendChild(ret_others[0]);
resultsElem.appendChild(ret_in_args[0]);
resultsElem.appendChild(ret_returned[0]);

search.innerHTML = output;
search.appendChild(resultsElem);
// Reset focused elements.
searchState.focusedByTab = [null, null, null];
searchState.showResults(search);
Expand Down
4 changes: 2 additions & 2 deletions src/librustdoc/html/static/themes/ayu.css
Original file line number Diff line number Diff line change
Expand Up @@ -565,10 +565,10 @@ kbd {
background-color: rgba(70, 70, 70, 0.33);
}

.search-results td span.alias {
.search-results .result-name span.alias {
color: #c5c5c5;
}
.search-results td span.grey {
.search-results .result-name span.grey {
color: #999;
}

Expand Down
4 changes: 2 additions & 2 deletions src/librustdoc/html/static/themes/dark.css
Original file line number Diff line number Diff line change
Expand Up @@ -444,10 +444,10 @@ kbd {
background-color: #606060;
}

.search-results td span.alias {
.search-results .result-name span.alias {
color: #fff;
}
.search-results td span.grey {
.search-results .result-name span.grey {
color: #ccc;
}

Expand Down
4 changes: 2 additions & 2 deletions src/librustdoc/html/static/themes/light.css
Original file line number Diff line number Diff line change
Expand Up @@ -435,10 +435,10 @@ kbd {
background-color: #f9f9f9;
}

.search-results td span.alias {
.search-results .result-name span.alias {
color: #000;
}
.search-results td span.grey {
.search-results .result-name span.grey {
color: #999;
}

Expand Down
14 changes: 14 additions & 0 deletions src/test/rustdoc-gui/search-result-colors.goml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
goto: file://|DOC_PATH|/test_docs/index.html
// We set the theme so we're sure that the corect values will be used, whatever the computer
// this test is running on.
local-storage: {"rustdoc-theme": "dark", "rustdoc-preferred-dark-theme": "dark", "rustdoc-use-system-theme": "false"}
// If the text isn't displayed, the browser doesn't compute color style correctly...
show-text: true
// We reload the page so the local storage settings are being used.
reload:
write: (".search-input", "thisisanalias")
// Waiting for the search results to appear...
wait-for: "#titles"
// Checking that the colors for the alias element are the ones expected.
assert: (".result-name > .alias", {"color": "rgb(255, 255, 255)"})
assert: (".result-name > .alias > .grey", {"color": "rgb(204, 204, 204)"})
1 change: 1 addition & 0 deletions src/test/rustdoc-gui/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ impl Foo {
}

/// Just a normal enum.
#[doc(alias = "ThisIsAnAlias")]
pub enum WhoLetTheDogOut {
/// Woof!
Woof,
Expand Down