Skip to content
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

[WIP] Start transforming documentation toggles into pure css ones #83355

Closed
Closed
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
7 changes: 4 additions & 3 deletions src/librustdoc/html/format.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,19 +51,20 @@ impl Print for &'_ str {
crate struct Buffer {
for_html: bool,
buffer: String,
crate label_id_count: usize,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Buffer seems like the wrong place to put this field. Maybe it should be on Context instead?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good point.

}

impl Buffer {
crate fn empty_from(v: &Buffer) -> Buffer {
Buffer { for_html: v.for_html, buffer: String::new() }
Buffer { for_html: v.for_html, buffer: String::new(), label_id_count: 0 }
}

crate fn html() -> Buffer {
Buffer { for_html: true, buffer: String::new() }
Buffer { for_html: true, buffer: String::new(), label_id_count: 0 }
}

crate fn new() -> Buffer {
Buffer { for_html: false, buffer: String::new() }
Buffer { for_html: false, buffer: String::new(), label_id_count: 0 }
}

crate fn is_empty(&self) -> bool {
Expand Down
26 changes: 19 additions & 7 deletions src/librustdoc/html/render/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -626,9 +626,14 @@ fn render_markdown(
is_hidden: bool,
) {
let mut ids = cx.id_map.borrow_mut();
let label_id_count = w.label_id_count;
w.label_id_count += 1;
write!(
w,
"<div class=\"docblock{}\">{}{}</div>",
"<input id=\"{0}i\" type=checkbox class=\"toggle\"{1}><label for=\"{0}i\"></label>\
<div class=\"docblock{2}\">{3}{4}</div>",
label_id_count,
if is_hidden { "" } else { " checked" },
if is_hidden { " hidden" } else { "" },
prefix,
Markdown(
Expand Down Expand Up @@ -1519,12 +1524,12 @@ fn render_impl(
} else {
(true, " hidden")
};
match *item.kind {
let need_close = match *item.kind {
clean::MethodItem(..) | clean::TyMethodItem(_) => {
// Only render when the method is not static or we allow static methods
if render_method_item {
let id = cx.derive_id(format!("{}.{}", item_type, name));
write!(w, "<h4 id=\"{}\" class=\"{}{}\">", id, item_type, extra_class);
write!(w, "<div><h4 id=\"{}\" class=\"{}{}\">", id, item_type, extra_class);
w.write_str("<code>");
render_assoc_item(w, item, link.anchor(&id), ItemType::Impl, cx);
w.write_str("</code>");
Expand All @@ -1538,10 +1543,11 @@ fn render_impl(
write_srclink(cx, item, w);
w.write_str("</h4>");
}
render_method_item
}
clean::TypedefItem(ref tydef, _) => {
let id = cx.derive_id(format!("{}.{}", ItemType::AssocType, name));
write!(w, "<h4 id=\"{}\" class=\"{}{}\"><code>", id, item_type, extra_class);
write!(w, "<div><h4 id=\"{}\" class=\"{}{}\"><code>", id, item_type, extra_class);
assoc_type(
w,
item,
Expand All @@ -1552,10 +1558,11 @@ fn render_impl(
cx.cache(),
);
w.write_str("</code></h4>");
true
}
clean::AssocConstItem(ref ty, ref default) => {
let id = cx.derive_id(format!("{}.{}", item_type, name));
write!(w, "<h4 id=\"{}\" class=\"{}{}\"><code>", id, item_type, extra_class);
write!(w, "<div><h4 id=\"{}\" class=\"{}{}\"><code>", id, item_type, extra_class);
assoc_const(w, item, ty, default.as_ref(), link.anchor(&id), "", cx);
w.write_str("</code>");
render_stability_since_raw(
Expand All @@ -1567,16 +1574,18 @@ fn render_impl(
);
write_srclink(cx, item, w);
w.write_str("</h4>");
true
}
clean::AssocTypeItem(ref bounds, ref default) => {
let id = cx.derive_id(format!("{}.{}", item_type, name));
write!(w, "<h4 id=\"{}\" class=\"{}{}\"><code>", id, item_type, extra_class);
write!(w, "<div><h4 id=\"{}\" class=\"{}{}\"><code>", id, item_type, extra_class);
assoc_type(w, item, bounds, default.as_ref(), link.anchor(&id), "", cx.cache());
w.write_str("</code></h4>");
true
}
clean::StrippedItem(..) => return,
_ => panic!("can't make docs for trait item with name {:?}", item.name),
}
};

if render_method_item {
if !is_default_item {
Expand Down Expand Up @@ -1614,6 +1623,9 @@ fn render_impl(
document_short(w, item, cx, link, "", is_hidden, Some(parent), show_def_docs);
}
}
if need_close {
w.write_str("</div>");
}
}

w.write_str("<div class=\"impl-items\">");
Expand Down
36 changes: 27 additions & 9 deletions src/librustdoc/html/static/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -2249,9 +2249,14 @@ function defocusSearchBar() {
});
innerToggle.title = "collapse all docs";
if (fromAutoCollapse !== true) {
// Old toggle handling.
onEachLazy(document.getElementsByClassName("collapse-toggle"), function(e) {
collapseDocs(e, "show");
});
// New toggle handling.
onEachLazy(document.getElementsByClassName("toggle"), function(e) {
e.checked = true;
});
}
} else {
updateLocalStorage("rustdoc-collapse", "true");
Expand All @@ -2270,6 +2275,7 @@ function defocusSearchBar() {
});
innerToggle.title = "expand all docs";
if (fromAutoCollapse !== true) {
// Old toggle handling.
onEachLazy(document.getElementsByClassName("collapse-toggle"), function(e) {
var parent = e.parentNode;
var superParent = null;
Expand All @@ -2282,6 +2288,19 @@ function defocusSearchBar() {
collapseDocs(e, "hide");
}
});
// New toggle handling.
onEachLazy(document.getElementsByClassName("toggle"), function(e) {
var parent = e.parentNode;
var superParent = null;

if (parent) {
superParent = parent.parentNode;
}
if (!parent || !superParent || superParent.id !== "main" ||
hasClass(parent, "impl") === false) {
e.checked = false;
}
});
}
}
}
Expand Down Expand Up @@ -2460,7 +2479,7 @@ function defocusSearchBar() {
span.style.display = "none";
}
if (!otherMessage) {
span.innerHTML = "&nbsp;Expand&nbsp;description";
return null;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: This is 3-space indent, it should be 4.

} else {
span.innerHTML = otherMessage;
}
Expand Down Expand Up @@ -2506,11 +2525,9 @@ function defocusSearchBar() {
if (!next) {
return;
}
if (hasClass(next, "docblock")) {
var newToggle = toggle.cloneNode(true);
insertAfter(newToggle, e.childNodes[e.childNodes.length - 1]);
if (hasClass(next, "toggle")) {
if (hideMethodDocs === true && hasClass(e, "method") === true) {
collapseDocs(newToggle, "hide");
next.checked = true;
}
}
};
Expand Down Expand Up @@ -2672,14 +2689,15 @@ function defocusSearchBar() {
extraClass = "marg-left";
}

e.parentNode.insertBefore(
createToggle(
var t = createToggle(
toggle,
otherMessage,
fontSize,
extraClass,
hasClass(e, "type-decl") === false || showItemDeclarations === true),
e);
hasClass(e, "type-decl") === false || showItemDeclarations === true);
if (t) {
e.parentNode.insertBefore(t, e);
}
if (hasClass(e, "type-decl") === true && showItemDeclarations === true) {
collapseDocs(e.previousSibling.childNodes[0], "toggle");
}
Expand Down
43 changes: 43 additions & 0 deletions src/librustdoc/html/static/rustdoc.css
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,10 @@ html {
}
}

div {
position: relative;
}

/* General structure and fonts */

body {
Expand Down Expand Up @@ -1768,3 +1772,42 @@ div.name.expand::before {
.type-decl > pre > .docblock.attributes {
margin-left: 4em;
}

input.toggle {
display: none;
}
input.toggle + label {
position: absolute;
top: 0;
font-family: "Fira Sans", Arial, sans-serif;
font-weight: 300;
cursor: pointer;
}
input.toggle + label:before {
content: "[+]";
}
input.toggle:checked + label:before {
content: "[-]";
}
input.toggle:not(:checked) + label + .docblock {
display: none;
}
h4 + input.toggle + label {
font-size: 0.8em;
top: 5px;
}
.type-decl + input.toggle + label {
top: initial;
}
.type-decl + input.toggle:not(:checked) + label {
margin-bottom: .6em;
height: 25px;
position: initial;
}
.type-decl + input.toggle:not(:checked) + label:before {
content: "[+]";
}
.type-decl + input.toggle:not(:checked) + label:after {
content: "Expand description";
margin-left: 6px;
}
2 changes: 1 addition & 1 deletion src/librustdoc/html/static/themes/dark.css
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,7 @@ a.test-arrow {
color: #dedede;
}

.collapse-toggle {
.collapse-toggle, input.toggle + label {
color: #999;
}

Expand Down