Skip to content

Commit

Permalink
Add check for HTML comments
Browse files Browse the repository at this point in the history
  • Loading branch information
GuillaumeGomez committed Oct 13, 2020
1 parent 861b892 commit 0009cba
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 6 deletions.
34 changes: 29 additions & 5 deletions src/librustdoc/passes/html_tags.rs
Expand Up @@ -4,11 +4,11 @@ use crate::core::DocContext;
use crate::fold::DocFolder;
use crate::html::markdown::opts;
use core::ops::Range;
use std::iter::Peekable;
use std::str::CharIndices;
use pulldown_cmark::{Event, Parser};
use rustc_feature::UnstableFeatures;
use rustc_session::lint;
use std::iter::Peekable;
use std::str::CharIndices;

pub const CHECK_INVALID_HTML_TAGS: Pass = Pass {
name: "check-invalid-html-tags",
Expand Down Expand Up @@ -104,8 +104,7 @@ fn extract_html_tag(
tag_name.push(c);
} else {
if !tag_name.is_empty() {
let mut r =
Range { start: range.start + start_pos, end: range.start + pos };
let mut r = Range { start: range.start + start_pos, end: range.start + pos };
if c == '>' {
// In case we have a tag without attribute, we can consider the span to
// refer to it fully.
Expand Down Expand Up @@ -143,6 +142,27 @@ 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,
Expand All @@ -153,7 +173,11 @@ fn extract_tags(

while let Some((start_pos, c)) = iter.next() {
if c == '<' {
extract_html_tag(tags, text, &range, start_pos, &mut iter, f);
if text[start_pos..].starts_with("<!--") {
extract_html_comment(text, &range, start_pos, &mut iter, f);
} else {
extract_html_tag(tags, text, &range, start_pos, &mut iter, f);
}
}
}
}
Expand Down
10 changes: 10 additions & 0 deletions src/test/rustdoc-ui/invalid-html-tags.rs
Expand Up @@ -73,3 +73,13 @@ pub fn e() {}
/// <div></div
//~^ ERROR unclosed HTML tag `div`
pub fn f() {}

/// <!---->
/// <!-- -->
/// <!-- <div> -->
/// <!-- <!-- -->
pub fn g() {}

/// <!--
//~^ ERROR Unclosed HTML comment
pub fn h() {}
8 changes: 7 additions & 1 deletion src/test/rustdoc-ui/invalid-html-tags.stderr
Expand Up @@ -76,5 +76,11 @@ error: unclosed HTML tag `div`
LL | /// <div></div
| ^^^^^

error: aborting due to 12 previous errors
error: Unclosed HTML comment
--> $DIR/invalid-html-tags.rs:83:5
|
LL | /// <!--
| ^^^

error: aborting due to 13 previous errors

0 comments on commit 0009cba

Please sign in to comment.