Skip to content

Commit ff5522f

Browse files
committed
Auto merge of rust-lang#85540 - GuillaumeGomez:better-result-dom-generation, r=jsha
Better result dom generation First commit is from rust-lang#85506. We realized in rust-lang#85506 (comment) thanks to `@dns2utf8` that in some cases, the generated search result DOM was invalid. This was not strict enough and the DOM was inserted as a big string, which wasn't great. r? `@jsha`
2 parents 9f75dbf + 0fae87a commit ff5522f

File tree

6 files changed

+70
-22
lines changed

6 files changed

+70
-22
lines changed

src/librustdoc/html/static/search.js

+49-16
Original file line numberDiff line numberDiff line change
@@ -968,11 +968,11 @@ window.initSearch = function(rawSearchIndex) {
968968
extraClass = " active";
969969
}
970970

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

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

997-
output += "<a class=\"result-" + type + "\" href=\"" + item.href + "\">" +
998-
"<div><div class=\"result-name\">" +
999-
(item.is_alias === true ?
1000-
("<span class=\"alias\"><b>" + item.alias + " </b></span><span " +
1001-
"class=\"grey\"><i>&nbsp;- see&nbsp;</i></span>") : "") +
1002-
item.displayPath + "<span class=\"" + type + "\">" +
1003-
name + extra + "</span></div><div class=\"desc\">" +
1004-
"<span>" + item.desc +
1005-
"&nbsp;</span></div></div></a>";
997+
var link = document.createElement("a");
998+
link.className = "result-" + type;
999+
link.href = item.href;
1000+
1001+
var wrapper = document.createElement("div");
1002+
var resultName = document.createElement("div");
1003+
resultName.className = "result-name";
1004+
1005+
if (item.is_alias) {
1006+
var alias = document.createElement("span");
1007+
alias.className = "alias";
1008+
1009+
var bold = document.createElement("b");
1010+
bold.innerText = item.alias;
1011+
alias.appendChild(bold);
1012+
1013+
alias.insertAdjacentHTML(
1014+
"beforeend",
1015+
"<span class=\"grey\"><i>&nbsp;- see&nbsp;</i></span>");
1016+
1017+
resultName.appendChild(alias);
1018+
}
1019+
resultName.insertAdjacentHTML(
1020+
"beforeend",
1021+
item.displayPath + "<span class=\"" + type + "\">" + name + extra + "</span>");
1022+
wrapper.appendChild(resultName);
1023+
1024+
var description = document.createElement("div");
1025+
description.className = "desc";
1026+
var spanDesc = document.createElement("span");
1027+
spanDesc.innerText = item.desc + "\u00A0";
1028+
1029+
description.appendChild(spanDesc);
1030+
wrapper.appendChild(description);
1031+
link.appendChild(wrapper);
1032+
output.appendChild(link);
10061033
});
1007-
output += "</div>";
10081034
} else {
1009-
output = "<div class=\"search-failed\"" + extraClass + ">No results :(<br/>" +
1035+
output.className = "search-failed" + extraClass;
1036+
output.innerHTML = "No results :(<br/>" +
10101037
"Try on <a href=\"https://duckduckgo.com/?q=" +
10111038
encodeURIComponent("rust " + query.query) +
10121039
"\">DuckDuckGo</a>?<br/><br/>" +
@@ -1018,7 +1045,7 @@ window.initSearch = function(rawSearchIndex) {
10181045
"href=\"https://doc.rust-lang.org/book/index.html\">Rust Book</a> for " +
10191046
"introductions to language features and the language itself.</li><li><a " +
10201047
"href=\"https://docs.rs\">Docs.rs</a> for documentation of crates released on" +
1021-
" <a href=\"https://crates.io/\">crates.io</a>.</li></ul></div>";
1048+
" <a href=\"https://crates.io/\">crates.io</a>.</li></ul>";
10221049
}
10231050
return [output, length];
10241051
}
@@ -1078,10 +1105,16 @@ window.initSearch = function(rawSearchIndex) {
10781105
makeTabHeader(0, "In Names", ret_others[1]) +
10791106
makeTabHeader(1, "In Parameters", ret_in_args[1]) +
10801107
makeTabHeader(2, "In Return Types", ret_returned[1]) +
1081-
"</div><div id=\"results\">" +
1082-
ret_others[0] + ret_in_args[0] + ret_returned[0] + "</div>";
1108+
"</div>";
1109+
1110+
var resultsElem = document.createElement("div");
1111+
resultsElem.id = "results";
1112+
resultsElem.appendChild(ret_others[0]);
1113+
resultsElem.appendChild(ret_in_args[0]);
1114+
resultsElem.appendChild(ret_returned[0]);
10831115

10841116
search.innerHTML = output;
1117+
search.appendChild(resultsElem);
10851118
// Reset focused elements.
10861119
searchState.focusedByTab = [null, null, null];
10871120
searchState.showResults(search);

src/librustdoc/html/static/themes/ayu.css

+2-2
Original file line numberDiff line numberDiff line change
@@ -565,10 +565,10 @@ kbd {
565565
background-color: rgba(70, 70, 70, 0.33);
566566
}
567567

568-
.search-results td span.alias {
568+
.search-results .result-name span.alias {
569569
color: #c5c5c5;
570570
}
571-
.search-results td span.grey {
571+
.search-results .result-name span.grey {
572572
color: #999;
573573
}
574574

src/librustdoc/html/static/themes/dark.css

+2-2
Original file line numberDiff line numberDiff line change
@@ -444,10 +444,10 @@ kbd {
444444
background-color: #606060;
445445
}
446446

447-
.search-results td span.alias {
447+
.search-results .result-name span.alias {
448448
color: #fff;
449449
}
450-
.search-results td span.grey {
450+
.search-results .result-name span.grey {
451451
color: #ccc;
452452
}
453453

src/librustdoc/html/static/themes/light.css

+2-2
Original file line numberDiff line numberDiff line change
@@ -435,10 +435,10 @@ kbd {
435435
background-color: #f9f9f9;
436436
}
437437

438-
.search-results td span.alias {
438+
.search-results .result-name span.alias {
439439
color: #000;
440440
}
441-
.search-results td span.grey {
441+
.search-results .result-name span.grey {
442442
color: #999;
443443
}
444444

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
goto: file://|DOC_PATH|/test_docs/index.html
2+
// We set the theme so we're sure that the corect values will be used, whatever the computer
3+
// this test is running on.
4+
local-storage: {"rustdoc-theme": "dark", "rustdoc-preferred-dark-theme": "dark", "rustdoc-use-system-theme": "false"}
5+
// If the text isn't displayed, the browser doesn't compute color style correctly...
6+
show-text: true
7+
// We reload the page so the local storage settings are being used.
8+
reload:
9+
write: (".search-input", "thisisanalias")
10+
// Waiting for the search results to appear...
11+
wait-for: "#titles"
12+
// Checking that the colors for the alias element are the ones expected.
13+
assert: (".result-name > .alias", {"color": "rgb(255, 255, 255)"})
14+
assert: (".result-name > .alias > .grey", {"color": "rgb(204, 204, 204)"})

src/test/rustdoc-gui/src/lib.rs

+1
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ impl Foo {
3636
}
3737

3838
/// Just a normal enum.
39+
#[doc(alias = "ThisIsAnAlias")]
3940
pub enum WhoLetTheDogOut {
4041
/// Woof!
4142
Woof,

0 commit comments

Comments
 (0)