Skip to content
This repository was archived by the owner on May 28, 2025. It is now read-only.

Commit 61c47ba

Browse files
Generate DOM more securely
1 parent fbf1b1a commit 61c47ba

File tree

1 file changed

+49
-16
lines changed

1 file changed

+49
-16
lines changed

src/librustdoc/html/static/search.js

Lines changed: 49 additions & 16 deletions
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);

0 commit comments

Comments
 (0)