Skip to content

Commit

Permalink
Fix the URL of about:blank iframes
Browse files Browse the repository at this point in the history
  • Loading branch information
SimonMueller committed Mar 7, 2021
1 parent 017568e commit 19b35da
Show file tree
Hide file tree
Showing 5 changed files with 61 additions and 4 deletions.
3 changes: 2 additions & 1 deletion lib/jsdom/browser/Window.js
Original file line number Diff line number Diff line change
Expand Up @@ -247,7 +247,8 @@ function Window(options) {
concurrentNodeIterators: options.concurrentNodeIterators,
parseOptions: options.parseOptions,
defaultView: this._globalProxy,
global: this
global: this,
parentOrigin: options.parentOrigin
}, { alwaysUseDocumentClass: true });

if (vm.isContext(window)) {
Expand Down
4 changes: 3 additions & 1 deletion lib/jsdom/living/helpers/document-base-url.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
"use strict";
const whatwgURL = require("whatwg-url");
const { implForWrapper } = require("../generated/utils");

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

if (document.URL === "about:blank" && document._defaultView &&
document._defaultView._parent !== document._defaultView) {
return exports.documentBaseURL(document._defaultView._parent._document);
const parentDocument = implForWrapper(document._defaultView._parent._document);
return exports.documentBaseURL(parentDocument);
}

return document._URL;
Expand Down
4 changes: 3 additions & 1 deletion lib/jsdom/living/nodes/Document-impl.js
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,9 @@ class DocumentImpl extends NodeImpl {
}

this._URL = parsed;
this._origin = whatwgURL.serializeURLOrigin(parsed);
this._origin = urlOption === "about:blank" && privateData.options.parentOrigin ?
privateData.options.parentOrigin :
whatwgURL.serializeURLOrigin(this._URL);

this._location = Location.createImpl(this._globalObject, [], { relevantDocument: this });
this._history = History.createImpl(this._globalObject, [], {
Expand Down
3 changes: 2 additions & 1 deletion lib/jsdom/living/nodes/HTMLFrameElement-impl.js
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,8 @@ function loadFrame(frame, attaching) {

const wnd = window.createWindow({
parsingMode: "html",
url: url.scheme === "javascript" || serializedURL === "about:blank" ? parentDoc.URL : serializedURL,
url: url.scheme === "javascript" ? parentDoc.URL : serializedURL,
parentOrigin: parentDoc._origin,
resourceLoader: parentDoc._defaultView._resourceLoader,
referrer: parentDoc.URL,
cookieJar: parentDoc._cookieJar,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
<!DOCTYPE html>
<title>location href in iframe when setting src attribute</title>
<link rel="help" href="https://html.spec.whatwg.org/multipage/history.html#the-location-interface">
<link rel="help" href="https://html.spec.whatwg.org/multipage/#the-iframe-element">
<script src="/resources/testharness.js"></script>
<script src="/resources/testharnessreport.js"></script>

<body>

<script>
"use strict";
setup({ explicit_done: true });

const tests = [
{
inputSrc: null,
expectedLocationHref: "about:blank"
},
{
inputSrc: "about:blank",
expectedLocationHref: "about:blank"
},
{
inputSrc: "",
expectedLocationHref: "about:blank"
},
{
inputSrc: "http://:80/invalid-url",
expectedLocationHref: "about:blank"
}
];

window.onload = () => {
for (const { inputSrc, expectedLocationHref } of tests) {
async_test(t => {
const iframe = document.createElement("iframe");

iframe.onload = t.step_func_done(() => {
assert_equals(iframe.contentWindow.location.href, expectedLocationHref);
});

if (inputSrc !== null) {
iframe.src = inputSrc;
}
document.body.append(iframe);
}, "Testing location href of iframe with input src: " + inputSrc);
}

done();
};
</script>

0 comments on commit 19b35da

Please sign in to comment.