@@ -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-
166145fn 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)
0 commit comments