Rollup merge of #94696 - GuillaumeGomez:align-line-numbers-right, r=notriddle

Remove whitespaces and use CSS to align line numbers to the right instead

Instead of generating whitespaces to create padding, we simply use the CSS rule: `text-align: right`.

Nice side-effect: it reduces the generated HTML size from **75.004** to **74.828** (MegaBytes) on the std source pages (it's not much but it's always a nice plus 😆 ).

There are no changes in the generated UI.

r? `@notriddle`
This commit is contained in:
Matthias Krüger 2022-03-07 18:39:03 +01:00 committed by GitHub
commit 77562f2350
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 8 additions and 8 deletions

View File

@ -272,22 +272,16 @@ crate fn print_src(
) {
let lines = s.lines().count();
let mut line_numbers = Buffer::empty_from(buf);
let mut cols = 0;
let mut tmp = lines;
while tmp > 0 {
cols += 1;
tmp /= 10;
}
line_numbers.write_str("<pre class=\"line-numbers\">");
match source_context {
SourceContext::Standalone => {
for line in 1..=lines {
writeln!(line_numbers, "<span id=\"{0}\">{0:1$}</span>", line, cols)
writeln!(line_numbers, "<span id=\"{0}\">{0}</span>", line)
}
}
SourceContext::Embedded { offset } => {
for line in 1..=lines {
writeln!(line_numbers, "<span>{0:1$}</span>", line + offset, cols)
writeln!(line_numbers, "<span>{0}</span>", line + offset)
}
}
}

View File

@ -541,6 +541,9 @@ h2.location a {
text-decoration: underline;
}
.line-numbers {
text-align: right;
}
.rustdoc:not(.source) .example-wrap > pre:not(.line-number) {
width: 100%;
overflow-x: auto;

View File

@ -14,3 +14,6 @@ assert-attribute: (".line-numbers > span:nth-child(6)", {"class": "line-highligh
assert-attribute-false: (".line-numbers > span:nth-child(7)", {"class": "line-highlighted"})
// This is to ensure that the content is correctly align with the line numbers.
compare-elements-position: ("//*[@id='1']", ".rust > code > span", ("y"))
// Assert that the line numbers text is aligned to the right.
assert-css: (".line-numbers", {"text-align": "right"})