Skip to content

Commit dd3be78

Browse files
Handle multi-line HTML comments
1 parent 0009cba commit dd3be78

File tree

3 files changed

+28
-26
lines changed

3 files changed

+28
-26
lines changed

Diff for: src/librustdoc/passes/html_tags.rs

+22-24
Original file line numberDiff line numberDiff line change
@@ -142,39 +142,30 @@ fn extract_html_tag(
142142
}
143143
}
144144

145-
fn extract_html_comment(
146-
text: &str,
147-
range: &Range<usize>,
148-
start_pos: usize,
149-
iter: &mut Peekable<CharIndices<'_>>,
150-
f: &impl Fn(&str, &Range<usize>),
151-
) {
152-
// We first skip the "!--" part.
153-
let mut iter = iter.skip(3);
154-
while let Some((pos, c)) = iter.next() {
155-
if c == '-' && text[pos..].starts_with("-->") {
156-
// All good, we can leave!
157-
return;
158-
}
159-
}
160-
f(
161-
"Unclosed HTML comment",
162-
&Range { start: range.start + start_pos, end: range.start + start_pos + 3 },
163-
);
164-
}
165-
166145
fn extract_tags(
167146
tags: &mut Vec<(String, Range<usize>)>,
168147
text: &str,
169148
range: Range<usize>,
149+
is_in_comment: &mut Option<Range<usize>>,
170150
f: &impl Fn(&str, &Range<usize>),
171151
) {
172152
let mut iter = text.char_indices().peekable();
173153

174154
while let Some((start_pos, c)) = iter.next() {
175-
if c == '<' {
155+
if is_in_comment.is_some() {
156+
if text[start_pos..].starts_with("-->") {
157+
*is_in_comment = None;
158+
}
159+
} else if c == '<' {
176160
if text[start_pos..].starts_with("<!--") {
177-
extract_html_comment(text, &range, start_pos, &mut iter, f);
161+
// We skip the "!--" part. (Once `advance_by` is stable, might be nice to use it!)
162+
iter.next();
163+
iter.next();
164+
iter.next();
165+
*is_in_comment = Some(Range {
166+
start: range.start + start_pos,
167+
end: range.start + start_pos + 3,
168+
});
178169
} else {
179170
extract_html_tag(tags, text, &range, start_pos, &mut iter, f);
180171
}
@@ -205,12 +196,15 @@ impl<'a, 'tcx> DocFolder for InvalidHtmlTagsLinter<'a, 'tcx> {
205196
};
206197

207198
let mut tags = Vec::new();
199+
let mut is_in_comment = None;
208200

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

211203
for (event, range) in p {
212204
match event {
213-
Event::Html(text) => extract_tags(&mut tags, &text, range, &report_diag),
205+
Event::Html(text) | Event::Text(text) => {
206+
extract_tags(&mut tags, &text, range, &mut is_in_comment, &report_diag)
207+
}
214208
_ => {}
215209
}
216210
}
@@ -221,6 +215,10 @@ impl<'a, 'tcx> DocFolder for InvalidHtmlTagsLinter<'a, 'tcx> {
221215
}) {
222216
report_diag(&format!("unclosed HTML tag `{}`", tag), range);
223217
}
218+
219+
if let Some(range) = is_in_comment {
220+
report_diag("Unclosed HTML comment", &range);
221+
}
224222
}
225223

226224
self.fold_item_recur(item)

Diff for: src/test/rustdoc-ui/invalid-html-tags.rs

+5-1
Original file line numberDiff line numberDiff line change
@@ -81,5 +81,9 @@ pub fn f() {}
8181
pub fn g() {}
8282

8383
/// <!--
84-
//~^ ERROR Unclosed HTML comment
84+
/// -->
8585
pub fn h() {}
86+
87+
/// <!--
88+
//~^ ERROR Unclosed HTML comment
89+
pub fn i() {}

Diff for: src/test/rustdoc-ui/invalid-html-tags.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ LL | /// <div></div
7777
| ^^^^^
7878

7979
error: Unclosed HTML comment
80-
--> $DIR/invalid-html-tags.rs:83:5
80+
--> $DIR/invalid-html-tags.rs:87:5
8181
|
8282
LL | /// <!--
8383
| ^^^

0 commit comments

Comments
 (0)