@@ -142,39 +142,30 @@ fn extract_html_tag(
142
142
}
143
143
}
144
144
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
-
166
145
fn extract_tags (
167
146
tags : & mut Vec < ( String , Range < usize > ) > ,
168
147
text : & str ,
169
148
range : Range < usize > ,
149
+ is_in_comment : & mut Option < Range < usize > > ,
170
150
f : & impl Fn ( & str , & Range < usize > ) ,
171
151
) {
172
152
let mut iter = text. char_indices ( ) . peekable ( ) ;
173
153
174
154
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 == '<' {
176
160
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
+ } ) ;
178
169
} else {
179
170
extract_html_tag ( tags, text, & range, start_pos, & mut iter, f) ;
180
171
}
@@ -205,12 +196,15 @@ impl<'a, 'tcx> DocFolder for InvalidHtmlTagsLinter<'a, 'tcx> {
205
196
} ;
206
197
207
198
let mut tags = Vec :: new ( ) ;
199
+ let mut is_in_comment = None ;
208
200
209
201
let p = Parser :: new_ext ( & dox, opts ( ) ) . into_offset_iter ( ) ;
210
202
211
203
for ( event, range) in p {
212
204
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
+ }
214
208
_ => { }
215
209
}
216
210
}
@@ -221,6 +215,10 @@ impl<'a, 'tcx> DocFolder for InvalidHtmlTagsLinter<'a, 'tcx> {
221
215
} ) {
222
216
report_diag ( & format ! ( "unclosed HTML tag `{}`" , tag) , range) ;
223
217
}
218
+
219
+ if let Some ( range) = is_in_comment {
220
+ report_diag ( "Unclosed HTML comment" , & range) ;
221
+ }
224
222
}
225
223
226
224
self . fold_item_recur ( item)
0 commit comments