Skip to content

Reduce right-side DOM size #100956

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Aug 26, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 34 additions & 15 deletions src/librustdoc/html/render/mod.rs
Original file line number Diff line number Diff line change
@@ -191,12 +191,6 @@ impl StylePath {
}
}

fn write_srclink(cx: &Context<'_>, item: &clean::Item, buf: &mut Buffer) {
if let Some(l) = cx.src_href(item) {
write!(buf, "<a class=\"srclink\" href=\"{}\">source</a>", l)
}
}

#[derive(Debug, Eq, PartialEq, Hash)]
struct ItemEntry {
url: String,
@@ -840,12 +834,13 @@ fn assoc_method(
/// Note that it is possible for an unstable function to be const-stable. In that case, the span
/// will include the const-stable version, but no stable version will be emitted, as a natural
/// consequence of the above rules.
fn render_stability_since_raw(
fn render_stability_since_raw_with_extra(
w: &mut Buffer,
ver: Option<Symbol>,
const_stability: Option<ConstStability>,
containing_ver: Option<Symbol>,
containing_const_ver: Option<Symbol>,
extra_class: &str,
) -> bool {
let stable_version = ver.filter(|inner| !inner.is_empty() && Some(*inner) != containing_ver);

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

if !stability.is_empty() {
write!(w, r#"<span class="since" title="{}">{}</span>"#, title, stability);
write!(w, r#"<span class="since{extra_class}" title="{title}">{stability}</span>"#);
}

!stability.is_empty()
}

#[inline]
fn render_stability_since_raw(
w: &mut Buffer,
ver: Option<Symbol>,
const_stability: Option<ConstStability>,
containing_ver: Option<Symbol>,
containing_const_ver: Option<Symbol>,
) -> bool {
render_stability_since_raw_with_extra(
w,
ver,
const_stability,
containing_ver,
containing_const_ver,
"",
)
}

fn render_assoc_item(
w: &mut Buffer,
item: &clean::Item,
@@ -1681,23 +1694,29 @@ fn render_rightside(
RenderMode::Normal => (item.const_stability(tcx), containing_item.const_stable_since(tcx)),
RenderMode::ForDeref { .. } => (None, None),
};
let src_href = cx.src_href(item);
let has_src_ref = src_href.is_some();

let mut rightside = Buffer::new();
let has_stability = render_stability_since_raw(
let has_stability = render_stability_since_raw_with_extra(
&mut rightside,
item.stable_since(tcx),
const_stability,
containing_item.stable_since(tcx),
const_stable_since,
if has_src_ref { "" } else { " rightside" },
);
let mut srclink = Buffer::empty_from(w);
write_srclink(cx, item, &mut srclink);
if has_stability && !srclink.is_empty() {
rightside.write_str(" · ");
if let Some(l) = src_href {
if has_stability {
write!(rightside, " · <a class=\"srclink\" href=\"{}\">source</a>", l)
} else {
write!(rightside, "<a class=\"srclink rightside\" href=\"{}\">source</a>", l)
}
}
rightside.push_buffer(srclink);
if !rightside.is_empty() {
if has_stability && has_src_ref {
write!(w, "<span class=\"rightside\">{}</span>", rightside.into_inner());
} else {
w.push_buffer(rightside);
}
}

34 changes: 9 additions & 25 deletions src/librustdoc/html/render/print_item.rs
Original file line number Diff line number Diff line change
@@ -18,7 +18,7 @@ use std::rc::Rc;
use super::{
collect_paths_for_type, document, ensure_trailing_slash, item_ty_to_section,
notable_traits_decl, render_assoc_item, render_assoc_items, render_attributes_in_code,
render_attributes_in_pre, render_impl, render_stability_since_raw, write_srclink,
render_attributes_in_pre, render_impl, render_rightside, render_stability_since_raw,
AssocItemLink, Context, ImplRenderingParameters,
};
use crate::clean;
@@ -709,14 +709,7 @@ fn item_trait(w: &mut Buffer, cx: &mut Context<'_>, it: &clean::Item, t: &clean:
write!(w, "<details class=\"rustdoc-toggle\" open><summary>");
}
write!(w, "<div id=\"{}\" class=\"method has-srclink\">", id);
write!(w, "<div class=\"rightside\">");

let has_stability = render_stability_since(w, m, t, cx.tcx());
if has_stability {
w.write_str(" · ");
}
write_srclink(cx, m, w);
write!(w, "</div>");
render_rightside(w, cx, m, t, RenderMode::Normal);
write!(w, "<h4 class=\"code-header\">");
render_assoc_item(
w,
@@ -1260,7 +1253,13 @@ fn item_enum(w: &mut Buffer, cx: &mut Context<'_>, it: &clean::Item, e: &clean::
w.write_str(")");
}
w.write_str("</code>");
render_stability_since(w, variant, it, cx.tcx());
render_stability_since_raw(
w,
variant.stable_since(cx.tcx()),
variant.const_stability(cx.tcx()),
it.stable_since(cx.tcx()),
it.const_stable_since(cx.tcx()),
);
w.write_str("</h3>");

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

fn render_stability_since(
w: &mut Buffer,
item: &clean::Item,
containing_item: &clean::Item,
tcx: TyCtxt<'_>,
) -> bool {
render_stability_since_raw(
w,
item.stable_since(tcx),
item.const_stability(tcx),
containing_item.stable_since(tcx),
containing_item.const_stable_since(tcx),
)
}

fn compare_impl<'a, 'b>(lhs: &'a &&Impl, rhs: &'b &&Impl, cx: &Context<'_>) -> Ordering {
let lhss = format!("{}", lhs.inner_impl().print(false, cx));
let rhss = format!("{}", rhs.inner_impl().print(false, cx));
2 changes: 1 addition & 1 deletion src/librustdoc/html/static/css/themes/ayu.css
Original file line number Diff line number Diff line change
@@ -199,7 +199,7 @@ details.rustdoc-toggle > summary::before {
background: none;
}

.rightside,
.rightside:not(a),
.out-of-band {
color: grey;
}
2 changes: 1 addition & 1 deletion src/librustdoc/html/static/css/themes/dark.css
Original file line number Diff line number Diff line change
@@ -165,7 +165,7 @@ details.rustdoc-toggle > summary::before {
background: none;
}

.rightside,
.rightside:not(a),
.out-of-band {
color: grey;
}
2 changes: 1 addition & 1 deletion src/librustdoc/html/static/css/themes/light.css
Original file line number Diff line number Diff line change
@@ -148,7 +148,7 @@ details.rustdoc-toggle > summary::before {
.stab { background: #FFF5D6; border-color: #FFC600; }
.stab.portability > code { background: none; }

.rightside,
.rightside:not(a),
.out-of-band {
color: grey;
}
128 changes: 124 additions & 4 deletions src/test/rustdoc-gui/anchors.goml
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
// This test is to ensure that the anchors (`§`) have the expected color and position.
goto: file://|DOC_PATH|/test_docs/struct.HeavilyDocumentedStruct.html
show-text: true
goto: file://|DOC_PATH|/staged_api/struct.Foo.html

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

move-cursor-to: ".main-heading .srclink"
assert-css: (".srclink", {"text-decoration": "underline solid rgb(56, 115, 173)"})
assert-css: (
".main-heading .srclink",
{"color": "rgb(56, 115, 173)", "text-decoration": "underline solid rgb(56, 115, 173)"},
)
move-cursor-to: ".impl-items .rightside .srclink"
assert-css: (
".impl-items .rightside .srclink",
{"color": "rgb(56, 115, 173)", "text-decoration": "none solid rgb(56, 115, 173)"},
)
move-cursor-to: ".impl-items .rightside.srclink"
assert-css: (
".impl-items .rightside.srclink",
{"color": "rgb(56, 115, 173)", "text-decoration": "none solid rgb(56, 115, 173)"},
)

goto: file://|DOC_PATH|/test_docs/struct.HeavilyDocumentedStruct.html

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

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

assert-css: ("#title-for-struct-impl-item-doc", {"margin-left": "0px"})

//
// We do the same checks with the dark theme now.
//
local-storage: {"rustdoc-theme": "dark", "rustdoc-use-system-theme": "false"}
goto: file://|DOC_PATH|/staged_api/struct.Foo.html

assert-css: ("#toggle-all-docs", {"color": "rgb(221, 221, 221)"})
assert-css: (".fqn .in-band a:nth-of-type(1)", {"color": "rgb(221, 221, 221)"})
assert-css: (".fqn .in-band a:nth-of-type(2)", {"color": "rgb(45, 191, 184)"})
assert-css: (
".rightside .srclink",
{"color": "rgb(210, 153, 29)", "text-decoration": "none solid rgb(210, 153, 29)"},
ALL,
)
compare-elements-css: (".rightside .srclink", ".rightside.srclink", ["color", "text-decoration"])
compare-elements-css: (".main-heading .srclink", ".rightside.srclink", ["color", "text-decoration"])

move-cursor-to: ".main-heading .srclink"
assert-css: (
".main-heading .srclink",
{"color": "rgb(210, 153, 29)", "text-decoration": "underline solid rgb(210, 153, 29)"},
)
move-cursor-to: ".impl-items .rightside .srclink"
assert-css: (
".impl-items .rightside .srclink",
{"color": "rgb(210, 153, 29)", "text-decoration": "none solid rgb(210, 153, 29)"},
)
move-cursor-to: ".impl-items .rightside.srclink"
assert-css: (
".impl-items .rightside.srclink",
{"color": "rgb(210, 153, 29)", "text-decoration": "none solid rgb(210, 153, 29)"},
)

goto: file://|DOC_PATH|/test_docs/struct.HeavilyDocumentedStruct.html

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

assert-css: (".sidebar a", {"color": "rgb(253, 191, 53)"})
assert-css: (".in-band a", {"color": "rgb(221, 221, 221)"})

// We move the cursor over the "Implementations" title so the anchor is displayed.
move-cursor-to: "h2#implementations"
assert-css: ("h2#implementations a.anchor", {"color": "rgb(221, 221, 221)"})

// Same thing with the impl block title.
move-cursor-to: "#impl-HeavilyDocumentedStruct"
assert-css: ("#impl-HeavilyDocumentedStruct a.anchor", {"color": "rgb(221, 221, 221)"})

assert-css: ("#title-for-struct-impl-item-doc", {"margin-left": "0px"})

//
// We do the same checks with the ayu theme now.
//
local-storage: {"rustdoc-theme": "ayu", "rustdoc-use-system-theme": "false"}
goto: file://|DOC_PATH|/staged_api/struct.Foo.html

assert-css: ("#toggle-all-docs", {"color": "rgb(197, 197, 197)"})
assert-css: (".fqn .in-band a:nth-of-type(1)", {"color": "rgb(255, 255, 255)"})
assert-css: (".fqn .in-band a:nth-of-type(2)", {"color": "rgb(255, 160, 165)"})
assert-css: (
".rightside .srclink",
{"color": "rgb(57, 175, 215)", "text-decoration": "none solid rgb(57, 175, 215)"},
ALL,
)
compare-elements-css: (".rightside .srclink", ".rightside.srclink", ["color", "text-decoration"])
compare-elements-css: (".main-heading .srclink", ".rightside.srclink", ["color", "text-decoration"])

move-cursor-to: ".main-heading .srclink"
assert-css: (
".main-heading .srclink",
{"color": "rgb(57, 175, 215)", "text-decoration": "underline solid rgb(57, 175, 215)"},
)
move-cursor-to: ".impl-items .rightside .srclink"
assert-css: (
".impl-items .rightside .srclink",
{"color": "rgb(57, 175, 215)", "text-decoration": "none solid rgb(57, 175, 215)"},
)
move-cursor-to: ".impl-items .rightside.srclink"
assert-css: (
".impl-items .rightside.srclink",
{"color": "rgb(57, 175, 215)", "text-decoration": "none solid rgb(57, 175, 215)"},
)

goto: file://|DOC_PATH|/test_docs/struct.HeavilyDocumentedStruct.html

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

assert-css: (".sidebar a", {"color": "rgb(83, 177, 219)"})
assert-css: (".in-band a", {"color": "rgb(255, 255, 255)"})

// We move the cursor over the "Implementations" title so the anchor is displayed.
move-cursor-to: "h2#implementations"
assert-css: ("h2#implementations a.anchor", {"color": "rgb(197, 197, 197)"})

// Same thing with the impl block title.
move-cursor-to: "#impl-HeavilyDocumentedStruct"
assert-css: ("#impl-HeavilyDocumentedStruct a.anchor", {"color": "rgb(197, 197, 197)"})

assert-css: ("#title-for-struct-impl-item-doc", {"margin-left": "0px"})
6 changes: 3 additions & 3 deletions src/test/rustdoc-gui/headings.goml
Original file line number Diff line number Diff line change
@@ -247,12 +247,12 @@ assert-css: (

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

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

local-storage: {"rustdoc-theme": "ayu"}
reload:
assert-css: (".since", {"color": "rgb(128, 128, 128)"})
assert-css: (".since", {"color": "rgb(128, 128, 128)"}, ALL)
3 changes: 2 additions & 1 deletion src/test/rustdoc-gui/src/staged_api/Cargo.toml
Original file line number Diff line number Diff line change
@@ -7,5 +7,6 @@ edition = "2021"
path = "lib.rs"

[features]
default = ["some_feature"]
default = ["some_feature", "some_other_feature"]
some_feature = []
some_other_feature = []
2 changes: 2 additions & 0 deletions src/test/rustdoc-gui/src/staged_api/lib.rs
Original file line number Diff line number Diff line change
@@ -7,4 +7,6 @@ pub struct Foo {}
impl Foo {
#[stable(feature = "some_feature", since = "1.3.5")]
pub fn bar() {}
#[stable(feature = "some_other_feature", since = "1.3.6")]
pub fn yo() {}
}
2 changes: 1 addition & 1 deletion src/test/rustdoc/anchors.no_const_anchor.html
Original file line number Diff line number Diff line change
@@ -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>
<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>
2 changes: 1 addition & 1 deletion src/test/rustdoc/anchors.no_const_anchor2.html
Original file line number Diff line number Diff line change
@@ -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>
<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>
2 changes: 1 addition & 1 deletion src/test/rustdoc/anchors.no_method_anchor.html
Original file line number Diff line number Diff line change
@@ -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>
<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>
2 changes: 1 addition & 1 deletion src/test/rustdoc/anchors.no_trait_method_anchor.html
Original file line number Diff line number Diff line change
@@ -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>
<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>
2 changes: 1 addition & 1 deletion src/test/rustdoc/anchors.no_tymethod_anchor.html
Original file line number Diff line number Diff line change
@@ -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>
<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>
2 changes: 1 addition & 1 deletion src/test/rustdoc/anchors.no_type_anchor.html
Original file line number Diff line number Diff line change
@@ -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>
<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>
2 changes: 1 addition & 1 deletion src/test/rustdoc/ensure-src-link.rs
Original file line number Diff line number Diff line change
@@ -2,5 +2,5 @@

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

// @has foo/trait.Iterator.html '//*[@id="method.zip"]//a[@class="srclink"]' "source"
// @has foo/trait.Iterator.html '//*[@id="method.zip"]//a[@class="srclink rightside"]' "source"
pub use std::iter::Iterator;
2 changes: 1 addition & 1 deletion src/test/rustdoc/src-links-auto-impls.rs
Original file line number Diff line number Diff line change
@@ -6,7 +6,7 @@
// @has - '//*[@id="impl-Sync-for-Unsized"]/h3[@class="code-header in-band"]' 'impl Sync for Unsized'
// @!has - '//*[@id="impl-Sync-for-Unsized"]//a[@class="srclink"]' 'source'
// @has - '//*[@id="impl-Any-for-Unsized"]/h3[@class="code-header in-band"]' 'impl<T> Any for T'
// @has - '//*[@id="impl-Any-for-Unsized"]//a[@class="srclink"]' 'source'
// @has - '//*[@id="impl-Any-for-Unsized"]//a[@class="srclink rightside"]' 'source'
pub struct Unsized {
data: [u8],
}
2 changes: 1 addition & 1 deletion src/test/rustdoc/version-separator-without-source.rs
Original file line number Diff line number Diff line change
@@ -16,7 +16,7 @@ pub fn foo() {}
pub struct Bar;

impl Bar {
// @has - '//*[@id="method.bar"]/*[@class="rightside"]' '2.0'
// @has - '//*[@id="method.bar"]/*[@class="since rightside"]' '2.0'
// @!has - '//*[@id="method.bar"]/*[@class="rightside"]' '2.0 ·'
#[stable(feature = "foobar", since = "2.0")]
pub fn bar() {}