Skip to content

rustdoc: remove unnecessary wrapper div.item-decl from HTML #107476

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 1 commit into from
Jan 31, 2023
Merged
Show file tree
Hide file tree
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
8 changes: 4 additions & 4 deletions src/librustdoc/html/highlight.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,11 +58,11 @@ pub(crate) fn render_example_with_highlighting(
write_footer(out, playground_button);
}

/// Highlights `src` as a macro, returning the HTML output.
pub(crate) fn render_macro_with_highlighting(src: &str, out: &mut Buffer) {
write_header(out, "macro", None, Tooltip::None);
/// Highlights `src` as an item-decl, returning the HTML output.
pub(crate) fn render_item_decl_with_highlighting(src: &str, out: &mut Buffer) {
write!(out, "<pre class=\"rust item-decl\">");
write_code(out, src, None, None);
write_footer(out, None);
write!(out, "</pre>");
}

/// Highlights `src` as a source code page, returning the HTML output.
Expand Down
50 changes: 5 additions & 45 deletions src/librustdoc/html/render/print_item.rs
Original file line number Diff line number Diff line change
Expand Up @@ -530,7 +530,6 @@ fn item_function(w: &mut Buffer, cx: &mut Context<'_>, it: &clean::Item, f: &cle
let notable_traits =
f.decl.output.as_return().and_then(|output| notable_traits_button(output, cx));

wrap_into_item_decl(w, |w| {
wrap_item(w, |w| {
render_attributes_in_pre(w, it, "");
w.reserve(header_len);
Expand All @@ -550,7 +549,6 @@ fn item_function(w: &mut Buffer, cx: &mut Context<'_>, it: &clean::Item, f: &cle
notable_traits = notable_traits.unwrap_or_default(),
);
});
});
document(w, cx, it, None, HeadingOffset::H2);
}

Expand All @@ -569,7 +567,6 @@ fn item_trait(w: &mut Buffer, cx: &mut Context<'_>, it: &clean::Item, t: &clean:
let must_implement_one_of_functions = tcx.trait_def(t.def_id).must_implement_one_of.clone();

// Output the trait definition
wrap_into_item_decl(w, |w| {
wrap_item(w, |w| {
render_attributes_in_pre(w, it, "");
write!(
Expand Down Expand Up @@ -601,10 +598,7 @@ fn item_trait(w: &mut Buffer, cx: &mut Context<'_>, it: &clean::Item, t: &clean:
toggle = true;
toggle_open(
w,
format_args!(
"{} associated items",
count_types + count_consts + count_methods
),
format_args!("{} associated items", count_types + count_consts + count_methods),
);
}
for types in [&required_types, &provided_types] {
Expand Down Expand Up @@ -708,7 +702,6 @@ fn item_trait(w: &mut Buffer, cx: &mut Context<'_>, it: &clean::Item, t: &clean:
w.write_str("}");
}
});
});

// Trait documentation
document(w, cx, it, None, HeadingOffset::H2);
Expand Down Expand Up @@ -1050,7 +1043,6 @@ fn item_trait(w: &mut Buffer, cx: &mut Context<'_>, it: &clean::Item, t: &clean:
}

fn item_trait_alias(w: &mut Buffer, cx: &mut Context<'_>, it: &clean::Item, t: &clean::TraitAlias) {
wrap_into_item_decl(w, |w| {
wrap_item(w, |w| {
render_attributes_in_pre(w, it, "");
write!(
Expand All @@ -1062,7 +1054,6 @@ fn item_trait_alias(w: &mut Buffer, cx: &mut Context<'_>, it: &clean::Item, t: &
bounds(&t.bounds, true, cx)
);
});
});

document(w, cx, it, None, HeadingOffset::H2);

Expand All @@ -1074,7 +1065,6 @@ fn item_trait_alias(w: &mut Buffer, cx: &mut Context<'_>, it: &clean::Item, t: &
}

fn item_opaque_ty(w: &mut Buffer, cx: &mut Context<'_>, it: &clean::Item, t: &clean::OpaqueTy) {
wrap_into_item_decl(w, |w| {
wrap_item(w, |w| {
render_attributes_in_pre(w, it, "");
write!(
Expand All @@ -1086,7 +1076,6 @@ fn item_opaque_ty(w: &mut Buffer, cx: &mut Context<'_>, it: &clean::Item, t: &cl
bounds = bounds(&t.bounds, false, cx),
);
});
});

document(w, cx, it, None, HeadingOffset::H2);

Expand All @@ -1113,7 +1102,7 @@ fn item_typedef(w: &mut Buffer, cx: &mut Context<'_>, it: &clean::Item, t: &clea
});
}

wrap_into_item_decl(w, |w| write_content(w, cx, it, t));
write_content(w, cx, it, t);

document(w, cx, it, None, HeadingOffset::H2);

Expand All @@ -1127,12 +1116,10 @@ fn item_typedef(w: &mut Buffer, cx: &mut Context<'_>, it: &clean::Item, t: &clea
}

fn item_union(w: &mut Buffer, cx: &mut Context<'_>, it: &clean::Item, s: &clean::Union) {
wrap_into_item_decl(w, |w| {
wrap_item(w, |w| {
render_attributes_in_pre(w, it, "");
render_union(w, it, Some(&s.generics), &s.fields, "", cx);
});
});

document(w, cx, it, None, HeadingOffset::H2);

Expand Down Expand Up @@ -1192,7 +1179,6 @@ fn print_tuple_struct_fields(w: &mut Buffer, cx: &Context<'_>, s: &[clean::Item]
fn item_enum(w: &mut Buffer, cx: &mut Context<'_>, it: &clean::Item, e: &clean::Enum) {
let tcx = cx.tcx();
let count_variants = e.variants().count();
wrap_into_item_decl(w, |w| {
wrap_item(w, |w| {
render_attributes_in_pre(w, it, "");
write!(
Expand Down Expand Up @@ -1246,7 +1232,6 @@ fn item_enum(w: &mut Buffer, cx: &mut Context<'_>, it: &clean::Item, e: &clean::
w.write_str("}");
}
});
});

document(w, cx, it, None, HeadingOffset::H2);

Expand Down Expand Up @@ -1346,28 +1331,21 @@ fn item_enum(w: &mut Buffer, cx: &mut Context<'_>, it: &clean::Item, e: &clean::
}

fn item_macro(w: &mut Buffer, cx: &mut Context<'_>, it: &clean::Item, t: &clean::Macro) {
wrap_into_item_decl(w, |w| {
highlight::render_macro_with_highlighting(&t.source, w);
});
highlight::render_item_decl_with_highlighting(&t.source, w);
document(w, cx, it, None, HeadingOffset::H2)
}

fn item_proc_macro(w: &mut Buffer, cx: &mut Context<'_>, it: &clean::Item, m: &clean::ProcMacro) {
wrap_into_item_decl(w, |w| {
wrap_item(w, |w| {
let name = it.name.expect("proc-macros always have names");
match m.kind {
MacroKind::Bang => {
wrap_item(w, |w| {
write!(w, "{}!() {{ /* proc-macro */ }}", name);
});
}
MacroKind::Attr => {
wrap_item(w, |w| {
write!(w, "#[{}]", name);
});
}
MacroKind::Derive => {
wrap_item(w, |w| {
write!(w, "#[derive({})]", name);
if !m.helpers.is_empty() {
w.push_str("\n{\n");
Expand All @@ -1377,7 +1355,6 @@ fn item_proc_macro(w: &mut Buffer, cx: &mut Context<'_>, it: &clean::Item, m: &c
}
w.push_str("}\n");
}
});
}
}
});
Expand All @@ -1400,7 +1377,6 @@ fn item_primitive(w: &mut Buffer, cx: &mut Context<'_>, it: &clean::Item) {
}

fn item_constant(w: &mut Buffer, cx: &mut Context<'_>, it: &clean::Item, c: &clean::Constant) {
wrap_into_item_decl(w, |w| {
wrap_item(w, |w| {
let tcx = cx.tcx();
render_attributes_in_code(w, it);
Expand Down Expand Up @@ -1444,18 +1420,15 @@ fn item_constant(w: &mut Buffer, cx: &mut Context<'_>, it: &clean::Item, c: &cle
}
}
});
});

document(w, cx, it, None, HeadingOffset::H2)
}

fn item_struct(w: &mut Buffer, cx: &mut Context<'_>, it: &clean::Item, s: &clean::Struct) {
wrap_into_item_decl(w, |w| {
wrap_item(w, |w| {
render_attributes_in_code(w, it);
render_struct(w, it, Some(&s.generics), s.ctor_kind, &s.fields, "", true, cx);
});
});

document(w, cx, it, None, HeadingOffset::H2);

Expand Down Expand Up @@ -1503,7 +1476,6 @@ fn item_struct(w: &mut Buffer, cx: &mut Context<'_>, it: &clean::Item, s: &clean
}

fn item_static(w: &mut Buffer, cx: &mut Context<'_>, it: &clean::Item, s: &clean::Static) {
wrap_into_item_decl(w, |w| {
wrap_item(w, |w| {
render_attributes_in_code(w, it);
write!(
Expand All @@ -1515,12 +1487,10 @@ fn item_static(w: &mut Buffer, cx: &mut Context<'_>, it: &clean::Item, s: &clean
typ = s.type_.print(cx)
);
});
});
document(w, cx, it, None, HeadingOffset::H2)
}

fn item_foreign_type(w: &mut Buffer, cx: &mut Context<'_>, it: &clean::Item) {
wrap_into_item_decl(w, |w| {
wrap_item(w, |w| {
w.write_str("extern {\n");
render_attributes_in_code(w, it);
Expand All @@ -1531,7 +1501,6 @@ fn item_foreign_type(w: &mut Buffer, cx: &mut Context<'_>, it: &clean::Item) {
it.name.unwrap(),
);
});
});

document(w, cx, it, None, HeadingOffset::H2);

Expand Down Expand Up @@ -1609,20 +1578,11 @@ fn bounds(t_bounds: &[clean::GenericBound], trait_alias: bool, cx: &Context<'_>)
bounds
}

fn wrap_into_item_decl<F>(w: &mut Buffer, f: F)
where
F: FnOnce(&mut Buffer),
{
w.write_str("<div class=\"item-decl\">");
f(w);
w.write_str("</div>")
}

fn wrap_item<F>(w: &mut Buffer, f: F)
where
F: FnOnce(&mut Buffer),
{
w.write_str(r#"<pre class="rust"><code>"#);
w.write_str(r#"<pre class="rust item-decl"><code>"#);
f(w);
w.write_str("</code></pre>");
}
Expand Down
2 changes: 1 addition & 1 deletion src/librustdoc/html/static/css/rustdoc.css
Original file line number Diff line number Diff line change
Expand Up @@ -335,7 +335,7 @@ pre {
padding: 14px;
line-height: 1.5; /* https://github.com/rust-lang/rust/issues/105906 */
}
.item-decl pre {
pre.item-decl {
overflow-x: auto;
}
/* This rule allows to have scrolling on the X axis. */
Expand Down
10 changes: 5 additions & 5 deletions tests/rustdoc-gui/code-tags.goml
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,16 @@ size: (1080, 600)
// Check that their content is inside <pre><code>
assert-count: (".example-wrap pre > code", 4)
// Check that function signature is inside <pre><code>
assert: ".item-decl pre.rust > code"
assert: "pre.rust.item-decl > code"

goto: "file://" + |DOC_PATH| + "/test_docs/struct.Foo.html"
assert: ".item-decl pre.rust > code"
assert: "pre.rust.item-decl > code"

goto: "file://" + |DOC_PATH| + "/test_docs/enum.AnEnum.html"
assert: ".item-decl pre.rust > code"
assert: "pre.rust.item-decl > code"

goto: "file://" + |DOC_PATH| + "/test_docs/trait.AnotherOne.html"
assert: ".item-decl pre.rust > code"
assert: "pre.rust.item-decl > code"

goto: "file://" + |DOC_PATH| + "/test_docs/type.SomeType.html"
assert: ".item-decl pre.rust > code"
assert: "pre.rust.item-decl > code"
9 changes: 4 additions & 5 deletions tests/rustdoc-gui/font-weight.goml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// This test checks that the font weight is correctly applied.
goto: "file://" + |DOC_PATH| + "/lib2/struct.Foo.html"
assert-css: ("//*[@class='item-decl']//a[text()='Alias']", {"font-weight": "400"})
assert-css: ("//*[@class='rust item-decl']//a[text()='Alias']", {"font-weight": "400"})
assert-css: (
"//*[@class='structfield small-section-header']//a[text()='Alias']",
{"font-weight": "400"},
Expand All @@ -19,8 +19,7 @@ goto: "file://" + |DOC_PATH| + "/lib2/trait.Trait.html"

// This is a complex selector, so here's how it works:
//
// * //*[@class='item-decl'] — selects element of any tag with classes docblock and item-decl
// * /pre[@class='rust'] — selects immediate child with tag pre and class rust
// * //pre[@class='rust item-decl'] — selects item-decl code block
// * /code — selects immediate child with tag code
// * /a[@class='constant'] — selects immediate child with tag a and class constant
// * //text() — selects child that is text node
Expand All @@ -29,11 +28,11 @@ goto: "file://" + |DOC_PATH| + "/lib2/trait.Trait.html"
// This uses '/parent::*' as a proxy for the style of the text node.
// We can't just select the '<a>' because intermediate tags could be added.
assert-count: (
"//*[@class='item-decl']/pre[@class='rust']/code/a[@class='constant']//text()/parent::*",
"//pre[@class='rust item-decl']/code/a[@class='constant']//text()/parent::*",
1,
)
assert-css: (
"//*[@class='item-decl']/pre[@class='rust']/code/a[@class='constant']//text()/parent::*",
"//pre[@class='rust item-decl']/code/a[@class='constant']//text()/parent::*",
{"font-weight": "400"},
)

Expand Down
2 changes: 1 addition & 1 deletion tests/rustdoc-gui/list_code_block.goml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// This test checks that code blocks in list are supported.
goto: "file://" + |DOC_PATH| + "/test_docs/index.html"
goto: "./fn.check_list_code_block.html"
assert: (".item-decl pre.rust")
assert: ("pre.rust.item-decl")
18 changes: 9 additions & 9 deletions tests/rustdoc-gui/type-declation-overflow.goml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ size: (1100, 800)
// Logically, the <body> scroll width should be the width of the window.
assert-property: ("body", {"scrollWidth": "1100"})
// However, since there is overflow in the type declaration, its scroll width is bigger.
assert-property: (".item-decl pre", {"scrollWidth": "1324"})
assert-property: ("pre.item-decl", {"scrollWidth": "1324"})

// In the table-ish view on the module index, the name should not be wrapped more than necessary.
goto: "file://" + |DOC_PATH| + "/lib2/too_long/index.html"
Expand All @@ -24,7 +24,7 @@ assert-property: ("body", {"scrollWidth": "1100"})
// We now check that the section width hasn't grown because of it.
assert-property: ("#main-content", {"scrollWidth": "840"})
// And now checking that it has scrollable content.
assert-property: (".item-decl pre", {"scrollWidth": "1103"})
assert-property: ("pre.item-decl", {"scrollWidth": "1103"})

// ... and constant.
// On a sidenote, it also checks that the (very) long title isn't changing the docblock width.
Expand All @@ -33,7 +33,7 @@ assert-property: ("body", {"scrollWidth": "1100"})
// We now check that the section width hasn't grown because of it.
assert-property: ("#main-content", {"scrollWidth": "840"})
// And now checking that it has scrollable content.
assert-property: (".item-decl pre", {"scrollWidth": "950"})
assert-property: ("pre.item-decl", {"scrollWidth": "950"})

// On mobile:
size: (600, 600)
Expand Down Expand Up @@ -65,12 +65,12 @@ compare-elements-position-false: (".main-heading h1", ".main-heading .out-of-ban
goto: "file://" + |DOC_PATH| + "/lib2/scroll_traits/trait.Iterator.html"

click: ".item-decl .type-contents-toggle"
assert-property: (".item-decl > pre", {"scrollLeft": 0})
scroll-to: "//*[@class='item-decl']//details/a[text()='String']"
assert-property-false: (".item-decl > pre", {"scrollLeft": 0})
assert-property: ("pre.item-decl", {"scrollLeft": 0})
scroll-to: "//*[@class='rust item-decl']//details/a[text()='String']"
assert-property-false: ("pre.item-decl", {"scrollLeft": 0})

// Then on an item without "hidden methods".
goto: "file://" + |DOC_PATH| + "/lib2/scroll_traits/trait.TraitWithLongItemsName.html"
assert-property: (".item-decl > pre", {"scrollLeft": 0})
scroll-to: "//*[@class='item-decl']//code/a[text()='String']"
assert-property-false: (".item-decl > pre", {"scrollLeft": 0})
assert-property: ("pre.item-decl", {"scrollLeft": 0})
scroll-to: "//*[@class='rust item-decl']//code/a[text()='String']"
assert-property-false: ("pre.item-decl", {"scrollLeft": 0})
8 changes: 4 additions & 4 deletions tests/rustdoc/array-links.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,25 +4,25 @@
pub struct MyBox<T: ?Sized>(*const T);

// @has 'foo/fn.alpha.html'
// @snapshot link_slice_u32 - '//div[@class="item-decl"]/pre[@class="rust"]/code'
// @snapshot link_slice_u32 - '//pre[@class="rust item-decl"]/code'
pub fn alpha() -> &'static [u32; 1] {
loop {}
}

// @has 'foo/fn.beta.html'
// @snapshot link_slice_generic - '//div[@class="item-decl"]/pre[@class="rust"]/code'
// @snapshot link_slice_generic - '//pre[@class="rust item-decl"]/code'
pub fn beta<T>() -> &'static [T; 1] {
loop {}
}

// @has 'foo/fn.gamma.html'
// @snapshot link_box_u32 - '//div[@class="item-decl"]/pre[@class="rust"]/code'
// @snapshot link_box_u32 - '//pre[@class="rust item-decl"]/code'
pub fn gamma() -> MyBox<[u32; 1]> {
loop {}
}

// @has 'foo/fn.delta.html'
// @snapshot link_box_generic - '//div[@class="item-decl"]/pre[@class="rust"]/code'
// @snapshot link_box_generic - '//pre[@class="rust item-decl"]/code'
pub fn delta<T>() -> MyBox<[T; 1]> {
loop {}
}
2 changes: 1 addition & 1 deletion tests/rustdoc/assoc-consts.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
pub trait Foo {
// @has assoc_consts/trait.Foo.html '//div[@class="item-decl"]/pre[@class="rust"]' \
// @has assoc_consts/trait.Foo.html '//pre[@class="rust item-decl"]' \
// 'const FOO: usize = 13usize;'
// @has - '//*[@id="associatedconstant.FOO"]' 'const FOO: usize'
const FOO: usize = 12 + 1;
Expand Down
Loading