diff --git a/Userland/Libraries/LibWeb/DOM/Document.h b/Userland/Libraries/LibWeb/DOM/Document.h index 6e40c04f54..40e8e454a4 100644 --- a/Userland/Libraries/LibWeb/DOM/Document.h +++ b/Userland/Libraries/LibWeb/DOM/Document.h @@ -387,6 +387,9 @@ public: void set_parser(Badge, HTML::HTMLParser&); void detach_parser(Badge); + void set_is_temporary_document_for_fragment_parsing(Badge) { m_temporary_document_for_fragment_parsing = true; } + [[nodiscard]] bool is_temporary_document_for_fragment_parsing() const { return m_temporary_document_for_fragment_parsing; } + static bool is_valid_name(DeprecatedString const&); struct PrefixAndTagName { @@ -688,6 +691,8 @@ private: bool m_will_declaratively_refresh { false }; RefPtr m_active_refresh_timer; + + bool m_temporary_document_for_fragment_parsing { false }; }; template<> diff --git a/Userland/Libraries/LibWeb/DOM/StyleElementUtils.cpp b/Userland/Libraries/LibWeb/DOM/StyleElementUtils.cpp index 743977d4ef..de71d98ff4 100644 --- a/Userland/Libraries/LibWeb/DOM/StyleElementUtils.cpp +++ b/Userland/Libraries/LibWeb/DOM/StyleElementUtils.cpp @@ -23,6 +23,11 @@ namespace Web::DOM { // https://html.spec.whatwg.org/multipage/semantics.html#update-a-style-block void StyleElementUtils::update_a_style_block(DOM::Element& style_element) { + // OPTIMIZATION: Skip parsing CSS if we're in the middle of parsing a HTML fragment. + // The style block will be parsed upon insertion into a proper document. + if (style_element.document().is_temporary_document_for_fragment_parsing()) + return; + // 1. Let element be the style element. // 2. If element has an associated CSS style sheet, remove the CSS style sheet in question. diff --git a/Userland/Libraries/LibWeb/HTML/Parser/HTMLParser.cpp b/Userland/Libraries/LibWeb/HTML/Parser/HTMLParser.cpp index 822738d3b9..876a40c881 100644 --- a/Userland/Libraries/LibWeb/HTML/Parser/HTMLParser.cpp +++ b/Userland/Libraries/LibWeb/HTML/Parser/HTMLParser.cpp @@ -3665,6 +3665,8 @@ Vector> HTMLParser::parse_html_fragment(DOM::Element& cont auto temp_document = DOM::Document::create(context_element.realm()).release_value_but_fixme_should_propagate_errors(); temp_document->set_document_type(DOM::Document::Type::HTML); + temp_document->set_is_temporary_document_for_fragment_parsing({}); + // 2. If the node document of the context element is in quirks mode, then let the Document be in quirks mode. // Otherwise, the node document of the context element is in limited-quirks mode, then let the Document be in limited-quirks mode. // Otherwise, leave the Document in no-quirks mode.