From 66ca4496c3e43a5636b0179adedf72f08ce8f560 Mon Sep 17 00:00:00 2001 From: Tim Ledbetter Date: Fri, 4 Jul 2025 20:29:03 +0100 Subject: [PATCH] LibWeb: Set Document origin for DOMParser created documents Previously, a crash would occur when accessing the origin of a document created with DOMParser. --- Libraries/LibWeb/HTML/DOMParser.cpp | 11 ++-- .../Node-cloneNode-document-with-doctype.txt | 8 +++ .../Node-cloneNode-document-with-doctype.html | 51 +++++++++++++++++++ 3 files changed, 67 insertions(+), 3 deletions(-) create mode 100644 Tests/LibWeb/Text/expected/wpt-import/dom/nodes/Node-cloneNode-document-with-doctype.txt create mode 100644 Tests/LibWeb/Text/input/wpt-import/dom/nodes/Node-cloneNode-document-with-doctype.html diff --git a/Libraries/LibWeb/HTML/DOMParser.cpp b/Libraries/LibWeb/HTML/DOMParser.cpp index 7327db31d2e..b1664fc4866 100644 --- a/Libraries/LibWeb/HTML/DOMParser.cpp +++ b/Libraries/LibWeb/HTML/DOMParser.cpp @@ -43,18 +43,18 @@ GC::Ref DOMParser::parse_from_string(StringView string, Bindings: // 2. Let document be a new Document, whose content type is type and url is this's relevant global object's associated Document's URL. GC::Ptr document; + auto& associated_document = as(relevant_global_object(*this)).associated_document(); // 3. Switch on type: if (type == Bindings::DOMParserSupportedType::Text_Html) { // -> "text/html" - document = HTML::HTMLDocument::create(realm(), as(relevant_global_object(*this)).associated_document().url()); - document->set_content_type(Bindings::idl_enum_to_string(type)); + document = HTML::HTMLDocument::create(realm(), associated_document.url()); // 1. Parse HTML from a string given document and compliantString. FIXME: Use compliantString. document->parse_html_from_a_string(string); } else { // -> Otherwise - document = DOM::XMLDocument::create(realm(), as(relevant_global_object(*this)).associated_document().url()); + document = DOM::XMLDocument::create(realm(), associated_document.url()); document->set_content_type(Bindings::idl_enum_to_string(type)); document->set_document_type(DOM::Document::Type::XML); @@ -76,6 +76,11 @@ GC::Ref DOMParser::parse_from_string(StringView string, Bindings: } } + // AD-HOC: Setting the origin to match that of the associated document matches the behavior of existing browsers + // and avoids a crash, since we expect the origin to always be set. + // Spec issue: https://github.com/whatwg/html/issues/11429 + document->set_origin(associated_document.origin()); + // 3. Return document. return *document; } diff --git a/Tests/LibWeb/Text/expected/wpt-import/dom/nodes/Node-cloneNode-document-with-doctype.txt b/Tests/LibWeb/Text/expected/wpt-import/dom/nodes/Node-cloneNode-document-with-doctype.txt new file mode 100644 index 00000000000..211ce61d40e --- /dev/null +++ b/Tests/LibWeb/Text/expected/wpt-import/dom/nodes/Node-cloneNode-document-with-doctype.txt @@ -0,0 +1,8 @@ +Harness status: OK + +Found 3 tests + +3 Pass +Pass Created with the createDocument/createDocumentType +Pass Created with the createHTMLDocument +Pass Created with DOMParser \ No newline at end of file diff --git a/Tests/LibWeb/Text/input/wpt-import/dom/nodes/Node-cloneNode-document-with-doctype.html b/Tests/LibWeb/Text/input/wpt-import/dom/nodes/Node-cloneNode-document-with-doctype.html new file mode 100644 index 00000000000..76b52b93f02 --- /dev/null +++ b/Tests/LibWeb/Text/input/wpt-import/dom/nodes/Node-cloneNode-document-with-doctype.html @@ -0,0 +1,51 @@ + + +Cloning of a document with a doctype + + + + + + +