Skip to content

Truly independent web browser

LibWeb: Account for a cloned document's base URL

Shannon Boothb3a9ceb902d1
diff --git a/Libraries/LibWeb/DOM/Document.cpp b/Libraries/LibWeb/DOM/Document.cpp
index d8a1a2269f7..31ea9294b09 100644
--- a/Libraries/LibWeb/DOM/Document.cpp
+++ b/Libraries/LibWeb/DOM/Document.cpp
@@ -1593,6 +1593,17 @@ bool Document::is_child_allowed(Node const& node) const
}
}
+// https://html.spec.whatwg.org/multipage/dom.html#the-document-object:concept-node-clone-ext
+WebIDL::ExceptionOr<void> Document::cloned(Node& copy, bool subtree) const
+{
+ TRY(Base::cloned(copy, subtree));
+
+ // The cloning steps for Document nodes given node, copy, and subtree are to set copy's about base URL to node's
+ // about base URL.
+ as<Document>(copy).set_about_base_url(m_about_base_url);
+ return {};
+}
+
Element* Document::document_element()
{
return first_child_of_type<Element>();
@@ -2048,37 +2059,37 @@ void Document::set_url(URL::URL const& url)
// https://html.spec.whatwg.org/multipage/urls-and-fetching.html#fallback-base-url
URL::URL Document::fallback_base_url() const
{
- // 1. If document is an iframe srcdoc document, then:
- if (HTML::url_matches_about_srcdoc(m_url)) {
- // 1. Assert: document's about base URL is non-null.
- // AD-HOC: Documents created by DOMParser or by cloning can have a URL that matches about:srcdoc
- // without an about base URL being set. Returning the document's URL in this case matches
- // the behavior of other engines.
- if (!m_about_base_url.has_value())
- return m_url;
-
- // 2. Return document's about base URL.
+ // 1. If document's URL matches about:blank or matches about:srcdoc, and document's about base URL is non-null,
+ // then return document's about base URL.
+ if ((HTML::url_matches_about_blank(m_url) || HTML::url_matches_about_srcdoc(m_url)) && m_about_base_url.has_value())
return m_about_base_url.value();
- }
- // 2. If document's URL matches about:blank and document's about base URL is non-null, then return document's about base URL.
- if (HTML::url_matches_about_blank(m_url) && m_about_base_url.has_value())
- return m_about_base_url.value();
-
- // 3. Return document's URL.
+ // 2. Return document's URL.
return m_url;
}
// https://html.spec.whatwg.org/multipage/urls-and-fetching.html#document-base-url
URL::URL Document::base_url() const
{
- // 1. If document has no descendant base element that has an href attribute, then return document's fallback base URL.
- auto base_element = first_base_element_with_href_in_tree_order();
- if (!base_element)
- return fallback_base_url();
+ // 1. If document has a descendant base element that has an href attribute, then return the frozen base URL of the
+ // first such base element in tree order.
+ if (auto base_element = first_base_element_with_href_in_tree_order())
+ return base_element->frozen_base_url();
+
+ // 2. If document's base URL override is non-null, then return document's base URL override.
+ if (m_base_url_override.has_value())
+ return m_base_url_override.value();
- // 2. Otherwise, return the frozen base URL of the first base element in document that has an href attribute, in tree order.
- return base_element->frozen_base_url();
+ // 3. Return document's fallback base URL.
+ return fallback_base_url();
+}
+
+// https://dom.spec.whatwg.org/#get-the-base-url
+Optional<URL::URL> Document::get_the_base_url() const
+{
+ // https://html.spec.whatwg.org/multipage/urls-and-fetching.html#get-the-base-url
+ // A Document document's get the base URL algorithm returns document's document base URL.
+ return base_url();
}
String const& Document::serialized_url() const
diff --git a/Libraries/LibWeb/DOM/Document.h b/Libraries/LibWeb/DOM/Document.h
index 05212ac70f2..b2b4bf39950 100644
--- a/Libraries/LibWeb/DOM/Document.h
+++ b/Libraries/LibWeb/DOM/Document.h
@@ -380,6 +380,7 @@ public:
URL::URL url() const { return m_url; }
URL::URL fallback_base_url() const;
URL::URL base_url() const;
+ Optional<URL::URL> get_the_base_url() const;
String const& serialized_url() const;
String const& serialized_base_url() const;
@@ -621,6 +622,7 @@ public:
void tear_down_layout_tree_for_svg_image_document(Badge<SVG::SVGDecodedImageData>);
virtual bool is_child_allowed(Node const&) const override;
+ virtual WebIDL::ExceptionOr<void> cloned(Node&, bool) const override;
Layout::Viewport const* layout_node(Layout::BegunRead const& read) const;
Layout::Viewport* layout_node(Layout::BegunRead const& read);
@@ -1027,6 +1029,13 @@ public:
m_serialized_base_url.clear();
}
+ // https://dom.spec.whatwg.org/#document-base-url-override
+ void set_base_url_override(Optional<URL::URL> url)
+ {
+ m_base_url_override = move(url);
+ m_serialized_base_url.clear();
+ }
+
Utf16String domain() const;
WebIDL::ExceptionOr<void> set_domain(Utf16View);
@@ -2023,6 +2032,9 @@ private:
// https://html.spec.whatwg.org/multipage/dom.html#concept-document-about-base-url
Optional<URL::URL> m_about_base_url;
+ // https://dom.spec.whatwg.org/#document-base-url-override
+ Optional<URL::URL> m_base_url_override;
+
// https://html.spec.whatwg.org/multipage/dom.html#concept-document-coop
HTML::OpenerPolicy m_opener_policy;
diff --git a/Libraries/LibWeb/DOM/Node.cpp b/Libraries/LibWeb/DOM/Node.cpp
index 0f52e270134..09ed85c8751 100644
--- a/Libraries/LibWeb/DOM/Node.cpp
+++ b/Libraries/LibWeb/DOM/Node.cpp
@@ -382,8 +382,15 @@ size_t Node::external_memory_size() const
// https://dom.spec.whatwg.org/#dom-node-baseuri
Utf16String Node::base_uri() const
{
- // Return this’s node document’s document base URL, serialized.
- return utf16_string_from_url_ascii(document().base_url().to_string());
+ // 1. Let baseURL be the result of running this’s node document’s get the base URL algorithm.
+ auto base_url = document().get_the_base_url();
+
+ // 2. If baseURL is null, then set baseURL to this’s node document’s URL.
+ if (!base_url.has_value())
+ base_url = document().url();
+
+ // 3. Return baseURL, serialized.
+ return utf16_string_from_url_ascii(base_url->serialize());
}
HTML::HTMLHyperlinkElementUtils const* Node::enclosing_link_element() const
@@ -2282,7 +2289,10 @@ WebIDL::ExceptionOr<GC::Ref<Node>> Node::clone_single_node(Document& document, G
document_copy->set_quirks_mode(document_.mode());
document_copy->set_allow_declarative_shadow_roots(document_.allow_declarative_shadow_roots());
- // 2. If node’s custom element registry’s is scoped is true, then set copy’s custom element registry to
+ // 2. Set copy’s base URL override to the result of running node’s get the base URL.
+ document_copy->set_base_url_override(document_.get_the_base_url());
+
+ // 3. If node’s custom element registry’s is scoped is true, then set copy’s custom element registry to
// node’s custom element registry.
if (auto registry = document_.custom_element_registry(); registry && registry->is_scoped())
document_copy->set_custom_element_registry(registry);
diff --git a/Tests/LibWeb/Text/expected/wpt-import/dom/nodes/Document-cloneNode-about-base-url-relative.window.txt b/Tests/LibWeb/Text/expected/wpt-import/dom/nodes/Document-cloneNode-about-base-url-relative.window.txt
new file mode 100644
index 00000000000..0d3608a687e
--- /dev/null
+++ b/Tests/LibWeb/Text/expected/wpt-import/dom/nodes/Document-cloneNode-about-base-url-relative.window.txt
@@ -0,0 +1,9 @@
+Harness status: OK
+
+Found 4 tests
+
+4 Pass
+Pass A relative <base> in an about:blank document resolves against the inherited base URL
+Pass Clone of an about:blank document with a relative <base>
+Pass A relative <base> inserted into a shallow clone of an about:blank document resolves against the inherited base URL
+Pass A data: <base> in an about:blank document and its clone falls back to the inherited base URL
\ No newline at end of file
diff --git a/Tests/LibWeb/Text/expected/wpt-import/dom/nodes/Document-cloneNode-about-base-url.window.txt b/Tests/LibWeb/Text/expected/wpt-import/dom/nodes/Document-cloneNode-about-base-url.window.txt
new file mode 100644
index 00000000000..8cddc18ef6e
--- /dev/null
+++ b/Tests/LibWeb/Text/expected/wpt-import/dom/nodes/Document-cloneNode-about-base-url.window.txt
@@ -0,0 +1,10 @@
+Harness status: OK
+
+Found 5 tests
+
+5 Pass
+Pass An about:blank document inherits the initiator base URL
+Pass Deep clone of an about:blank document
+Pass Shallow clone of an about:blank document
+Pass Clone of an about:blank document after detaching the source
+Pass Clone of an about:blank document with a <base>
\ No newline at end of file
diff --git a/Tests/LibWeb/Text/expected/wpt-import/dom/nodes/Document-cloneNode-base-url-xml.window.txt b/Tests/LibWeb/Text/expected/wpt-import/dom/nodes/Document-cloneNode-base-url-xml.window.txt
new file mode 100644
index 00000000000..d9ca0e184be
--- /dev/null
+++ b/Tests/LibWeb/Text/expected/wpt-import/dom/nodes/Document-cloneNode-base-url-xml.window.txt
@@ -0,0 +1,8 @@
+Harness status: OK
+
+Found 3 tests
+
+3 Pass
+Pass Clone of an XML document ignores a non-HTML <base> element
+Pass Clone of an XHTML document with a <base>
+Pass Clone of an XML document without a <base>
\ No newline at end of file
diff --git a/Tests/LibWeb/Text/expected/wpt-import/dom/nodes/Document-cloneNode-base-url.window.txt b/Tests/LibWeb/Text/expected/wpt-import/dom/nodes/Document-cloneNode-base-url.window.txt
new file mode 100644
index 00000000000..b590120025c
--- /dev/null
+++ b/Tests/LibWeb/Text/expected/wpt-import/dom/nodes/Document-cloneNode-base-url.window.txt
@@ -0,0 +1,16 @@
+Harness status: OK
+
+Found 11 tests
+
+11 Pass
+Pass Deep clone of a document with a <base>
+Pass Shallow clone of a document with a <base>
+Pass Clone of a document without a <base>
+Pass Clone of a createHTMLDocument() document without a <base>
+Pass Clone of a createHTMLDocument() document with a <base>
+Pass Deep clone of a document with a relative <base>
+Pass Shallow clone of a document with a relative <base>
+Pass Shallow clone is unaffected by later mutation of the source
+Pass Shallow clone's base URL survives later mutation of the clone
+Pass Inserting a <base> into a shallow clone overrides the copied base URL
+Pass Clone of a blob: URL document
\ No newline at end of file
diff --git a/Tests/LibWeb/Text/expected/wpt-import/dom/nodes/Document-cloneNode-srcdoc-base-url.window.txt b/Tests/LibWeb/Text/expected/wpt-import/dom/nodes/Document-cloneNode-srcdoc-base-url.window.txt
new file mode 100644
index 00000000000..4717551bfd6
--- /dev/null
+++ b/Tests/LibWeb/Text/expected/wpt-import/dom/nodes/Document-cloneNode-srcdoc-base-url.window.txt
@@ -0,0 +1,9 @@
+Harness status: OK
+
+Found 4 tests
+
+4 Pass
+Pass A relative <base> in an about:srcdoc document resolves against the inherited base URL
+Pass Clone of an about:srcdoc document with a relative <base>
+Pass A relative <base> inserted into a shallow clone of an about:srcdoc document resolves against the inherited base URL
+Pass A data: <base> in an about:srcdoc document and its clone falls back to the inherited base URL
\ No newline at end of file
diff --git a/Tests/LibWeb/Text/expected/wpt-import/domparsing/DOMParser-parseFromString-url-base-about.window.txt b/Tests/LibWeb/Text/expected/wpt-import/domparsing/DOMParser-parseFromString-url-base-about.window.txt
new file mode 100644
index 00000000000..76b7bf4f293
--- /dev/null
+++ b/Tests/LibWeb/Text/expected/wpt-import/domparsing/DOMParser-parseFromString-url-base-about.window.txt
@@ -0,0 +1,7 @@
+Harness status: OK
+
+Found 2 tests
+
+2 Pass
+Pass DOMParser in an about:blank document
+Pass DOMParser in an about:srcdoc document
\ No newline at end of file
diff --git a/Tests/LibWeb/Text/input/wpt-import/dom/nodes/Document-cloneNode-about-base-url-relative.window.html b/Tests/LibWeb/Text/input/wpt-import/dom/nodes/Document-cloneNode-about-base-url-relative.window.html
new file mode 100644
index 00000000000..372404b5ae2
--- /dev/null
+++ b/Tests/LibWeb/Text/input/wpt-import/dom/nodes/Document-cloneNode-about-base-url-relative.window.html
@@ -0,0 +1,8 @@
+<!doctype html>
+<meta charset=utf-8>
+
+<script src="../../resources/testharness.js"></script>
+<script src="../../resources/testharnessreport.js"></script>
+
+<div id=log></div>
+<script src="../../dom/nodes/Document-cloneNode-about-base-url-relative.window.js"></script>
diff --git a/Tests/LibWeb/Text/input/wpt-import/dom/nodes/Document-cloneNode-about-base-url-relative.window.js b/Tests/LibWeb/Text/input/wpt-import/dom/nodes/Document-cloneNode-about-base-url-relative.window.js
new file mode 100644
index 00000000000..f5e143fd8bf
--- /dev/null
+++ b/Tests/LibWeb/Text/input/wpt-import/dom/nodes/Document-cloneNode-about-base-url-relative.window.js
@@ -0,0 +1,61 @@
+// See https://github.com/whatwg/dom/issues/454
+
+function aboutBlankDocument(t) {
+ return new Promise(resolve => {
+ const iframe = document.createElement("iframe");
+ iframe.src = "about:blank";
+ iframe.onload = () => resolve(iframe);
+ t.add_cleanup(() => iframe.remove());
+ document.body.appendChild(iframe);
+ });
+}
+
+promise_test(async t => {
+ const doc = (await aboutBlankDocument(t)).contentDocument;
+ const expected = new URL("sub/", doc.baseURI).href;
+
+ const base = doc.createElement("base");
+ base.setAttribute("href", "sub/");
+ doc.head.appendChild(base);
+ assert_equals(doc.baseURI, expected);
+}, "A relative <base> in an about:blank document resolves against the inherited base URL");
+
+promise_test(async t => {
+ const doc = (await aboutBlankDocument(t)).contentDocument;
+ const expected = new URL("sub/", doc.baseURI).href;
+
+ const base = doc.createElement("base");
+ base.setAttribute("href", "sub/");
+ doc.head.appendChild(base);
+
+ assert_equals(doc.cloneNode(true).baseURI, expected, "deep");
+ assert_equals(doc.cloneNode(false).baseURI, expected, "shallow");
+}, "Clone of an about:blank document with a relative <base>");
+
+promise_test(async t => {
+ const doc = (await aboutBlankDocument(t)).contentDocument;
+ const expected = new URL("sub/", doc.baseURI).href;
+
+ const shallow = doc.cloneNode(false);
+ const base = shallow.createElement("base");
+ base.setAttribute("href", "sub/");
+ shallow.appendChild(base);
+ assert_equals(shallow.baseURI, expected);
+}, "A relative <base> inserted into a shallow clone of an about:blank document resolves against the inherited base URL");
+
+promise_test(async t => {
+ const doc = (await aboutBlankDocument(t)).contentDocument;
+ const inherited = doc.baseURI;
+
+ const base = doc.createElement("base");
+ base.setAttribute("href", "https://example.com/");
+ doc.head.appendChild(base);
+ const deep = doc.cloneNode(true);
+ assert_equals(deep.baseURI, "https://example.com/", "deep clone before");
+
+ for (const d of [doc, deep]) {
+ d.querySelector("base").setAttribute("href", "data:/,test");
+ }
+ assert_equals(doc.baseURI, inherited, "source");
+ assert_equals(deep.baseURI, inherited, "deep clone");
+}, "A data: <base> in an about:blank document and its clone falls back to the inherited base URL");
diff --git a/Tests/LibWeb/Text/input/wpt-import/dom/nodes/Document-cloneNode-about-base-url.window.html b/Tests/LibWeb/Text/input/wpt-import/dom/nodes/Document-cloneNode-about-base-url.window.html
new file mode 100644
index 00000000000..b9fa993b347
--- /dev/null
+++ b/Tests/LibWeb/Text/input/wpt-import/dom/nodes/Document-cloneNode-about-base-url.window.html
@@ -0,0 +1,8 @@
+<!doctype html>
+<meta charset=utf-8>
+
+<script src="../../resources/testharness.js"></script>
+<script src="../../resources/testharnessreport.js"></script>
+
+<div id=log></div>
+<script src="../../dom/nodes/Document-cloneNode-about-base-url.window.js"></script>
diff --git a/Tests/LibWeb/Text/input/wpt-import/dom/nodes/Document-cloneNode-about-base-url.window.js b/Tests/LibWeb/Text/input/wpt-import/dom/nodes/Document-cloneNode-about-base-url.window.js
new file mode 100644
index 00000000000..3b8041f2a15
--- /dev/null
+++ b/Tests/LibWeb/Text/input/wpt-import/dom/nodes/Document-cloneNode-about-base-url.window.js
@@ -0,0 +1,65 @@
+// An about:blank document inherits its initiator's base URL. Cloning copies the
+// computed base URL, so the clone reports the same value.
+// See https://github.com/whatwg/dom/issues/454
+
+const BASE_HREF = "http://example.com/foo/bar";
+
+function aboutBlankDocument(t) {
+ return new Promise(resolve => {
+ const iframe = document.createElement("iframe");
+ iframe.src = "about:blank";
+ iframe.onload = () => resolve(iframe);
+ t.add_cleanup(() => iframe.remove());
+ document.body.appendChild(iframe);
+ });
+}
+
+promise_test(async t => {
+ const doc = (await aboutBlankDocument(t)).contentDocument;
+ assert_equals(doc.URL, "about:blank", "URL");
+ assert_equals(doc.baseURI, document.baseURI, "inherited base URL");
+}, "An about:blank document inherits the initiator base URL");
+
+promise_test(async t => {
+ const doc = (await aboutBlankDocument(t)).contentDocument;
+ const expected = doc.baseURI;
+
+ const deep = doc.cloneNode(true);
+ assert_equals(deep.URL, "about:blank", "URL");
+ assert_equals(deep.baseURI, expected, "baseURI");
+}, "Deep clone of an about:blank document");
+
+promise_test(async t => {
+ const doc = (await aboutBlankDocument(t)).contentDocument;
+ const expected = doc.baseURI;
+
+ const shallow = doc.cloneNode(false);
+ assert_equals(shallow.URL, "about:blank", "URL");
+ assert_equals(shallow.baseURI, expected, "baseURI");
+}, "Shallow clone of an about:blank document");
+
+// The inherited base URL is a snapshot, not a live lookup: the clone keeps it
+// after the source iframe is detached.
+promise_test(async t => {
+ const iframe = await aboutBlankDocument(t);
+ const doc = iframe.contentDocument;
+ const expected = doc.baseURI;
+
+ const shallow = doc.cloneNode(false);
+ const deep = doc.cloneNode(true);
+ iframe.remove();
+
+ assert_equals(shallow.baseURI, expected, "shallow");
+ assert_equals(deep.baseURI, expected, "deep");
+}, "Clone of an about:blank document after detaching the source");
+
+promise_test(async t => {
+ const doc = (await aboutBlankDocument(t)).contentDocument;
+ const base = doc.createElement("base");
+ base.setAttribute("href", BASE_HREF);
+ doc.head.appendChild(base);
+ assert_equals(doc.baseURI, BASE_HREF, "source");
+
+ assert_equals(doc.cloneNode(true).baseURI, BASE_HREF, "deep");
+ assert_equals(doc.cloneNode(false).baseURI, BASE_HREF, "shallow");
+}, "Clone of an about:blank document with a <base>");
diff --git a/Tests/LibWeb/Text/input/wpt-import/dom/nodes/Document-cloneNode-base-url-xml.window.html b/Tests/LibWeb/Text/input/wpt-import/dom/nodes/Document-cloneNode-base-url-xml.window.html
new file mode 100644
index 00000000000..cef113cd143
--- /dev/null
+++ b/Tests/LibWeb/Text/input/wpt-import/dom/nodes/Document-cloneNode-base-url-xml.window.html
@@ -0,0 +1,8 @@
+<!doctype html>
+<meta charset=utf-8>
+
+<script src="../../resources/testharness.js"></script>
+<script src="../../resources/testharnessreport.js"></script>
+
+<div id=log></div>
+<script src="../../dom/nodes/Document-cloneNode-base-url-xml.window.js"></script>
diff --git a/Tests/LibWeb/Text/input/wpt-import/dom/nodes/Document-cloneNode-base-url-xml.window.js b/Tests/LibWeb/Text/input/wpt-import/dom/nodes/Document-cloneNode-base-url-xml.window.js
new file mode 100644
index 00000000000..2102982b704
--- /dev/null
+++ b/Tests/LibWeb/Text/input/wpt-import/dom/nodes/Document-cloneNode-base-url-xml.window.js
@@ -0,0 +1,33 @@
+// There is no xml:base: only an HTML <base> element (including one in the XHTML
+// namespace) contributes to the document base URL.
+// See https://github.com/whatwg/dom/issues/454
+
+const BASE_HREF = "http://example.com/foo/bar";
+
+test(() => {
+ const doc = new DOMParser().parseFromString(
+ `<root><base href="${BASE_HREF}"/></root>`, "application/xml");
+ assert_equals(doc.baseURI, doc.URL, "source");
+
+ assert_equals(doc.cloneNode(true).baseURI, doc.URL, "deep");
+ assert_equals(doc.cloneNode(false).baseURI, doc.URL, "shallow");
+}, "Clone of an XML document ignores a non-HTML <base> element");
+
+test(() => {
+ const doc = new DOMParser().parseFromString(
+ `<html xmlns="http://www.w3.org/1999/xhtml">` +
+ `<head><base href="${BASE_HREF}"/></head><body></body></html>`,
+ "application/xhtml+xml");
+ assert_equals(doc.baseURI, BASE_HREF, "source");
+
+ assert_equals(doc.cloneNode(true).baseURI, BASE_HREF, "deep");
+ assert_equals(doc.cloneNode(false).baseURI, BASE_HREF, "shallow");
+}, "Clone of an XHTML document with a <base>");
+
+test(() => {
+ const doc = new DOMParser().parseFromString("<root/>", "application/xml");
+ assert_equals(doc.baseURI, doc.URL, "source");
+
+ assert_equals(doc.cloneNode(true).baseURI, doc.URL, "deep");
+ assert_equals(doc.cloneNode(false).baseURI, doc.URL, "shallow");
+}, "Clone of an XML document without a <base>");
diff --git a/Tests/LibWeb/Text/input/wpt-import/dom/nodes/Document-cloneNode-base-url.window.html b/Tests/LibWeb/Text/input/wpt-import/dom/nodes/Document-cloneNode-base-url.window.html
new file mode 100644
index 00000000000..201fa601dda
--- /dev/null
+++ b/Tests/LibWeb/Text/input/wpt-import/dom/nodes/Document-cloneNode-base-url.window.html
@@ -0,0 +1,8 @@
+<!doctype html>
+<meta charset=utf-8>
+
+<script src="../../resources/testharness.js"></script>
+<script src="../../resources/testharnessreport.js"></script>
+
+<div id=log></div>
+<script src="../../dom/nodes/Document-cloneNode-base-url.window.js"></script>
diff --git a/Tests/LibWeb/Text/input/wpt-import/dom/nodes/Document-cloneNode-base-url.window.js b/Tests/LibWeb/Text/input/wpt-import/dom/nodes/Document-cloneNode-base-url.window.js
new file mode 100644
index 00000000000..629ce603fd4
--- /dev/null
+++ b/Tests/LibWeb/Text/input/wpt-import/dom/nodes/Document-cloneNode-base-url.window.js
@@ -0,0 +1,121 @@
+// Cloning a document copies its computed base URL. A shallow clone therefore
+// reports the same base URL as the source even though it has no <base> element.
+// See https://github.com/whatwg/dom/issues/454
+
+const BASE_HREF = "http://example.com/foo/bar";
+
+test(() => {
+ const doc = new DOMParser().parseFromString(
+ `<!DOCTYPE html><base href="${BASE_HREF}">`, "text/html");
+ assert_equals(doc.baseURI, BASE_HREF, "source");
+
+ const deep = doc.cloneNode(true);
+ assert_equals(deep.URL, doc.URL, "URL");
+ assert_equals(deep.baseURI, BASE_HREF, "baseURI");
+}, "Deep clone of a document with a <base>");
+
+test(() => {
+ const doc = new DOMParser().parseFromString(
+ `<!DOCTYPE html><base href="${BASE_HREF}">`, "text/html");
+ assert_equals(doc.baseURI, BASE_HREF, "source");
+
+ const shallow = doc.cloneNode(false);
+ assert_equals(shallow.URL, doc.URL, "URL");
+ assert_equals(shallow.baseURI, BASE_HREF, "baseURI");
+}, "Shallow clone of a document with a <base>");
+
+test(() => {
+ const doc = new DOMParser().parseFromString("<!DOCTYPE html>", "text/html");
+ assert_equals(doc.baseURI, doc.URL, "source");
+
+ assert_equals(doc.cloneNode(true).baseURI, doc.URL, "deep");
+ assert_equals(doc.cloneNode(false).baseURI, doc.URL, "shallow");
+}, "Clone of a document without a <base>");
+
+test(() => {
+ const doc = document.implementation.createHTMLDocument("");
+ assert_equals(doc.URL, "about:blank", "URL");
+ assert_equals(doc.baseURI, "about:blank", "source");
+
+ assert_equals(doc.cloneNode(true).baseURI, "about:blank", "deep");
+ assert_equals(doc.cloneNode(false).baseURI, "about:blank", "shallow");
+}, "Clone of a createHTMLDocument() document without a <base>");
+
+test(() => {
+ const doc = document.implementation.createHTMLDocument("");
+ const base = doc.createElement("base");
+ base.setAttribute("href", BASE_HREF);
+ doc.head.appendChild(base);
+ assert_equals(doc.baseURI, BASE_HREF, "source");
+
+ assert_equals(doc.cloneNode(true).baseURI, BASE_HREF, "deep");
+ assert_equals(doc.cloneNode(false).baseURI, BASE_HREF, "shallow");
+}, "Clone of a createHTMLDocument() document with a <base>");
+
+// A relative <base> must resolve exactly once, against the copied URL, rather
+// than a second time against the source's already-computed base URL.
+test(() => {
+ const doc = new DOMParser().parseFromString(
+ `<!DOCTYPE html><base href="sub/">`, "text/html");
+ const expected = new URL("sub/", doc.URL).href;
+ assert_equals(doc.baseURI, expected, "source");
+ assert_equals(doc.cloneNode(true).baseURI, expected, "baseURI");
+}, "Deep clone of a document with a relative <base>");
+
+test(() => {
+ const doc = new DOMParser().parseFromString(
+ `<!DOCTYPE html><base href="sub/">`, "text/html");
+ const expected = new URL("sub/", doc.URL).href;
+ assert_equals(doc.baseURI, expected, "source");
+ assert_equals(doc.cloneNode(false).baseURI, expected, "baseURI");
+}, "Shallow clone of a document with a relative <base>");
+
+test(() => {
+ const doc = new DOMParser().parseFromString(
+ `<!DOCTYPE html><base href="${BASE_HREF}">`, "text/html");
+ const shallow = doc.cloneNode(false);
+
+ doc.querySelector("base").remove();
+ assert_equals(doc.baseURI, doc.URL, "source updated");
+ assert_equals(shallow.baseURI, BASE_HREF, "clone unchanged");
+}, "Shallow clone is unaffected by later mutation of the source");
+
+test(() => {
+ const doc = new DOMParser().parseFromString(
+ `<!DOCTYPE html><base href="${BASE_HREF}">`, "text/html");
+ const shallow = doc.cloneNode(false);
+
+ shallow.appendChild(shallow.createComment("mutate"));
+ assert_equals(shallow.baseURI, BASE_HREF);
+}, "Shallow clone's base URL survives later mutation of the clone");
+
+test(() => {
+ const doc = new DOMParser().parseFromString(
+ `<!DOCTYPE html><base href="${BASE_HREF}">`, "text/html");
+ const shallow = doc.cloneNode(false);
+ assert_equals(shallow.baseURI, BASE_HREF, "before inserting <base>");
+
+ const base = shallow.createElement("base");
+ base.setAttribute("href", "http://example.org/other/");
+ shallow.appendChild(base);
+ assert_equals(shallow.baseURI, "http://example.org/other/",
+ "an inserted <base> wins over the copied base URL");
+}, "Inserting a <base> into a shallow clone overrides the copied base URL");
+
+async_test(t => {
+ const blobURL = URL.createObjectURL(
+ new Blob(["<!doctype html>"], { type: "text/html" }));
+ t.add_cleanup(() => URL.revokeObjectURL(blobURL));
+
+ const iframe = document.createElement("iframe");
+ iframe.src = blobURL;
+ iframe.onload = t.step_func_done(() => {
+ const doc = iframe.contentDocument;
+ assert_equals(doc.baseURI, blobURL, "source");
+
+ assert_equals(doc.cloneNode(true).baseURI, blobURL, "deep");
+ assert_equals(doc.cloneNode(false).baseURI, blobURL, "shallow");
+ iframe.remove();
+ });
+ document.body.appendChild(iframe);
+}, "Clone of a blob: URL document");
diff --git a/Tests/LibWeb/Text/input/wpt-import/dom/nodes/Document-cloneNode-srcdoc-base-url.window.html b/Tests/LibWeb/Text/input/wpt-import/dom/nodes/Document-cloneNode-srcdoc-base-url.window.html
new file mode 100644
index 00000000000..581e735bda7
--- /dev/null
+++ b/Tests/LibWeb/Text/input/wpt-import/dom/nodes/Document-cloneNode-srcdoc-base-url.window.html
@@ -0,0 +1,8 @@
+<!doctype html>
+<meta charset=utf-8>
+
+<script src="../../resources/testharness.js"></script>
+<script src="../../resources/testharnessreport.js"></script>
+
+<div id=log></div>
+<script src="../../dom/nodes/Document-cloneNode-srcdoc-base-url.window.js"></script>
diff --git a/Tests/LibWeb/Text/input/wpt-import/dom/nodes/Document-cloneNode-srcdoc-base-url.window.js b/Tests/LibWeb/Text/input/wpt-import/dom/nodes/Document-cloneNode-srcdoc-base-url.window.js
new file mode 100644
index 00000000000..93b3658d8d0
--- /dev/null
+++ b/Tests/LibWeb/Text/input/wpt-import/dom/nodes/Document-cloneNode-srcdoc-base-url.window.js
@@ -0,0 +1,46 @@
+// See https://github.com/whatwg/dom/issues/454
+
+function srcdocDocument(t, srcdoc) {
+ return new Promise(resolve => {
+ const iframe = document.createElement("iframe");
+ iframe.srcdoc = srcdoc;
+ iframe.onload = () => resolve(iframe);
+ t.add_cleanup(() => iframe.remove());
+ document.body.appendChild(iframe);
+ });
+}
+
+promise_test(async t => {
+ const doc = (await srcdocDocument(t, "<base href='sub/'>x")).contentDocument;
+ assert_equals(doc.baseURI, new URL("sub/", document.baseURI).href);
+}, "A relative <base> in an about:srcdoc document resolves against the inherited base URL");
+
+promise_test(async t => {
+ const doc = (await srcdocDocument(t, "<base href='sub/'>x")).contentDocument;
+ const expected = new URL("sub/", document.baseURI).href;
+
+ assert_equals(doc.cloneNode(true).baseURI, expected, "deep");
+ assert_equals(doc.cloneNode(false).baseURI, expected, "shallow");
+}, "Clone of an about:srcdoc document with a relative <base>");
+
+promise_test(async t => {
+ const doc = (await srcdocDocument(t, "x")).contentDocument;
+
+ const shallow = doc.cloneNode(false);
+ const base = shallow.createElement("base");
+ base.setAttribute("href", "sub/");
+ shallow.appendChild(base);
+ assert_equals(shallow.baseURI, new URL("sub/", document.baseURI).href);
+}, "A relative <base> inserted into a shallow clone of an about:srcdoc document resolves against the inherited base URL");
+
+promise_test(async t => {
+ const doc = (await srcdocDocument(t, "<base href='https://example.com/'>x")).contentDocument;
+ const deep = doc.cloneNode(true);
+ assert_equals(deep.baseURI, "https://example.com/", "deep clone before");
+
+ for (const d of [doc, deep]) {
+ d.querySelector("base").setAttribute("href", "data:/,test");
+ }
+ assert_equals(doc.baseURI, document.baseURI, "source");
+ assert_equals(deep.baseURI, document.baseURI, "deep clone");
+}, "A data: <base> in an about:srcdoc document and its clone falls back to the inherited base URL");
diff --git a/Tests/LibWeb/Text/input/wpt-import/domparsing/DOMParser-parseFromString-url-base-about.window.html b/Tests/LibWeb/Text/input/wpt-import/domparsing/DOMParser-parseFromString-url-base-about.window.html
new file mode 100644
index 00000000000..72bc6a5340f
--- /dev/null
+++ b/Tests/LibWeb/Text/input/wpt-import/domparsing/DOMParser-parseFromString-url-base-about.window.html
@@ -0,0 +1,8 @@
+<!doctype html>
+<meta charset=utf-8>
+
+<script src="../resources/testharness.js"></script>
+<script src="../resources/testharnessreport.js"></script>
+
+<div id=log></div>
+<script src="../domparsing/DOMParser-parseFromString-url-base-about.window.js"></script>
diff --git a/Tests/LibWeb/Text/input/wpt-import/domparsing/DOMParser-parseFromString-url-base-about.window.js b/Tests/LibWeb/Text/input/wpt-import/domparsing/DOMParser-parseFromString-url-base-about.window.js
new file mode 100644
index 00000000000..6618b59e0b2
--- /dev/null
+++ b/Tests/LibWeb/Text/input/wpt-import/domparsing/DOMParser-parseFromString-url-base-about.window.js
@@ -0,0 +1,27 @@
+// A document created by DOMParser gets the URL of the document it is created in, but not that
+// document's inherited base URL.
+// See https://github.com/whatwg/dom/issues/454
+
+function loadIframe(t, configure) {
+ return new Promise(resolve => {
+ const iframe = document.createElement("iframe");
+ configure(iframe);
+ iframe.onload = () => resolve(iframe);
+ t.add_cleanup(() => iframe.remove());
+ document.body.appendChild(iframe);
+ });
+}
+
+promise_test(async t => {
+ const win = (await loadIframe(t, iframe => iframe.src = "about:blank")).contentWindow;
+ const doc = new win.DOMParser().parseFromString("x", "text/html");
+ assert_equals(doc.URL, "about:blank", "URL");
+ assert_equals(doc.baseURI, "about:blank", "baseURI");
+}, "DOMParser in an about:blank document");
+
+promise_test(async t => {
+ const win = (await loadIframe(t, iframe => iframe.srcdoc = "x")).contentWindow;
+ const doc = new win.DOMParser().parseFromString("x", "text/html");
+ assert_equals(doc.URL, "about:srcdoc", "URL");
+ assert_equals(doc.baseURI, "about:srcdoc", "baseURI");
+}, "DOMParser in an about:srcdoc document");