Rollup merge of #103505 - notriddle:notriddle/rustdoc-self-closing-tags, r=GuillaumeGomez

rustdoc: parse self-closing tags and attributes in `invalid_html_tags`

Fixes #103460
This commit is contained in:
Matthias Krüger 2022-10-27 15:03:57 +02:00 committed by GitHub
commit 5623024cf8
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 204 additions and 1 deletions

View file

@ -184,7 +184,60 @@ fn extract_html_tag(
}
drop_tag(tags, tag_name, r, f);
} else {
tags.push((tag_name, r));
let mut is_self_closing = false;
let mut quote_pos = None;
if c != '>' {
let mut quote = None;
let mut after_eq = false;
for (i, c) in text[pos..].char_indices() {
if !c.is_whitespace() {
if let Some(q) = quote {
if c == q {
quote = None;
quote_pos = None;
after_eq = false;
}
} else if c == '>' {
break;
} else if c == '/' && !after_eq {
is_self_closing = true;
} else {
if is_self_closing {
is_self_closing = false;
}
if (c == '"' || c == '\'') && after_eq {
quote = Some(c);
quote_pos = Some(pos + i);
} else if c == '=' {
after_eq = true;
}
}
} else if quote.is_none() {
after_eq = false;
}
}
}
if let Some(quote_pos) = quote_pos {
let qr = Range { start: quote_pos, end: quote_pos };
f(
&format!("unclosed quoted HTML attribute on tag `{}`", tag_name),
&qr,
false,
);
}
if is_self_closing {
// https://html.spec.whatwg.org/#parse-error-non-void-html-element-start-tag-with-trailing-solidus
let valid = ALLOWED_UNCLOSED.contains(&&tag_name[..])
|| tags.iter().take(pos + 1).any(|(at, _)| {
let at = at.to_lowercase();
at == "svg" || at == "math"
});
if !valid {
f(&format!("invalid self-closing HTML tag `{}`", tag_name), &r, false);
}
} else {
tags.push((tag_name, r));
}
}
}
break;