Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Handle multi-line HTML comments
  • Loading branch information
GuillaumeGomez committed Oct 14, 2020
1 parent 0009cba commit dd3be78
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 26 deletions.
46 changes: 22 additions & 24 deletions src/librustdoc/passes/html_tags.rs
Expand Up @@ -142,39 +142,30 @@ fn extract_html_tag(
}
}

fn extract_html_comment(
text: &str,
range: &Range<usize>,
start_pos: usize,
iter: &mut Peekable<CharIndices<'_>>,
f: &impl Fn(&str, &Range<usize>),
) {
// We first skip the "!--" part.
let mut iter = iter.skip(3);
while let Some((pos, c)) = iter.next() {
if c == '-' && text[pos..].starts_with("-->") {
// All good, we can leave!
return;
}
}
f(
"Unclosed HTML comment",
&Range { start: range.start + start_pos, end: range.start + start_pos + 3 },
);
}

fn extract_tags(
tags: &mut Vec<(String, Range<usize>)>,
text: &str,
range: Range<usize>,
is_in_comment: &mut Option<Range<usize>>,
f: &impl Fn(&str, &Range<usize>),
) {
let mut iter = text.char_indices().peekable();

while let Some((start_pos, c)) = iter.next() {
if c == '<' {
if is_in_comment.is_some() {
if text[start_pos..].starts_with("-->") {
*is_in_comment = None;
}
} else if c == '<' {
if text[start_pos..].starts_with("<!--") {
extract_html_comment(text, &range, start_pos, &mut iter, f);
// We skip the "!--" part. (Once `advance_by` is stable, might be nice to use it!)
iter.next();
iter.next();
iter.next();
*is_in_comment = Some(Range {
start: range.start + start_pos,
end: range.start + start_pos + 3,
});
} else {
extract_html_tag(tags, text, &range, start_pos, &mut iter, f);
}
Expand Down Expand Up @@ -205,12 +196,15 @@ impl<'a, 'tcx> DocFolder for InvalidHtmlTagsLinter<'a, 'tcx> {
};

let mut tags = Vec::new();
let mut is_in_comment = None;

let p = Parser::new_ext(&dox, opts()).into_offset_iter();

for (event, range) in p {
match event {
Event::Html(text) => extract_tags(&mut tags, &text, range, &report_diag),
Event::Html(text) | Event::Text(text) => {
extract_tags(&mut tags, &text, range, &mut is_in_comment, &report_diag)
}
_ => {}
}
}
Expand All @@ -221,6 +215,10 @@ impl<'a, 'tcx> DocFolder for InvalidHtmlTagsLinter<'a, 'tcx> {
}) {
report_diag(&format!("unclosed HTML tag `{}`", tag), range);
}

if let Some(range) = is_in_comment {
report_diag("Unclosed HTML comment", &range);
}
}

self.fold_item_recur(item)
Expand Down
6 changes: 5 additions & 1 deletion src/test/rustdoc-ui/invalid-html-tags.rs
Expand Up @@ -81,5 +81,9 @@ pub fn f() {}
pub fn g() {}

/// <!--
//~^ ERROR Unclosed HTML comment
/// -->
pub fn h() {}

/// <!--
//~^ ERROR Unclosed HTML comment
pub fn i() {}
2 changes: 1 addition & 1 deletion src/test/rustdoc-ui/invalid-html-tags.stderr
Expand Up @@ -77,7 +77,7 @@ LL | /// <div></div
| ^^^^^

error: Unclosed HTML comment
--> $DIR/invalid-html-tags.rs:83:5
--> $DIR/invalid-html-tags.rs:87:5
|
LL | /// <!--
| ^^^
Expand Down

0 comments on commit dd3be78

Please sign in to comment.