From 61c47ba88085b8be2872ee968a30868d25d0f1ef Mon Sep 17 00:00:00 2001 From: Guillaume Gomez Date: Thu, 20 May 2021 15:14:50 +0200 Subject: [PATCH 1/3] Generate DOM more securely --- src/librustdoc/html/static/search.js | 65 +++++++++++++++++++++------- 1 file changed, 49 insertions(+), 16 deletions(-) diff --git a/src/librustdoc/html/static/search.js b/src/librustdoc/html/static/search.js index 26b14f675f6..b3242bf4df9 100644 --- a/src/librustdoc/html/static/search.js +++ b/src/librustdoc/html/static/search.js @@ -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 = "
"; + output.className = "search-results " + extraClass; array.forEach(function(item) { if (item.is_alias !== true) { @@ -994,19 +994,46 @@ window.initSearch = function(rawSearchIndex) { extra = " (keyword)"; } - output += "" + - "
" + - (item.is_alias === true ? - ("" + item.alias + "  - see ") : "") + - item.displayPath + "" + - name + extra + "
" + - "" + item.desc + - " 
"; + 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", + " - see "); + + resultName.appendChild(alias); + } + resultName.insertAdjacentHTML( + "beforeend", + item.displayPath + "" + name + extra + ""); + wrapper.appendChild(resultName); + + var description = document.createElement("div"); + 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 += "
"; } else { - output = "
No results :(
" + + output.className = "search-failed" + extraClass; + output.innerHTML = "No results :(
" + "Try on DuckDuckGo?

" + @@ -1018,7 +1045,7 @@ window.initSearch = function(rawSearchIndex) { "href=\"https://doc.rust-lang.org/book/index.html\">Rust Book for " + "introductions to language features and the language itself.
  • Docs.rs for documentation of crates released on" + - " crates.io.
  • "; + " crates.io."; } return [output, length]; } @@ -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]) + - "
    " + - ret_others[0] + ret_in_args[0] + ret_returned[0] + "
    "; + ""; + + 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); From e4067a30c79bfba100c2f018053887eca3ae4046 Mon Sep 17 00:00:00 2001 From: Guillaume Gomez Date: Tue, 25 May 2021 17:18:04 +0200 Subject: [PATCH 2/3] Fix more search results CSS rules --- src/librustdoc/html/static/themes/ayu.css | 4 ++-- src/librustdoc/html/static/themes/dark.css | 4 ++-- src/librustdoc/html/static/themes/light.css | 4 ++-- 3 files changed, 6 insertions(+), 6 deletions(-) diff --git a/src/librustdoc/html/static/themes/ayu.css b/src/librustdoc/html/static/themes/ayu.css index e59909ffdf0..fd47c085b84 100644 --- a/src/librustdoc/html/static/themes/ayu.css +++ b/src/librustdoc/html/static/themes/ayu.css @@ -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; } diff --git a/src/librustdoc/html/static/themes/dark.css b/src/librustdoc/html/static/themes/dark.css index a2bcb43f44e..d6e1a880a4e 100644 --- a/src/librustdoc/html/static/themes/dark.css +++ b/src/librustdoc/html/static/themes/dark.css @@ -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; } diff --git a/src/librustdoc/html/static/themes/light.css b/src/librustdoc/html/static/themes/light.css index 2ad3551d900..c8151f1cf97 100644 --- a/src/librustdoc/html/static/themes/light.css +++ b/src/librustdoc/html/static/themes/light.css @@ -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; } From 0fae87a24ce432a4b8a1a1530324246cc2345a5b Mon Sep 17 00:00:00 2001 From: Guillaume Gomez Date: Tue, 25 May 2021 17:48:57 +0200 Subject: [PATCH 3/3] Add GUI test to check colors of alias in search results --- src/test/rustdoc-gui/search-result-colors.goml | 14 ++++++++++++++ src/test/rustdoc-gui/src/lib.rs | 1 + 2 files changed, 15 insertions(+) create mode 100644 src/test/rustdoc-gui/search-result-colors.goml diff --git a/src/test/rustdoc-gui/search-result-colors.goml b/src/test/rustdoc-gui/search-result-colors.goml new file mode 100644 index 00000000000..25a01512159 --- /dev/null +++ b/src/test/rustdoc-gui/search-result-colors.goml @@ -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)"}) diff --git a/src/test/rustdoc-gui/src/lib.rs b/src/test/rustdoc-gui/src/lib.rs index 7b247a19b8e..272b1d05452 100644 --- a/src/test/rustdoc-gui/src/lib.rs +++ b/src/test/rustdoc-gui/src/lib.rs @@ -36,6 +36,7 @@ impl Foo { } /// Just a normal enum. +#[doc(alias = "ThisIsAnAlias")] pub enum WhoLetTheDogOut { /// Woof! Woof,