Skip to content

Commit 19b35da

Browse files
authored
Fix the URL of about:blank iframes
1 parent 017568e commit 19b35da

5 files changed

Lines changed: 61 additions & 4 deletions

File tree

‎lib/jsdom/browser/Window.js‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -247,7 +247,8 @@ function Window(options) {
247247
concurrentNodeIterators: options.concurrentNodeIterators,
248248
parseOptions: options.parseOptions,
249249
defaultView: this._globalProxy,
250-
global: this
250+
global: this,
251+
parentOrigin: options.parentOrigin
251252
}, { alwaysUseDocumentClass: true });
252253

253254
if (vm.isContext(window)) {

‎lib/jsdom/living/helpers/document-base-url.js‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
"use strict";
22
const whatwgURL = require("whatwg-url");
3+
const { implForWrapper } = require("../generated/utils");
34

45
exports.documentBaseURL = document => {
56
// https://html.spec.whatwg.org/multipage/infrastructure.html#document-base-url
@@ -25,7 +26,8 @@ exports.fallbackBaseURL = document => {
2526

2627
if (document.URL === "about:blank" && document._defaultView &&
2728
document._defaultView._parent !== document._defaultView) {
28-
return exports.documentBaseURL(document._defaultView._parent._document);
29+
const parentDocument = implForWrapper(document._defaultView._parent._document);
30+
return exports.documentBaseURL(parentDocument);
2931
}
3032

3133
return document._URL;

‎lib/jsdom/living/nodes/Document-impl.js‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,9 @@ class DocumentImpl extends NodeImpl {
158158
}
159159

160160
this._URL = parsed;
161-
this._origin = whatwgURL.serializeURLOrigin(parsed);
161+
this._origin = urlOption === "about:blank" && privateData.options.parentOrigin ?
162+
privateData.options.parentOrigin :
163+
whatwgURL.serializeURLOrigin(this._URL);
162164

163165
this._location = Location.createImpl(this._globalObject, [], { relevantDocument: this });
164166
this._history = History.createImpl(this._globalObject, [], {

‎lib/jsdom/living/nodes/HTMLFrameElement-impl.js‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,8 @@ function loadFrame(frame, attaching) {
127127

128128
const wnd = window.createWindow({
129129
parsingMode: "html",
130-
url: url.scheme === "javascript" || serializedURL === "about:blank" ? parentDoc.URL : serializedURL,
130+
url: url.scheme === "javascript" ? parentDoc.URL : serializedURL,
131+
parentOrigin: parentDoc._origin,
131132
resourceLoader: parentDoc._defaultView._resourceLoader,
132133
referrer: parentDoc.URL,
133134
cookieJar: parentDoc._cookieJar,
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
<!DOCTYPE html>
2+
<title>location href in iframe when setting src attribute</title>
3+
<link rel="help" href="https://html.spec.whatwg.org/multipage/history.html#the-location-interface">
4+
<link rel="help" href="https://html.spec.whatwg.org/multipage/#the-iframe-element">
5+
<script src="/resources/testharness.js"></script>
6+
<script src="/resources/testharnessreport.js"></script>
7+
8+
<body>
9+
10+
<script>
11+
"use strict";
12+
setup({ explicit_done: true });
13+
14+
const tests = [
15+
{
16+
inputSrc: null,
17+
expectedLocationHref: "about:blank"
18+
},
19+
{
20+
inputSrc: "about:blank",
21+
expectedLocationHref: "about:blank"
22+
},
23+
{
24+
inputSrc: "",
25+
expectedLocationHref: "about:blank"
26+
},
27+
{
28+
inputSrc: "http://:80/invalid-url",
29+
expectedLocationHref: "about:blank"
30+
}
31+
];
32+
33+
window.onload = () => {
34+
for (const { inputSrc, expectedLocationHref } of tests) {
35+
async_test(t => {
36+
const iframe = document.createElement("iframe");
37+
38+
iframe.onload = t.step_func_done(() => {
39+
assert_equals(iframe.contentWindow.location.href, expectedLocationHref);
40+
});
41+
42+
if (inputSrc !== null) {
43+
iframe.src = inputSrc;
44+
}
45+
document.body.append(iframe);
46+
}, "Testing location href of iframe with input src: " + inputSrc);
47+
}
48+
49+
done();
50+
};
51+
</script>

0 commit comments

Comments
 (0)