From 9fe35ddddf895b0835a347eab7b45b33d1638ce7 Mon Sep 17 00:00:00 2001 From: Timothy Flynn Date: Mon, 29 Jul 2024 17:14:34 -0400 Subject: [PATCH] LibWeb: Use an infallible method to add attributes to nodes In the HTML parser spec, there are 2 instances of the following text: add the attribute and its corresponding value to that element The "add the attribute" text does not have a corresponding spec link to actually specify what to do. We currently use `set_attribute`, which can throw an exception if the attribute name contains an invalid character (such as '<'). Instead, switch to `append_attribute`, which allows such attribute names. This behavior matches Firefox. Note we cannot yet make the unclosed-html-element.html test match the expectations of the unclosed-body-element.html due to another bug that would prevent checking if the expected element has the right attribute. That will be fixed in an upcoming commit. --- Tests/LibWeb/Text/expected/unclosed-body-element.txt | 1 + Tests/LibWeb/Text/expected/unclosed-html-element.txt | 1 + Tests/LibWeb/Text/input/unclosed-body-element.html | 10 ++++++++++ Tests/LibWeb/Text/input/unclosed-html-element.html | 10 ++++++++++ Userland/Libraries/LibWeb/HTML/Parser/HTMLParser.cpp | 5 ++--- 5 files changed, 24 insertions(+), 3 deletions(-) create mode 100644 Tests/LibWeb/Text/expected/unclosed-body-element.txt create mode 100644 Tests/LibWeb/Text/expected/unclosed-html-element.txt create mode 100644 Tests/LibWeb/Text/input/unclosed-body-element.html create mode 100644 Tests/LibWeb/Text/input/unclosed-html-element.html diff --git a/Tests/LibWeb/Text/expected/unclosed-body-element.txt b/Tests/LibWeb/Text/expected/unclosed-body-element.txt new file mode 100644 index 00000000000..f769a25f8f8 --- /dev/null +++ b/Tests/LibWeb/Text/expected/unclosed-body-element.txt @@ -0,0 +1 @@ + Body element has ' + + + + diff --git a/Tests/LibWeb/Text/input/unclosed-html-element.html b/Tests/LibWeb/Text/input/unclosed-html-element.html new file mode 100644 index 00000000000..c1bab5c6f6e --- /dev/null +++ b/Tests/LibWeb/Text/input/unclosed-html-element.html @@ -0,0 +1,10 @@ + + + + + diff --git a/Userland/Libraries/LibWeb/HTML/Parser/HTMLParser.cpp b/Userland/Libraries/LibWeb/HTML/Parser/HTMLParser.cpp index 2fe7f11f564..23539578935 100644 --- a/Userland/Libraries/LibWeb/HTML/Parser/HTMLParser.cpp +++ b/Userland/Libraries/LibWeb/HTML/Parser/HTMLParser.cpp @@ -1721,7 +1721,7 @@ void HTMLParser::handle_in_body(HTMLToken& token) // If it is not, add the attribute and its corresponding value to that element. token.for_each_attribute([&](auto& attribute) { if (!current_node().has_attribute(attribute.local_name)) - MUST(current_node().set_attribute(attribute.local_name, attribute.value)); + current_node().append_attribute(attribute.local_name, attribute.value); return IterationDecision::Continue; }); return; @@ -1743,7 +1743,6 @@ void HTMLParser::handle_in_body(HTMLToken& token) // -> A start tag whose tag name is "body" if (token.is_start_tag() && token.tag_name() == HTML::TagNames::body) { - // Parse error. log_parse_error(); @@ -1763,7 +1762,7 @@ void HTMLParser::handle_in_body(HTMLToken& token) auto& body_element = m_stack_of_open_elements.elements().at(1); token.for_each_attribute([&](auto& attribute) { if (!body_element->has_attribute(attribute.local_name)) - MUST(body_element->set_attribute(attribute.local_name, attribute.value)); + body_element->append_attribute(attribute.local_name, attribute.value); return IterationDecision::Continue; }); return;