From c838ca78c81261e6111aa255c79e4a0599759c80 Mon Sep 17 00:00:00 2001 From: Timothy Flynn Date: Wed, 31 Jul 2024 12:02:38 -0400 Subject: [PATCH] LibWeb: Indicate documents are for fragment parsing during construction This will allow testing if they are for fragment parsing during methods invoked from Document::initialize. --- Userland/Libraries/LibWeb/DOM/Document.cpp | 10 ++++++++-- Userland/Libraries/LibWeb/DOM/Document.h | 13 +++++++++---- .../Libraries/LibWeb/HTML/Parser/HTMLParser.cpp | 4 +--- 3 files changed, 18 insertions(+), 9 deletions(-) diff --git a/Userland/Libraries/LibWeb/DOM/Document.cpp b/Userland/Libraries/LibWeb/DOM/Document.cpp index 2a19b21d7b5..da7153c6252 100644 --- a/Userland/Libraries/LibWeb/DOM/Document.cpp +++ b/Userland/Libraries/LibWeb/DOM/Document.cpp @@ -354,11 +354,17 @@ JS::NonnullGCPtr Document::create(JS::Realm& realm, URL::URL const& ur return realm.heap().allocate(realm, realm, url); } -Document::Document(JS::Realm& realm, const URL::URL& url) +JS::NonnullGCPtr Document::create_for_fragment_parsing(JS::Realm& realm) +{ + return realm.heap().allocate(realm, realm, "about:blank"sv, TemporaryDocumentForFragmentParsing::Yes); +} + +Document::Document(JS::Realm& realm, const URL::URL& url, TemporaryDocumentForFragmentParsing temporary_document_for_fragment_parsing) : ParentNode(realm, *this, NodeType::DOCUMENT_NODE) , m_page(Bindings::host_defined_page(realm)) , m_style_computer(make(*this)) , m_url(url) + , m_temporary_document_for_fragment_parsing(temporary_document_for_fragment_parsing) { m_legacy_platform_object_flags = PlatformObject::LegacyPlatformObjectFlags { .supports_named_properties = true, @@ -5253,7 +5259,7 @@ JS::NonnullGCPtr Document::parse_html_unsafe(JS::VM& vm, StringView ht // FIXME: 1. Let compliantHTML to the result of invoking the Get Trusted Type compliant string algorithm with TrustedHTML, this's relevant global object, html, "Document parseHTMLUnsafe", and "script". // 2. Let document be a new Document, whose content type is "text/html". - JS::NonnullGCPtr document = Document::create(realm); + auto document = Document::create_for_fragment_parsing(realm); document->set_content_type("text/html"_string); // 3. Set document's allow declarative shadow roots to true. diff --git a/Userland/Libraries/LibWeb/DOM/Document.h b/Userland/Libraries/LibWeb/DOM/Document.h index 61dbdc6e960..71ba86094a2 100644 --- a/Userland/Libraries/LibWeb/DOM/Document.h +++ b/Userland/Libraries/LibWeb/DOM/Document.h @@ -93,9 +93,15 @@ public: HTML }; + enum class TemporaryDocumentForFragmentParsing { + No, + Yes, + }; + static WebIDL::ExceptionOr> create_and_initialize(Type, String content_type, HTML::NavigationParams const&); [[nodiscard]] static JS::NonnullGCPtr create(JS::Realm&, URL::URL const& url = "about:blank"sv); + [[nodiscard]] static JS::NonnullGCPtr create_for_fragment_parsing(JS::Realm&); static WebIDL::ExceptionOr> construct_impl(JS::Realm&); virtual ~Document() override; @@ -447,8 +453,7 @@ 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; } + [[nodiscard]] bool is_temporary_document_for_fragment_parsing() const { return m_temporary_document_for_fragment_parsing == TemporaryDocumentForFragmentParsing::Yes; } static bool is_valid_name(String const&); @@ -684,7 +689,7 @@ protected: virtual void visit_edges(Cell::Visitor&) override; virtual void finalize() override; - Document(JS::Realm&, URL::URL const&); + Document(JS::Realm&, URL::URL const&, TemporaryDocumentForFragmentParsing = TemporaryDocumentForFragmentParsing::No); private: // ^HTML::GlobalEventHandlers @@ -889,7 +894,7 @@ private: RefPtr m_active_refresh_timer; - bool m_temporary_document_for_fragment_parsing { false }; + TemporaryDocumentForFragmentParsing m_temporary_document_for_fragment_parsing { TemporaryDocumentForFragmentParsing::No }; // https://html.spec.whatwg.org/multipage/browsing-the-web.html#latest-entry JS::GCPtr m_latest_entry; diff --git a/Userland/Libraries/LibWeb/HTML/Parser/HTMLParser.cpp b/Userland/Libraries/LibWeb/HTML/Parser/HTMLParser.cpp index bba2efa6644..e005c7f6213 100644 --- a/Userland/Libraries/LibWeb/HTML/Parser/HTMLParser.cpp +++ b/Userland/Libraries/LibWeb/HTML/Parser/HTMLParser.cpp @@ -4269,11 +4269,9 @@ DOM::Document& HTMLParser::document() Vector> HTMLParser::parse_html_fragment(DOM::Element& context_element, StringView markup, AllowDeclarativeShadowRoots allow_declarative_shadow_roots) { // 1. Create a new Document node, and mark it as being an HTML document. - auto temp_document = DOM::Document::create(context_element.realm()); + auto temp_document = DOM::Document::create_for_fragment_parsing(context_element.realm()); 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.