Skip to content

Commit

Permalink
Fix invalid generation of HTML in highlight
Browse files Browse the repository at this point in the history
  • Loading branch information
GuillaumeGomez committed Aug 5, 2021
1 parent ef0d909 commit b336f28
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 41 deletions.
83 changes: 42 additions & 41 deletions src/librustdoc/html/highlight.rs
Expand Up @@ -313,6 +313,7 @@ impl<'a> Classifier<'a> {
.unwrap_or(false)
{
let tokens = self.get_full_ident_path();
// We need this variable because `tokens` is consumed in the loop.
let skip = !tokens.is_empty();
for (token, start, end) in tokens {
let text = &self.src[start..end];
Expand Down Expand Up @@ -549,51 +550,51 @@ fn string<T: Display>(
None => return write!(out, "{}", text),
Some(klass) => klass,
};
if let Some(def_span) = klass.get_span() {
let mut text = text.to_string();
if text.contains("::") {
text = text.split("::").intersperse("::").fold(String::new(), |mut path, t| {
match t {
"self" | "Self" => write!(
&mut path,
"<span class=\"{}\">{}</span>",
Class::Self_(LightSpan::empty()).as_html(),
t
),
"crate" | "super" => write!(
&mut path,
"<span class=\"{}\">{}</span>",
Class::KeyWord.as_html(),
t
),
t => write!(&mut path, "{}", t),
}
.expect("Failed to build source HTML path");
path
});
let def_span = match klass.get_span() {
Some(d) => d,
None => {
write!(out, "<span class=\"{}\">{}</span>", klass.as_html(), text);
return;
}
if let Some(context_info) = context_info {
if let Some(href) =
context_info.context.shared.span_correspondance_map.get(&def_span).and_then(
|href| {
let context = context_info.context;
match href {
LinkFromSrc::Local(span) => context
.href_from_span(*span)
.map(|s| format!("{}{}", context_info.root_path, s)),
LinkFromSrc::External(def_id) => {
format::href(*def_id, context).map(|(url, _, _)| url)
}
}
},
)
{
write!(out, "<a class=\"{}\" href=\"{}\">{}</a>", klass.as_html(), href, text);
return;
};
let mut text_s = text.to_string();
if text_s.contains("::") {
text_s = text_s.split("::").intersperse("::").fold(String::new(), |mut path, t| {
match t {
"self" | "Self" => write!(
&mut path,
"<span class=\"{}\">{}</span>",
Class::Self_(LightSpan::empty()).as_html(),
t
),
"crate" | "super" => {
write!(&mut path, "<span class=\"{}\">{}</span>", Class::KeyWord.as_html(), t)
}
t => write!(&mut path, "{}", t),
}
.expect("Failed to build source HTML path");
path
});
}
if let Some(context_info) = context_info {
if let Some(href) =
context_info.context.shared.span_correspondance_map.get(&def_span).and_then(|href| {
let context = context_info.context;
match href {
LinkFromSrc::Local(span) => context
.href_from_span(*span)
.map(|s| format!("{}{}", context_info.root_path, s)),
LinkFromSrc::External(def_id) => {
format::href(*def_id, context).map(|(url, _, _)| url)
}
}
})
{
write!(out, "<a class=\"{}\" href=\"{}\">{}</a>", klass.as_html(), href, text_s);
return;
}
}
write!(out, "<span class=\"{}\">{}</span>", klass.as_html(), text);
write!(out, "<span class=\"{}\">{}</span>", klass.as_html(), text_s);
}

#[cfg(test)]
Expand Down
4 changes: 4 additions & 0 deletions src/librustdoc/html/highlight/fixtures/highlight.html
@@ -0,0 +1,4 @@
<span class="kw">use</span> <span class="ident"><span class="kw">crate</span>::a::foo</span>;
<span class="kw">use</span> <span class="ident"><span class="self">self</span>::whatever</span>;
<span class="kw">let</span> <span class="ident">x</span> <span class="op">=</span> <span class="ident"><span class="kw">super</span>::b::foo</span>;
<span class="kw">let</span> <span class="ident">y</span> <span class="op">=</span> <span class="ident"><span class="self">Self</span>::whatever</span>;
14 changes: 14 additions & 0 deletions src/librustdoc/html/highlight/tests.rs
Expand Up @@ -40,3 +40,17 @@ fn test_dos_backline() {
expect_file!["fixtures/dos_line.html"].assert_eq(&html.into_inner());
});
}

#[test]
fn test_keyword_highlight() {
create_default_session_globals_then(|| {
let src = "use crate::a::foo;
use self::whatever;
let x = super::b::foo;
let y = Self::whatever;";

let mut html = Buffer::new();
write_code(&mut html, src, Edition::Edition2018, None);
expect_file!["fixtures/highlight.html"].assert_eq(&html.into_inner());
});
}

0 comments on commit b336f28

Please sign in to comment.