Skip to content

Commit 1d1ab21

Browse files
Remove inline script tags
1 parent efdb859 commit 1d1ab21

File tree

5 files changed

+33
-23
lines changed

5 files changed

+33
-23
lines changed

Diff for: src/librustdoc/html/layout.rs

+1-4
Original file line numberDiff line numberDiff line change
@@ -111,10 +111,7 @@ crate fn render<T: Print, S: Print>(
111111
<section id=\"search\" class=\"content hidden\"></section>\
112112
<section class=\"footer\"></section>\
113113
{after_content}\
114-
<script>\
115-
window.rootPath = \"{root_path}\";\
116-
window.currentCrate = \"{krate}\";\
117-
</script>\
114+
<div id=\"rustdoc-vars\" data-root-path=\"{root_path}\" data-current-crate=\"{krate}\"></div>
118115
<script src=\"{static_root_path}main{suffix}.js\"></script>\
119116
{static_extra_scripts}\
120117
{extra_scripts}\

Diff for: src/librustdoc/html/markdown.rs

+2
Original file line numberDiff line numberDiff line change
@@ -1313,6 +1313,8 @@ fn init_id_map() -> FxHashMap<String, usize> {
13131313
map.insert("toggle-all-docs".to_owned(), 1);
13141314
map.insert("all-types".to_owned(), 1);
13151315
map.insert("default-settings".to_owned(), 1);
1316+
map.insert("rustdoc-vars".to_owned(), 1);
1317+
map.insert("sidebar-vars".to_owned(), 1);
13161318
// This is the list of IDs used by rustdoc sections.
13171319
map.insert("fields".to_owned(), 1);
13181320
map.insert("variants".to_owned(), 1);

Diff for: src/librustdoc/html/render/mod.rs

+2-5
Original file line numberDiff line numberDiff line change
@@ -4216,11 +4216,8 @@ fn print_sidebar(cx: &Context<'_>, it: &clean::Item, buffer: &mut Buffer, cache:
42164216
let relpath = if it.is_mod() { "../" } else { "" };
42174217
write!(
42184218
buffer,
4219-
"<script>window.sidebarCurrent = {{\
4220-
name: \"{name}\", \
4221-
ty: \"{ty}\", \
4222-
relpath: \"{path}\"\
4223-
}};</script>",
4219+
"<div id=\"sidebar-vars\" data-name=\"{name}\" data-ty=\"{ty}\" data-relpath=\"{path}\">\
4220+
</div>",
42244221
name = it.name.unwrap_or(kw::Empty),
42254222
ty = it.type_(),
42264223
path = relpath

Diff for: src/librustdoc/html/static/main.js

+27-12
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
// From rust:
2-
/* global ALIASES, currentCrate, rootPath */
2+
/* global ALIASES */
33

44
// Local js definitions:
55
/* global addClass, getCurrentValue, hasClass */
@@ -40,6 +40,21 @@ if (!DOMTokenList.prototype.remove) {
4040
};
4141
}
4242

43+
(function () {
44+
var rustdocVars = document.getElementById("rustdoc-vars");
45+
if (rustdocVars) {
46+
window.rootPath = rustdocVars.attributes["data-root-path"].value;
47+
window.currentCrate = rustdocVars.attributes["data-current-crate"].value;
48+
}
49+
var sidebarVars = document.getElementById("sidebar-vars");
50+
if (sidebarVars) {
51+
window.sidebarCurrent = {
52+
name: sidebarVars.attributes["data-name"].value,
53+
ty: sidebarVars.attributes["data-ty"].value,
54+
relpath: sidebarVars.attributes["data-relpath"].value,
55+
};
56+
}
57+
}());
4358

4459
// Gets the human-readable string for the virtual-key code of the
4560
// given KeyboardEvent, ev.
@@ -565,7 +580,7 @@ function defocusSearchBar() {
565580
var i, match,
566581
url = document.location.href,
567582
stripped = "",
568-
len = rootPath.match(/\.\.\//g).length + 1;
583+
len = window.rootPath.match(/\.\.\//g).length + 1;
569584

570585
for (i = 0; i < len; ++i) {
571586
match = url.match(/\/[^\/]*$/);
@@ -1504,15 +1519,15 @@ function defocusSearchBar() {
15041519

15051520
if (type === "mod") {
15061521
displayPath = path + "::";
1507-
href = rootPath + path.replace(/::/g, "/") + "/" +
1522+
href = window.rootPath + path.replace(/::/g, "/") + "/" +
15081523
name + "/index.html";
15091524
} else if (type === "primitive" || type === "keyword") {
15101525
displayPath = "";
1511-
href = rootPath + path.replace(/::/g, "/") +
1526+
href = window.rootPath + path.replace(/::/g, "/") +
15121527
"/" + type + "." + name + ".html";
15131528
} else if (type === "externcrate") {
15141529
displayPath = "";
1515-
href = rootPath + name + "/index.html";
1530+
href = window.rootPath + name + "/index.html";
15161531
} else if (item.parent !== undefined) {
15171532
var myparent = item.parent;
15181533
var anchor = "#" + type + "." + name;
@@ -1535,13 +1550,13 @@ function defocusSearchBar() {
15351550
} else {
15361551
displayPath = path + "::" + myparent.name + "::";
15371552
}
1538-
href = rootPath + path.replace(/::/g, "/") +
1553+
href = window.rootPath + path.replace(/::/g, "/") +
15391554
"/" + pageType +
15401555
"." + pageName +
15411556
".html" + anchor;
15421557
} else {
15431558
displayPath = item.path + "::";
1544-
href = rootPath + item.path.replace(/::/g, "/") +
1559+
href = window.rootPath + item.path.replace(/::/g, "/") +
15451560
"/" + type + "." + name + ".html";
15461561
}
15471562
return [displayPath, href];
@@ -1973,7 +1988,7 @@ function defocusSearchBar() {
19731988
startSearch();
19741989

19751990
// Draw a convenient sidebar of known crates if we have a listing
1976-
if (rootPath === "../" || rootPath === "./") {
1991+
if (window.rootPath === "../" || window.rootPath === "./") {
19771992
var sidebar = document.getElementsByClassName("sidebar-elems")[0];
19781993
if (sidebar) {
19791994
var div = document.createElement("div");
@@ -1992,11 +2007,11 @@ function defocusSearchBar() {
19922007
crates.sort();
19932008
for (var i = 0; i < crates.length; ++i) {
19942009
var klass = "crate";
1995-
if (rootPath !== "./" && crates[i] === window.currentCrate) {
2010+
if (window.rootPath !== "./" && crates[i] === window.currentCrate) {
19962011
klass += " current";
19972012
}
19982013
var link = document.createElement("a");
1999-
link.href = rootPath + crates[i] + "/index.html";
2014+
link.href = window.rootPath + crates[i] + "/index.html";
20002015
// The summary in the search index has HTML, so we need to
20012016
// dynamically render it as plaintext.
20022017
link.title = convertHTMLToPlaintext(rawSearchIndex[crates[i]].doc);
@@ -2118,7 +2133,7 @@ function defocusSearchBar() {
21182133

21192134
var libs = Object.getOwnPropertyNames(imp);
21202135
for (var i = 0, llength = libs.length; i < llength; ++i) {
2121-
if (libs[i] === currentCrate) { continue; }
2136+
if (libs[i] === window.currentCrate) { continue; }
21222137
var structs = imp[libs[i]];
21232138

21242139
struct_loop:
@@ -2143,7 +2158,7 @@ function defocusSearchBar() {
21432158
var href = elem.getAttribute("href");
21442159

21452160
if (href && href.indexOf("http") !== 0) {
2146-
elem.setAttribute("href", rootPath + href);
2161+
elem.setAttribute("href", window.rootPath + href);
21472162
}
21482163
});
21492164

Diff for: src/tools/rustdoc-js/tester.js

+1-2
Original file line numberDiff line numberDiff line change
@@ -263,8 +263,7 @@ function loadMainJsAndIndex(mainJs, searchIndex, storageJs, crate) {
263263
"handleAliases", "getQuery", "buildIndex", "execQuery", "execSearch"];
264264

265265
ALIASES = {};
266-
finalJS += 'window = { "currentCrate": "' + crate + '" };\n';
267-
finalJS += 'var rootPath = "../";\n';
266+
finalJS += 'window = { "currentCrate": "' + crate + '", rootPath: "../" };\n';
268267
finalJS += loadThings(["hasOwnProperty", "onEach"], 'function', extractFunction, storageJs);
269268
finalJS += loadThings(arraysToLoad, 'array', extractArrayVariable, mainJs);
270269
finalJS += loadThings(variablesToLoad, 'variable', extractVariable, mainJs);

0 commit comments

Comments
 (0)