Skip to content

Commit 2b08e13

Browse files
committed
Test that document.open() keeps an inherited base URL
document.open() called from another document rewrites the target document's URL, which does not change the base URL an about:blank or about:srcdoc document inherited when it was created.
1 parent 71afcc3 commit 2b08e13

1 file changed

Lines changed: 56 additions & 0 deletions

File tree

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
<!DOCTYPE html>
2+
<meta charset="utf-8">
3+
<title>document.open() does not change an about: document's inherited base URL</title>
4+
<link rel="help" href="https://html.spec.whatwg.org/multipage/urls-and-fetching.html#document-base-url">
5+
<link rel="help" href="https://html.spec.whatwg.org/multipage/dynamic-markup-insertion.html#dom-document-open">
6+
<script src="/resources/testharness.js"></script>
7+
<script src="/resources/testharnessreport.js"></script>
8+
<base href="/base-url-override/">
9+
<body>
10+
<script>
11+
// See https://github.com/whatwg/dom/issues/454
12+
13+
const INHERITED_BASE_URL = new URL("/base-url-override/", location.href).href;
14+
15+
// document.open() copies the calling document's URL, without its fragment.
16+
const REWRITTEN_URL = (() => {
17+
const url = new URL(location.href);
18+
url.hash = "";
19+
return url.href;
20+
})();
21+
22+
function loadIframe(t, configure) {
23+
return new Promise(resolve => {
24+
const iframe = document.createElement("iframe");
25+
configure(iframe);
26+
iframe.addEventListener("load", () => resolve(iframe), { once: true });
27+
t.add_cleanup(() => iframe.remove());
28+
document.body.append(iframe);
29+
});
30+
}
31+
32+
function documentOpenTest(configure) {
33+
return async t => {
34+
assert_equals(document.baseURI, INHERITED_BASE_URL, "this document's base URL");
35+
assert_not_equals(document.baseURI, REWRITTEN_URL,
36+
"this document's base URL has to differ from its URL");
37+
38+
const doc = (await loadIframe(t, configure)).contentDocument;
39+
assert_equals(doc.baseURI, INHERITED_BASE_URL, "inherited base URL");
40+
41+
doc.open();
42+
assert_equals(doc.URL, REWRITTEN_URL, "URL after document.open()");
43+
assert_equals(doc.baseURI, INHERITED_BASE_URL, "base URL after document.open()");
44+
doc.close();
45+
};
46+
}
47+
48+
promise_test(documentOpenTest(() => {}),
49+
"document.open() on an initial about:blank document");
50+
51+
promise_test(documentOpenTest(iframe => iframe.src = "about:blank"),
52+
"document.open() on an about:blank document");
53+
54+
promise_test(documentOpenTest(iframe => iframe.srcdoc = "x"),
55+
"document.open() on an about:srcdoc document");
56+
</script>

0 commit comments

Comments
 (0)