Skip to content

Commit 90f6fe8

Browse files
Rollup merge of rust-lang#85117 - jsha:bubble-bubble-toil-and-trouble, r=GuillaumeGomez
Move global click handlers to per-element ones. In rustdoc's main.js, we had an onclick handler for the whole document that would dispatch to handlers for various elements. This change attaches the handlers to the elements that trigger them, instead. This simplifies the code and avoids reimplementing the browser's bubbling functionality. As part of this change, change from a class to an id for help button. Move the handlers and associated code for highlighting source lines into source-script.js (and factor out a shared regex). Demo at https://hoffman-andrews.com/rust/bubble-bubble-toil-and-trouble/std/string/struct.String.html Note: this conflicts with / depends on rust-lang#85074. Once that's merged I'll rebase this and resolve conflicts. Part of rust-lang#83332. Thanks to `@Manishearth` for the [suggestion to not reimplement bubbling](rust-lang#83332 (comment)). r? `@GuillaumeGomez`
2 parents 9461780 + 0945451 commit 90f6fe8

File tree

8 files changed

+138
-128
lines changed

8 files changed

+138
-128
lines changed

src/librustdoc/html/layout.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ crate fn render<T: Print, S: Print>(
105105
placeholder=\"Click or press ‘S’ to search, ‘?’ for more options…\" \
106106
type=\"search\">\
107107
</div>\
108-
<button type=\"button\" class=\"help-button\">?</button>
108+
<button type=\"button\" id=\"help-button\">?</button>
109109
<a id=\"settings-menu\" href=\"{root_path}settings.html\">\
110110
<img src=\"{static_root_path}wheel{suffix}.svg\" \
111111
width=\"18\" height=\"18\" \

src/librustdoc/html/markdown.rs

+1
Original file line numberDiff line numberDiff line change
@@ -1347,6 +1347,7 @@ fn init_id_map() -> FxHashMap<String, usize> {
13471347
map.insert("theme-picker".to_owned(), 1);
13481348
map.insert("theme-choices".to_owned(), 1);
13491349
map.insert("settings-menu".to_owned(), 1);
1350+
map.insert("help-button".to_owned(), 1);
13501351
map.insert("main".to_owned(), 1);
13511352
map.insert("search".to_owned(), 1);
13521353
map.insert("crate-search".to_owned(), 1);

src/librustdoc/html/static/main.js

+29-117
Original file line numberDiff line numberDiff line change
@@ -381,56 +381,9 @@ function hideThemeButtonState() {
381381
}
382382
}
383383

384-
function highlightSourceLines(match, ev) {
385-
if (typeof match === "undefined") {
386-
// If we're in mobile mode, we should hide the sidebar in any case.
387-
hideSidebar();
388-
match = window.location.hash.match(/^#?(\d+)(?:-(\d+))?$/);
389-
}
390-
if (!match) {
391-
return;
392-
}
393-
var from = parseInt(match[1], 10);
394-
var to = from;
395-
if (typeof match[2] !== "undefined") {
396-
to = parseInt(match[2], 10);
397-
}
398-
if (to < from) {
399-
var tmp = to;
400-
to = from;
401-
from = tmp;
402-
}
403-
var elem = document.getElementById(from);
404-
if (!elem) {
405-
return;
406-
}
407-
if (!ev) {
408-
var x = document.getElementById(from);
409-
if (x) {
410-
x.scrollIntoView();
411-
}
412-
}
413-
onEachLazy(document.getElementsByClassName("line-numbers"), function(e) {
414-
onEachLazy(e.getElementsByTagName("span"), function(i_e) {
415-
removeClass(i_e, "line-highlighted");
416-
});
417-
});
418-
for (var i = from; i <= to; ++i) {
419-
elem = document.getElementById(i);
420-
if (!elem) {
421-
break;
422-
}
423-
addClass(elem, "line-highlighted");
424-
}
425-
}
426-
427384
function onHashChange(ev) {
428385
// If we're in mobile mode, we should hide the sidebar in any case.
429386
hideSidebar();
430-
var match = window.location.hash.match(/^#?(\d+)(?:-(\d+))?$/);
431-
if (match) {
432-
return highlightSourceLines(match, ev);
433-
}
434387
handleHashes(ev);
435388
}
436389

@@ -585,78 +538,9 @@ function hideThemeButtonState() {
585538
}
586539
}
587540

588-
function findParentElement(elem, tagName) {
589-
do {
590-
if (elem && elem.tagName === tagName) {
591-
return elem;
592-
}
593-
elem = elem.parentNode;
594-
} while (elem);
595-
return null;
596-
}
597-
598541
document.addEventListener("keypress", handleShortcut);
599542
document.addEventListener("keydown", handleShortcut);
600543

601-
var handleSourceHighlight = (function() {
602-
var prev_line_id = 0;
603-
604-
var set_fragment = function(name) {
605-
var x = window.scrollX,
606-
y = window.scrollY;
607-
if (searchState.browserSupportsHistoryApi()) {
608-
history.replaceState(null, null, "#" + name);
609-
highlightSourceLines();
610-
} else {
611-
location.replace("#" + name);
612-
}
613-
// Prevent jumps when selecting one or many lines
614-
window.scrollTo(x, y);
615-
};
616-
617-
return function(ev) {
618-
var cur_line_id = parseInt(ev.target.id, 10);
619-
ev.preventDefault();
620-
621-
if (ev.shiftKey && prev_line_id) {
622-
// Swap selection if needed
623-
if (prev_line_id > cur_line_id) {
624-
var tmp = prev_line_id;
625-
prev_line_id = cur_line_id;
626-
cur_line_id = tmp;
627-
}
628-
629-
set_fragment(prev_line_id + "-" + cur_line_id);
630-
} else {
631-
prev_line_id = cur_line_id;
632-
633-
set_fragment(cur_line_id);
634-
}
635-
};
636-
}());
637-
638-
document.addEventListener("click", function(ev) {
639-
var helpElem = getHelpElement(false);
640-
if (hasClass(ev.target, "help-button")) {
641-
displayHelp(true, ev);
642-
} else if (ev.target.tagName === "SPAN" && hasClass(ev.target.parentNode, "line-numbers")) {
643-
handleSourceHighlight(ev);
644-
} else if (helpElem && hasClass(helpElem, "hidden") === false) {
645-
var is_inside_help_popup = ev.target !== helpElem && helpElem.contains(ev.target);
646-
if (is_inside_help_popup === false) {
647-
addClass(helpElem, "hidden");
648-
removeClass(document.body, "blur");
649-
}
650-
} else {
651-
// Making a collapsed element visible on onhashchange seems
652-
// too late
653-
var a = findParentElement(ev.target, "A");
654-
if (a && a.hash) {
655-
expandSection(a.hash.replace(/^#/, ""));
656-
}
657-
}
658-
});
659-
660544
(function() {
661545
var x = document.getElementsByClassName("version-selector");
662546
if (x.length > 0) {
@@ -1121,6 +1005,27 @@ function hideThemeButtonState() {
11211005
});
11221006
}());
11231007

1008+
function handleClick(id, f) {
1009+
var elem = document.getElementById(id);
1010+
if (elem) {
1011+
elem.addEventListener("click", f);
1012+
}
1013+
}
1014+
handleClick("help-button", function(ev) {
1015+
displayHelp(true, ev);
1016+
});
1017+
1018+
onEachLazy(document.getElementsByTagName("a"), function(el) {
1019+
// For clicks on internal links (<A> tags with a hash property), we expand the section we're
1020+
// jumping to *before* jumping there. We can't do this in onHashChange, because it changes
1021+
// the height of the document so we wind up scrolled to the wrong place.
1022+
if (el.hash) {
1023+
el.addEventListener("click", function() {
1024+
expandSection(el.hash.slice(1));
1025+
});
1026+
}
1027+
});
1028+
11241029
onEachLazy(document.getElementsByClassName("notable-traits"), function(e) {
11251030
e.onclick = function() {
11261031
this.getElementsByClassName('notable-traits-tooltiptext')[0]
@@ -1165,6 +1070,13 @@ function hideThemeButtonState() {
11651070
addClass(popup, "hidden");
11661071
popup.id = "help";
11671072

1073+
popup.addEventListener("click", function(ev) {
1074+
if (ev.target === popup) {
1075+
// Clicked the blurred zone outside the help popup; dismiss help.
1076+
displayHelp(false, ev);
1077+
}
1078+
});
1079+
11681080
var book_info = document.createElement("span");
11691081
book_info.innerHTML = "You can find more information in \
11701082
<a href=\"https://doc.rust-lang.org/rustdoc/\">the rustdoc book</a>.";
@@ -1223,7 +1135,7 @@ function hideThemeButtonState() {
12231135
}
12241136

12251137
onHashChange(null);
1226-
window.onhashchange = onHashChange;
1138+
window.addEventListener("hashchange", onHashChange);
12271139
searchState.setup();
12281140
}());
12291141

src/librustdoc/html/static/rustdoc.css

+4-4
Original file line numberDiff line numberDiff line change
@@ -1289,7 +1289,7 @@ h4 > .notable-traits {
12891289
outline: none;
12901290
}
12911291

1292-
#settings-menu, .help-button {
1292+
#settings-menu, #help-button {
12931293
position: absolute;
12941294
top: 10px;
12951295
}
@@ -1299,7 +1299,7 @@ h4 > .notable-traits {
12991299
outline: none;
13001300
}
13011301

1302-
#theme-picker, #settings-menu, .help-button, #copy-path {
1302+
#theme-picker, #settings-menu, #help-button, #copy-path {
13031303
padding: 4px;
13041304
width: 27px;
13051305
height: 29px;
@@ -1308,7 +1308,7 @@ h4 > .notable-traits {
13081308
cursor: pointer;
13091309
}
13101310

1311-
.help-button {
1311+
#help-button {
13121312
right: 30px;
13131313
font-family: "Fira Sans", Arial, sans-serif;
13141314
text-align: center;
@@ -1593,7 +1593,7 @@ h4 > .notable-traits {
15931593
}
15941594

15951595
/* We don't display the help button on mobile devices. */
1596-
.help-button {
1596+
#help-button {
15971597
display: none;
15981598
}
15991599
.search-container > div {

src/librustdoc/html/static/source-script.js

+97
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
// Local js definitions:
55
/* global addClass, getCurrentValue, hasClass, removeClass, updateLocalStorage */
6+
(function() {
67

78
function getCurrentFilePath() {
89
var parts = window.location.pathname.split("/");
@@ -149,3 +150,99 @@ function createSourceSidebar() {
149150
selected_elem.focus();
150151
}
151152
}
153+
154+
var lineNumbersRegex = /^#?(\d+)(?:-(\d+))?$/;
155+
156+
function highlightSourceLines(match, ev) {
157+
if (typeof match === "undefined") {
158+
match = window.location.hash.match(lineNumbersRegex);
159+
}
160+
if (!match) {
161+
return;
162+
}
163+
var from = parseInt(match[1], 10);
164+
var to = from;
165+
if (typeof match[2] !== "undefined") {
166+
to = parseInt(match[2], 10);
167+
}
168+
if (to < from) {
169+
var tmp = to;
170+
to = from;
171+
from = tmp;
172+
}
173+
var elem = document.getElementById(from);
174+
if (!elem) {
175+
return;
176+
}
177+
if (!ev) {
178+
var x = document.getElementById(from);
179+
if (x) {
180+
x.scrollIntoView();
181+
}
182+
}
183+
onEachLazy(document.getElementsByClassName("line-numbers"), function(e) {
184+
onEachLazy(e.getElementsByTagName("span"), function(i_e) {
185+
removeClass(i_e, "line-highlighted");
186+
});
187+
});
188+
for (var i = from; i <= to; ++i) {
189+
elem = document.getElementById(i);
190+
if (!elem) {
191+
break;
192+
}
193+
addClass(elem, "line-highlighted");
194+
}
195+
}
196+
197+
var handleSourceHighlight = (function() {
198+
var prev_line_id = 0;
199+
200+
var set_fragment = function(name) {
201+
var x = window.scrollX,
202+
y = window.scrollY;
203+
if (searchState.browserSupportsHistoryApi()) {
204+
history.replaceState(null, null, "#" + name);
205+
highlightSourceLines();
206+
} else {
207+
location.replace("#" + name);
208+
}
209+
// Prevent jumps when selecting one or many lines
210+
window.scrollTo(x, y);
211+
};
212+
213+
return function(ev) {
214+
var cur_line_id = parseInt(ev.target.id, 10);
215+
ev.preventDefault();
216+
217+
if (ev.shiftKey && prev_line_id) {
218+
// Swap selection if needed
219+
if (prev_line_id > cur_line_id) {
220+
var tmp = prev_line_id;
221+
prev_line_id = cur_line_id;
222+
cur_line_id = tmp;
223+
}
224+
225+
set_fragment(prev_line_id + "-" + cur_line_id);
226+
} else {
227+
prev_line_id = cur_line_id;
228+
229+
set_fragment(cur_line_id);
230+
}
231+
};
232+
}());
233+
234+
window.addEventListener("hashchange", function() {
235+
var match = window.location.hash.match(lineNumbersRegex);
236+
if (match) {
237+
return highlightSourceLines(match, ev);
238+
}
239+
});
240+
241+
onEachLazy(document.getElementsByClassName("line-numbers"), function(el) {
242+
el.addEventListener("click", handleSourceHighlight);
243+
});
244+
245+
highlightSourceLines();
246+
247+
window.createSourceSidebar = createSourceSidebar;
248+
})();

src/librustdoc/html/static/themes/ayu.css

+2-2
Original file line numberDiff line numberDiff line change
@@ -503,7 +503,7 @@ kbd {
503503
box-shadow-color: #c6cbd1;
504504
}
505505

506-
#theme-picker, #settings-menu, .help-button, #copy-path {
506+
#theme-picker, #settings-menu, #help-button, #copy-path {
507507
border-color: #5c6773;
508508
background-color: #0f1419;
509509
color: #fff;
@@ -515,7 +515,7 @@ kbd {
515515

516516
#theme-picker:hover, #theme-picker:focus,
517517
#settings-menu:hover, #settings-menu:focus,
518-
.help-button:hover, .help-button:focus,
518+
#help-button:hover, #help-button:focus,
519519
#copy-path:hover, #copy-path:focus {
520520
border-color: #e0e0e0;
521521
}

src/librustdoc/html/static/themes/dark.css

+2-2
Original file line numberDiff line numberDiff line change
@@ -393,15 +393,15 @@ kbd {
393393
box-shadow-color: #c6cbd1;
394394
}
395395

396-
#theme-picker, #settings-menu, .help-button, #copy-path {
396+
#theme-picker, #settings-menu, #help-button, #copy-path {
397397
border-color: #e0e0e0;
398398
background: #f0f0f0;
399399
color: #000;
400400
}
401401

402402
#theme-picker:hover, #theme-picker:focus,
403403
#settings-menu:hover, #settings-menu:focus,
404-
.help-button:hover, .help-button:focus,
404+
#help-button:hover, #help-button:focus,
405405
#copy-path:hover, #copy-path:focus {
406406
border-color: #ffb900;
407407
}

0 commit comments

Comments
 (0)