Skip to content

Commit 5a5cd6b

Browse files
authored
Rollup merge of rust-lang#100374 - GuillaumeGomez:improve_rustdoc_search_results_page_crates_selection, r=notriddle
Improve crate selection on rustdoc search results page Take over of rust-lang#98855 (screenshots and explanations are there). You can test it [here](https://rustdoc.crud.net/imperio/improve_rustdoc_search_results_page_crates_selection/std/index.html?search=test). cc `@steffahn` `@jsha` r? `@notriddle`
2 parents a9f3a27 + ea05be2 commit 5a5cd6b

File tree

10 files changed

+116
-68
lines changed

10 files changed

+116
-68
lines changed

src/librustdoc/html/markdown.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -1439,14 +1439,15 @@ fn init_id_map() -> FxHashMap<Cow<'static, str>, usize> {
14391439
map.insert("not-displayed".into(), 1);
14401440
map.insert("alternative-display".into(), 1);
14411441
map.insert("search".into(), 1);
1442+
map.insert("crate-search".into(), 1);
1443+
map.insert("crate-search-div".into(), 1);
14421444
// This is the list of IDs used in HTML generated in Rust (including the ones
14431445
// used in tera template files).
14441446
map.insert("mainThemeStyle".into(), 1);
14451447
map.insert("themeStyle".into(), 1);
14461448
map.insert("settings-menu".into(), 1);
14471449
map.insert("help-button".into(), 1);
14481450
map.insert("main-content".into(), 1);
1449-
map.insert("crate-search".into(), 1);
14501451
map.insert("toggle-all-docs".into(), 1);
14511452
map.insert("all-types".into(), 1);
14521453
map.insert("default-settings".into(), 1);

src/librustdoc/html/static/css/rustdoc.css

+50-11
Original file line numberDiff line numberDiff line change
@@ -947,32 +947,71 @@ table,
947947
height: 100%;
948948
}
949949
.search-results-title {
950-
display: inline;
950+
margin-top: 0;
951+
white-space: nowrap;
952+
/* flex layout allows shrinking the <select> appropriately if it becomes too large */
953+
display: inline-flex;
954+
max-width: 100%;
955+
/* make things look like in a line, despite the fact that we're using a layout
956+
with boxes (i.e. from the flex layout) */
957+
align-items: baseline;
951958
}
952-
#search-settings {
953-
font-size: 1.5rem;
954-
font-weight: 500;
955-
margin-bottom: 20px;
959+
#crate-search-div {
960+
display: inline-block;
961+
/* ensures that 100% in properties of #crate-search-div:after
962+
are relative to the size of this div */
963+
position: relative;
964+
/* allows this div (and with it the <select>-element "#crate-search") to be shrunk */
965+
min-width: 5em;
956966
}
957967
#crate-search {
958968
min-width: 115px;
959-
margin-top: 5px;
960-
padding-left: 0.15em;
969+
padding: 0;
970+
/* keep these two in sync with "@-moz-document url-prefix()" below */
971+
padding-left: 4px;
961972
padding-right: 23px;
973+
/* prevents the <select> from overflowing the containing div in case it's shrunk */
974+
max-width: 100%;
975+
/* contents can overflow because of max-width limit, then show ellipsis */
976+
text-overflow: ellipsis;
962977
border: 1px solid;
963978
border-radius: 4px;
964979
outline: none;
965980
cursor: pointer;
966981
-moz-appearance: none;
967982
-webkit-appearance: none;
968983
/* Removes default arrow from firefox */
984+
text-indent: 0.01px;
985+
background-color: var(--main-background-color);
986+
}
987+
/* cancel stylistic differences in padding in firefox
988+
for "appearance: none"-style (or equivalent) <select>s */
989+
@-moz-document url-prefix() {
990+
#crate-search {
991+
padding-left: 0px; /* == 4px - 4px */
992+
padding-right: 19px; /* == 23px - 4px */
993+
}
994+
}
995+
/* pseudo-element for holding the dropdown-arrow image; needs to be a separate thing
996+
so that we can apply CSS-filters to change the arrow color in themes */
997+
#crate-search-div::after {
998+
/* lets clicks through! */
999+
pointer-events: none;
1000+
/* completely covers the underlying div */
1001+
width: 100%;
1002+
height: 100%;
1003+
position: absolute;
1004+
top: 0;
1005+
left: 0;
1006+
content: "";
9691007
background-repeat: no-repeat;
970-
background-color: transparent;
9711008
background-size: 20px;
972-
background-position: calc(100% - 1px) 56%;
1009+
background-position: calc(100% - 2px) 56%;
1010+
/* image is black color, themes should apply a "filter" property to change the color */
9731011
background-image: /* AUTOREPLACE: */url("down-arrow.svg");
974-
max-width: 100%;
975-
text-overflow: ellipsis;
1012+
}
1013+
#crate-search > option {
1014+
font-size: 1rem;
9761015
}
9771016
.search-container {
9781017
margin-top: 4px;

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

+12-2
Original file line numberDiff line numberDiff line change
@@ -182,7 +182,7 @@ details.rustdoc-toggle > summary::before {
182182
filter: invert(100%);
183183
}
184184

185-
#crate-search, .search-input {
185+
.search-input {
186186
background-color: #141920;
187187
border-color: #424c57;
188188
}
@@ -191,7 +191,17 @@ details.rustdoc-toggle > summary::before {
191191
/* Without the `!important`, the border-color is ignored for `<select>`...
192192
It cannot be in the group above because `.search-input` has a different border color on
193193
hover. */
194-
border-color: #424c57 !important;
194+
border-color: #5c6773 !important;
195+
}
196+
#crate-search-div::after {
197+
/* match border-color; uses https://codepen.io/sosuke/pen/Pjoqqp */
198+
filter: invert(41%) sepia(12%) saturate(487%) hue-rotate(171deg) brightness(94%) contrast(94%);
199+
}
200+
#crate-search:hover, #crate-search:focus {
201+
border-color: #e0e0e0 !important;
202+
}
203+
#crate-search-div:hover::after, #crate-search-div:focus-within::after {
204+
filter: invert(98%) sepia(12%) saturate(81%) hue-rotate(343deg) brightness(113%) contrast(76%);
195205
}
196206

197207
.search-input {

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

+12-2
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,7 @@ details.rustdoc-toggle > summary::before {
152152
filter: invert(100%);
153153
}
154154

155-
#crate-search, .search-input {
155+
.search-input {
156156
color: #111;
157157
background-color: #f0f0f0;
158158
border-color: #f0f0f0;
@@ -162,7 +162,17 @@ details.rustdoc-toggle > summary::before {
162162
/* Without the `!important`, the border-color is ignored for `<select>`...
163163
It cannot be in the group above because `.search-input` has a different border color on
164164
hover. */
165-
border-color: #f0f0f0 !important;
165+
border-color: #d2d2d2 !important;
166+
}
167+
#crate-search-div::after {
168+
/* match border-color; uses https://codepen.io/sosuke/pen/Pjoqqp */
169+
filter: invert(94%) sepia(0%) saturate(721%) hue-rotate(255deg) brightness(90%) contrast(90%);
170+
}
171+
#crate-search:hover, #crate-search:focus {
172+
border-color: #2196f3 !important;
173+
}
174+
#crate-search-div:hover::after, #crate-search-div:focus-within::after {
175+
filter: invert(69%) sepia(60%) saturate(6613%) hue-rotate(184deg) brightness(100%) contrast(91%);
166176
}
167177

168178
.search-input {

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

+12-3
Original file line numberDiff line numberDiff line change
@@ -144,17 +144,26 @@ details.rustdoc-toggle > summary::before {
144144
color: #999;
145145
}
146146

147-
#crate-search, .search-input {
147+
.search-input {
148148
background-color: white;
149149
border-color: #e0e0e0;
150150
}
151-
152151
#crate-search {
153152
/* Without the `!important`, the border-color is ignored for `<select>`...
154153
It cannot be in the group above because `.search-input` has a different border color on
155154
hover. */
156155
border-color: #e0e0e0 !important;
157156
}
157+
#crate-search-div::after {
158+
/* match border-color; uses https://codepen.io/sosuke/pen/Pjoqqp */
159+
filter: invert(100%) sepia(0%) saturate(4223%) hue-rotate(289deg) brightness(114%) contrast(76%);
160+
}
161+
#crate-search:hover, #crate-search:focus {
162+
border-color: #717171 !important;
163+
}
164+
#crate-search-div:hover::after, #crate-search-div:focus-within::after {
165+
filter: invert(44%) sepia(18%) saturate(23%) hue-rotate(317deg) brightness(96%) contrast(93%);
166+
}
158167

159168
.search-input:focus {
160169
border-color: #66afe9;
@@ -321,7 +330,7 @@ kbd {
321330

322331
.popover, .popover::before,
323332
#help-button span.top, #help-button span.bottom {
324-
border-color: #DDDDDD;
333+
border-color: #e0e0e0;
325334
}
326335

327336
#copy-path {
Loading

src/librustdoc/html/static/js/search.js

+9-22
Original file line numberDiff line numberDiff line change
@@ -429,9 +429,9 @@ function initSearch(rawSearchIndex) {
429429
}
430430
const posBefore = parserState.pos;
431431
getNextElem(query, parserState, elems, endChar === ">");
432-
// This case can be encountered if `getNextElem` encounted a "stop character" right from
433-
// the start. For example if you have `,,` or `<>`. In this case, we simply move up the
434-
// current position to continue the parsing.
432+
// This case can be encountered if `getNextElem` encountered a "stop character" right
433+
// from the start. For example if you have `,,` or `<>`. In this case, we simply move up
434+
// the current position to continue the parsing.
435435
if (posBefore === parserState.pos) {
436436
parserState.pos += 1;
437437
}
@@ -581,7 +581,7 @@ function initSearch(rawSearchIndex) {
581581
const elem = document.getElementById("crate-search");
582582

583583
if (elem &&
584-
elem.value !== "All crates" &&
584+
elem.value !== "all crates" &&
585585
hasOwnPropertyRustdoc(rawSearchIndex, elem.value)
586586
) {
587587
return elem.value;
@@ -1551,12 +1551,6 @@ function initSearch(rawSearchIndex) {
15511551
return [displayPath, href];
15521552
}
15531553

1554-
function escape(content) {
1555-
const h1 = document.createElement("h1");
1556-
h1.textContent = content;
1557-
return h1.innerHTML;
1558-
}
1559-
15601554
function pathSplitter(path) {
15611555
const tmp = "<span>" + path.replace(/::/g, "::</span><span>");
15621556
if (tmp.endsWith("<span>")) {
@@ -1710,22 +1704,15 @@ function initSearch(rawSearchIndex) {
17101704
let crates = "";
17111705
const crates_list = Object.keys(rawSearchIndex);
17121706
if (crates_list.length > 1) {
1713-
crates = " in <select id=\"crate-search\"><option value=\"All crates\">" +
1714-
"All crates</option>";
1707+
crates = " in&nbsp;<div id=\"crate-search-div\"><select id=\"crate-search\">" +
1708+
"<option value=\"all crates\">all crates</option>";
17151709
for (const c of crates_list) {
17161710
crates += `<option value="${c}" ${c === filterCrates && "selected"}>${c}</option>`;
17171711
}
1718-
crates += "</select>";
1719-
}
1720-
1721-
let typeFilter = "";
1722-
if (results.query.typeFilter !== NO_TYPE_FILTER) {
1723-
typeFilter = " (type: " + escape(itemTypes[results.query.typeFilter]) + ")";
1712+
crates += "</select></div>";
17241713
}
17251714

1726-
let output = "<div id=\"search-settings\">" +
1727-
`<h1 class="search-results-title">Results for ${escape(results.query.userQuery)}` +
1728-
`${typeFilter}</h1>${crates}</div>`;
1715+
let output = `<h1 class="search-results-title">Results${crates}</h1>`;
17291716
if (results.query.error !== null) {
17301717
output += `<h3>Query parser error: "${results.query.error}".</h3>`;
17311718
output += "<div id=\"titles\">" +
@@ -2245,7 +2232,7 @@ function initSearch(rawSearchIndex) {
22452232
}
22462233

22472234
function updateCrate(ev) {
2248-
if (ev.target.value === "All crates") {
2235+
if (ev.target.value === "all crates") {
22492236
// If we don't remove it from the URL, it'll be picked up again by the search.
22502237
const params = searchState.getQueryStringParams();
22512238
const query = searchState.input.value.trim();

src/test/rustdoc-gui/pocket-menu.goml

+1-1
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ reload:
7171
click: "#help-button"
7272
assert-css: (
7373
"#help-button .popover",
74-
{"display": "block", "border-color": "rgb(221, 221, 221)"},
74+
{"display": "block", "border-color": "rgb(224, 224, 224)"},
7575
)
7676
compare-elements-css: ("#help-button .popover", "#help-button .top", ["border-color"])
7777
compare-elements-css: ("#help-button .popover", "#help-button .bottom", ["border-color"])

src/test/rustdoc-gui/search-filter.goml

+9-9
Original file line numberDiff line numberDiff line change
@@ -40,16 +40,16 @@ press-key: "ArrowUp"
4040
press-key: "Enter"
4141
// Waiting for the search results to appear...
4242
wait-for: "#titles"
43-
assert-property: ("#crate-search", {"value": "All crates"})
43+
assert-property: ("#crate-search", {"value": "all crates"})
4444

4545
// Checking that the URL parameter is taken into account for crate filtering.
4646
goto: file://|DOC_PATH|/test_docs/index.html?search=test&filter-crate=lib2
4747
wait-for: "#crate-search"
4848
assert-property: ("#crate-search", {"value": "lib2"})
4949
assert-false: "#results .externcrate"
5050

51-
// Checking that the text for the "title" is correct (the "All" comes from the "<select>").
52-
assert-text: ("#search-settings", "Results for test in All", STARTS_WITH)
51+
// Checking that the text for the "title" is correct (the "all crates" comes from the "<select>").
52+
assert-text: (".search-results-title", "Results in all crates", STARTS_WITH)
5353

5454
// Checking the display of the crate filter.
5555
// We start with the light theme.
@@ -69,15 +69,15 @@ click: "#settings-menu"
6969
wait-for: "#settings"
7070
click: "#theme-dark"
7171
wait-for-css: ("#crate-search", {
72-
"border": "1px solid rgb(240, 240, 240)",
73-
"color": "rgb(17, 17, 17)",
74-
"background-color": "rgb(240, 240, 240)",
72+
"border": "1px solid rgb(210, 210, 210)",
73+
"color": "rgb(221, 221, 221)",
74+
"background-color": "rgb(53, 53, 53)",
7575
})
7676

7777
// And finally we check the ayu theme.
7878
click: "#theme-ayu"
7979
wait-for-css: ("#crate-search", {
80-
"border": "1px solid rgb(66, 76, 87)",
81-
"color": "rgb(197, 197, 197)",
82-
"background-color": "rgb(20, 25, 32)",
80+
"border": "1px solid rgb(92, 103, 115)",
81+
"color": "rgb(255, 255, 255)",
82+
"background-color": "rgb(15, 20, 25)",
8383
})

src/test/rustdoc-gui/search-result-display.goml

+8-16
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ size: (900, 1000)
44
write: (".search-input", "test")
55
// To be SURE that the search will be run.
66
press-key: 'Enter'
7-
wait-for: "#search-settings"
7+
wait-for: "#crate-search"
88
// The width is returned by "getComputedStyle" which returns the exact number instead of the
99
// CSS rule which is "50%"...
1010
assert-css: (".search-results div.desc", {"width": "295px"})
@@ -17,26 +17,18 @@ assert-css: (".search-results div.desc", {"width": "570px"})
1717
// To do so we need to update the length of one of its `<option>`.
1818
size: (900, 900)
1919

20-
// First we check the current width and position.
21-
assert-css: ("#crate-search", {"width": "218px"})
22-
compare-elements-position-near: (
23-
"#crate-search",
24-
"#search-settings .search-results-title",
25-
{"y": 5},
26-
)
20+
// First we check the current width, height and position.
21+
assert-css: ("#crate-search", {"width": "223px"})
22+
assert-css: (".search-results-title", {"height": "44px", "width": "336px"})
2723

2824
// Then we update the text of one of the `<option>`.
2925
text: (
3026
"#crate-search option",
3127
"sdjfaksdjfaksjdbfkadsbfkjsadbfkdsbkfbsadkjfbkdsabfkadsfkjdsafa",
3228
)
3329

34-
// Then we compare again.
35-
assert-css: ("#crate-search", {"width": "640px"})
36-
compare-elements-position-near-false: (
37-
"#crate-search",
38-
"#search-settings .search-results-title",
39-
{"y": 5},
40-
)
41-
// And we check that the `<select>` isn't bigger than its container.
30+
// Then we compare again to confirm the height didn't change.
31+
assert-css: ("#crate-search", {"width": "527px"})
32+
assert-css: (".search-results-title", {"height": "44px", "width": "640px"})
33+
// And we check that the `<select>` isn't bigger than its container (".search-results-title").
4234
assert-css: ("#search", {"width": "640px"})

0 commit comments

Comments
 (0)