From 6b20a109c61777197f0fe4072cd316119e22ad3f Mon Sep 17 00:00:00 2001 From: Andreas Kling Date: Sat, 4 Nov 2023 10:19:21 +0100 Subject: [PATCH] LibWeb: Pass DOM namespace strings as FlyString in more places --- Userland/Libraries/LibWeb/DOM/Document.cpp | 10 ++--- Userland/Libraries/LibWeb/DOM/Document.h | 2 +- Userland/Libraries/LibWeb/DOM/Element.cpp | 4 +- .../Libraries/LibWeb/DOM/ElementFactory.cpp | 3 +- .../LibWeb/HTML/Parser/HTMLParser.cpp | 38 +++++++++---------- .../Libraries/LibWeb/HTML/Parser/HTMLParser.h | 6 +-- .../HTML/Parser/StackOfOpenElements.cpp | 2 +- 7 files changed, 32 insertions(+), 33 deletions(-) diff --git a/Userland/Libraries/LibWeb/DOM/Document.cpp b/Userland/Libraries/LibWeb/DOM/Document.cpp index 427327756a..223f58cf85 100644 --- a/Userland/Libraries/LibWeb/DOM/Document.cpp +++ b/Userland/Libraries/LibWeb/DOM/Document.cpp @@ -2432,10 +2432,10 @@ void Document::set_window(HTML::Window& window) } // https://html.spec.whatwg.org/multipage/custom-elements.html#look-up-a-custom-element-definition -JS::GCPtr Document::lookup_custom_element_definition(DeprecatedFlyString const& namespace_, DeprecatedFlyString const& local_name, Optional const& is) const +JS::GCPtr Document::lookup_custom_element_definition(Optional const& namespace_, FlyString const& local_name, Optional const& is) const { // 1. If namespace is not the HTML namespace, return null. - if (namespace_ != Namespace::HTML) + if (namespace_ != MUST(FlyString::from_deprecated_fly_string(Namespace::HTML))) return nullptr; // 2. If document's browsing context is null, return null. @@ -2446,8 +2446,8 @@ JS::GCPtr Document::lookup_custom_element_definit auto registry = verify_cast(relevant_global_object(*this)).custom_elements(); // 4. If there is custom element definition in registry with name and local name both equal to localName, return that custom element definition. - auto converted_local_name = String::from_deprecated_string(local_name).release_value_but_fixme_should_propagate_errors(); - auto maybe_definition = registry->get_definition_with_name_and_local_name(converted_local_name, converted_local_name); + auto converted_local_name = local_name; + auto maybe_definition = registry->get_definition_with_name_and_local_name(converted_local_name.to_string(), converted_local_name.to_string()); if (maybe_definition) return maybe_definition; @@ -2458,7 +2458,7 @@ JS::GCPtr Document::lookup_custom_element_definit if (!is.has_value()) return nullptr; - return registry->get_definition_with_name_and_local_name(is.value(), converted_local_name); + return registry->get_definition_with_name_and_local_name(is.value(), converted_local_name.to_string()); } CSS::StyleSheetList& Document::style_sheets() diff --git a/Userland/Libraries/LibWeb/DOM/Document.h b/Userland/Libraries/LibWeb/DOM/Document.h index a6b904d007..78930cec41 100644 --- a/Userland/Libraries/LibWeb/DOM/Document.h +++ b/Userland/Libraries/LibWeb/DOM/Document.h @@ -435,7 +435,7 @@ public: bool has_active_favicon() const { return m_active_favicon; } void check_favicon_after_loading_link_resource(); - JS::GCPtr lookup_custom_element_definition(DeprecatedFlyString const& namespace_, DeprecatedFlyString const& local_name, Optional const& is) const; + JS::GCPtr lookup_custom_element_definition(Optional const& namespace_, FlyString const& local_name, Optional const& is) const; void increment_throw_on_dynamic_markup_insertion_counter(Badge); void decrement_throw_on_dynamic_markup_insertion_counter(Badge); diff --git a/Userland/Libraries/LibWeb/DOM/Element.cpp b/Userland/Libraries/LibWeb/DOM/Element.cpp index e929df53a0..f27c9009e3 100644 --- a/Userland/Libraries/LibWeb/DOM/Element.cpp +++ b/Userland/Libraries/LibWeb/DOM/Element.cpp @@ -647,7 +647,7 @@ WebIDL::ExceptionOr> Element::attach_shadow(ShadowR // 3. If this’s local name is a valid custom element name, or this’s is value is not null, then: if (HTML::is_valid_custom_element_name(local_name()) || m_is_value.has_value()) { // 1. Let definition be the result of looking up a custom element definition given this’s node document, its namespace, its local name, and its is value. - auto definition = document().lookup_custom_element_definition(namespace_(), local_name().to_deprecated_fly_string(), m_is_value); + auto definition = document().lookup_custom_element_definition(namespace_uri(), local_name(), m_is_value); // 2. If definition is not null and definition’s disable shadow is true, then throw a "NotSupportedError" DOMException. if (definition && definition->disable_shadow()) @@ -1844,7 +1844,7 @@ JS::ThrowCompletionOr Element::upgrade_element(JS::NonnullGCPtr> create_element(Document& document // NOTE: We collapse this into just returning an element where necessary. // 4. Let definition be the result of looking up a custom element definition given document, namespace, localName, and is. - DeprecatedFlyString deprecated_namespace = namespace_.has_value() ? namespace_.value().to_deprecated_fly_string() : DeprecatedFlyString {}; - auto definition = document.lookup_custom_element_definition(deprecated_namespace, local_name.to_deprecated_fly_string(), is_value); + auto definition = document.lookup_custom_element_definition(namespace_, local_name, is_value); // 5. If definition is non-null, and definition’s name is not equal to its local name (i.e., definition represents a customized built-in element), then: if (definition && definition->name() != definition->local_name()) { diff --git a/Userland/Libraries/LibWeb/HTML/Parser/HTMLParser.cpp b/Userland/Libraries/LibWeb/HTML/Parser/HTMLParser.cpp index 697957d32d..9f4bc1be53 100644 --- a/Userland/Libraries/LibWeb/HTML/Parser/HTMLParser.cpp +++ b/Userland/Libraries/LibWeb/HTML/Parser/HTMLParser.cpp @@ -539,7 +539,7 @@ void HTMLParser::handle_before_html(HTMLToken& token) // -> A start tag whose tag name is "html" if (token.is_start_tag() && token.tag_name() == HTML::TagNames::html) { // Create an element for the token in the HTML namespace, with the Document as the intended parent. Append it to the Document object. Put this element in the stack of open elements. - auto element = create_element_for(token, Namespace::HTML, document()); + auto element = create_element_for(token, MUST(FlyString::from_deprecated_fly_string(Namespace::HTML)), document()); MUST(document().append_child(*element)); m_stack_of_open_elements.push(move(element)); @@ -644,7 +644,7 @@ HTMLParser::AdjustedInsertionLocation HTMLParser::find_appropriate_place_for_ins return adjusted_insertion_location; } -JS::NonnullGCPtr HTMLParser::create_element_for(HTMLToken const& token, DeprecatedFlyString const& namespace_, DOM::Node& intended_parent) +JS::NonnullGCPtr HTMLParser::create_element_for(HTMLToken const& token, Optional const& namespace_, DOM::Node& intended_parent) { // FIXME: 1. If the active speculative HTML parser is not null, then return the result of creating a speculative mock element given given namespace, the tag name of the given token, and the attributes of the given token. // FIXME: 2. Otherwise, optionally create a speculative mock element given given namespace, the tag name of the given token, and the attributes of the given token. @@ -662,7 +662,7 @@ JS::NonnullGCPtr HTMLParser::create_element_for(HTMLToken const& t is_value = String::from_utf8(is_value_deprecated_string).release_value_but_fixme_should_propagate_errors(); // 6. Let definition be the result of looking up a custom element definition given document, given namespace, local name, and is. - auto definition = document->lookup_custom_element_definition(namespace_, local_name.to_deprecated_fly_string(), is_value); + auto definition = document->lookup_custom_element_definition(namespace_, local_name, is_value); // 7. If definition is non-null and the parser was not created as part of the HTML fragment parsing algorithm, then let will execute script be true. Otherwise, let it be false. bool will_execute_script = definition && !m_parsing_fragment; @@ -684,7 +684,7 @@ JS::NonnullGCPtr HTMLParser::create_element_for(HTMLToken const& t // 9. Let element be the result of creating an element given document, localName, given namespace, null, and is. // If will execute script is true, set the synchronous custom elements flag; otherwise, leave it unset. - auto element = create_element(*document, local_name, MUST(FlyString::from_deprecated_fly_string(namespace_)), {}, is_value, will_execute_script).release_value_but_fixme_should_propagate_errors(); + auto element = create_element(*document, local_name, namespace_, {}, is_value, will_execute_script).release_value_but_fixme_should_propagate_errors(); // 10. Append each attribute in the given token to element. // FIXME: This isn't the exact `append` the spec is talking about. @@ -736,7 +736,7 @@ JS::NonnullGCPtr HTMLParser::create_element_for(HTMLToken const& t } // https://html.spec.whatwg.org/multipage/parsing.html#insert-a-foreign-element -JS::NonnullGCPtr HTMLParser::insert_foreign_element(HTMLToken const& token, DeprecatedFlyString const& namespace_) +JS::NonnullGCPtr HTMLParser::insert_foreign_element(HTMLToken const& token, Optional const& namespace_) { auto adjusted_insertion_location = find_appropriate_place_for_inserting_node(); @@ -772,7 +772,7 @@ JS::NonnullGCPtr HTMLParser::insert_foreign_element(HTMLToken cons JS::NonnullGCPtr HTMLParser::insert_html_element(HTMLToken const& token) { - return insert_foreign_element(token, Namespace::HTML); + return insert_foreign_element(token, MUST(FlyString::from_deprecated_fly_string(Namespace::HTML))); } void HTMLParser::handle_before_head(HTMLToken& token) @@ -882,7 +882,7 @@ void HTMLParser::handle_in_head(HTMLToken& token) if (token.is_start_tag() && token.tag_name() == HTML::TagNames::script) { auto adjusted_insertion_location = find_appropriate_place_for_inserting_node(); - auto element = create_element_for(token, Namespace::HTML, *adjusted_insertion_location.parent); + auto element = create_element_for(token, MUST(FlyString::from_deprecated_fly_string(Namespace::HTML)), *adjusted_insertion_location.parent); auto& script_element = verify_cast(*element); script_element.set_parser_document(Badge {}, document()); script_element.set_force_async(Badge {}, false); @@ -1383,7 +1383,7 @@ HTMLParser::AdoptionAgencyAlgorithmOutcome HTMLParser::run_the_adoption_agency_a // 6. Create an element for the token for which the element node was created, // in the HTML namespace, with common ancestor as the intended parent; // FIXME: hold onto the real token - auto element = create_element_for(HTMLToken::make_start_tag(node->local_name()), Namespace::HTML, *common_ancestor); + auto element = create_element_for(HTMLToken::make_start_tag(node->local_name()), MUST(FlyString::from_deprecated_fly_string(Namespace::HTML)), *common_ancestor); // replace the entry for node in the list of active formatting elements with an entry for the new element, m_list_of_active_formatting_elements.replace(*node, *element); // replace the entry for node in the stack of open elements with an entry for the new element, @@ -1412,7 +1412,7 @@ HTMLParser::AdoptionAgencyAlgorithmOutcome HTMLParser::run_the_adoption_agency_a // 15. Create an element for the token for which formatting element was created, // in the HTML namespace, with furthest block as the intended parent. // FIXME: hold onto the real token - auto element = create_element_for(HTMLToken::make_start_tag(formatting_element->local_name()), Namespace::HTML, *furthest_block); + auto element = create_element_for(HTMLToken::make_start_tag(formatting_element->local_name()), MUST(FlyString::from_deprecated_fly_string(Namespace::HTML)), *furthest_block); // 16. Take all of the child nodes of furthest block and append them to the element created in the last step. for (auto& child : furthest_block->children_as_vector()) @@ -1437,9 +1437,9 @@ HTMLParser::AdoptionAgencyAlgorithmOutcome HTMLParser::run_the_adoption_agency_a } // https://html.spec.whatwg.org/multipage/parsing.html#special -bool HTMLParser::is_special_tag(FlyString const& tag_name, DeprecatedFlyString const& namespace_) +bool HTMLParser::is_special_tag(FlyString const& tag_name, Optional const& namespace_) { - if (namespace_ == Namespace::HTML) { + if (namespace_ == MUST(FlyString::from_deprecated_fly_string(Namespace::HTML))) { return tag_name.is_one_of( HTML::TagNames::address, HTML::TagNames::applet, @@ -1523,12 +1523,12 @@ bool HTMLParser::is_special_tag(FlyString const& tag_name, DeprecatedFlyString c HTML::TagNames::ul, HTML::TagNames::wbr, HTML::TagNames::xmp); - } else if (namespace_ == Namespace::SVG) { + } else if (namespace_ == MUST(FlyString::from_deprecated_fly_string(Namespace::SVG))) { return tag_name.is_one_of( SVG::TagNames::desc, SVG::TagNames::foreignObject, SVG::TagNames::title); - } else if (namespace_ == Namespace::MathML) { + } else if (namespace_ == MUST(FlyString::from_deprecated_fly_string(Namespace::MathML))) { return tag_name.is_one_of( MathML::TagNames::mi, MathML::TagNames::mo, @@ -1740,7 +1740,7 @@ void HTMLParser::handle_in_body(HTMLToken& token) break; } - if (is_special_tag(node->local_name(), node->namespace_()) && !node->local_name().is_one_of(HTML::TagNames::address, HTML::TagNames::div, HTML::TagNames::p)) + if (is_special_tag(node->local_name(), node->namespace_uri()) && !node->local_name().is_one_of(HTML::TagNames::address, HTML::TagNames::div, HTML::TagNames::p)) break; } @@ -1771,7 +1771,7 @@ void HTMLParser::handle_in_body(HTMLToken& token) m_stack_of_open_elements.pop_until_an_element_with_tag_name_has_been_popped(HTML::TagNames::dt); break; } - if (is_special_tag(node->local_name(), node->namespace_()) && !node->local_name().is_one_of(HTML::TagNames::address, HTML::TagNames::div, HTML::TagNames::p)) + if (is_special_tag(node->local_name(), node->namespace_uri()) && !node->local_name().is_one_of(HTML::TagNames::address, HTML::TagNames::div, HTML::TagNames::p)) break; } if (m_stack_of_open_elements.has_in_button_scope(HTML::TagNames::p)) @@ -2127,7 +2127,7 @@ void HTMLParser::handle_in_body(HTMLToken& token) adjust_mathml_attributes(token); adjust_foreign_attributes(token); - (void)insert_foreign_element(token, Namespace::MathML); + (void)insert_foreign_element(token, MUST(FlyString::from_deprecated_fly_string(Namespace::MathML))); if (token.is_self_closing()) { (void)m_stack_of_open_elements.pop(); @@ -2141,7 +2141,7 @@ void HTMLParser::handle_in_body(HTMLToken& token) adjust_svg_attributes(token); adjust_foreign_attributes(token); - (void)insert_foreign_element(token, Namespace::SVG); + (void)insert_foreign_element(token, MUST(FlyString::from_deprecated_fly_string(Namespace::SVG))); if (token.is_self_closing()) { (void)m_stack_of_open_elements.pop(); @@ -2178,7 +2178,7 @@ void HTMLParser::handle_in_body(HTMLToken& token) (void)m_stack_of_open_elements.pop(); break; } - if (is_special_tag(node->local_name(), node->namespace_())) { + if (is_special_tag(node->local_name(), node->namespace_uri())) { log_parse_error(); return; } @@ -3508,7 +3508,7 @@ void HTMLParser::process_using_the_rules_for_foreign_content(HTMLToken& token) adjust_foreign_attributes(token); // Insert a foreign element for the token, in the same namespace as the adjusted current node. - (void)insert_foreign_element(token, adjusted_current_node().namespace_()); + (void)insert_foreign_element(token, adjusted_current_node().namespace_uri()); // If the token has its self-closing flag set, then run the appropriate steps from the following list: if (token.is_self_closing()) { diff --git a/Userland/Libraries/LibWeb/HTML/Parser/HTMLParser.h b/Userland/Libraries/LibWeb/HTML/Parser/HTMLParser.h index 15f4888c2e..4f691c9dd8 100644 --- a/Userland/Libraries/LibWeb/HTML/Parser/HTMLParser.h +++ b/Userland/Libraries/LibWeb/HTML/Parser/HTMLParser.h @@ -68,7 +68,7 @@ public: InsertionMode insertion_mode() const { return m_insertion_mode; } - static bool is_special_tag(FlyString const& tag_name, DeprecatedFlyString const& namespace_); + static bool is_special_tag(FlyString const& tag_name, Optional const& namespace_); HTMLTokenizer& tokenizer() { return m_tokenizer; } @@ -120,7 +120,7 @@ private: void generate_implied_end_tags(FlyString const& exception = {}); void generate_all_implied_end_tags_thoroughly(); - JS::NonnullGCPtr create_element_for(HTMLToken const&, DeprecatedFlyString const& namespace_, DOM::Node& intended_parent); + JS::NonnullGCPtr create_element_for(HTMLToken const&, Optional const& namespace_, DOM::Node& intended_parent); struct AdjustedInsertionLocation { JS::GCPtr parent; @@ -131,7 +131,7 @@ private: DOM::Text* find_character_insertion_node(); void flush_character_insertions(); - JS::NonnullGCPtr insert_foreign_element(HTMLToken const&, DeprecatedFlyString const&); + JS::NonnullGCPtr insert_foreign_element(HTMLToken const&, Optional const& namespace_); JS::NonnullGCPtr insert_html_element(HTMLToken const&); DOM::Element& current_node(); DOM::Element& adjusted_current_node(); diff --git a/Userland/Libraries/LibWeb/HTML/Parser/StackOfOpenElements.cpp b/Userland/Libraries/LibWeb/HTML/Parser/StackOfOpenElements.cpp index 6546341962..a88d9c60ac 100644 --- a/Userland/Libraries/LibWeb/HTML/Parser/StackOfOpenElements.cpp +++ b/Userland/Libraries/LibWeb/HTML/Parser/StackOfOpenElements.cpp @@ -128,7 +128,7 @@ JS::GCPtr StackOfOpenElements::topmost_special_node_below(DOM::Ele for (auto& element : m_elements.in_reverse()) { if (element.ptr() == &formatting_element) break; - if (HTMLParser::is_special_tag(element->local_name(), element->namespace_())) + if (HTMLParser::is_special_tag(element->local_name(), element->namespace_uri())) found_element = element.ptr(); } return found_element.ptr();