Fix invalid generation of HTML in highlight
This commit is contained in:
parent
ef0d909f26
commit
b336f2801c
3 changed files with 60 additions and 41 deletions
|
@ -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];
|
||||
|
@ -549,10 +550,16 @@ 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| {
|
||||
let def_span = match klass.get_span() {
|
||||
Some(d) => d,
|
||||
None => {
|
||||
write!(out, "<span class=\"{}\">{}</span>", klass.as_html(), 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,
|
||||
|
@ -560,12 +567,9 @@ fn string<T: Display>(
|
|||
Class::Self_(LightSpan::empty()).as_html(),
|
||||
t
|
||||
),
|
||||
"crate" | "super" => write!(
|
||||
&mut path,
|
||||
"<span class=\"{}\">{}</span>",
|
||||
Class::KeyWord.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");
|
||||
|
@ -574,8 +578,7 @@ fn string<T: Display>(
|
|||
}
|
||||
if let Some(context_info) = context_info {
|
||||
if let Some(href) =
|
||||
context_info.context.shared.span_correspondance_map.get(&def_span).and_then(
|
||||
|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
|
||||
|
@ -585,15 +588,13 @@ fn string<T: Display>(
|
|||
format::href(*def_id, context).map(|(url, _, _)| url)
|
||||
}
|
||||
}
|
||||
},
|
||||
)
|
||||
})
|
||||
{
|
||||
write!(out, "<a class=\"{}\" href=\"{}\">{}</a>", klass.as_html(), href, text);
|
||||
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)]
|
||||
|
|
4
src/librustdoc/html/highlight/fixtures/highlight.html
Normal file
4
src/librustdoc/html/highlight/fixtures/highlight.html
Normal file
|
@ -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>;
|
|
@ -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());
|
||||
});
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue