Skip to content

Commit e78fa8a

Browse files
authored
Rollup merge of #107232 - notriddle:notriddle/settings-choice, r=GuillaumeGomez
rustdoc: simplify settings popover DOM, CSS, JS * Change the class names so that they all start with `setting-`. That should make it harder to accidentally use a setting class outside the settings popover, where loading the CSS might accidentally change the styles of something unrelated. * Get rid of an unnecessary wrapper DIV around the radio button line. * Simplify CSS selectors by making the DOM easier and more intuitive to target. * Remove dead settings JS for obsolete select-wrapper
2 parents 139a3c5 + 1c41b4d commit e78fa8a

File tree

5 files changed

+60
-73
lines changed

5 files changed

+60
-73
lines changed

src/librustdoc/html/static/css/settings.css

+12-16
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,7 @@
33
position: relative;
44
}
55

6-
.setting-line .radio-line input,
7-
.setting-line .settings-toggle input {
6+
.setting-radio input, .setting-check input {
87
margin-right: 0.3em;
98
height: 1.2rem;
109
width: 1.2rem;
@@ -14,21 +13,20 @@
1413
-webkit-appearance: none;
1514
cursor: pointer;
1615
}
17-
.setting-line .radio-line input {
16+
.setting-radio input {
1817
border-radius: 50%;
1918
}
20-
.setting-line .settings-toggle input:checked {
19+
.setting-check input:checked {
2120
content: url('data:image/svg+xml,<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 40 40">\
2221
<path d="M7,25L17,32L33,12" fill="none" stroke="black" stroke-width="5"/>\
2322
<path d="M7,23L17,30L33,10" fill="none" stroke="white" stroke-width="5"/></svg>');
2423
}
2524

26-
.setting-line .radio-line input + span,
27-
.setting-line .settings-toggle span {
25+
.setting-radio span, .setting-check span {
2826
padding-bottom: 1px;
2927
}
3028

31-
.radio-line .choice {
29+
.setting-radio {
3230
margin-top: 0.1em;
3331
margin-bottom: 0.1em;
3432
min-width: 3.8em;
@@ -37,11 +35,11 @@
3735
align-items: center;
3836
cursor: pointer;
3937
}
40-
.radio-line .choice + .choice {
38+
.setting-radio + .setting-radio {
4139
margin-left: 0.5em;
4240
}
4341

44-
.settings-toggle {
42+
.setting-check {
4543
position: relative;
4644
width: 100%;
4745
margin-right: 20px;
@@ -50,23 +48,21 @@
5048
cursor: pointer;
5149
}
5250

53-
.setting-line .radio-line input:checked {
51+
.setting-radio input:checked {
5452
box-shadow: inset 0 0 0 3px var(--main-background-color);
5553
background-color: var(--settings-input-color);
5654
}
57-
.setting-line .settings-toggle input:checked {
55+
.setting-check input:checked {
5856
background-color: var(--settings-input-color);
5957
}
60-
.setting-line .radio-line input:focus,
61-
.setting-line .settings-toggle input:focus {
58+
.setting-radio input:focus, .setting-check input:focus {
6259
box-shadow: 0 0 1px 1px var(--settings-input-color);
6360
}
6461
/* In here we combine both `:focus` and `:checked` properties. */
65-
.setting-line .radio-line input:checked:focus {
62+
.setting-radio input:checked:focus {
6663
box-shadow: inset 0 0 0 3px var(--main-background-color),
6764
0 0 2px 2px var(--settings-input-color);
6865
}
69-
.setting-line .radio-line input:hover,
70-
.setting-line .settings-toggle input:hover {
66+
.setting-radio input:hover, .setting-check input:hover {
7167
border-color: var(--settings-input-color) !important;
7268
}

src/librustdoc/html/static/js/settings.js

+21-30
Original file line numberDiff line numberDiff line change
@@ -48,13 +48,13 @@
4848
}
4949

5050
function showLightAndDark() {
51-
removeClass(document.getElementById("preferred-light-theme").parentElement, "hidden");
52-
removeClass(document.getElementById("preferred-dark-theme").parentElement, "hidden");
51+
removeClass(document.getElementById("preferred-light-theme"), "hidden");
52+
removeClass(document.getElementById("preferred-dark-theme"), "hidden");
5353
}
5454

5555
function hideLightAndDark() {
56-
addClass(document.getElementById("preferred-light-theme").parentElement, "hidden");
57-
addClass(document.getElementById("preferred-dark-theme").parentElement, "hidden");
56+
addClass(document.getElementById("preferred-light-theme"), "hidden");
57+
addClass(document.getElementById("preferred-dark-theme"), "hidden");
5858
}
5959

6060
function updateLightAndDark() {
@@ -80,17 +80,6 @@
8080
toggle.onkeyup = handleKey;
8181
toggle.onkeyrelease = handleKey;
8282
});
83-
onEachLazy(settingsElement.getElementsByClassName("select-wrapper"), elem => {
84-
const select = elem.getElementsByTagName("select")[0];
85-
const settingId = select.id;
86-
const settingValue = getSettingValue(settingId);
87-
if (settingValue !== null) {
88-
select.value = settingValue;
89-
}
90-
select.onchange = function() {
91-
changeSetting(this.id, this.value);
92-
};
93-
});
9483
onEachLazy(settingsElement.querySelectorAll("input[type=\"radio\"]"), elem => {
9584
const settingId = elem.name;
9685
let settingValue = getSettingValue(settingId);
@@ -127,38 +116,40 @@
127116
let output = "";
128117

129118
for (const setting of settings) {
130-
output += "<div class=\"setting-line\">";
131119
const js_data_name = setting["js_name"];
132120
const setting_name = setting["name"];
133121

134122
if (setting["options"] !== undefined) {
135123
// This is a select setting.
136124
output += `\
137-
<div class="radio-line" id="${js_data_name}">
138-
<div class="setting-name">${setting_name}</div>
139-
<div class="choices">`;
125+
<div class="setting-line" id="${js_data_name}">
126+
<div class="setting-radio-name">${setting_name}</div>
127+
<div class="setting-radio-choices">`;
140128
onEach(setting["options"], option => {
141129
const checked = option === setting["default"] ? " checked" : "";
142130
const full = `${js_data_name}-${option.replace(/ /g,"-")}`;
143131

144132
output += `\
145-
<label for="${full}" class="choice">
146-
<input type="radio" name="${js_data_name}"
147-
id="${full}" value="${option}"${checked}>
148-
<span>${option}</span>
149-
</label>`;
133+
<label for="${full}" class="setting-radio">
134+
<input type="radio" name="${js_data_name}"
135+
id="${full}" value="${option}"${checked}>
136+
<span>${option}</span>
137+
</label>`;
150138
});
151-
output += "</div></div>";
139+
output += `\
140+
</div>
141+
</div>`;
152142
} else {
153143
// This is a checkbox toggle.
154144
const checked = setting["default"] === true ? " checked" : "";
155145
output += `\
156-
<label class="settings-toggle">\
157-
<input type="checkbox" id="${js_data_name}"${checked}>\
158-
<span class="label">${setting_name}</span>\
159-
</label>`;
146+
<div class="setting-line">\
147+
<label class="setting-check">\
148+
<input type="checkbox" id="${js_data_name}"${checked}>\
149+
<span>${setting_name}</span>\
150+
</label>\
151+
</div>`;
160152
}
161-
output += "</div>";
162153
}
163154
return output;
164155
}

tests/rustdoc-gui/mobile.goml

+2-2
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ goto: "file://" + |DOC_PATH| + "/settings.html"
2828
size: (400, 600)
2929
// Ignored for now https://github.com/rust-lang/rust/issues/93784.
3030
// compare-elements-position-near-false: (
31-
// "#preferred-light-theme .setting-name",
32-
// "#preferred-light-theme .choice",
31+
// "#preferred-light-theme .setting-radio-name",
32+
// "#preferred-light-theme .setting-radio",
3333
// {"y": 16},
3434
// )

tests/rustdoc-gui/settings.goml

+21-21
Original file line numberDiff line numberDiff line change
@@ -43,12 +43,12 @@ wait-for: "#settings"
4343
// We check that the "Use system theme" is disabled.
4444
assert-property: ("#theme-system-preference", {"checked": "false"})
4545
// Meaning that only the "theme" menu is showing up.
46-
assert: ".setting-line:not(.hidden) #theme"
47-
assert: ".setting-line.hidden #preferred-dark-theme"
48-
assert: ".setting-line.hidden #preferred-light-theme"
46+
assert: "#theme.setting-line:not(.hidden)"
47+
assert: "#preferred-dark-theme.setting-line.hidden"
48+
assert: "#preferred-light-theme.setting-line.hidden"
4949

5050
// We check that the correct theme is selected.
51-
assert-property: ("#theme .choices #theme-dark", {"checked": "true"})
51+
assert-property: ("#theme .setting-radio-choices #theme-dark", {"checked": "true"})
5252

5353
// Some style checks...
5454
move-cursor-to: "#settings-menu > a"
@@ -109,31 +109,31 @@ assert-css: (
109109
"box-shadow": "rgb(33, 150, 243) 0px 0px 1px 1px",
110110
},
111111
)
112-
// Now we check the setting-name for radio buttons is on a different line than the label.
112+
// Now we check the setting-radio-name is on a different line than the label.
113113
compare-elements-position-near: (
114-
"#theme .setting-name",
115-
"#theme .choices",
114+
"#theme .setting-radio-name",
115+
"#theme .setting-radio-choices",
116116
{"x": 1}
117117
)
118118
compare-elements-position-near-false: (
119-
"#theme .setting-name",
120-
"#theme .choices",
119+
"#theme .setting-radio-name",
120+
"#theme .setting-radio-choices",
121121
{"y": 1}
122122
)
123123
// Now we check that the label positions are all on the same line.
124124
compare-elements-position-near: (
125-
"#theme .choices #theme-light",
126-
"#theme .choices #theme-dark",
125+
"#theme .setting-radio-choices #theme-light",
126+
"#theme .setting-radio-choices #theme-dark",
127127
{"y": 1}
128128
)
129129
compare-elements-position-near: (
130-
"#theme .choices #theme-dark",
131-
"#theme .choices #theme-ayu",
130+
"#theme .setting-radio-choices #theme-dark",
131+
"#theme .setting-radio-choices #theme-ayu",
132132
{"y": 1}
133133
)
134134
compare-elements-position-near: (
135-
"#theme .choices #theme-ayu",
136-
"#theme .choices #theme-system-preference",
135+
"#theme .setting-radio-choices #theme-ayu",
136+
"#theme .setting-radio-choices #theme-system-preference",
137137
{"y": 1}
138138
)
139139

@@ -180,17 +180,17 @@ assert-css: (
180180
// We now switch the display.
181181
click: "#theme-system-preference"
182182
// Wait for the hidden element to show up.
183-
wait-for: ".setting-line:not(.hidden) #preferred-dark-theme"
184-
assert: ".setting-line:not(.hidden) #preferred-light-theme"
183+
wait-for: "#preferred-dark-theme.setting-line:not(.hidden)"
184+
assert: "#preferred-light-theme.setting-line:not(.hidden)"
185185

186186
// We check their text as well.
187-
assert-text: ("#preferred-dark-theme .setting-name", "Preferred dark theme")
188-
assert-text: ("#preferred-light-theme .setting-name", "Preferred light theme")
187+
assert-text: ("#preferred-dark-theme .setting-radio-name", "Preferred dark theme")
188+
assert-text: ("#preferred-light-theme .setting-radio-name", "Preferred light theme")
189189

190190
// We now check that clicking on the toggles' text is like clicking on the checkbox.
191191
// To test it, we use the "Disable keyboard shortcuts".
192192
local-storage: {"rustdoc-disable-shortcuts": "false"}
193-
click: ".setting-line:last-child .settings-toggle .label"
193+
click: ".setting-line:last-child .setting-check span"
194194
assert-local-storage: {"rustdoc-disable-shortcuts": "true"}
195195

196196
// Make sure that "Disable keyboard shortcuts" actually took effect.
@@ -200,7 +200,7 @@ assert-false: "#help-button .popover"
200200
wait-for-css: ("#settings-menu .popover", {"display": "block"})
201201

202202
// Now turn keyboard shortcuts back on, and see if they work.
203-
click: ".setting-line:last-child .settings-toggle .label"
203+
click: ".setting-line:last-child .setting-check span"
204204
assert-local-storage: {"rustdoc-disable-shortcuts": "false"}
205205
press-key: "Escape"
206206
press-key: "?"

tests/rustdoc-gui/theme-change.goml

+4-4
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ assert-local-storage: { "rustdoc-theme": "ayu" }
4343

4444
assert-local-storage-false: { "rustdoc-use-system-theme": "true" }
4545
click: "#theme-system-preference"
46-
wait-for: ".setting-line:not(.hidden) #preferred-light-theme"
46+
wait-for: "#preferred-light-theme.setting-line:not(.hidden)"
4747
assert-local-storage: { "rustdoc-use-system-theme": "true" }
4848
// We click on both preferred light and dark themes to be sure that there is a change.
4949
click: "#preferred-light-theme-dark"
@@ -52,16 +52,16 @@ wait-for-css: ("body", { "background-color": |background_dark| })
5252

5353
reload:
5454
// Ensure that the "preferred themes" are still displayed.
55-
wait-for: ".setting-line:not(.hidden) #preferred-light-theme"
55+
wait-for: "#preferred-light-theme.setting-line:not(.hidden)"
5656
click: "#theme-light"
5757
wait-for-css: ("body", { "background-color": |background_light| })
5858
assert-local-storage: { "rustdoc-theme": "light" }
5959
// Ensure it's now hidden again
60-
wait-for: ".setting-line.hidden #preferred-light-theme"
60+
wait-for: "#preferred-light-theme.setting-line.hidden"
6161
// And ensure the theme was rightly set.
6262
wait-for-css: ("body", { "background-color": |background_light| })
6363
assert-local-storage: { "rustdoc-theme": "light" }
6464

6565
reload:
6666
wait-for: "#settings"
67-
assert: ".setting-line.hidden #preferred-light-theme"
67+
assert: "#preferred-light-theme.setting-line.hidden"

0 commit comments

Comments
 (0)