Skip to content

Commit 155b055

Browse files
ijacksonGuillaumeGomez
authored andcommitted
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 --default-theme="zork&" escapes the value in the HTML. Closes rust-lang#87263. CC: Jacob Hoffman-Andrews <github@hoffman-andrews.com> Signed-off-by: Ian Jackson <ijackson@chiark.greenend.org.uk>
1 parent 05f2326 commit 155b055

File tree

2 files changed

+25
-1
lines changed

2 files changed

+25
-1
lines changed

src/librustdoc/config.rs

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

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

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

Lines changed: 2 additions & 0 deletions
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;

0 commit comments

Comments
 (0)