Skip to content

Commit b336f28

Browse files
Fix invalid generation of HTML in highlight
1 parent ef0d909 commit b336f28

File tree

3 files changed

+60
-41
lines changed

3 files changed

+60
-41
lines changed

Diff for: src/librustdoc/html/highlight.rs

+42-41
Original file line numberDiff line numberDiff line change
@@ -313,6 +313,7 @@ impl<'a> Classifier<'a> {
313313
.unwrap_or(false)
314314
{
315315
let tokens = self.get_full_ident_path();
316+
// We need this variable because `tokens` is consumed in the loop.
316317
let skip = !tokens.is_empty();
317318
for (token, start, end) in tokens {
318319
let text = &self.src[start..end];
@@ -549,51 +550,51 @@ fn string<T: Display>(
549550
None => return write!(out, "{}", text),
550551
Some(klass) => klass,
551552
};
552-
if let Some(def_span) = klass.get_span() {
553-
let mut text = text.to_string();
554-
if text.contains("::") {
555-
text = text.split("::").intersperse("::").fold(String::new(), |mut path, t| {
556-
match t {
557-
"self" | "Self" => write!(
558-
&mut path,
559-
"<span class=\"{}\">{}</span>",
560-
Class::Self_(LightSpan::empty()).as_html(),
561-
t
562-
),
563-
"crate" | "super" => write!(
564-
&mut path,
565-
"<span class=\"{}\">{}</span>",
566-
Class::KeyWord.as_html(),
567-
t
568-
),
569-
t => write!(&mut path, "{}", t),
570-
}
571-
.expect("Failed to build source HTML path");
572-
path
573-
});
553+
let def_span = match klass.get_span() {
554+
Some(d) => d,
555+
None => {
556+
write!(out, "<span class=\"{}\">{}</span>", klass.as_html(), text);
557+
return;
574558
}
575-
if let Some(context_info) = context_info {
576-
if let Some(href) =
577-
context_info.context.shared.span_correspondance_map.get(&def_span).and_then(
578-
|href| {
579-
let context = context_info.context;
580-
match href {
581-
LinkFromSrc::Local(span) => context
582-
.href_from_span(*span)
583-
.map(|s| format!("{}{}", context_info.root_path, s)),
584-
LinkFromSrc::External(def_id) => {
585-
format::href(*def_id, context).map(|(url, _, _)| url)
586-
}
587-
}
588-
},
589-
)
590-
{
591-
write!(out, "<a class=\"{}\" href=\"{}\">{}</a>", klass.as_html(), href, text);
592-
return;
559+
};
560+
let mut text_s = text.to_string();
561+
if text_s.contains("::") {
562+
text_s = text_s.split("::").intersperse("::").fold(String::new(), |mut path, t| {
563+
match t {
564+
"self" | "Self" => write!(
565+
&mut path,
566+
"<span class=\"{}\">{}</span>",
567+
Class::Self_(LightSpan::empty()).as_html(),
568+
t
569+
),
570+
"crate" | "super" => {
571+
write!(&mut path, "<span class=\"{}\">{}</span>", Class::KeyWord.as_html(), t)
572+
}
573+
t => write!(&mut path, "{}", t),
593574
}
575+
.expect("Failed to build source HTML path");
576+
path
577+
});
578+
}
579+
if let Some(context_info) = context_info {
580+
if let Some(href) =
581+
context_info.context.shared.span_correspondance_map.get(&def_span).and_then(|href| {
582+
let context = context_info.context;
583+
match href {
584+
LinkFromSrc::Local(span) => context
585+
.href_from_span(*span)
586+
.map(|s| format!("{}{}", context_info.root_path, s)),
587+
LinkFromSrc::External(def_id) => {
588+
format::href(*def_id, context).map(|(url, _, _)| url)
589+
}
590+
}
591+
})
592+
{
593+
write!(out, "<a class=\"{}\" href=\"{}\">{}</a>", klass.as_html(), href, text_s);
594+
return;
594595
}
595596
}
596-
write!(out, "<span class=\"{}\">{}</span>", klass.as_html(), text);
597+
write!(out, "<span class=\"{}\">{}</span>", klass.as_html(), text_s);
597598
}
598599

599600
#[cfg(test)]
+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
<span class="kw">use</span> <span class="ident"><span class="kw">crate</span>::a::foo</span>;
2+
<span class="kw">use</span> <span class="ident"><span class="self">self</span>::whatever</span>;
3+
<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>;
4+
<span class="kw">let</span> <span class="ident">y</span> <span class="op">=</span> <span class="ident"><span class="self">Self</span>::whatever</span>;

Diff for: src/librustdoc/html/highlight/tests.rs

+14
Original file line numberDiff line numberDiff line change
@@ -40,3 +40,17 @@ fn test_dos_backline() {
4040
expect_file!["fixtures/dos_line.html"].assert_eq(&html.into_inner());
4141
});
4242
}
43+
44+
#[test]
45+
fn test_keyword_highlight() {
46+
create_default_session_globals_then(|| {
47+
let src = "use crate::a::foo;
48+
use self::whatever;
49+
let x = super::b::foo;
50+
let y = Self::whatever;";
51+
52+
let mut html = Buffer::new();
53+
write_code(&mut html, src, Edition::Edition2018, None);
54+
expect_file!["fixtures/highlight.html"].assert_eq(&html.into_inner());
55+
});
56+
}

0 commit comments

Comments
 (0)