Skip to content

Commit e3af6dc

Browse files
Simplify CSS but wrapping scraped example into a div and move the title out of the code block
1 parent 01d8235 commit e3af6dc

8 files changed

+93
-91
lines changed

Diff for: src/librustdoc/html/static/css/rustdoc.css

+15-36
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,6 @@ xmlns="http://www.w3.org/2000/svg" fill="black" height="18px">\
3434
</g></svg>');
3535
--button-left-margin: 4px;
3636
--button-border-radius: 2px;
37-
--pre-line-height: 1.5rem;
3837
}
3938

4039
/* See FiraSans-LICENSE.txt for the Fira Sans license. */
@@ -366,7 +365,7 @@ code, pre, .code-header {
366365
}
367366
pre {
368367
padding: 14px;
369-
line-height: var(--pre-line-height); /* https://github.com/rust-lang/rust/issues/105906 */
368+
line-height: 1.5; /* https://github.com/rust-lang/rust/issues/105906 */
370369
}
371370
pre.item-decl {
372371
overflow-x: auto;
@@ -379,7 +378,7 @@ pre.item-decl {
379378
.src .content pre {
380379
padding: 20px;
381380
}
382-
.rustdoc.src .example-wrap pre.src-line-numbers {
381+
.rustdoc.src .example-wrap .src-line-numbers {
383382
padding: 20px 0 20px 4px;
384383
}
385384

@@ -766,6 +765,10 @@ both the code example and the line numbers, so we need to remove the radius in t
766765
border-bottom-left-radius: 0;
767766
}
768767

768+
.rustdoc .scraped-example {
769+
position: relative;
770+
}
771+
769772
/* For the last child of a div, the margin will be taken care of
770773
by the margin-top of the next item. */
771774
.rustdoc .example-wrap:last-child {
@@ -777,19 +780,18 @@ both the code example and the line numbers, so we need to remove the radius in t
777780
flex-grow: 1;
778781
}
779782

780-
.scraped-example:not(.expanded) {
783+
.scraped-example:not(.expanded) .example-wrap {
781784
/* scrape-examples.js has a constant DEFAULT_MAX_LINES (call it N) for the number
782785
* of lines shown in the un-expanded example code viewer. This pre needs to have
783786
* a max-height equal to line-height * N. The line-height is currently 1.5em,
784787
* and we include additional 10px for padding. */
785788
max-height: calc(1.5em * 5 + 10px);
786789
}
787790

788-
.rustdoc:not(.src) .scraped-example:not(.expanded) pre.src-line-numbers,
791+
.rustdoc:not(.src) .scraped-example:not(.expanded) .src-line-numbers,
789792
.rustdoc:not(.src) .scraped-example:not(.expanded) pre.rust {
790793
padding-bottom: 0;
791794
/* See above comment, should be the same max-height. */
792-
max-height: calc(1.5em * 5 + 10px);
793795
overflow: auto hidden;
794796
}
795797

@@ -798,7 +800,7 @@ both the code example and the line numbers, so we need to remove the radius in t
798800
}
799801

800802
.rustdoc .example-wrap pre.example-line-numbers,
801-
.rustdoc .example-wrap pre.src-line-numbers {
803+
.rustdoc .example-wrap .src-line-numbers {
802804
min-width: fit-content; /* prevent collapsing into nothing in truncated scraped examples */
803805
flex-grow: 0;
804806
text-align: right;
@@ -808,7 +810,7 @@ both the code example and the line numbers, so we need to remove the radius in t
808810
color: var(--src-line-numbers-span-color);
809811
}
810812

811-
.rustdoc .scraped-example pre.src-line-numbers {
813+
.rustdoc .scraped-example .src-line-numbers {
812814
padding: 14px 0;
813815
}
814816
.src-line-numbers a, .src-line-numbers span {
@@ -2268,24 +2270,6 @@ in src-script.js and main.js
22682270
margin: 0;
22692271
padding: var(--nav-sub-mobile-padding);
22702272
}
2271-
2272-
.example-wrap.scraped-example {
2273-
flex-wrap: wrap;
2274-
}
2275-
.example-wrap .scraped-example-title {
2276-
width: 100%;
2277-
}
2278-
.example-wrap.scraped-example .rust {
2279-
/* Dirty hacky to force it to remain on the same line as the line numbers. */
2280-
width: 10px;
2281-
}
2282-
.example-wrap.scraped-example .button-holder {
2283-
top: calc(var(--pre-line-height) + 4px);
2284-
}
2285-
.scraped-example:not(.expanded)::before {
2286-
/* The gradient effect needs to be moved under the title */
2287-
top: var(--pre-line-height);
2288-
}
22892273
}
22902274

22912275

@@ -2310,12 +2294,6 @@ in src-script.js and main.js
23102294
.item-table > li > div {
23112295
overflow-wrap: anywhere;
23122296
}
2313-
2314-
/* Starting this width, the "title" of scraped example will be in the code block so we can
2315-
put the background gradient at the top. */
2316-
.scraped-example:not(.expanded)::before {
2317-
top: 0;
2318-
}
23192297
}
23202298

23212299
@media print {
@@ -2397,20 +2375,21 @@ in src-script.js and main.js
23972375
color: var(--scrape-example-help-hover-color);
23982376
}
23992377

2400-
.scraped-example:not(.expanded)::before,
2401-
.scraped-example:not(.expanded)::after {
2378+
.scraped-example:not(.expanded) .example-wrap::before,
2379+
.scraped-example:not(.expanded) .example-wrap::after {
24022380
content: " ";
24032381
width: 100%;
24042382
height: 5px;
24052383
position: absolute;
24062384
z-index: 1;
24072385
}
2408-
.scraped-example:not(.expanded)::before {
2386+
.scraped-example:not(.expanded) .example-wrap::before {
2387+
top: 0;
24092388
background: linear-gradient(to bottom,
24102389
var(--scrape-example-code-wrapper-background-start),
24112390
var(--scrape-example-code-wrapper-background-end));
24122391
}
2413-
.scraped-example:not(.expanded)::after {
2392+
.scraped-example:not(.expanded) .example-wrap::after {
24142393
bottom: 0;
24152394
background: linear-gradient(to top,
24162395
var(--scrape-example-code-wrapper-background-start),

Diff for: src/librustdoc/html/static/js/scrape-examples.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313

1414
// Scroll code block to the given code location
1515
function scrollToLoc(elt, loc, isHidden) {
16-
const lines = elt.querySelector(".src-line-numbers");
16+
const lines = elt.querySelector(".src-line-numbers > pre");
1717
let scrollOffset;
1818

1919
// If the block is greater than the size of the viewer,
@@ -32,7 +32,7 @@
3232
scrollOffset = offsetMid - halfHeight;
3333
}
3434

35-
lines.scrollTo(0, scrollOffset);
35+
lines.parentElement.scrollTo(0, scrollOffset);
3636
elt.querySelector(".rust").scrollTo(0, scrollOffset);
3737
}
3838

Diff for: src/librustdoc/html/templates/scraped_source.html

+29-25
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,33 @@
1-
<div class="example-wrap scraped-example{% if !info.needs_expansion +%} expanded{% endif %}" data-locs="{{info.locations}}">
1+
<div class="scraped-example{% if !info.needs_expansion +%} expanded{% endif %}" data-locs="{{info.locations}}"> {# #}
22
<div class="scraped-example-title">
33
{{info.name +}} (<a href="{{info.url}}">{{info.title}}</a>) {# #}
44
</div>
5-
{# https://developers.google.com/search/docs/crawling-indexing/robots-meta-tag#data-nosnippet-attr
6-
Do not show "1 2 3 4 5 ..." in web search results. #}
7-
<div data-nosnippet><pre class="src-line-numbers">
8-
{% for line in lines.clone() %}
9-
{# ~#}
10-
<span>{{line|safe}}</span>
11-
{% endfor %}
12-
</pre></div> {# #}
13-
<pre class="rust"> {# #}
14-
<code>
15-
{{code_html|safe}}
16-
</code> {# #}
17-
</pre> {# #}
18-
{% if info.needs_prev_next_buttons || info.needs_expansion %}
19-
<div class="button-holder">
20-
{% if info.needs_prev_next_buttons %}
21-
<button class="prev">&pr;</button> {# #}
22-
<button class="next">&sc;</button>
23-
{% endif %}
24-
{% if info.needs_expansion %}
25-
<button class="expand">&varr;</button>
26-
{% endif %}
27-
</div>
28-
{% endif %}
5+
<div class="example-wrap"> {# #}
6+
{# https://developers.google.com/search/docs/crawling-indexing/robots-meta-tag#data-nosnippet-attr
7+
Do not show "1 2 3 4 5 ..." in web search results. #}
8+
<div class="src-line-numbers" data-nosnippet> {# #}
9+
<pre>
10+
{% for line in lines.clone() %}
11+
{# ~#}
12+
<span>{{line|safe}}</span>
13+
{% endfor %}
14+
</pre> {# #}
15+
</div> {# #}
16+
<pre class="rust"> {# #}
17+
<code>
18+
{{code_html|safe}}
19+
</code> {# #}
20+
</pre> {# #}
21+
{% if info.needs_prev_next_buttons || info.needs_expansion %}
22+
<div class="button-holder">
23+
{% if info.needs_prev_next_buttons %}
24+
<button class="prev">&pr;</button> {# #}
25+
<button class="next">&sc;</button>
26+
{% endif %}
27+
{% if info.needs_expansion %}
28+
<button class="expand">&varr;</button>
29+
{% endif %}
30+
</div>
31+
{% endif %}
32+
</div> {# #}
2933
</div> {# #}

Diff for: tests/rustdoc-gui/code-example-buttons.goml

+2-2
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ call-function: ("check-buttons-position", {"pre_selector": ".example-wrap"})
111111

112112
go-to: "file://" + |DOC_PATH| + "/scrape_examples/fn.test_many.html"
113113
// We should work as well for scraped examples.
114-
call-function: ("check-buttons-position", {"pre_selector": ".example-wrap.scraped-example"})
114+
call-function: ("check-buttons-position", {"pre_selector": ".scraped-example .example-wrap"})
115115
// And also when the scraped example "title" goes above.
116116
set-window-size: (600, 600)
117-
call-function: ("check-buttons-position", {"pre_selector": ".example-wrap.scraped-example"})
117+
call-function: ("check-buttons-position", {"pre_selector": ".scraped-example .example-wrap"})

Diff for: tests/rustdoc-gui/docblock-code-block-line-number.goml

+1-1
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ assert-local-storage: {"rustdoc-line-numbers": "true" }
109109
go-to: "file://" + |DOC_PATH| + "/scrape_examples/fn.test_many.html"
110110

111111
assert-css: (
112-
".scraped-example pre.src-line-numbers",
112+
".scraped-example .src-line-numbers",
113113
{
114114
// There should not be a radius on the right of the line numbers.
115115
"border-top-left-radius": "6px",

Diff for: tests/rustdoc-gui/scrape-examples-button-focus.goml

+30-7
Original file line numberDiff line numberDiff line change
@@ -4,29 +4,52 @@ go-to: "file://" + |DOC_PATH| + "/scrape_examples/fn.test.html"
44

55
// The next/prev buttons vertically scroll the code viewport between examples
66
move-cursor-to: ".scraped-example-list > .scraped-example"
7-
store-property: (".scraped-example-list > .scraped-example pre", {"scrollTop": initialScrollTop})
7+
store-property: (".scraped-example-list > .scraped-example .src-line-numbers", {
8+
"scrollTop": initialScrollTop,
9+
})
10+
assert-property: (".scraped-example-list > .scraped-example .rust", {
11+
"scrollTop": |initialScrollTop|,
12+
})
813
focus: ".scraped-example-list > .scraped-example .next"
914
press-key: "Enter"
10-
assert-property-false: (".scraped-example-list > .scraped-example pre", {
15+
assert-property-false: (".scraped-example-list > .scraped-example .src-line-numbers", {
16+
"scrollTop": |initialScrollTop|
17+
}, NEAR)
18+
assert-property-false: (".scraped-example-list > .scraped-example .rust", {
1119
"scrollTop": |initialScrollTop|
1220
}, NEAR)
1321
focus: ".scraped-example-list > .scraped-example .prev"
1422
press-key: "Enter"
15-
assert-property: (".scraped-example-list > .scraped-example pre", {
23+
assert-property: (".scraped-example-list > .scraped-example .src-line-numbers", {
24+
"scrollTop": |initialScrollTop|
25+
}, NEAR)
26+
assert-property: (".scraped-example-list > .scraped-example .rust", {
1627
"scrollTop": |initialScrollTop|
1728
}, NEAR)
1829

1930
// The expand button increases the scrollHeight of the minimized code viewport
2031
store-property: (".scraped-example-list > .scraped-example pre", {"offsetHeight": smallOffsetHeight})
21-
assert-property-false: (".scraped-example-list > .scraped-example pre", {
32+
assert-property-false: (".scraped-example-list > .scraped-example .src-line-numbers", {
33+
"scrollHeight": |smallOffsetHeight|
34+
}, NEAR)
35+
assert-property-false: (".scraped-example-list > .scraped-example .rust", {
2236
"scrollHeight": |smallOffsetHeight|
2337
}, NEAR)
2438
focus: ".scraped-example-list > .scraped-example .expand"
2539
press-key: "Enter"
26-
assert-property-false: (".scraped-example-list > .scraped-example pre", {
40+
assert-property-false: (".scraped-example-list > .scraped-example .src-line-numbers", {
41+
"offsetHeight": |smallOffsetHeight|
42+
}, NEAR)
43+
assert-property-false: (".scraped-example-list > .scraped-example .rust", {
2744
"offsetHeight": |smallOffsetHeight|
2845
}, NEAR)
29-
store-property: (".scraped-example-list > .scraped-example pre", {"offsetHeight": fullOffsetHeight})
30-
assert-property: (".scraped-example-list > .scraped-example pre", {
46+
store-property: (".scraped-example-list > .scraped-example .src-line-numbers", {
47+
"offsetHeight": fullOffsetHeight,
48+
})
49+
assert-property: (".scraped-example-list > .scraped-example .rust", {
50+
"offsetHeight": |fullOffsetHeight|,
51+
"scrollHeight": |fullOffsetHeight|,
52+
})
53+
assert-property: (".scraped-example-list > .scraped-example .src-line-numbers", {
3154
"scrollHeight": |fullOffsetHeight|
3255
}, NEAR)

Diff for: tests/rustdoc-gui/scrape-examples-color.goml

+2-2
Original file line numberDiff line numberDiff line change
@@ -67,11 +67,11 @@ define-function: (
6767
[theme, background_color_start, background_color_end],
6868
block {
6969
call-function: ("switch-theme", {"theme": |theme|})
70-
assert-css: (".scraped-example:not(.expanded)::before", {
70+
assert-css: (".scraped-example:not(.expanded) .example-wrap::before", {
7171
"background-image": "linear-gradient(" + |background_color_start| + ", " +
7272
|background_color_end| + ")",
7373
})
74-
assert-css: (".scraped-example:not(.expanded)::after", {
74+
assert-css: (".scraped-example:not(.expanded) .example-wrap::after", {
7575
"background-image": "linear-gradient(to top, " + |background_color_start| + ", " +
7676
|background_color_end| + ")",
7777
})

Diff for: tests/rustdoc-gui/scrape-examples-layout.goml

+12-16
Original file line numberDiff line numberDiff line change
@@ -36,13 +36,13 @@ assert-property: (
3636
)
3737

3838
// The "title" should be located at the right bottom corner of the code example.
39-
store-position: (".example-wrap.scraped-example", {"x": x, "y": y})
40-
store-size: (".example-wrap.scraped-example", {"width": width, "height": height})
41-
store-size: (".example-wrap.scraped-example .scraped-example-title", {
39+
store-position: (".scraped-example .example-wrap", {"x": x, "y": y})
40+
store-size: (".scraped-example .example-wrap", {"width": width, "height": height})
41+
store-size: (".scraped-example .scraped-example-title", {
4242
"width": title_width,
4343
"height": title_height,
4444
})
45-
assert-position: (".example-wrap.scraped-example .scraped-example-title", {
45+
assert-position: (".scraped-example .scraped-example-title", {
4646
"x": |x| + |width| - |title_width| - 5,
4747
"y": |y| + |height| - |title_height| - 8,
4848
})
@@ -57,36 +57,32 @@ assert-position: (".scraped-example", {"y": 226})
5757
assert-position: (".scraped-example .prev", {"y": 226 + |offset_y|})
5858

5959
// Gradient background should be at the top of the code block.
60-
assert-css: (".scraped-example::before", {"top": "0px"})
61-
assert-css: (".scraped-example::after", {"bottom": "0px"})
60+
assert-css: (".scraped-example .example-wrap::before", {"top": "0px"})
61+
assert-css: (".scraped-example .example-wrap::after", {"bottom": "0px"})
6262

6363
// Then with mobile
6464
set-window-size: (600, 600)
65-
store-size: (".example-wrap.scraped-example .scraped-example-title", {"height": title_height})
65+
store-size: (".scraped-example .scraped-example-title", {"height": title_height})
6666
assert-position: (".scraped-example", {"y": 284})
6767
assert-position: (".scraped-example .prev", {"y": 284 + |offset_y| + |title_height|})
6868

69-
// Gradient background should be at the top of the code block, which is now below the "title".
70-
assert-css: (".scraped-example::before", {"top": |title_height| + "px"})
71-
assert-css: (".scraped-example::after", {"bottom": "0px"})
72-
7369
define-function: (
7470
"check_title_and_code_position",
7571
[],
7672
block {
7773
// Title should be above the code.
78-
store-position: (".example-wrap.scraped-example .src-line-numbers", {"x": x, "y": y})
79-
store-size: (".example-wrap.scraped-example .scraped-example-title", { "height": title_height })
74+
store-position: (".scraped-example .example-wrap .src-line-numbers", {"x": x, "y": y})
75+
store-size: (".scraped-example .scraped-example-title", { "height": title_height })
8076

81-
assert-position: (".example-wrap.scraped-example .scraped-example-title", {
77+
assert-position: (".scraped-example .scraped-example-title", {
8278
"x": |x|, // same X position.
8379
"y": |y| - |title_height|,
8480
})
8581

8682
// Line numbers should be right beside the code.
8783
compare-elements-position: (
88-
".example-wrap.scraped-example .src-line-numbers",
89-
".example-wrap.scraped-example .rust",
84+
".scraped-example .example-wrap .src-line-numbers",
85+
".scraped-example .example-wrap .rust",
9086
["y"],
9187
)
9288
}

0 commit comments

Comments
 (0)