Skip to content

Commit 8846c08

Browse files
committed
rustdoc: use CSS containment to speed up render
https://developer.mozilla.org/en-US/docs/Web/CSS/CSS_Containment This affected layout a little and required adjustments to the CSS to keep spacing the same. In particular, the margins of adjacent items usually overlap with each other. However, when an item has contain: layout, any margins of child nodes push out the size of the item itself. This was making spacing between items a little too big. To solve that, I removed margins in some places: in particular for certain classes that often occur at the end of a `details.rustdoc-toggle` block, I removed their bottom margin. Generally, the margins provided by the next item down are sufficient. Also remove an unnecessary margin-top on .code-header.
1 parent 57ee5cf commit 8846c08

File tree

2 files changed

+32
-16
lines changed

2 files changed

+32
-16
lines changed

src/librustdoc/html/static/css/rustdoc.css

+29-13
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,7 @@ h1, h2, h3, h4, h5, h6 {
132132
font-weight: 500;
133133
}
134134
h1, h2, h3, h4 {
135-
margin: 20px 0 15px 0;
135+
margin: 25px 0 15px 0;
136136
padding-bottom: 6px;
137137
}
138138
.docblock h3, .docblock h4, h5, h6 {
@@ -176,8 +176,6 @@ h4.code-header {
176176
border-bottom-style: none;
177177
margin: 0;
178178
padding: 0;
179-
margin-top: 0.6rem;
180-
margin-bottom: 0.4rem;
181179
}
182180
.impl,
183181
.impl-items .method,
@@ -292,6 +290,11 @@ p {
292290
https://www.w3.org/WAI/WCAG21/Understanding/visual-presentation.html */
293291
margin: 0 0 .75em 0;
294292
}
293+
/* For the last child of a div, the margin will be taken care of
294+
by the margin-top of the next item. */
295+
p:last-child {
296+
margin: 0;
297+
}
295298

296299
summary {
297300
outline: none;
@@ -565,9 +568,16 @@ h2.location a {
565568

566569
.rustdoc .example-wrap {
567570
display: flex;
568-
margin-bottom: 10px;
569571
position: relative;
570572
}
573+
.rustdoc .example-wrap {
574+
margin-bottom: 10px;
575+
}
576+
/* For the last child of a div, the margin will be taken care of
577+
by the margin-top of the next item. */
578+
.rustdoc .example-wrap:last-child {
579+
margin-bottom: 0px;
580+
}
571581

572582
pre.example-line-numbers {
573583
overflow: initial;
@@ -726,10 +736,6 @@ pre, .rustdoc.source .example-wrap {
726736
margin-left: 24px;
727737
}
728738

729-
.content .impl-items .docblock, .content .impl-items .item-info {
730-
margin-bottom: .6em;
731-
}
732-
733739
#main-content > .item-info {
734740
margin-top: 0;
735741
margin-left: 0;
@@ -1532,6 +1538,16 @@ details.dir-entry a {
15321538
display: block;
15331539
}
15341540

1541+
/* We use CSS containment on the details elements because most sizeable elements
1542+
of the page are contained in one of these. This also makes re-rendering
1543+
faster on document changes (like closing and opening toggles).
1544+
Unfortunately we can't yet specify contain: content or contain: strict
1545+
because the [-]/[+] toggles extend past the boundaries of the <details>
1546+
https://developer.mozilla.org/en-US/docs/Web/CSS/contain */
1547+
details.rustdoc-toggle {
1548+
contain: layout;
1549+
}
1550+
15351551
/* The hideme class is used on summary tags that contain a span with
15361552
placeholder text shown only when the toggle is closed. For instance,
15371553
"Expand description" or "Show methods". */
@@ -2012,17 +2028,17 @@ in storage.js plus the media query with (min-width: 701px)
20122028
margin-bottom: 0.75em;
20132029
}
20142030

2015-
.method-toggle[open] {
2031+
.method-toggle[open]:not(:last-child) {
20162032
margin-bottom: 2em;
20172033
}
20182034

2019-
.implementors-toggle[open] {
2035+
.implementors-toggle[open]:not(:last-child) {
20202036
margin-bottom: 2em;
20212037
}
20222038

2023-
#trait-implementations-list .method-toggle,
2024-
#synthetic-implementations-list .method-toggle,
2025-
#blanket-implementations-list .method-toggle {
2039+
#trait-implementations-list .method-toggle:not(:last-child),
2040+
#synthetic-implementations-list .method-toggle:not(:last-child),
2041+
#blanket-implementations-list .method-toggle:not(:last-child) {
20262042
margin-bottom: 1em;
20272043
}
20282044

src/test/rustdoc-gui/sidebar-mobile-scroll.goml

+3-3
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ assert-css: (".sidebar", {"display": "block", "left": "-1000px"})
66

77
// Scroll down.
88
scroll-to: "//h2[@id='blanket-implementations']"
9-
assert-window-property: {"pageYOffset": "643"}
9+
assert-window-property: {"pageYOffset": "645"}
1010

1111
// Open the sidebar menu.
1212
click: ".sidebar-menu-toggle"
@@ -21,11 +21,11 @@ assert-window-property: {"pageYOffset": "0"}
2121
// Close the sidebar menu. Make sure the scroll position gets restored.
2222
click: ".sidebar-menu-toggle"
2323
wait-for-css: (".sidebar", {"left": "-1000px"})
24-
assert-window-property: {"pageYOffset": "643"}
24+
assert-window-property: {"pageYOffset": "645"}
2525

2626
// Now test that scrollability returns when the browser window is just resized.
2727
click: ".sidebar-menu-toggle"
2828
wait-for-css: (".sidebar", {"left": "0px"})
2929
assert-window-property: {"pageYOffset": "0"}
3030
size: (900, 600)
31-
assert-window-property: {"pageYOffset": "643"}
31+
assert-window-property: {"pageYOffset": "645"}

0 commit comments

Comments
 (0)