Skip to content

Rustdoc improvements #50259

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 9 commits into from
May 15, 2018
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
38 changes: 30 additions & 8 deletions src/librustdoc/html/render.rs
Original file line number Diff line number Diff line change
Expand Up @@ -413,9 +413,9 @@ impl ToJson for Type {
match self.name {
Some(ref name) => {
let mut data = BTreeMap::new();
data.insert("name".to_owned(), name.to_json());
data.insert("n".to_owned(), name.to_json());
if let Some(ref generics) = self.generics {
data.insert("generics".to_owned(), generics.to_json());
data.insert("g".to_owned(), generics.to_json());
}
Json::Object(data)
},
Expand All @@ -438,8 +438,12 @@ impl ToJson for IndexItemFunctionType {
Json::Null
} else {
let mut data = BTreeMap::new();
data.insert("inputs".to_owned(), self.inputs.to_json());
data.insert("output".to_owned(), self.output.to_json());
if !self.inputs.is_empty() {
data.insert("i".to_owned(), self.inputs.to_json());
}
if let Some(ref output) = self.output {
data.insert("o".to_owned(), output.to_json());
}
Json::Object(data)
}
}
Expand Down Expand Up @@ -789,7 +793,8 @@ fn write_shared(cx: &Context,
format!(
r#"var themes = document.getElementById("theme-choices");
var themePicker = document.getElementById("theme-picker");
themePicker.onclick = function() {{

function switchThemeButtonState() {{
if (themes.style.display === "block") {{
themes.style.display = "none";
themePicker.style.borderBottomRightRadius = "3px";
Expand All @@ -800,12 +805,29 @@ themePicker.onclick = function() {{
themePicker.style.borderBottomLeftRadius = "0";
}}
}};

function handleThemeButtonsBlur(e) {{
var active = document.activeElement;
var related = e.relatedTarget;

if (active.id !== "themePicker" &&
(!active.parentNode || active.parentNode.id !== "theme-choices") &&
(!related ||
(related.id !== "themePicker" &&
(!related.parentNode || related.parentNode.id !== "theme-choices")))) {{
switchThemeButtonState();
}}
}}

themePicker.onclick = switchThemeButtonState;
themePicker.onblur = handleThemeButtonsBlur;
[{}].forEach(function(item) {{
var but = document.createElement('button');
but.innerHTML = item;
but.onclick = function(el) {{
switchTheme(currentTheme, mainTheme, item);
}};
but.onblur = handleThemeButtonsBlur;
themes.appendChild(but);
}});"#,
themes.iter()
Expand Down Expand Up @@ -879,8 +901,8 @@ themePicker.onclick = function() {{
}

fn show_item(item: &IndexItem, krate: &str) -> String {
format!("{{'crate':'{}','ty':{},'name':'{}','path':'{}'{}}}",
krate, item.ty as usize, item.name, item.path,
format!("{{'crate':'{}','ty':{},'name':'{}','desc':'{}','p':'{}'{}}}",
krate, item.ty as usize, item.name, item.desc.replace("'", "\\'"), item.path,
Copy link
Member

Choose a reason for hiding this comment

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

.replace("'", "\\'") isn't how you escape strings for javascript. You need to use something like as_json.

Copy link
Member Author

Choose a reason for hiding this comment

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

Bad surprise, quotes aren't escaped when using Json object or json conversion functions so for now, it's the best solution.

if let Some(p) = item.parent_idx {
format!(",'parent':{}", p)
} else {
Expand Down Expand Up @@ -1442,7 +1464,7 @@ impl<'a> Cache {
ty: item.type_(),
name: item_name.to_string(),
path: path.clone(),
desc: String::new(),
desc: plain_summary_line(item.doc_value()),
parent: None,
parent_idx: None,
search_type: get_index_search_type(&item),
Expand Down
Loading