Skip to content

Truly independent web browser

LibWeb: Give HTML tag tokens a small id for their known tag name

Andreas Kling2761f3189a5b
diff --git a/Libraries/LibWeb/HTML/Parser/Rust/build.rs b/Libraries/LibWeb/HTML/Parser/Rust/build.rs
index 32cfda556af..1e11a0a78c0 100644
--- a/Libraries/LibWeb/HTML/Parser/Rust/build.rs
+++ b/Libraries/LibWeb/HTML/Parser/Rust/build.rs
@@ -401,9 +401,57 @@ fn emit_known_names(
&mut std::collections::HashSet::new(),
);
out.push_str("}\npub(crate) use attribute_name;\n");
+ emit_tag_ids(&mut out, &[tag_names, svg_tag_names, mathml_tag_names]);
fs::write(out_path, out).expect("Failed to write known_names_generated.rs");
}
+/// Emits a small number for every known tag name, the `tag_id!` macro that gives the number of a name, and
+/// `tag_id_for_name`, which finds the number of a name the tokenizer has read.
+fn emit_tag_ids(out: &mut String, tag_name_lists: &[&[(String, String)]]) {
+ let names: std::collections::BTreeSet<&str> = tag_name_lists
+ .iter()
+ .flat_map(|names| names.iter().map(|(_, name)| name.as_str()))
+ .collect();
+ // NB: 0 is the id of every name that isn't a known one.
+ let ids: Vec<(&str, usize)> = names.iter().copied().zip(1..).collect();
+
+ out.push_str(&format!(
+ "\npub(crate) const TAG_SET_WORD_COUNT: usize = {};\n\nmacro_rules! tag_id {{\n",
+ (ids.len() + 1).div_ceil(64)
+ ));
+ for (name, id) in &ids {
+ out.push_str(&format!(
+ " (\"{name}\") => {{ $crate::token::TagId::from_index({id}) }};\n"
+ ));
+ }
+ out.push_str("}\npub(crate) use tag_id;\n\n");
+
+ // NB: Names are told apart by their length and first byte before any of them is compared in full, which leaves
+ // only a few to compare with.
+ let mut by_length_and_first_byte = std::collections::BTreeMap::<(usize, u8), Vec<(&str, usize)>>::new();
+ for (name, id) in &ids {
+ by_length_and_first_byte
+ .entry((name.len(), name.as_bytes()[0]))
+ .or_default()
+ .push((name, *id));
+ }
+ out.push_str("pub(crate) fn tag_id_for_name(name: &[u8]) -> crate::token::TagId {\n");
+ out.push_str(
+ " let Some(&first_byte) = name.first() else {\n return crate::token::TagId::UNKNOWN;\n };\n",
+ );
+ out.push_str(" match (name.len(), first_byte) {\n");
+ for ((length, first_byte), names) in &by_length_and_first_byte {
+ out.push_str(&format!(" ({length}, {first_byte}) => match name {{\n"));
+ for (name, id) in names {
+ out.push_str(&format!(
+ " b\"{name}\" => crate::token::TagId::from_index({id}),\n"
+ ));
+ }
+ out.push_str(" _ => crate::token::TagId::UNKNOWN,\n },\n");
+ }
+ out.push_str(" _ => crate::token::TagId::UNKNOWN,\n }\n}\n");
+}
+
fn emit_known_name_externs(out: &mut String, names: &[(String, String)], prefix: &str, target_pointer_size: usize) {
for (identifier, name) in names {
if needs_static(name, target_pointer_size) {
diff --git a/Libraries/LibWeb/HTML/Parser/Rust/src/lib.rs b/Libraries/LibWeb/HTML/Parser/Rust/src/lib.rs
index 90155033092..924a50c2217 100644
--- a/Libraries/LibWeb/HTML/Parser/Rust/src/lib.rs
+++ b/Libraries/LibWeb/HTML/Parser/Rust/src/lib.rs
@@ -234,6 +234,7 @@ fn next_token_slow(
match token.payload {
TokenPayload::Tag {
tag_name,
+ tag_id: _,
self_closing,
had_duplicate_attribute,
attributes,
diff --git a/Libraries/LibWeb/HTML/Parser/Rust/src/parser.rs b/Libraries/LibWeb/HTML/Parser/Rust/src/parser.rs
index 273b6bdc5f4..dea193a65a4 100644
--- a/Libraries/LibWeb/HTML/Parser/Rust/src/parser.rs
+++ b/Libraries/LibWeb/HTML/Parser/Rust/src/parser.rs
@@ -9,13 +9,17 @@ use crate::input::Input;
use crate::input::InputRun;
use crate::input::InputSlice;
use crate::known_names::attribute_name;
+use crate::known_names::tag_id;
use crate::known_names::tag_name;
use crate::token::AttributeList;
use crate::token::HtmlName;
use crate::token::KnownName;
+use crate::token::TagId;
+use crate::token::TagSet;
use crate::token::Token;
use crate::token::TokenPayload;
use crate::token::TokenType;
+use crate::token::tag_set;
use crate::tokenizer::HtmlTokenizer;
use crate::tokenizer::State;
use std::collections::HashMap;
@@ -781,7 +785,7 @@ impl TreeBuilder {
}
if is_mathml_text_integration_point(adjusted_current_node) {
- if token.is_start_tag() && !token.is_start_tag_one_of(&[tag_name!("mglyph"), tag_name!("malignmark")]) {
+ if token.is_start_tag() && !token.is_start_tag_one_of(tag_set!["mglyph", "malignmark"]) {
return true;
}
if token.token_type == TokenType::Character {
@@ -791,7 +795,7 @@ impl TreeBuilder {
if adjusted_current_node.namespace_ == RustFfiHtmlNamespace::MathMl
&& adjusted_current_node.local_name == tag_name!("annotation-xml")
- && token.is_start_tag_named(tag_name!("svg"))
+ && token.is_start_tag_named(tag_id!("svg"))
{
return true;
}
@@ -900,16 +904,14 @@ impl TreeBuilder {
return;
}
- if token.is_start_tag_named(tag_name!("html")) {
+ if token.is_start_tag_named(tag_id!("html")) {
let document = self.document_node();
self.insert_html_element_for_document(&token, document);
self.insertion_mode = InsertionMode::BeforeHead;
return;
}
- if token.is_end_tag_one_of(&[tag_name!("head"), tag_name!("body"), tag_name!("html"), tag_name!("br")])
- || !token.is_end_tag()
- {
+ if token.is_end_tag_one_of(tag_set!["head", "body", "html", "br"]) || !token.is_end_tag() {
let document = self.document_node();
if let Some(html_element) = self.document_html_element() {
self.stack_of_open_elements.push(StackNode {
@@ -955,21 +957,19 @@ impl TreeBuilder {
return;
}
- if token.is_start_tag_named(tag_name!("html")) {
+ if token.is_start_tag_named(tag_id!("html")) {
self.process_using_the_rules_for(InsertionMode::InBody, token);
return;
}
- if token.is_start_tag_named(tag_name!("head")) {
+ if token.is_start_tag_named(tag_id!("head")) {
let head = self.insert_html_element_for(&token);
self.head_element = Some(head);
self.insertion_mode = InsertionMode::InHead;
return;
}
- if token.is_end_tag_one_of(&[tag_name!("head"), tag_name!("body"), tag_name!("html"), tag_name!("br")])
- || !token.is_end_tag()
- {
+ if token.is_end_tag_one_of(tag_set!["head", "body", "html", "br"]) || !token.is_end_tag() {
let head = self.insert_html_element_named("head");
self.head_element = Some(head);
self.insertion_mode = InsertionMode::InHead;
@@ -1012,19 +1012,14 @@ impl TreeBuilder {
}
// -> A start tag whose tag name is "html"
- if token.is_start_tag_named(tag_name!("html")) {
+ if token.is_start_tag_named(tag_id!("html")) {
// Process the token using the rules for the "in body" insertion mode.
self.process_using_the_rules_for(InsertionMode::InBody, token);
return;
}
// -> A start tag whose tag name is one of: "base", "basefont", "bgsound", "link"
- if token.is_start_tag_one_of(&[
- tag_name!("base"),
- tag_name!("basefont"),
- tag_name!("bgsound"),
- tag_name!("link"),
- ]) {
+ if token.is_start_tag_one_of(tag_set!["base", "basefont", "bgsound", "link"]) {
// Insert an HTML element for the token. Immediately pop the current node off the stack of open elements.
self.insert_html_element_for(&token);
self.pop_current_node();
@@ -1034,7 +1029,7 @@ impl TreeBuilder {
}
// -> A start tag whose tag name is "meta"
- if token.is_start_tag_named(tag_name!("meta")) {
+ if token.is_start_tag_named(tag_id!("meta")) {
// Insert an HTML element for the token. Immediately pop the current node off the stack of open elements.
let element = self.insert_html_element_for(&token);
self.pop_current_node();
@@ -1046,7 +1041,7 @@ impl TreeBuilder {
}
// -> A start tag whose tag name is "title"
- if token.is_start_tag_named(tag_name!("title")) {
+ if token.is_start_tag_named(tag_id!("title")) {
// Follow the generic RCDATA element parsing algorithm.
self.parse_generic_rcdata_element(token);
return;
@@ -1054,8 +1049,8 @@ impl TreeBuilder {
// -> A start tag whose tag name is "noscript", if scripting mode is not Disabled
// -> A start tag whose tag name is one of: "noframes", "style"
- if token.is_start_tag_one_of(&[tag_name!("style"), tag_name!("noframes")])
- || (token.is_start_tag_named(tag_name!("noscript")) && self.scripting_enabled)
+ if token.is_start_tag_one_of(tag_set!["style", "noframes"])
+ || (token.is_start_tag_named(tag_id!("noscript")) && self.scripting_enabled)
{
// Follow the generic raw text element parsing algorithm.
self.parse_generic_raw_text_element(token);
@@ -1063,7 +1058,7 @@ impl TreeBuilder {
}
// -> A start tag whose tag name is "noscript", if scripting mode is Disabled
- if token.is_start_tag_named(tag_name!("noscript")) && !self.scripting_enabled {
+ if token.is_start_tag_named(tag_id!("noscript")) && !self.scripting_enabled {
// Insert an HTML element for the token.
self.insert_html_element_for(&token);
@@ -1073,7 +1068,7 @@ impl TreeBuilder {
}
// -> A start tag whose tag name is "script"
- if token.is_start_tag_named(tag_name!("script")) {
+ if token.is_start_tag_named(tag_id!("script")) {
// Run these steps:
//
// 1. Create an element for the token in the HTML namespace, with the intended parent being the target parent
@@ -1110,7 +1105,7 @@ impl TreeBuilder {
}
// -> An end tag whose tag name is "head"
- if token.is_end_tag_named(tag_name!("head")) {
+ if token.is_end_tag_named(tag_id!("head")) {
// Pop the current node (which will be the head element) off the stack of open elements.
self.pop_current_node();
@@ -1120,7 +1115,7 @@ impl TreeBuilder {
}
// -> An end tag whose tag name is one of: "body", "html", "br"
- if token.is_end_tag_one_of(&[tag_name!("body"), tag_name!("html"), tag_name!("br")]) {
+ if token.is_end_tag_one_of(tag_set!["body", "html", "br"]) {
// Act as described in the "anything else" entry below.
self.pop_current_node();
self.insertion_mode = InsertionMode::AfterHead;
@@ -1129,20 +1124,20 @@ impl TreeBuilder {
}
// -> A start tag whose tag name is "template"
- if token.is_start_tag_named(tag_name!("template")) {
+ if token.is_start_tag_named(tag_id!("template")) {
self.handle_template_start_tag(&token);
return;
}
// -> An end tag whose tag name is "template"
- if token.is_end_tag_named(tag_name!("template")) {
+ if token.is_end_tag_named(tag_id!("template")) {
self.handle_template_end_tag();
return;
}
// -> A start tag whose tag name is "head"
// -> Any other end tag
- if token.is_start_tag_named(tag_name!("head")) || token.is_end_tag() {
+ if token.is_start_tag_named(tag_id!("head")) || token.is_end_tag() {
// Parse error. Ignore the token.
self.parse_error("unexpected tag in in head insertion mode");
return;
@@ -1167,12 +1162,12 @@ impl TreeBuilder {
return;
}
- if token.is_start_tag_named(tag_name!("html")) {
+ if token.is_start_tag_named(tag_id!("html")) {
self.process_using_the_rules_for(InsertionMode::InBody, token);
return;
}
- if token.is_end_tag_named(tag_name!("noscript")) {
+ if token.is_end_tag_named(tag_id!("noscript")) {
self.pop_current_node();
self.insertion_mode = InsertionMode::InHead;
return;
@@ -1180,21 +1175,14 @@ impl TreeBuilder {
if token.is_parser_whitespace()
|| token.token_type == TokenType::Comment
- || token.is_start_tag_one_of(&[
- tag_name!("basefont"),
- tag_name!("bgsound"),
- tag_name!("link"),
- tag_name!("meta"),
- tag_name!("noframes"),
- tag_name!("style"),
- ])
+ || token.is_start_tag_one_of(tag_set!["basefont", "bgsound", "link", "meta", "noframes", "style"])
{
self.process_using_the_rules_for(InsertionMode::InHead, token);
return;
}
- if token.is_start_tag_one_of(&[tag_name!("head"), tag_name!("noscript")])
- || (token.is_end_tag() && !token.is_end_tag_named(tag_name!("br")))
+ if token.is_start_tag_one_of(tag_set!["head", "noscript"])
+ || (token.is_end_tag() && !token.is_end_tag_named(tag_id!("br")))
{
// Parse error. Ignore the token.
self.parse_error("unexpected tag in in head noscript insertion mode");
@@ -1230,35 +1218,26 @@ impl TreeBuilder {
return;
}
- if token.is_start_tag_named(tag_name!("html")) {
+ if token.is_start_tag_named(tag_id!("html")) {
self.process_using_the_rules_for(InsertionMode::InBody, token);
return;
}
- if token.is_start_tag_named(tag_name!("body")) {
+ if token.is_start_tag_named(tag_id!("body")) {
self.insert_html_element_for(&token);
self.frameset_ok = false;
self.insertion_mode = InsertionMode::InBody;
return;
}
- if token.is_start_tag_named(tag_name!("frameset")) {
+ if token.is_start_tag_named(tag_id!("frameset")) {
self.insert_html_element_for(&token);
self.insertion_mode = InsertionMode::InFrameset;
return;
}
- if token.is_start_tag_one_of(&[
- tag_name!("base"),
- tag_name!("basefont"),
- tag_name!("bgsound"),
- tag_name!("link"),
- tag_name!("meta"),
- tag_name!("noframes"),
- tag_name!("script"),
- tag_name!("style"),
- tag_name!("template"),
- tag_name!("title"),
+ if token.is_start_tag_one_of(tag_set![
+ "base", "basefont", "bgsound", "link", "meta", "noframes", "script", "style", "template", "title"
]) {
// Parse error.
self.parse_error("head-only start tag in after head insertion mode");
@@ -1277,13 +1256,13 @@ impl TreeBuilder {
return;
}
- if token.is_end_tag_named(tag_name!("template")) {
+ if token.is_end_tag_named(tag_id!("template")) {
self.process_using_the_rules_for(InsertionMode::InHead, token);
return;
}
// An end tag whose tag name is one of: "body", "html", "br"
- if token.is_end_tag_one_of(&[tag_name!("body"), tag_name!("html"), tag_name!("br")]) {
+ if token.is_end_tag_one_of(tag_set!["body", "html", "br"]) {
// Act as described in the "anything else" entry below.
// FIXME: Set the frameset-ok flag to "ok" after inserting the implicit body element.
self.insert_html_element_named("body");
@@ -1294,7 +1273,7 @@ impl TreeBuilder {
// A start tag whose tag name is "head"
// Any other end tag
- if token.is_start_tag_named(tag_name!("head")) || token.is_end_tag() {
+ if token.is_start_tag_named(tag_id!("head")) || token.is_end_tag() {
// Parse error. Ignore the token.
self.parse_error("unexpected tag in after head insertion mode");
return;
@@ -1349,7 +1328,7 @@ impl TreeBuilder {
return;
}
- if token.is_start_tag_named(tag_name!("html")) {
+ if token.is_start_tag_named(tag_id!("html")) {
// Parse error.
self.parse_error("html start tag in in body insertion mode");
@@ -1360,7 +1339,7 @@ impl TreeBuilder {
return;
}
- if token.is_start_tag_named(tag_name!("body")) {
+ if token.is_start_tag_named(tag_id!("body")) {
// Parse error.
self.parse_error("body start tag in in body insertion mode");
@@ -1377,7 +1356,7 @@ impl TreeBuilder {
return;
}
- if token.is_start_tag_named(tag_name!("frameset")) {
+ if token.is_start_tag_named(tag_id!("frameset")) {
// Parse error.
self.parse_error("frameset start tag in in body insertion mode");
@@ -1407,51 +1386,43 @@ impl TreeBuilder {
return;
}
- if token.is_start_tag_one_of(&[
- tag_name!("base"),
- tag_name!("basefont"),
- tag_name!("bgsound"),
- tag_name!("link"),
- tag_name!("meta"),
- tag_name!("noframes"),
- tag_name!("script"),
- tag_name!("style"),
- tag_name!("title"),
+ if token.is_start_tag_one_of(tag_set![
+ "base", "basefont", "bgsound", "link", "meta", "noframes", "script", "style", "title"
]) {
self.process_using_the_rules_for(InsertionMode::InHead, token);
return;
}
- if token.is_start_tag_named(tag_name!("template")) {
+ if token.is_start_tag_named(tag_id!("template")) {
self.process_using_the_rules_for(InsertionMode::InHead, token);
return;
}
- if token.is_start_tag_one_of(&[
- tag_name!("address"),
- tag_name!("article"),
- tag_name!("aside"),
- tag_name!("blockquote"),
- tag_name!("center"),
- tag_name!("details"),
- tag_name!("dialog"),
- tag_name!("dir"),
- tag_name!("div"),
- tag_name!("dl"),
- tag_name!("fieldset"),
- tag_name!("figcaption"),
- tag_name!("figure"),
- tag_name!("footer"),
- tag_name!("header"),
- tag_name!("hgroup"),
- tag_name!("main"),
- tag_name!("menu"),
- tag_name!("nav"),
- tag_name!("ol"),
- tag_name!("search"),
- tag_name!("section"),
- tag_name!("summary"),
- tag_name!("ul"),
+ if token.is_start_tag_one_of(tag_set![
+ "address",
+ "article",
+ "aside",
+ "blockquote",
+ "center",
+ "details",
+ "dialog",
+ "dir",
+ "div",
+ "dl",
+ "fieldset",
+ "figcaption",
+ "figure",
+ "footer",
+ "header",
+ "hgroup",
+ "main",
+ "menu",
+ "nav",
+ "ol",
+ "search",
+ "section",
+ "summary",
+ "ul"
]) {
if self.has_in_button_scope(tag_name!("p")) {
self.close_a_p_element();
@@ -1460,7 +1431,7 @@ impl TreeBuilder {
return;
}
- if token.is_start_tag_named(tag_name!("p")) {
+ if token.is_start_tag_named(tag_id!("p")) {
if self.has_in_button_scope(tag_name!("p")) {
self.close_a_p_element();
}
@@ -1468,7 +1439,7 @@ impl TreeBuilder {
return;
}
- if token.is_start_tag_named(tag_name!("li")) {
+ if token.is_start_tag_named(tag_id!("li")) {
self.frameset_ok = false;
for node in self.stack_of_open_elements.iter().rev() {
if node.local_name == tag_name!("li") {
@@ -1495,7 +1466,7 @@ impl TreeBuilder {
return;
}
- if token.is_start_tag_one_of(&[tag_name!("dd"), tag_name!("dt")]) {
+ if token.is_start_tag_one_of(tag_set!["dd", "dt"]) {
self.frameset_ok = false;
for node in self.stack_of_open_elements.iter().rev() {
if node.local_name == tag_name!("dd") || node.local_name == tag_name!("dt") {
@@ -1523,7 +1494,7 @@ impl TreeBuilder {
return;
}
- if token.is_start_tag_named(tag_name!("button")) {
+ if token.is_start_tag_named(tag_id!("button")) {
if self.has_in_button_scope(tag_name!("button")) {
// Parse error.
self.parse_error("button start tag with button element in scope");
@@ -1536,7 +1507,7 @@ impl TreeBuilder {
return;
}
- if token.is_start_tag_named(tag_name!("form")) {
+ if token.is_start_tag_named(tag_id!("form")) {
// FIXME: Use "parsing template contents", which also includes a template fragment context element.
// https://html.spec.whatwg.org/multipage/parsing.html#parsing-template-contents
let has_template_element_on_stack = self.has_template_element_on_stack_of_open_elements();
@@ -1555,7 +1526,7 @@ impl TreeBuilder {
return;
}
- if token.is_end_tag_named(tag_name!("form")) {
+ if token.is_end_tag_named(tag_id!("form")) {
// FIXME: The current rule branches on whether the parser is parsing template contents, including the
// template fragment-context case, rather than only checking the stack of open elements.
if !self.has_template_element_on_stack_of_open_elements() {
@@ -1592,7 +1563,7 @@ impl TreeBuilder {
}
// An end tag whose tag name is "li"
- if token.is_end_tag_named(tag_name!("li")) {
+ if token.is_end_tag_named(tag_id!("li")) {
if !self.has_in_list_item_scope(tag_name!("li")) {
// Parse error. Ignore the token.
self.parse_error("li end tag without li element in list item scope");
@@ -1608,7 +1579,7 @@ impl TreeBuilder {
}
// An end tag whose tag name is one of: "dd", "dt"
- if token.is_end_tag_one_of(&[tag_name!("dd"), tag_name!("dt")]) {
+ if token.is_end_tag_one_of(tag_set!["dd", "dt"]) {
if !self.has_in_scope(token.tag_name()) {
// Parse error. Ignore the token.
self.parse_error("definition-list item end tag without matching element in scope");
@@ -1623,14 +1594,7 @@ impl TreeBuilder {
return;
}
- if token.is_start_tag_one_of(&[
- tag_name!("h1"),
- tag_name!("h2"),
- tag_name!("h3"),
- tag_name!("h4"),
- tag_name!("h5"),
- tag_name!("h6"),
- ]) {
+ if token.is_start_tag_one_of(tag_set!["h1", "h2", "h3", "h4", "h5", "h6"]) {
if self.has_in_button_scope(tag_name!("p")) {
self.close_a_p_element();
}
@@ -1644,14 +1608,7 @@ impl TreeBuilder {
}
// An end tag whose tag name is one of: "h1", "h2", "h3", "h4", "h5", "h6"
- if token.is_end_tag_one_of(&[
- tag_name!("h1"),
- tag_name!("h2"),
- tag_name!("h3"),
- tag_name!("h4"),
- tag_name!("h5"),
- tag_name!("h6"),
- ]) {
+ if token.is_end_tag_one_of(tag_set!["h1", "h2", "h3", "h4", "h5", "h6"]) {
if !self.has_heading_in_scope() {
// Parse error. Ignore the token.
self.parse_error("heading end tag without heading element in scope");
@@ -1666,7 +1623,7 @@ impl TreeBuilder {
return;
}
- if token.is_start_tag_one_of(&[tag_name!("pre"), tag_name!("listing")]) {
+ if token.is_start_tag_one_of(tag_set!["pre", "listing"]) {
if self.has_in_button_scope(tag_name!("p")) {
self.close_a_p_element();
}
@@ -1676,7 +1633,7 @@ impl TreeBuilder {
return;
}
- if token.is_start_tag_named(tag_name!("plaintext")) {
+ if token.is_start_tag_named(tag_id!("plaintext")) {
if self.has_in_button_scope(tag_name!("p")) {
self.close_a_p_element();
}
@@ -1687,7 +1644,7 @@ impl TreeBuilder {
// FIXME: A "ruby" start tag belongs to the "any other start tag" rule: reconstruct the active formatting
// elements, then insert an HTML element for the token. It must not close an open paragraph here.
- if token.is_start_tag_named(tag_name!("ruby")) {
+ if token.is_start_tag_named(tag_id!("ruby")) {
if self.has_in_button_scope(tag_name!("p")) {
self.close_a_p_element();
}
@@ -1695,16 +1652,16 @@ impl TreeBuilder {
return;
}
- if token.is_start_tag_named(tag_name!("image")) {
+ if token.is_start_tag_named(tag_id!("image")) {
// Parse error. Change the token's tag name to "img" and reprocess it. (Don't ask.)
self.parse_error("image start tag in in body insertion mode");
let mut token = token;
- token.set_tag_name(HtmlName::from_utf8("img"));
+ token.set_tag_name("img");
self.process_using_the_rules_for(self.insertion_mode, token);
return;
}
- if token.is_start_tag_named(tag_name!("table")) {
+ if token.is_start_tag_named(tag_id!("table")) {
if self.document_quirks_mode != RustFfiHtmlQuirksMode::Yes && self.has_in_button_scope(tag_name!("p")) {
self.close_a_p_element();
}
@@ -1714,7 +1671,7 @@ impl TreeBuilder {
return;
}
- if token.is_start_tag_named(tag_name!("select")) {
+ if token.is_start_tag_named(tag_id!("select")) {
if self.parsing_fragment
&& self.context_element.as_ref().is_some_and(|element| {
element.local_name == tag_name!("select") && element.namespace_ == RustFfiHtmlNamespace::Html
@@ -1736,12 +1693,12 @@ impl TreeBuilder {
return;
}
- if token.is_end_tag_named(tag_name!("template")) {
+ if token.is_end_tag_named(tag_id!("template")) {
self.process_using_the_rules_for(InsertionMode::InHead, token);
return;
}
- if token.is_start_tag_named(tag_name!("svg")) {
+ if token.is_start_tag_named(tag_id!("svg")) {
self.reconstruct_the_active_formatting_elements();
self.insert_foreign_element_for(&token, RustFfiHtmlNamespace::Svg, false);
if token.is_self_closing() {
@@ -1750,7 +1707,7 @@ impl TreeBuilder {
return;
}
- if token.is_start_tag_named(tag_name!("math")) {
+ if token.is_start_tag_named(tag_id!("math")) {
self.reconstruct_the_active_formatting_elements();
self.insert_foreign_element_for(&token, RustFfiHtmlNamespace::MathMl, false);
if token.is_self_closing() {
@@ -1759,25 +1716,15 @@ impl TreeBuilder {
return;
}
- if token.is_start_tag_one_of(&[
- tag_name!("caption"),
- tag_name!("col"),
- tag_name!("colgroup"),
- tag_name!("frame"),
- tag_name!("head"),
- tag_name!("tbody"),
- tag_name!("td"),
- tag_name!("tfoot"),
- tag_name!("th"),
- tag_name!("thead"),
- tag_name!("tr"),
+ if token.is_start_tag_one_of(tag_set![
+ "caption", "col", "colgroup", "frame", "head", "tbody", "td", "tfoot", "th", "thead", "tr"
]) {
// Parse error. Ignore the token.
self.parse_error("table-related start tag in in body insertion mode");
return;
}
- if token.is_start_tag_named(tag_name!("option")) {
+ if token.is_start_tag_named(tag_id!("option")) {
if self.has_in_scope(tag_name!("select")) {
self.generate_implied_end_tags_except(tag_name!("optgroup"));
if self.has_in_scope(tag_name!("option")) {
@@ -1791,7 +1738,7 @@ impl TreeBuilder {
return;
}
- if token.is_start_tag_named(tag_name!("optgroup")) {
+ if token.is_start_tag_named(tag_id!("optgroup")) {
if self.has_in_scope(tag_name!("select")) {
self.generate_implied_end_tags();
if self.has_in_scope(tag_name!("option")) || self.has_in_scope(tag_name!("optgroup")) {
@@ -1806,7 +1753,7 @@ impl TreeBuilder {
}
// A start tag whose tag name is one of: "rb", "rtc"
- if token.is_start_tag_one_of(&[tag_name!("rb"), tag_name!("rtc")]) {
+ if token.is_start_tag_one_of(tag_set!["rb", "rtc"]) {
// If the stack of open elements has a ruby element in scope, then generate implied end tags. If the current node is not now a ruby element, this is a parse error.
if self.has_in_scope(tag_name!("ruby")) {
self.generate_implied_end_tags();
@@ -1821,7 +1768,7 @@ impl TreeBuilder {
}
// A start tag whose tag name is one of: "rp", "rt"
- if token.is_start_tag_one_of(&[tag_name!("rp"), tag_name!("rt")]) {
+ if token.is_start_tag_one_of(tag_set!["rp", "rt"]) {
// If the stack of open elements has a ruby element in scope, then generate implied end tags, except for rtc elements. If the current node is not now a rtc element or a ruby element, this is a parse error.
if self.has_in_scope(tag_name!("ruby")) {
self.generate_implied_end_tags_except(tag_name!("rtc"));
@@ -1835,14 +1782,14 @@ impl TreeBuilder {
return;
}
- if token.is_start_tag_one_of(&[tag_name!("textarea")]) {
+ if token.is_start_tag_one_of(tag_set!["textarea"]) {
self.next_line_feed_can_be_ignored = true;
self.parse_generic_rcdata_element(token);
self.frameset_ok = false;
return;
}
- if token.is_start_tag_named(tag_name!("xmp")) {
+ if token.is_start_tag_named(tag_id!("xmp")) {
if self.has_in_button_scope(tag_name!("p")) {
self.close_a_p_element();
}
@@ -1852,20 +1799,20 @@ impl TreeBuilder {
return;
}
- if token.is_start_tag_named(tag_name!("iframe")) {
+ if token.is_start_tag_named(tag_id!("iframe")) {
self.frameset_ok = false;
self.parse_generic_raw_text_element(token);
return;
}
- if token.is_start_tag_one_of(&[tag_name!("style"), tag_name!("noembed"), tag_name!("noframes")])
- || (token.is_start_tag_named(tag_name!("noscript")) && self.scripting_enabled)
+ if token.is_start_tag_one_of(tag_set!["style", "noembed", "noframes"])
+ || (token.is_start_tag_named(tag_id!("noscript")) && self.scripting_enabled)
{
self.parse_generic_raw_text_element(token);
return;
}
- if token.is_start_tag_named(tag_name!("hr")) {
+ if token.is_start_tag_named(tag_id!("hr")) {
if self.has_in_button_scope(tag_name!("p")) {
self.close_a_p_element();
}
@@ -1881,7 +1828,7 @@ impl TreeBuilder {
return;
}
- if token.is_start_tag_named(tag_name!("br")) {
+ if token.is_start_tag_named(tag_id!("br")) {
self.reconstruct_the_active_formatting_elements();
self.insert_html_element_for(&token);
self.pop_current_node();
@@ -1889,7 +1836,7 @@ impl TreeBuilder {
return;
}
- if token.is_start_tag_named(tag_name!("input")) {
+ if token.is_start_tag_named(tag_id!("input")) {
if self.parsing_fragment
&& self.context_element.as_ref().is_some_and(|element| {
element.local_name == tag_name!("select") && element.namespace_ == RustFfiHtmlNamespace::Html
@@ -1916,7 +1863,7 @@ impl TreeBuilder {
return;
}
- if token.is_end_tag_named(tag_name!("p")) {
+ if token.is_end_tag_named(tag_id!("p")) {
if !self.has_in_button_scope(tag_name!("p")) {
// Parse error.
self.parse_error("p end tag without p element in button scope");
@@ -1926,42 +1873,42 @@ impl TreeBuilder {
return;
}
- if token.is_end_tag_named(tag_name!("br")) {
+ if token.is_end_tag_named(tag_id!("br")) {
// Parse error.
self.parse_error("br end tag in in body insertion mode");
self.process_using_the_rules_for(self.insertion_mode, Token::synthetic_start_tag("br"));
return;
}
- if token.is_end_tag_one_of(&[
- tag_name!("address"),
- tag_name!("article"),
- tag_name!("aside"),
- tag_name!("blockquote"),
- tag_name!("button"),
- tag_name!("center"),
- tag_name!("details"),
- tag_name!("dialog"),
- tag_name!("dir"),
- tag_name!("div"),
- tag_name!("dl"),
- tag_name!("fieldset"),
- tag_name!("figcaption"),
- tag_name!("figure"),
- tag_name!("footer"),
- tag_name!("header"),
- tag_name!("hgroup"),
- tag_name!("listing"),
- tag_name!("main"),
- tag_name!("menu"),
- tag_name!("nav"),
- tag_name!("ol"),
- tag_name!("pre"),
- tag_name!("search"),
- tag_name!("section"),
- tag_name!("select"),
- tag_name!("summary"),
- tag_name!("ul"),
+ if token.is_end_tag_one_of(tag_set![
+ "address",
+ "article",
+ "aside",
+ "blockquote",
+ "button",
+ "center",
+ "details",
+ "dialog",
+ "dir",
+ "div",
+ "dl",
+ "fieldset",
+ "figcaption",
+ "figure",
+ "footer",
+ "header",
+ "hgroup",
+ "listing",
+ "main",
+ "menu",
+ "nav",
+ "ol",
+ "pre",
+ "search",
+ "section",
+ "select",
+ "summary",
+ "ul"
]) {
if !self.has_in_scope(token.tag_name()) {
// Parse error. Ignore the token.
@@ -1976,7 +1923,7 @@ impl TreeBuilder {
return;
}
- if token.is_start_tag_named(tag_name!("a")) {
+ if token.is_start_tag_named(tag_id!("a")) {
let active_formatting_elements_after_last_marker =
self.active_formatting_elements_after_last_marker_start_index();
if let Some(active_index) = self
@@ -2000,7 +1947,7 @@ impl TreeBuilder {
return;
}
- if token.is_start_tag_named(tag_name!("nobr")) {
+ if token.is_start_tag_named(tag_id!("nobr")) {
self.reconstruct_the_active_formatting_elements();
if self.has_in_scope(tag_name!("nobr")) {
// Parse error.
@@ -2025,7 +1972,7 @@ impl TreeBuilder {
return;
}
- if token.is_start_tag_one_of(&[tag_name!("applet"), tag_name!("marquee"), tag_name!("object")]) {
+ if token.is_start_tag_one_of(tag_set!["applet", "marquee", "object"]) {
self.reconstruct_the_active_formatting_elements();
self.insert_html_element_for(&token);
self.insert_marker_at_the_end_of_the_list_of_active_formatting_elements();
@@ -2033,7 +1980,7 @@ impl TreeBuilder {
return;
}
- if token.is_end_tag_one_of(&[tag_name!("applet"), tag_name!("marquee"), tag_name!("object")]) {
+ if token.is_end_tag_one_of(tag_set!["applet", "marquee", "object"]) {
if !self.has_in_scope(token.tag_name()) {
// Parse error. Ignore the token.
self.parse_error("end tag without matching applet, marquee, or object element in scope");
@@ -2048,7 +1995,7 @@ impl TreeBuilder {
return;
}
- if token.is_start_tag_one_of(&[tag_name!("param"), tag_name!("source"), tag_name!("track")]) {
+ if token.is_start_tag_one_of(tag_set!["param", "source", "track"]) {
self.insert_html_element_for(&token);
self.pop_current_node();
return;
@@ -2072,7 +2019,7 @@ impl TreeBuilder {
return;
}
- if token.is_end_tag_named(tag_name!("body")) {
+ if token.is_end_tag_named(tag_id!("body")) {
if !self.has_in_scope(tag_name!("body")) {
// Parse error. Ignore the token.
self.parse_error("body end tag without body element in scope");
@@ -2083,7 +2030,7 @@ impl TreeBuilder {
return;
}
- if token.is_end_tag_named(tag_name!("html")) {
+ if token.is_end_tag_named(tag_id!("html")) {
if !self.has_in_scope(tag_name!("body")) {
// Parse error. Ignore the token.
self.parse_error("html end tag without body element in scope");
@@ -2127,21 +2074,21 @@ impl TreeBuilder {
}
// -> A start tag whose tag name is "html"
- if token.is_start_tag_named(tag_name!("html")) {
+ if token.is_start_tag_named(tag_id!("html")) {
// Process the token using the rules for the "in body" insertion mode.
self.process_using_the_rules_for(InsertionMode::InBody, token);
return;
}
// -> A start tag whose tag name is "frameset"
- if token.is_start_tag_named(tag_name!("frameset")) {
+ if token.is_start_tag_named(tag_id!("frameset")) {
// Insert an HTML element for the token.
self.insert_html_element_for(&token);
return;
}
// -> An end tag whose tag name is "frameset"
- if token.is_end_tag_named(tag_name!("frameset")) {
+ if token.is_end_tag_named(tag_id!("frameset")) {
// If the current node is the root html element, then this is a parse error; ignore the token. (fragment case)
if self.stack_of_open_elements.len() == 1 {
self.parse_error("frameset end tag with only root element on stack");
@@ -2160,7 +2107,7 @@ impl TreeBuilder {
}
// -> A start tag whose tag name is "frame"
- if token.is_start_tag_named(tag_name!("frame")) {
+ if token.is_start_tag_named(tag_id!("frame")) {
// Insert an HTML element for the token. Immediately pop the current node off the stack of open elements.
self.insert_html_element_for(&token);
self.pop_current_node();
@@ -2170,7 +2117,7 @@ impl TreeBuilder {
}
// -> A start tag whose tag name is "noframes"
- if token.is_start_tag_named(tag_name!("noframes")) {
+ if token.is_start_tag_named(tag_id!("noframes")) {
// Process the token using the rules for the "in head" insertion mode.
self.process_using_the_rules_for(InsertionMode::InHead, token);
return;
@@ -2221,21 +2168,21 @@ impl TreeBuilder {
}
// -> A start tag whose tag name is "html"
- if token.is_start_tag_named(tag_name!("html")) {
+ if token.is_start_tag_named(tag_id!("html")) {
// Process the token using the rules for the "in body" insertion mode.
self.process_using_the_rules_for(InsertionMode::InBody, token);
return;
}
// An end tag whose tag name is "html"
- if token.is_end_tag_named(tag_name!("html")) {
+ if token.is_end_tag_named(tag_id!("html")) {
// Switch the insertion mode to "after after frameset".
self.insertion_mode = InsertionMode::AfterAfterFrameset;
return;
}
// -> A start tag whose tag name is "noframes"
- if token.is_start_tag_named(tag_name!("noframes")) {
+ if token.is_start_tag_named(tag_id!("noframes")) {
// Process the token using the rules for the "in head" insertion mode.
self.process_using_the_rules_for(InsertionMode::InHead, token);
return;
@@ -2284,14 +2231,14 @@ impl TreeBuilder {
return;
}
- if token.is_start_tag_one_of(&[tag_name!("script"), tag_name!("style"), tag_name!("template")])
- || token.is_end_tag_named(tag_name!("template"))
+ if token.is_start_tag_one_of(tag_set!["script", "style", "template"])
+ || token.is_end_tag_named(tag_id!("template"))
{
self.process_using_the_rules_for(InsertionMode::InHead, token);
return;
}
- if token.is_start_tag_named(tag_name!("input"))
+ if token.is_start_tag_named(tag_id!("input"))
&& token
.attribute(attribute_name!("type"))
.is_some_and(|type_| type_.eq_ignore_ascii_case("hidden"))
@@ -2303,7 +2250,7 @@ impl TreeBuilder {
return;
}
- if token.is_start_tag_named(tag_name!("form")) {
+ if token.is_start_tag_named(tag_id!("form")) {
// Parse error.
self.parse_error("form start tag in in table insertion mode");
// FIXME: If the form element pointer is not null and the parser is not parsing template contents, ignore
@@ -2318,14 +2265,14 @@ impl TreeBuilder {
return;
}
- if token.is_start_tag_one_of(&[tag_name!("tbody"), tag_name!("tfoot"), tag_name!("thead")]) {
+ if token.is_start_tag_one_of(tag_set!["tbody", "tfoot", "thead"]) {
self.clear_the_stack_back_to_a_table_context();
self.insert_html_element_for(&token);
self.insertion_mode = InsertionMode::InTableBody;
return;
}
- if token.is_start_tag_named(tag_name!("caption")) {
+ if token.is_start_tag_named(tag_id!("caption")) {
self.clear_the_stack_back_to_a_table_context();
self.insert_marker_at_the_end_of_the_list_of_active_formatting_elements();
self.insert_html_element_for(&token);
@@ -2333,14 +2280,14 @@ impl TreeBuilder {
return;
}
- if token.is_start_tag_named(tag_name!("colgroup")) {
+ if token.is_start_tag_named(tag_id!("colgroup")) {
self.clear_the_stack_back_to_a_table_context();
self.insert_html_element_for(&token);
self.insertion_mode = InsertionMode::InColumnGroup;
return;
}
- if token.is_start_tag_named(tag_name!("col")) {
+ if token.is_start_tag_named(tag_id!("col")) {
self.clear_the_stack_back_to_a_table_context();
self.insert_html_element_named("colgroup");
self.insertion_mode = InsertionMode::InColumnGroup;
@@ -2348,7 +2295,7 @@ impl TreeBuilder {
return;
}
- if token.is_start_tag_named(tag_name!("tr")) {
+ if token.is_start_tag_named(tag_id!("tr")) {
self.clear_the_stack_back_to_a_table_context();
self.insert_html_element_named("tbody");
self.insertion_mode = InsertionMode::InTableBody;
@@ -2356,7 +2303,7 @@ impl TreeBuilder {
return;
}
- if token.is_start_tag_one_of(&[tag_name!("td"), tag_name!("th")]) {
+ if token.is_start_tag_one_of(tag_set!["td", "th"]) {
self.clear_the_stack_back_to_a_table_context();
self.insert_html_element_named("tbody");
self.insertion_mode = InsertionMode::InTableBody;
@@ -2364,7 +2311,7 @@ impl TreeBuilder {
return;
}
- if token.is_start_tag_named(tag_name!("table")) {
+ if token.is_start_tag_named(tag_id!("table")) {
// Parse error.
self.parse_error("table start tag in in table insertion mode");
if !self.has_in_table_scope(tag_name!("table")) {
@@ -2376,7 +2323,7 @@ impl TreeBuilder {
return;
}
- if token.is_end_tag_named(tag_name!("table")) {
+ if token.is_end_tag_named(tag_id!("table")) {
if !self.has_in_table_scope(tag_name!("table")) {
// Parse error. Ignore the token.
self.parse_error("table end tag without table element in table scope");
@@ -2387,18 +2334,8 @@ impl TreeBuilder {
return;
}
- if token.is_end_tag_one_of(&[
- tag_name!("body"),
- tag_name!("caption"),
- tag_name!("col"),
- tag_name!("colgroup"),
- tag_name!("html"),
- tag_name!("tbody"),
- tag_name!("td"),
- tag_name!("tfoot"),
- tag_name!("th"),
- tag_name!("thead"),
- tag_name!("tr"),
+ if token.is_end_tag_one_of(tag_set![
+ "body", "caption", "col", "colgroup", "html", "tbody", "td", "tfoot", "th", "thead", "tr"
]) {
// Parse error. Ignore the token.
self.parse_error("unexpected end tag in in table insertion mode");
@@ -2471,18 +2408,18 @@ impl TreeBuilder {
return;
}
- if token.is_start_tag_named(tag_name!("html")) {
+ if token.is_start_tag_named(tag_id!("html")) {
self.process_using_the_rules_for(InsertionMode::InBody, token);
return;
}
- if token.is_start_tag_named(tag_name!("col")) {
+ if token.is_start_tag_named(tag_id!("col")) {
self.insert_html_element_for(&token);
self.pop_current_node();
return;
}
- if token.is_end_tag_named(tag_name!("colgroup")) {
+ if token.is_end_tag_named(tag_id!("colgroup")) {
if !self.current_node_named(tag_name!("colgroup")) {
// Parse error. Ignore the token.
self.parse_error("colgroup end tag without colgroup as current node");
@@ -2493,13 +2430,13 @@ impl TreeBuilder {
return;
}
- if token.is_end_tag_named(tag_name!("col")) {
+ if token.is_end_tag_named(tag_id!("col")) {
// Parse error. Ignore the token.
self.parse_error("col end tag in in column group insertion mode");
return;
}
- if token.is_start_tag_named(tag_name!("template")) || token.is_end_tag_named(tag_name!("template")) {
+ if token.is_start_tag_named(tag_id!("template")) || token.is_end_tag_named(tag_id!("template")) {
self.process_using_the_rules_for(InsertionMode::InHead, token);
return;
}
@@ -2521,7 +2458,7 @@ impl TreeBuilder {
// https://html.spec.whatwg.org/multipage/parsing.html#parsing-main-incaption
fn handle_in_caption(&mut self, token: Token) {
- if token.is_end_tag_named(tag_name!("caption")) {
+ if token.is_end_tag_named(tag_id!("caption")) {
if !self.has_in_table_scope(tag_name!("caption")) {
// Parse error. Ignore the token.
self.parse_error("caption end tag without caption element in table scope");
@@ -2537,17 +2474,9 @@ impl TreeBuilder {
return;
}
- if token.is_start_tag_one_of(&[
- tag_name!("caption"),
- tag_name!("col"),
- tag_name!("colgroup"),
- tag_name!("tbody"),
- tag_name!("td"),
- tag_name!("tfoot"),
- tag_name!("th"),
- tag_name!("thead"),
- tag_name!("tr"),
- ]) || token.is_end_tag_named(tag_name!("table"))
+ if token.is_start_tag_one_of(tag_set![
+ "caption", "col", "colgroup", "tbody", "td", "tfoot", "th", "thead", "tr"
+ ]) || token.is_end_tag_named(tag_id!("table"))
{
if !self.has_in_table_scope(tag_name!("caption")) {
// Parse error. Ignore the token.
@@ -2565,17 +2494,8 @@ impl TreeBuilder {
return;
}
- if token.is_end_tag_one_of(&[
- tag_name!("body"),
- tag_name!("col"),
- tag_name!("colgroup"),
- tag_name!("html"),
- tag_name!("tbody"),
- tag_name!("td"),
- tag_name!("tfoot"),
- tag_name!("th"),
- tag_name!("thead"),
- tag_name!("tr"),
+ if token.is_end_tag_one_of(tag_set![
+ "body", "col", "colgroup", "html", "tbody", "td", "tfoot", "th", "thead", "tr"
]) {
// Parse error. Ignore the token.
self.parse_error("unexpected end tag in in caption insertion mode");
@@ -2587,14 +2507,8 @@ impl TreeBuilder {
// https://html.spec.whatwg.org/multipage/parsing.html#parsing-main-intbody
fn handle_in_table_body(&mut self, token: Token) {
- if token.is_start_tag_one_of(&[
- tag_name!("caption"),
- tag_name!("col"),
- tag_name!("colgroup"),
- tag_name!("tbody"),
- tag_name!("tfoot"),
- tag_name!("thead"),
- ]) || token.is_end_tag_named(tag_name!("table"))
+ if token.is_start_tag_one_of(tag_set!["caption", "col", "colgroup", "tbody", "tfoot", "thead"])
+ || token.is_end_tag_named(tag_id!("table"))
{
if !self.has_in_table_scope(tag_name!("tbody"))
&& !self.has_in_table_scope(tag_name!("thead"))
@@ -2611,14 +2525,14 @@ impl TreeBuilder {
return;
}
- if token.is_start_tag_named(tag_name!("tr")) {
+ if token.is_start_tag_named(tag_id!("tr")) {
self.clear_the_stack_back_to_a_table_body_context();
self.insert_html_element_for(&token);
self.insertion_mode = InsertionMode::InRow;
return;
}
- if token.is_start_tag_one_of(&[tag_name!("td"), tag_name!("th")]) {
+ if token.is_start_tag_one_of(tag_set!["td", "th"]) {
// Parse error.
self.parse_error("cell start tag in in table body insertion mode");
self.clear_the_stack_back_to_a_table_body_context();
@@ -2628,7 +2542,7 @@ impl TreeBuilder {
return;
}
- if token.is_end_tag_one_of(&[tag_name!("tbody"), tag_name!("tfoot"), tag_name!("thead")]) {
+ if token.is_end_tag_one_of(tag_set!["tbody", "tfoot", "thead"]) {
if !self.has_in_table_scope(token.tag_name()) {
// Parse error. Ignore the token.
self.parse_error("table body end tag without matching element in table scope");
@@ -2640,16 +2554,7 @@ impl TreeBuilder {
return;
}
- if token.is_end_tag_one_of(&[
- tag_name!("body"),
- tag_name!("caption"),
- tag_name!("col"),
- tag_name!("colgroup"),
- tag_name!("html"),
- tag_name!("td"),
- tag_name!("th"),
- tag_name!("tr"),
- ]) {
+ if token.is_end_tag_one_of(tag_set!["body", "caption", "col", "colgroup", "html", "td", "th", "tr"]) {
// Parse error. Ignore the token.
self.parse_error("unexpected end tag in in table body insertion mode");
return;
@@ -2661,7 +2566,7 @@ impl TreeBuilder {
// https://html.spec.whatwg.org/multipage/parsing.html#parsing-main-intr
fn handle_in_row(&mut self, token: Token) {
// -> A start tag whose tag name is one of: "th", "td"
- if token.is_start_tag_one_of(&[tag_name!("td"), tag_name!("th")]) {
+ if token.is_start_tag_one_of(tag_set!["td", "th"]) {
// Clear the stack back to a table row context. (See below.)
self.clear_the_stack_back_to_a_table_row_context();
@@ -2675,7 +2580,7 @@ impl TreeBuilder {
}
// -> An end tag whose tag name is "tr"
- if token.is_end_tag_named(tag_name!("tr")) {
+ if token.is_end_tag_named(tag_id!("tr")) {
// If the stack of open elements does not have a tr element in table scope, this is a parse error; ignore the token.
if !self.has_in_table_scope(tag_name!("tr")) {
// Parse error. Ignore the token.
@@ -2695,15 +2600,8 @@ impl TreeBuilder {
// -> A start tag whose tag name is one of: "caption", "col", "colgroup", "tbody", "tfoot", "thead", "tr"
// -> An end tag whose tag name is "table"
- if token.is_start_tag_one_of(&[
- tag_name!("caption"),
- tag_name!("col"),
- tag_name!("colgroup"),
- tag_name!("tbody"),
- tag_name!("tfoot"),
- tag_name!("thead"),
- tag_name!("tr"),
- ]) || token.is_end_tag_named(tag_name!("table"))
+ if token.is_start_tag_one_of(tag_set!["caption", "col", "colgroup", "tbody", "tfoot", "thead", "tr"])
+ || token.is_end_tag_named(tag_id!("table"))
{
// If the stack of open elements does not have a tr element in table scope, this is a parse error; ignore the token.
if !self.has_in_table_scope(tag_name!("tr")) {
@@ -2722,7 +2620,7 @@ impl TreeBuilder {
}
// -> An end tag whose tag name is one of: "tbody", "tfoot", "thead"
- if token.is_end_tag_one_of(&[tag_name!("tbody"), tag_name!("tfoot"), tag_name!("thead")]) {
+ if token.is_end_tag_one_of(tag_set!["tbody", "tfoot", "thead"]) {
// If the stack of open elements does not have an element in table scope that is an HTML element with the same
// tag name as the token, this is a parse error; ignore the token.
if !self.has_in_table_scope(token.tag_name()) {
@@ -2746,15 +2644,7 @@ impl TreeBuilder {
}
// -> An end tag whose tag name is one of: "body", "caption", "col", "colgroup", "html", "td", "th"
- if token.is_end_tag_one_of(&[
- tag_name!("body"),
- tag_name!("caption"),
- tag_name!("col"),
- tag_name!("colgroup"),
- tag_name!("html"),
- tag_name!("td"),
- tag_name!("th"),
- ]) {
+ if token.is_end_tag_one_of(tag_set!["body", "caption", "col", "colgroup", "html", "td", "th"]) {
// Parse error. Ignore the token.
self.parse_error("unexpected end tag in in row insertion mode");
return;
@@ -2768,7 +2658,7 @@ impl TreeBuilder {
// https://html.spec.whatwg.org/multipage/parsing.html#parsing-main-intd
fn handle_in_cell(&mut self, token: Token) {
// -> An end tag whose tag name is one of: "td", "th"
- if token.is_end_tag_one_of(&[tag_name!("td"), tag_name!("th")]) {
+ if token.is_end_tag_one_of(tag_set!["td", "th"]) {
// If the stack of open elements does not have an element in table scope that is an HTML element with the
// same tag name as that of the token, then this is a parse error; ignore the token.
if !self.has_in_table_scope(token.tag_name()) {
@@ -2798,16 +2688,8 @@ impl TreeBuilder {
}
// -> A start tag whose tag name is one of: "caption", "col", "colgroup", "tbody", "td", "tfoot", "th", "thead", "tr"
- if token.is_start_tag_one_of(&[
- tag_name!("caption"),
- tag_name!("col"),
- tag_name!("colgroup"),
- tag_name!("tbody"),
- tag_name!("td"),
- tag_name!("tfoot"),
- tag_name!("th"),
- tag_name!("thead"),
- tag_name!("tr"),
+ if token.is_start_tag_one_of(tag_set![
+ "caption", "col", "colgroup", "tbody", "td", "tfoot", "th", "thead", "tr"
]) {
// FIXME: Assert: the stack of open elements has a td or th element in table scope.
if !self.has_in_table_scope(tag_name!("td")) && !self.has_in_table_scope(tag_name!("th")) {
@@ -2822,26 +2704,14 @@ impl TreeBuilder {
}
// -> An end tag whose tag name is one of: "body", "caption", "col", "colgroup", "html"
- if token.is_end_tag_one_of(&[
- tag_name!("body"),
- tag_name!("caption"),
- tag_name!("col"),
- tag_name!("colgroup"),
- tag_name!("html"),
- ]) {
+ if token.is_end_tag_one_of(tag_set!["body", "caption", "col", "colgroup", "html"]) {
// Parse error. Ignore the token.
self.parse_error("unexpected end tag in in cell insertion mode");
return;
}
// -> An end tag whose tag name is one of: "table", "tbody", "tfoot", "thead", "tr"
- if token.is_end_tag_one_of(&[
- tag_name!("table"),
- tag_name!("tbody"),
- tag_name!("tfoot"),
- tag_name!("thead"),
- tag_name!("tr"),
- ]) {
+ if token.is_end_tag_one_of(tag_set!["table", "tbody", "tfoot", "thead", "tr"]) {
// If the stack of open elements does not have an element in table scope that is an HTML element with the same
// tag name as that of the token, then this is a parse error; ignore the token.
if !self.has_in_table_scope(token.tag_name()) {
@@ -2879,7 +2749,7 @@ impl TreeBuilder {
return;
}
- if token.is_end_tag_named(tag_name!("script")) && self.current_node_named(tag_name!("script")) {
+ if token.is_end_tag_named(tag_id!("script")) && self.current_node_named(tag_name!("script")) {
self.flush_character_insertions();
let script = self.current_node_handle();
self.pop_current_node();
@@ -2920,18 +2790,9 @@ impl TreeBuilder {
// -> A start tag whose tag name is one of: "base", "basefont", "bgsound", "link", "meta", "noframes", "script", "style", "template", "title"
// -> An end tag whose tag name is "template"
- if token.is_start_tag_one_of(&[
- tag_name!("base"),
- tag_name!("basefont"),
- tag_name!("bgsound"),
- tag_name!("link"),
- tag_name!("meta"),
- tag_name!("noframes"),
- tag_name!("script"),
- tag_name!("style"),
- tag_name!("template"),
- tag_name!("title"),
- ]) || token.is_end_tag_named(tag_name!("template"))
+ if token.is_start_tag_one_of(tag_set![
+ "base", "basefont", "bgsound", "link", "meta", "noframes", "script", "style", "template", "title"
+ ]) || token.is_end_tag_named(tag_id!("template"))
{
// Process the token using the rules for the "in head" insertion mode.
self.process_using_the_rules_for(InsertionMode::InHead, token);
@@ -2939,13 +2800,7 @@ impl TreeBuilder {
}
// -> A start tag whose tag name is one of: "caption", "colgroup", "tbody", "tfoot", "thead"
- if token.is_start_tag_one_of(&[
- tag_name!("caption"),
- tag_name!("colgroup"),
- tag_name!("tbody"),
- tag_name!("tfoot"),
- tag_name!("thead"),
- ]) {
+ if token.is_start_tag_one_of(tag_set!["caption", "colgroup", "tbody", "tfoot", "thead"]) {
// Pop the current template insertion mode off the stack of template insertion modes.
self.stack_of_template_insertion_modes.pop();
// Push "in table" onto the stack of template insertion modes so that it is the new current template insertion mode.
@@ -2957,7 +2812,7 @@ impl TreeBuilder {
}
// -> A start tag whose tag name is "col"
- if token.is_start_tag_named(tag_name!("col")) {
+ if token.is_start_tag_named(tag_id!("col")) {
// Pop the current template insertion mode off the stack of template insertion modes.
self.stack_of_template_insertion_modes.pop();
// Push "in column group" onto the stack of template insertion modes so that it is the new current template insertion mode.
@@ -2970,7 +2825,7 @@ impl TreeBuilder {
}
// -> A start tag whose tag name is "tr"
- if token.is_start_tag_named(tag_name!("tr")) {
+ if token.is_start_tag_named(tag_id!("tr")) {
// Pop the current template insertion mode off the stack of template insertion modes.
self.stack_of_template_insertion_modes.pop();
// Push "in table body" onto the stack of template insertion modes so that it is the new current template insertion mode.
@@ -2982,7 +2837,7 @@ impl TreeBuilder {
}
// -> A start tag whose tag name is one of: "td", "th"
- if token.is_start_tag_one_of(&[tag_name!("td"), tag_name!("th")]) {
+ if token.is_start_tag_one_of(tag_set!["td", "th"]) {
// Pop the current template insertion mode off the stack of template insertion modes.
self.stack_of_template_insertion_modes.pop();
// Push "in row" onto the stack of template insertion modes so that it is the new current template insertion mode.
@@ -3069,14 +2924,14 @@ impl TreeBuilder {
}
// -> A start tag whose tag name is "html"
- if token.is_start_tag_named(tag_name!("html")) {
+ if token.is_start_tag_named(tag_id!("html")) {
// Process the token using the rules for the "in body" insertion mode.
self.process_using_the_rules_for(InsertionMode::InBody, token);
return;
}
// -> An end tag whose tag name is "html"
- if token.is_end_tag_named(tag_name!("html")) {
+ if token.is_end_tag_named(tag_id!("html")) {
// If the parser's fragment context element is non-null, this is a parse error; ignore the token.
// (fragment case)
if self.parsing_fragment {
@@ -3122,7 +2977,7 @@ impl TreeBuilder {
// -> A start tag whose tag name is "html"
if token.token_type == TokenType::Doctype
|| token.is_parser_whitespace()
- || token.is_start_tag_named(tag_name!("html"))
+ || token.is_start_tag_named(tag_id!("html"))
{
// Process the token using the rules for the "in body" insertion mode.
self.process_using_the_rules_for(InsertionMode::InBody, token);
@@ -3160,7 +3015,7 @@ impl TreeBuilder {
// -> A start tag whose tag name is "html"
if token.token_type == TokenType::Doctype
|| token.is_parser_whitespace()
- || token.is_start_tag_named(tag_name!("html"))
+ || token.is_start_tag_named(tag_id!("html"))
{
// Process the token using the rules for the "in body" insertion mode.
self.process_using_the_rules_for(InsertionMode::InBody, token);
@@ -3175,7 +3030,7 @@ impl TreeBuilder {
}
// -> A start tag whose tag name is "noframes"
- if token.is_start_tag_named(tag_name!("noframes")) {
+ if token.is_start_tag_named(tag_id!("noframes")) {
// Process the token using the rules for the "in head" insertion mode.
self.process_using_the_rules_for(InsertionMode::InHead, token);
return;
@@ -3291,7 +3146,7 @@ impl TreeBuilder {
}
// -> An end tag whose tag name is "script", if the current node is an SVG script element
- if token.is_end_tag_named(tag_name!("script"))
+ if token.is_end_tag_named(tag_id!("script"))
&& self.stack_of_open_elements.last().is_some_and(|node| {
node.namespace_ == RustFfiHtmlNamespace::Svg && node.local_name == tag_name!("script")
})
@@ -5706,57 +5561,57 @@ fn is_scope_boundary(node: &StackNode) -> bool {
fn is_foreign_content_breakout_token(token: &Token) -> bool {
// -> A start tag whose tag name is one of: "b", "big", "blockquote", "body", "br", "center", "code", "dd", "div", "dl", "dt", "em", "embed", "h1", "h2", "h3", "h4", "h5", "h6", "head", "hr", "i", "img", "li", "listing", "menu", "meta", "nobr", "ol", "p", "pre", "ruby", "s", "small", "span", "strong", "strike", "sub", "sup", "table", "tt", "u", "ul", "var"
- if token.is_start_tag_one_of(&[
- tag_name!("b"),
- tag_name!("big"),
- tag_name!("blockquote"),
- tag_name!("body"),
- tag_name!("br"),
- tag_name!("center"),
- tag_name!("code"),
- tag_name!("dd"),
- tag_name!("div"),
- tag_name!("dl"),
- tag_name!("dt"),
- tag_name!("em"),
- tag_name!("embed"),
- tag_name!("h1"),
- tag_name!("h2"),
- tag_name!("h3"),
- tag_name!("h4"),
- tag_name!("h5"),
- tag_name!("h6"),
- tag_name!("head"),
- tag_name!("hr"),
- tag_name!("i"),
- tag_name!("img"),
- tag_name!("li"),
- tag_name!("listing"),
- tag_name!("menu"),
- tag_name!("meta"),
- tag_name!("nobr"),
- tag_name!("ol"),
- tag_name!("p"),
- tag_name!("pre"),
- tag_name!("ruby"),
- tag_name!("s"),
- tag_name!("small"),
- tag_name!("span"),
- tag_name!("strong"),
- tag_name!("strike"),
- tag_name!("sub"),
- tag_name!("sup"),
- tag_name!("table"),
- tag_name!("tt"),
- tag_name!("u"),
- tag_name!("ul"),
- tag_name!("var"),
+ if token.is_start_tag_one_of(tag_set![
+ "b",
+ "big",
+ "blockquote",
+ "body",
+ "br",
+ "center",
+ "code",
+ "dd",
+ "div",
+ "dl",
+ "dt",
+ "em",
+ "embed",
+ "h1",
+ "h2",
+ "h3",
+ "h4",
+ "h5",
+ "h6",
+ "head",
+ "hr",
+ "i",
+ "img",
+ "li",
+ "listing",
+ "menu",
+ "meta",
+ "nobr",
+ "ol",
+ "p",
+ "pre",
+ "ruby",
+ "s",
+ "small",
+ "span",
+ "strong",
+ "strike",
+ "sub",
+ "sup",
+ "table",
+ "tt",
+ "u",
+ "ul",
+ "var"
]) {
return true;
}
// -> A start tag whose tag name is "font", if the token has any attributes named "color", "face", or "size"
- if token.is_start_tag_named(tag_name!("font"))
+ if token.is_start_tag_named(tag_id!("font"))
&& (token.has_attribute(attribute_name!("color"))
|| token.has_attribute(attribute_name!("face"))
|| token.has_attribute(attribute_name!("size")))
@@ -5765,7 +5620,7 @@ fn is_foreign_content_breakout_token(token: &Token) -> bool {
}
// -> An end tag whose tag name is "br", "p"
- token.is_end_tag_one_of(&[tag_name!("br"), tag_name!("p")])
+ token.is_end_tag_one_of(tag_set!["br", "p"])
}
fn is_formatting_element(tag_name: &HtmlName) -> bool {
@@ -5958,10 +5813,10 @@ fn is_void_html_element(tag_name: &HtmlName) -> bool {
trait TokenExt {
fn is_start_tag(&self) -> bool;
fn is_end_tag(&self) -> bool;
- fn is_start_tag_named(&self, name: KnownName) -> bool;
- fn is_end_tag_named(&self, name: KnownName) -> bool;
- fn is_start_tag_one_of(&self, names: &[KnownName]) -> bool;
- fn is_end_tag_one_of(&self, names: &[KnownName]) -> bool;
+ fn is_start_tag_named(&self, id: TagId) -> bool;
+ fn is_end_tag_named(&self, id: TagId) -> bool;
+ fn is_start_tag_one_of(&self, names: &TagSet) -> bool;
+ fn is_end_tag_one_of(&self, names: &TagSet) -> bool;
fn is_parser_whitespace(&self) -> bool;
fn comment_data(&self) -> &str;
fn doctype_data(&self) -> &crate::token::DoctypeData;
@@ -5978,20 +5833,24 @@ impl TokenExt for Token {
self.token_type == TokenType::EndTag
}
- fn is_start_tag_named(&self, name: KnownName) -> bool {
- self.is_start_tag() && *self.tag_name() == name
+ #[inline(always)]
+ fn is_start_tag_named(&self, id: TagId) -> bool {
+ self.is_start_tag() && self.tag_id() == id
}
- fn is_end_tag_named(&self, name: KnownName) -> bool {
- self.is_end_tag() && *self.tag_name() == name
+ #[inline(always)]
+ fn is_end_tag_named(&self, id: TagId) -> bool {
+ self.is_end_tag() && self.tag_id() == id
}
- fn is_start_tag_one_of(&self, names: &[KnownName]) -> bool {
- self.is_start_tag() && names.contains(&KnownName::from_raw(self.tag_name().raw_identity()))
+ #[inline(always)]
+ fn is_start_tag_one_of(&self, names: &TagSet) -> bool {
+ self.is_start_tag() && names.contains(self.tag_id())
}
- fn is_end_tag_one_of(&self, names: &[KnownName]) -> bool {
- self.is_end_tag() && names.contains(&KnownName::from_raw(self.tag_name().raw_identity()))
+ #[inline(always)]
+ fn is_end_tag_one_of(&self, names: &TagSet) -> bool {
+ self.is_end_tag() && names.contains(self.tag_id())
}
fn is_parser_whitespace(&self) -> bool {
@@ -6047,6 +5906,7 @@ impl Token {
token_type: TokenType::EndTag,
payload: TokenPayload::Tag {
tag_name: HtmlName::from_utf8(tag_name),
+ tag_id: TagId::for_name(tag_name),
self_closing: false,
had_duplicate_attribute: false,
attributes: AttributeList::new(),
@@ -6060,6 +5920,7 @@ impl Token {
token_type: TokenType::StartTag,
payload: TokenPayload::Tag {
tag_name: HtmlName::from_utf8(tag_name),
+ tag_id: TagId::for_name(tag_name),
self_closing: false,
had_duplicate_attribute: false,
attributes: AttributeList::new(),
diff --git a/Libraries/LibWeb/HTML/Parser/Rust/src/token.rs b/Libraries/LibWeb/HTML/Parser/Rust/src/token.rs
index 351c183ec47..9398e1556d5 100644
--- a/Libraries/LibWeb/HTML/Parser/Rust/src/token.rs
+++ b/Libraries/LibWeb/HTML/Parser/Rust/src/token.rs
@@ -40,6 +40,54 @@ impl Default for SourceOffset {
}
}
+/// A small number that stands for a known tag name, so that the tree builder can tell which of them a token has
+/// without comparing names. Every other name has the id `UNKNOWN`.
+#[derive(Clone, Copy, Debug, Default, Eq, PartialEq)]
+pub struct TagId(u16);
+
+impl TagId {
+ pub const UNKNOWN: Self = Self(0);
+
+ pub const fn from_index(index: u16) -> Self {
+ Self(index)
+ }
+
+ pub fn for_name(name: &str) -> Self {
+ crate::known_names::tag_id_for_name(name.as_bytes())
+ }
+}
+
+/// A set of known tag names.
+pub struct TagSet([u64; crate::known_names::TAG_SET_WORD_COUNT]);
+
+impl TagSet {
+ pub const fn new(ids: &[TagId]) -> Self {
+ let mut words = [0; crate::known_names::TAG_SET_WORD_COUNT];
+ let mut i = 0;
+ while i < ids.len() {
+ let index = ids[i].0 as usize;
+ assert!(index != 0, "a tag set can only have known tag names in it");
+ words[index / 64] |= 1 << (index % 64);
+ i += 1;
+ }
+ Self(words)
+ }
+
+ #[inline(always)]
+ pub const fn contains(&self, id: TagId) -> bool {
+ let index = id.0 as usize;
+ (self.0[index / 64] >> (index % 64)) & 1 != 0
+ }
+}
+
+/// A set of the known tag names it is given, worked out when the parser is compiled.
+macro_rules! tag_set {
+ ($($name:tt),+ $(,)?) => {
+ const { &$crate::token::TagSet::new(&[$($crate::known_names::tag_id!($name)),+]) }
+ };
+}
+pub(crate) use tag_set;
+
/// A single attribute on a start or end tag token.
///
#[derive(Clone, Debug, Default)]
@@ -390,6 +438,7 @@ pub enum TokenPayload {
None,
Tag {
tag_name: HtmlName,
+ tag_id: TagId,
self_closing: bool,
// AD-HOC: See AD-HOC comment on Element.m_had_duplicate_attribute_during_tokenization about why this is tracked.
had_duplicate_attribute: bool,
@@ -433,9 +482,20 @@ impl Token {
}
}
- pub fn set_tag_name(&mut self, name: HtmlName) {
+ #[inline(always)]
+ pub fn tag_id(&self) -> TagId {
+ match &self.payload {
+ TokenPayload::Tag { tag_id, .. } => *tag_id,
+ _ => panic!("tag_id called on non-tag token"),
+ }
+ }
+
+ pub fn set_tag_name(&mut self, name: &str) {
match &mut self.payload {
- TokenPayload::Tag { tag_name, .. } => *tag_name = name,
+ TokenPayload::Tag { tag_name, tag_id, .. } => {
+ *tag_name = HtmlName::from_utf8(name);
+ *tag_id = TagId::for_name(name);
+ }
_ => panic!("set_tag_name called on non-tag token"),
}
}
diff --git a/Libraries/LibWeb/HTML/Parser/Rust/src/tokenizer.rs b/Libraries/LibWeb/HTML/Parser/Rust/src/tokenizer.rs
index 4d8a8ffe6f5..2723315bc5c 100644
--- a/Libraries/LibWeb/HTML/Parser/Rust/src/tokenizer.rs
+++ b/Libraries/LibWeb/HTML/Parser/Rust/src/tokenizer.rs
@@ -734,8 +734,7 @@ impl HtmlTokenizer {
/// Commit `current_builder` into `current_token.tag_name`.
#[inline]
fn commit_current_builder_as_tag_name(&mut self) {
- self.current_token
- .set_tag_name(crate::token::HtmlName::from_utf8(&self.current_builder));
+ self.current_token.set_tag_name(&self.current_builder);
self.current_builder.clear();
}
@@ -743,6 +742,7 @@ impl HtmlTokenizer {
let payload = match token_type {
TokenType::StartTag | TokenType::EndTag => TokenPayload::Tag {
tag_name: Default::default(),
+ tag_id: Default::default(),
self_closing: false,
had_duplicate_attribute: false,
attributes: crate::token::AttributeList::new(),