Skip to content

Commit 378f851

Browse files
Rollup merge of rust-lang#100956 - GuillaumeGomez:reduce-rightside-dom-size, r=notriddle
Reduce right-side DOM size This is another follow-up of rust-lang#100429 but not in code blocks this time. So the idea is: if there is only one element in the `.rightside` element, there is no need to wrap it, we can just create one node. On each page, I run this JS: `document.getElementsByTagName('*').length`. Important to note: the bigger the number of elements inside the page, the greater the gain. It also doesn't work very nicely on std docs because there are a lot of version annotations. So with this PR, It allows to get the following results: | file name | before this PR | with this PR | diff | |-|-|-|-| | std/default/trait.Default.html | 2189 | 1331 | 39.2% | | std/vec/struct.Vec.html | 14073 | 13842 | 1.7% | | std/fmt/trait.Debug.html | 5313 | 4907 | 7.7% | | std/ops/trait.Index.html | 642 | 630 | 1.9% | | gtk4/WidgetExt | 3269 | 3061 | 6.4% | You can test it [here](https://rustdoc.crud.net/imperio/reduce-rightsize-dom-size/gtk4/prelude/trait.WidgetExt.html). r? `@notriddle`
2 parents 7881e05 + 38eb33b commit 378f851

18 files changed

+186
-60
lines changed

src/librustdoc/html/render/mod.rs

+34-15
Original file line numberDiff line numberDiff line change
@@ -191,12 +191,6 @@ impl StylePath {
191191
}
192192
}
193193

194-
fn write_srclink(cx: &Context<'_>, item: &clean::Item, buf: &mut Buffer) {
195-
if let Some(l) = cx.src_href(item) {
196-
write!(buf, "<a class=\"srclink\" href=\"{}\">source</a>", l)
197-
}
198-
}
199-
200194
#[derive(Debug, Eq, PartialEq, Hash)]
201195
struct ItemEntry {
202196
url: String,
@@ -840,12 +834,13 @@ fn assoc_method(
840834
/// Note that it is possible for an unstable function to be const-stable. In that case, the span
841835
/// will include the const-stable version, but no stable version will be emitted, as a natural
842836
/// consequence of the above rules.
843-
fn render_stability_since_raw(
837+
fn render_stability_since_raw_with_extra(
844838
w: &mut Buffer,
845839
ver: Option<Symbol>,
846840
const_stability: Option<ConstStability>,
847841
containing_ver: Option<Symbol>,
848842
containing_const_ver: Option<Symbol>,
843+
extra_class: &str,
849844
) -> bool {
850845
let stable_version = ver.filter(|inner| !inner.is_empty() && Some(*inner) != containing_ver);
851846

@@ -893,12 +888,30 @@ fn render_stability_since_raw(
893888
}
894889

895890
if !stability.is_empty() {
896-
write!(w, r#"<span class="since" title="{}">{}</span>"#, title, stability);
891+
write!(w, r#"<span class="since{extra_class}" title="{title}">{stability}</span>"#);
897892
}
898893

899894
!stability.is_empty()
900895
}
901896

897+
#[inline]
898+
fn render_stability_since_raw(
899+
w: &mut Buffer,
900+
ver: Option<Symbol>,
901+
const_stability: Option<ConstStability>,
902+
containing_ver: Option<Symbol>,
903+
containing_const_ver: Option<Symbol>,
904+
) -> bool {
905+
render_stability_since_raw_with_extra(
906+
w,
907+
ver,
908+
const_stability,
909+
containing_ver,
910+
containing_const_ver,
911+
"",
912+
)
913+
}
914+
902915
fn render_assoc_item(
903916
w: &mut Buffer,
904917
item: &clean::Item,
@@ -1681,23 +1694,29 @@ fn render_rightside(
16811694
RenderMode::Normal => (item.const_stability(tcx), containing_item.const_stable_since(tcx)),
16821695
RenderMode::ForDeref { .. } => (None, None),
16831696
};
1697+
let src_href = cx.src_href(item);
1698+
let has_src_ref = src_href.is_some();
16841699

16851700
let mut rightside = Buffer::new();
1686-
let has_stability = render_stability_since_raw(
1701+
let has_stability = render_stability_since_raw_with_extra(
16871702
&mut rightside,
16881703
item.stable_since(tcx),
16891704
const_stability,
16901705
containing_item.stable_since(tcx),
16911706
const_stable_since,
1707+
if has_src_ref { "" } else { " rightside" },
16921708
);
1693-
let mut srclink = Buffer::empty_from(w);
1694-
write_srclink(cx, item, &mut srclink);
1695-
if has_stability && !srclink.is_empty() {
1696-
rightside.write_str(" · ");
1709+
if let Some(l) = src_href {
1710+
if has_stability {
1711+
write!(rightside, " · <a class=\"srclink\" href=\"{}\">source</a>", l)
1712+
} else {
1713+
write!(rightside, "<a class=\"srclink rightside\" href=\"{}\">source</a>", l)
1714+
}
16971715
}
1698-
rightside.push_buffer(srclink);
1699-
if !rightside.is_empty() {
1716+
if has_stability && has_src_ref {
17001717
write!(w, "<span class=\"rightside\">{}</span>", rightside.into_inner());
1718+
} else {
1719+
w.push_buffer(rightside);
17011720
}
17021721
}
17031722

src/librustdoc/html/render/print_item.rs

+9-25
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ use std::rc::Rc;
1818
use super::{
1919
collect_paths_for_type, document, ensure_trailing_slash, item_ty_to_section,
2020
notable_traits_decl, render_assoc_item, render_assoc_items, render_attributes_in_code,
21-
render_attributes_in_pre, render_impl, render_stability_since_raw, write_srclink,
21+
render_attributes_in_pre, render_impl, render_rightside, render_stability_since_raw,
2222
AssocItemLink, Context, ImplRenderingParameters,
2323
};
2424
use crate::clean;
@@ -709,14 +709,7 @@ fn item_trait(w: &mut Buffer, cx: &mut Context<'_>, it: &clean::Item, t: &clean:
709709
write!(w, "<details class=\"rustdoc-toggle\" open><summary>");
710710
}
711711
write!(w, "<div id=\"{}\" class=\"method has-srclink\">", id);
712-
write!(w, "<div class=\"rightside\">");
713-
714-
let has_stability = render_stability_since(w, m, t, cx.tcx());
715-
if has_stability {
716-
w.write_str(" · ");
717-
}
718-
write_srclink(cx, m, w);
719-
write!(w, "</div>");
712+
render_rightside(w, cx, m, t, RenderMode::Normal);
720713
write!(w, "<h4 class=\"code-header\">");
721714
render_assoc_item(
722715
w,
@@ -1260,7 +1253,13 @@ fn item_enum(w: &mut Buffer, cx: &mut Context<'_>, it: &clean::Item, e: &clean::
12601253
w.write_str(")");
12611254
}
12621255
w.write_str("</code>");
1263-
render_stability_since(w, variant, it, cx.tcx());
1256+
render_stability_since_raw(
1257+
w,
1258+
variant.stable_since(cx.tcx()),
1259+
variant.const_stability(cx.tcx()),
1260+
it.stable_since(cx.tcx()),
1261+
it.const_stable_since(cx.tcx()),
1262+
);
12641263
w.write_str("</h3>");
12651264

12661265
use crate::clean::Variant;
@@ -1591,21 +1590,6 @@ where
15911590
w.write_str("</code></pre>");
15921591
}
15931592

1594-
fn render_stability_since(
1595-
w: &mut Buffer,
1596-
item: &clean::Item,
1597-
containing_item: &clean::Item,
1598-
tcx: TyCtxt<'_>,
1599-
) -> bool {
1600-
render_stability_since_raw(
1601-
w,
1602-
item.stable_since(tcx),
1603-
item.const_stability(tcx),
1604-
containing_item.stable_since(tcx),
1605-
containing_item.const_stable_since(tcx),
1606-
)
1607-
}
1608-
16091593
fn compare_impl<'a, 'b>(lhs: &'a &&Impl, rhs: &'b &&Impl, cx: &Context<'_>) -> Ordering {
16101594
let lhss = format!("{}", lhs.inner_impl().print(false, cx));
16111595
let rhss = format!("{}", rhs.inner_impl().print(false, cx));

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -199,7 +199,7 @@ details.rustdoc-toggle > summary::before {
199199
background: none;
200200
}
201201

202-
.rightside,
202+
.rightside:not(a),
203203
.out-of-band {
204204
color: grey;
205205
}

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -165,7 +165,7 @@ details.rustdoc-toggle > summary::before {
165165
background: none;
166166
}
167167

168-
.rightside,
168+
.rightside:not(a),
169169
.out-of-band {
170170
color: grey;
171171
}

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,7 @@ details.rustdoc-toggle > summary::before {
148148
.stab { background: #FFF5D6; border-color: #FFC600; }
149149
.stab.portability > code { background: none; }
150150

151-
.rightside,
151+
.rightside:not(a),
152152
.out-of-band {
153153
color: grey;
154154
}

src/test/rustdoc-gui/anchors.goml

+124-4
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
// This test is to ensure that the anchors (`§`) have the expected color and position.
2-
goto: file://|DOC_PATH|/test_docs/struct.HeavilyDocumentedStruct.html
3-
show-text: true
2+
goto: file://|DOC_PATH|/staged_api/struct.Foo.html
43

54
// This is needed to ensure that the text color is computed.
65
show-text: true
@@ -13,10 +12,31 @@ reload:
1312
assert-css: ("#toggle-all-docs", {"color": "rgb(0, 0, 0)"})
1413
assert-css: (".fqn .in-band a:nth-of-type(1)", {"color": "rgb(0, 0, 0)"})
1514
assert-css: (".fqn .in-band a:nth-of-type(2)", {"color": "rgb(173, 55, 138)"})
16-
assert-css: (".srclink", {"color": "rgb(56, 115, 173)"})
15+
assert-css: (
16+
".rightside .srclink",
17+
{"color": "rgb(56, 115, 173)", "text-decoration": "none solid rgb(56, 115, 173)"},
18+
ALL,
19+
)
20+
compare-elements-css: (".rightside .srclink", ".rightside.srclink", ["color", "text-decoration"])
21+
compare-elements-css: (".main-heading .srclink", ".rightside.srclink", ["color", "text-decoration"])
1722

1823
move-cursor-to: ".main-heading .srclink"
19-
assert-css: (".srclink", {"text-decoration": "underline solid rgb(56, 115, 173)"})
24+
assert-css: (
25+
".main-heading .srclink",
26+
{"color": "rgb(56, 115, 173)", "text-decoration": "underline solid rgb(56, 115, 173)"},
27+
)
28+
move-cursor-to: ".impl-items .rightside .srclink"
29+
assert-css: (
30+
".impl-items .rightside .srclink",
31+
{"color": "rgb(56, 115, 173)", "text-decoration": "none solid rgb(56, 115, 173)"},
32+
)
33+
move-cursor-to: ".impl-items .rightside.srclink"
34+
assert-css: (
35+
".impl-items .rightside.srclink",
36+
{"color": "rgb(56, 115, 173)", "text-decoration": "none solid rgb(56, 115, 173)"},
37+
)
38+
39+
goto: file://|DOC_PATH|/test_docs/struct.HeavilyDocumentedStruct.html
2040

2141
assert-css: ("#top-doc-prose-title", {"color": "rgb(0, 0, 0)"})
2242

@@ -32,3 +52,103 @@ move-cursor-to: "#impl-HeavilyDocumentedStruct"
3252
assert-css: ("#impl-HeavilyDocumentedStruct a.anchor", {"color": "rgb(0, 0, 0)"})
3353

3454
assert-css: ("#title-for-struct-impl-item-doc", {"margin-left": "0px"})
55+
56+
//
57+
// We do the same checks with the dark theme now.
58+
//
59+
local-storage: {"rustdoc-theme": "dark", "rustdoc-use-system-theme": "false"}
60+
goto: file://|DOC_PATH|/staged_api/struct.Foo.html
61+
62+
assert-css: ("#toggle-all-docs", {"color": "rgb(221, 221, 221)"})
63+
assert-css: (".fqn .in-band a:nth-of-type(1)", {"color": "rgb(221, 221, 221)"})
64+
assert-css: (".fqn .in-band a:nth-of-type(2)", {"color": "rgb(45, 191, 184)"})
65+
assert-css: (
66+
".rightside .srclink",
67+
{"color": "rgb(210, 153, 29)", "text-decoration": "none solid rgb(210, 153, 29)"},
68+
ALL,
69+
)
70+
compare-elements-css: (".rightside .srclink", ".rightside.srclink", ["color", "text-decoration"])
71+
compare-elements-css: (".main-heading .srclink", ".rightside.srclink", ["color", "text-decoration"])
72+
73+
move-cursor-to: ".main-heading .srclink"
74+
assert-css: (
75+
".main-heading .srclink",
76+
{"color": "rgb(210, 153, 29)", "text-decoration": "underline solid rgb(210, 153, 29)"},
77+
)
78+
move-cursor-to: ".impl-items .rightside .srclink"
79+
assert-css: (
80+
".impl-items .rightside .srclink",
81+
{"color": "rgb(210, 153, 29)", "text-decoration": "none solid rgb(210, 153, 29)"},
82+
)
83+
move-cursor-to: ".impl-items .rightside.srclink"
84+
assert-css: (
85+
".impl-items .rightside.srclink",
86+
{"color": "rgb(210, 153, 29)", "text-decoration": "none solid rgb(210, 153, 29)"},
87+
)
88+
89+
goto: file://|DOC_PATH|/test_docs/struct.HeavilyDocumentedStruct.html
90+
91+
assert-css: ("#top-doc-prose-title", {"color": "rgb(221, 221, 221)"})
92+
93+
assert-css: (".sidebar a", {"color": "rgb(253, 191, 53)"})
94+
assert-css: (".in-band a", {"color": "rgb(221, 221, 221)"})
95+
96+
// We move the cursor over the "Implementations" title so the anchor is displayed.
97+
move-cursor-to: "h2#implementations"
98+
assert-css: ("h2#implementations a.anchor", {"color": "rgb(221, 221, 221)"})
99+
100+
// Same thing with the impl block title.
101+
move-cursor-to: "#impl-HeavilyDocumentedStruct"
102+
assert-css: ("#impl-HeavilyDocumentedStruct a.anchor", {"color": "rgb(221, 221, 221)"})
103+
104+
assert-css: ("#title-for-struct-impl-item-doc", {"margin-left": "0px"})
105+
106+
//
107+
// We do the same checks with the ayu theme now.
108+
//
109+
local-storage: {"rustdoc-theme": "ayu", "rustdoc-use-system-theme": "false"}
110+
goto: file://|DOC_PATH|/staged_api/struct.Foo.html
111+
112+
assert-css: ("#toggle-all-docs", {"color": "rgb(197, 197, 197)"})
113+
assert-css: (".fqn .in-band a:nth-of-type(1)", {"color": "rgb(255, 255, 255)"})
114+
assert-css: (".fqn .in-band a:nth-of-type(2)", {"color": "rgb(255, 160, 165)"})
115+
assert-css: (
116+
".rightside .srclink",
117+
{"color": "rgb(57, 175, 215)", "text-decoration": "none solid rgb(57, 175, 215)"},
118+
ALL,
119+
)
120+
compare-elements-css: (".rightside .srclink", ".rightside.srclink", ["color", "text-decoration"])
121+
compare-elements-css: (".main-heading .srclink", ".rightside.srclink", ["color", "text-decoration"])
122+
123+
move-cursor-to: ".main-heading .srclink"
124+
assert-css: (
125+
".main-heading .srclink",
126+
{"color": "rgb(57, 175, 215)", "text-decoration": "underline solid rgb(57, 175, 215)"},
127+
)
128+
move-cursor-to: ".impl-items .rightside .srclink"
129+
assert-css: (
130+
".impl-items .rightside .srclink",
131+
{"color": "rgb(57, 175, 215)", "text-decoration": "none solid rgb(57, 175, 215)"},
132+
)
133+
move-cursor-to: ".impl-items .rightside.srclink"
134+
assert-css: (
135+
".impl-items .rightside.srclink",
136+
{"color": "rgb(57, 175, 215)", "text-decoration": "none solid rgb(57, 175, 215)"},
137+
)
138+
139+
goto: file://|DOC_PATH|/test_docs/struct.HeavilyDocumentedStruct.html
140+
141+
assert-css: ("#top-doc-prose-title", {"color": "rgb(255, 255, 255)"})
142+
143+
assert-css: (".sidebar a", {"color": "rgb(83, 177, 219)"})
144+
assert-css: (".in-band a", {"color": "rgb(255, 255, 255)"})
145+
146+
// We move the cursor over the "Implementations" title so the anchor is displayed.
147+
move-cursor-to: "h2#implementations"
148+
assert-css: ("h2#implementations a.anchor", {"color": "rgb(197, 197, 197)"})
149+
150+
// Same thing with the impl block title.
151+
move-cursor-to: "#impl-HeavilyDocumentedStruct"
152+
assert-css: ("#impl-HeavilyDocumentedStruct a.anchor", {"color": "rgb(197, 197, 197)"})
153+
154+
assert-css: ("#title-for-struct-impl-item-doc", {"margin-left": "0px"})

src/test/rustdoc-gui/headings.goml

+3-3
Original file line numberDiff line numberDiff line change
@@ -247,12 +247,12 @@ assert-css: (
247247

248248
local-storage: {"rustdoc-theme": "light"}
249249
goto: file://|DOC_PATH|/staged_api/struct.Foo.html
250-
assert-css: (".since", {"color": "rgb(128, 128, 128)"})
250+
assert-css: (".since", {"color": "rgb(128, 128, 128)"}, ALL)
251251

252252
local-storage: {"rustdoc-theme": "dark"}
253253
reload:
254-
assert-css: (".since", {"color": "rgb(128, 128, 128)"})
254+
assert-css: (".since", {"color": "rgb(128, 128, 128)"}, ALL)
255255

256256
local-storage: {"rustdoc-theme": "ayu"}
257257
reload:
258-
assert-css: (".since", {"color": "rgb(128, 128, 128)"})
258+
assert-css: (".since", {"color": "rgb(128, 128, 128)"}, ALL)

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

+2-1
Original file line numberDiff line numberDiff line change
@@ -7,5 +7,6 @@ edition = "2021"
77
path = "lib.rs"
88

99
[features]
10-
default = ["some_feature"]
10+
default = ["some_feature", "some_other_feature"]
1111
some_feature = []
12+
some_other_feature = []

src/test/rustdoc-gui/src/staged_api/lib.rs

+2
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,6 @@ pub struct Foo {}
77
impl Foo {
88
#[stable(feature = "some_feature", since = "1.3.5")]
99
pub fn bar() {}
10+
#[stable(feature = "some_other_feature", since = "1.3.6")]
11+
pub fn yo() {}
1012
}
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
<div id="associatedconstant.YOLO" class="method has-srclink"><div class="rightside"><a class="srclink" href="../src/foo/anchors.rs.html#16">source</a></div><h4 class="code-header">const <a href="#associatedconstant.YOLO" class="constant">YOLO</a>: <a class="primitive" href="{{channel}}/std/primitive.u32.html">u32</a></h4></div>
1+
<div id="associatedconstant.YOLO" class="method has-srclink"><a class="srclink rightside" href="../src/foo/anchors.rs.html#16">source</a><h4 class="code-header">const <a href="#associatedconstant.YOLO" class="constant">YOLO</a>: <a class="primitive" href="{{channel}}/std/primitive.u32.html">u32</a></h4></div>
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
<section id="associatedconstant.X" class="associatedconstant has-srclink"><span class="rightside"><a class="srclink" href="../src/foo/anchors.rs.html#42">source</a></span><h4 class="code-header">pub const <a href="#associatedconstant.X" class="constant">X</a>: <a class="primitive" href="{{channel}}/std/primitive.i32.html">i32</a> = 0i32</h4></section>
1+
<section id="associatedconstant.X" class="associatedconstant has-srclink"><a class="srclink rightside" href="../src/foo/anchors.rs.html#42">source</a><h4 class="code-header">pub const <a href="#associatedconstant.X" class="constant">X</a>: <a class="primitive" href="{{channel}}/std/primitive.i32.html">i32</a> = 0i32</h4></section>
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
<section id="method.new" class="method has-srclink"><span class="rightside"><a class="srclink" href="../src/foo/anchors.rs.html#48">source</a></span><h4 class="code-header">pub fn <a href="#method.new" class="fnname">new</a>() -&gt; Self</h4></section>
1+
<section id="method.new" class="method has-srclink"><a class="srclink rightside" href="../src/foo/anchors.rs.html#48">source</a><h4 class="code-header">pub fn <a href="#method.new" class="fnname">new</a>() -&gt; Self</h4></section>
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
<div id="method.bar" class="method has-srclink"><div class="rightside"><a class="srclink" href="../src/foo/anchors.rs.html#23">source</a></div><h4 class="code-header">fn <a href="#method.bar" class="fnname">bar</a>()</h4></div>
1+
<div id="method.bar" class="method has-srclink"><a class="srclink rightside" href="../src/foo/anchors.rs.html#23">source</a><h4 class="code-header">fn <a href="#method.bar" class="fnname">bar</a>()</h4></div>
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
<div id="tymethod.foo" class="method has-srclink"><div class="rightside"><a class="srclink" href="../src/foo/anchors.rs.html#20">source</a></div><h4 class="code-header">fn <a href="#tymethod.foo" class="fnname">foo</a>()</h4></div>
1+
<div id="tymethod.foo" class="method has-srclink"><a class="srclink rightside" href="../src/foo/anchors.rs.html#20">source</a><h4 class="code-header">fn <a href="#tymethod.foo" class="fnname">foo</a>()</h4></div>
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
<div id="associatedtype.T" class="method has-srclink"><div class="rightside"><a class="srclink" href="../src/foo/anchors.rs.html#13">source</a></div><h4 class="code-header">type <a href="#associatedtype.T" class="associatedtype">T</a></h4></div>
1+
<div id="associatedtype.T" class="method has-srclink"><a class="srclink rightside" href="../src/foo/anchors.rs.html#13">source</a><h4 class="code-header">type <a href="#associatedtype.T" class="associatedtype">T</a></h4></div>

src/test/rustdoc/ensure-src-link.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,5 @@
22

33
// This test ensures that the [src] link is present on traits items.
44

5-
// @has foo/trait.Iterator.html '//*[@id="method.zip"]//a[@class="srclink"]' "source"
5+
// @has foo/trait.Iterator.html '//*[@id="method.zip"]//a[@class="srclink rightside"]' "source"
66
pub use std::iter::Iterator;

0 commit comments

Comments
 (0)