Selaa lähdekoodia

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.
Timothy Flynn 1 vuosi sitten
vanhempi
commit
c838ca78c8

+ 8 - 2
Userland/Libraries/LibWeb/DOM/Document.cpp

@@ -354,11 +354,17 @@ JS::NonnullGCPtr<Document> Document::create(JS::Realm& realm, URL::URL const& ur
     return realm.heap().allocate<Document>(realm, realm, url);
     return realm.heap().allocate<Document>(realm, realm, url);
 }
 }
 
 
-Document::Document(JS::Realm& realm, const URL::URL& url)
+JS::NonnullGCPtr<Document> Document::create_for_fragment_parsing(JS::Realm& realm)
+{
+    return realm.heap().allocate<Document>(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)
     : ParentNode(realm, *this, NodeType::DOCUMENT_NODE)
     , m_page(Bindings::host_defined_page(realm))
     , m_page(Bindings::host_defined_page(realm))
     , m_style_computer(make<CSS::StyleComputer>(*this))
     , m_style_computer(make<CSS::StyleComputer>(*this))
     , m_url(url)
     , m_url(url)
+    , m_temporary_document_for_fragment_parsing(temporary_document_for_fragment_parsing)
 {
 {
     m_legacy_platform_object_flags = PlatformObject::LegacyPlatformObjectFlags {
     m_legacy_platform_object_flags = PlatformObject::LegacyPlatformObjectFlags {
         .supports_named_properties = true,
         .supports_named_properties = true,
@@ -5253,7 +5259,7 @@ JS::NonnullGCPtr<Document> 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".
     // 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".
     // 2. Let document be a new Document, whose content type is "text/html".
-    JS::NonnullGCPtr<DOM::Document> document = Document::create(realm);
+    auto document = Document::create_for_fragment_parsing(realm);
     document->set_content_type("text/html"_string);
     document->set_content_type("text/html"_string);
 
 
     // 3. Set document's allow declarative shadow roots to true.
     // 3. Set document's allow declarative shadow roots to true.

+ 9 - 4
Userland/Libraries/LibWeb/DOM/Document.h

@@ -93,9 +93,15 @@ public:
         HTML
         HTML
     };
     };
 
 
+    enum class TemporaryDocumentForFragmentParsing {
+        No,
+        Yes,
+    };
+
     static WebIDL::ExceptionOr<JS::NonnullGCPtr<Document>> create_and_initialize(Type, String content_type, HTML::NavigationParams const&);
     static WebIDL::ExceptionOr<JS::NonnullGCPtr<Document>> create_and_initialize(Type, String content_type, HTML::NavigationParams const&);
 
 
     [[nodiscard]] static JS::NonnullGCPtr<Document> create(JS::Realm&, URL::URL const& url = "about:blank"sv);
     [[nodiscard]] static JS::NonnullGCPtr<Document> create(JS::Realm&, URL::URL const& url = "about:blank"sv);
+    [[nodiscard]] static JS::NonnullGCPtr<Document> create_for_fragment_parsing(JS::Realm&);
     static WebIDL::ExceptionOr<JS::NonnullGCPtr<Document>> construct_impl(JS::Realm&);
     static WebIDL::ExceptionOr<JS::NonnullGCPtr<Document>> construct_impl(JS::Realm&);
     virtual ~Document() override;
     virtual ~Document() override;
 
 
@@ -447,8 +453,7 @@ public:
     void set_parser(Badge<HTML::HTMLParser>, HTML::HTMLParser&);
     void set_parser(Badge<HTML::HTMLParser>, HTML::HTMLParser&);
     void detach_parser(Badge<HTML::HTMLParser>);
     void detach_parser(Badge<HTML::HTMLParser>);
 
 
-    void set_is_temporary_document_for_fragment_parsing(Badge<HTML::HTMLParser>) { 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&);
     static bool is_valid_name(String const&);
 
 
@@ -684,7 +689,7 @@ protected:
     virtual void visit_edges(Cell::Visitor&) override;
     virtual void visit_edges(Cell::Visitor&) override;
     virtual void finalize() override;
     virtual void finalize() override;
 
 
-    Document(JS::Realm&, URL::URL const&);
+    Document(JS::Realm&, URL::URL const&, TemporaryDocumentForFragmentParsing = TemporaryDocumentForFragmentParsing::No);
 
 
 private:
 private:
     // ^HTML::GlobalEventHandlers
     // ^HTML::GlobalEventHandlers
@@ -889,7 +894,7 @@ private:
 
 
     RefPtr<Core::Timer> m_active_refresh_timer;
     RefPtr<Core::Timer> 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
     // https://html.spec.whatwg.org/multipage/browsing-the-web.html#latest-entry
     JS::GCPtr<HTML::SessionHistoryEntry> m_latest_entry;
     JS::GCPtr<HTML::SessionHistoryEntry> m_latest_entry;

+ 1 - 3
Userland/Libraries/LibWeb/HTML/Parser/HTMLParser.cpp

@@ -4269,11 +4269,9 @@ DOM::Document& HTMLParser::document()
 Vector<JS::Handle<DOM::Node>> HTMLParser::parse_html_fragment(DOM::Element& context_element, StringView markup, AllowDeclarativeShadowRoots allow_declarative_shadow_roots)
 Vector<JS::Handle<DOM::Node>> 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.
     // 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_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.
     // 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, 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.
     //    Otherwise, leave the Document in no-quirks mode.