Skip to content

Commit 8dba898

Browse files
Rollup merge of rust-lang#87288 - ijackson:rustdoc-theme, r=GuillaumeGomez
rustdoc: Restore --default-theme, etc, by restoring varname escaping In rust-lang#86157 cd0f931 Use Tera templates for rustdoc. dropped the following transformation from the keys of the default settings element's `data-` attribute names: .map(|(k, v)| format!(r#" data-{}="{}""#, k.replace('-', "_"), Escape(v))) The `Escape` part is indeed no longer needed, because Tera does that for us. But the massaging of `-` to `_` is needed, for the (bizarre) reasons explained in the new comments. I have tested that the default theme function works again for me. I have also verified that passing (in shell syntax) '--default-theme="zork&"' escapes the value in the HTML. Closes rust-lang#87263
2 parents e16d023 + df6bdd7 commit 8dba898

File tree

17 files changed

+95
-23
lines changed

17 files changed

+95
-23
lines changed

Cargo.toml

+1
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ members = [
4040
exclude = [
4141
"build",
4242
"compiler/rustc_codegen_cranelift",
43+
"src/test/rustdoc-gui",
4344
# HACK(eddyb) This hardcodes the fact that our CI uses `/checkout/obj`.
4445
"obj",
4546
# The `x` binary is a thin wrapper that calls `x.py`, which initializes

src/bootstrap/test.rs

+20-11
Original file line numberDiff line numberDiff line change
@@ -907,18 +907,27 @@ impl Step for RustdocGUI {
907907
// We remove existing folder to be sure there won't be artifacts remaining.
908908
let _ = fs::remove_dir_all(&out_dir);
909909

910-
let src_path = "src/test/rustdoc-gui/src";
910+
let src_path = builder.build.src.join("src/test/rustdoc-gui/src");
911911
// We generate docs for the libraries present in the rustdoc-gui's src folder.
912-
let mut cargo = Command::new(&builder.initial_cargo);
913-
cargo
914-
.arg("doc")
915-
.arg("--workspace")
916-
.arg("--target-dir")
917-
.arg(&out_dir)
918-
.env("RUSTDOC", builder.rustdoc(self.compiler))
919-
.env("RUSTC", builder.rustc(self.compiler))
920-
.current_dir(&builder.build.src.join(src_path));
921-
builder.run(&mut cargo);
912+
for entry in src_path.read_dir().expect("read_dir call failed") {
913+
if let Ok(entry) = entry {
914+
let path = entry.path();
915+
916+
if !path.is_dir() {
917+
continue;
918+
}
919+
920+
let mut cargo = Command::new(&builder.initial_cargo);
921+
cargo
922+
.arg("doc")
923+
.arg("--target-dir")
924+
.arg(&out_dir)
925+
.env("RUSTDOC", builder.rustdoc(self.compiler))
926+
.env("RUSTC", builder.rustc(self.compiler))
927+
.current_dir(path);
928+
builder.run(&mut cargo);
929+
}
930+
}
922931

923932
// We now run GUI tests.
924933
let mut command = Command::new(&nodejs);

src/librustdoc/config.rs

+25-1
Original file line numberDiff line numberDiff line change
@@ -459,7 +459,31 @@ impl Options {
459459
})
460460
.collect(),
461461
];
462-
let default_settings = default_settings.into_iter().flatten().collect();
462+
let default_settings = default_settings
463+
.into_iter()
464+
.flatten()
465+
.map(
466+
// The keys here become part of `data-` attribute names in the generated HTML. The
467+
// browser does a strange mapping when converting them into attributes on the
468+
// `dataset` property on the DOM HTML Node:
469+
// https://developer.mozilla.org/en-US/docs/Web/API/HTMLElement/dataset
470+
//
471+
// The original key values we have are the same as the DOM storage API keys and the
472+
// command line options, so contain `-`. Our Javascript needs to be able to look
473+
// these values up both in `dataset` and in the storage API, so it needs to be able
474+
// to convert the names back and forth. Despite doing this kebab-case to
475+
// StudlyCaps transformation automatically, the JS DOM API does not provide a
476+
// mechanism for doing the just transformation on a string. So we want to avoid
477+
// the StudlyCaps representation in the `dataset` property.
478+
//
479+
// We solve this by replacing all the `-`s with `_`s. We do that here, when we
480+
// generate the `data-` attributes, and in the JS, when we look them up. (See
481+
// `getSettingValue` in `storage.js.`) Converting `-` to `_` is simple in JS.
482+
//
483+
// The values will be HTML-escaped by the default Tera escaping.
484+
|(k, v)| (k.replace('-', "_"), v),
485+
)
486+
.collect();
463487

464488
let test_args = matches.opt_strs("test-args");
465489
let test_args: Vec<String> =

src/librustdoc/html/static/js/storage.js

+2
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@ function getSettingValue(settingName) {
2222
return current;
2323
}
2424
if (settingsDataset !== null) {
25+
// See the comment for `default_settings.into_iter()` etc. in
26+
// `Options::from_matches` in `librustdoc/config.rs`.
2527
var def = settingsDataset[settingName.replace(/-/g,'_')];
2628
if (def !== undefined) {
2729
return def;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
// This test ensures that the default settings are correctly applied.
2+
//
3+
// The "settings" crate uses "ayu" as default setting, which is what we will
4+
// check.
5+
goto: file://|DOC_PATH|/settings/index.html
6+
// Wait a bit to be sure the default theme is applied.
7+
wait-for: 1000
8+
assert-css: ("body", {"background-color": "rgb(15, 20, 25)"})

src/test/rustdoc-gui/src/Cargo.toml

-6
This file was deleted.

src/test/rustdoc-gui/src/Cargo.lock renamed to src/test/rustdoc-gui/src/lib2/Cargo.lock

-4
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,3 @@ version = "0.1.0"
1212
dependencies = [
1313
"implementors",
1414
]
15-
16-
[[package]]
17-
name = "test_docs"
18-
version = "0.1.0"

src/test/rustdoc-gui/src/lib2/Cargo.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,4 @@ edition = "2018"
77
path = "lib.rs"
88

99
[dependencies]
10-
implementors = { path = "../implementors" }
10+
implementors = { path = "./implementors" }
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
# This file is automatically @generated by Cargo.
2+
# It is not intended for manual editing.
3+
version = 3
4+
5+
[[package]]
6+
name = "implementors"
7+
version = "0.1.0"
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
[build]
2+
rustdocflags = ["--default-theme", "ayu"]
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
# This file is automatically @generated by Cargo.
2+
# It is not intended for manual editing.
3+
version = 3
4+
5+
[[package]]
6+
name = "settings"
7+
version = "0.1.0"
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
[package]
2+
name = "settings"
3+
version = "0.1.0"
4+
edition = "2018"
5+
6+
[lib]
7+
path = "lib.rs"
+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
pub fn foo() {}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
# This file is automatically @generated by Cargo.
2+
# It is not intended for manual editing.
3+
version = 3
4+
5+
[[package]]
6+
name = "test_docs"
7+
version = "0.1.0"

src/test/rustdoc/default-theme.rs

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
// compile-flags: --default-theme ayu
2+
3+
// @has default_theme/index.html
4+
// @has - '//script[@id="default-settings"]/@data-theme' 'ayu'
5+
// @has - '//script[@id="default-settings"]/@data-use_system_theme' 'false'
6+
7+
pub fn whatever() {}

0 commit comments

Comments
 (0)