Skip to content

Commit

Permalink
Remove document.origin and add window.origin
Browse files Browse the repository at this point in the history
Both document and window also gain an internal _origin property,
representing the document's origin and environment settings object's
origin, respectively. Other parts of jsdom are converted to use either
of those internal _origin depending on what the spec says to use.
  • Loading branch information
TimothyGu authored and domenic committed Jan 9, 2020
1 parent 678141f commit 020539e
Show file tree
Hide file tree
Showing 11 changed files with 29 additions and 70 deletions.
2 changes: 1 addition & 1 deletion lib/api.js
Expand Up @@ -94,7 +94,7 @@ class JSDOM {
}

document._URL = url;
document.origin = whatwgURL.serializeURLOrigin(document._URL);
document._origin = whatwgURL.serializeURLOrigin(document._URL);
}
}

Expand Down
25 changes: 20 additions & 5 deletions lib/jsdom/browser/Window.js
Expand Up @@ -141,6 +141,9 @@ function Window(options) {
documentImpl._defaultView = window._globalProxy = vm.runInContext("this", window);
}

const documentOrigin = idlUtils.implForWrapper(this._document)._origin;
this._origin = documentOrigin;

// https://html.spec.whatwg.org/#session-history
this._sessionHistory = new SessionHistory({
document: idlUtils.implForWrapper(this._document),
Expand Down Expand Up @@ -168,19 +171,19 @@ function Window(options) {
// Some properties (such as localStorage and sessionStorage) share data
// between windows in the same origin. This object is intended
// to contain such data.
if (options.commonForOrigin && options.commonForOrigin[this._document.origin]) {
if (options.commonForOrigin && options.commonForOrigin[documentOrigin]) {
this._commonForOrigin = options.commonForOrigin;
} else {
this._commonForOrigin = {
[this._document.origin]: {
[documentOrigin]: {
localStorageArea: new Map(),
sessionStorageArea: new Map(),
windowsInSameOrigin: [this]
}
};
}

this._currentOriginData = this._commonForOrigin[this._document.origin];
this._currentOriginData = this._commonForOrigin[documentOrigin];

///// WEB STORAGE

Expand Down Expand Up @@ -283,8 +286,20 @@ function Window(options) {
get screen() {
return screen;
},
get origin() {
return window._origin;
},
// The origin IDL attribute is defined with [Replaceable].
set origin(value) {
Object.defineProperty(this, "origin", {
value,
writable: true,
enumerable: true,
configurable: true
});
},
get localStorage() {
if (this._document.origin === "null") {
if (idlUtils.implForWrapper(this._document)._origin === "null") {
throw DOMException.create(window, [
"localStorage is not available for opaque origins",
"SecurityError"
Expand All @@ -294,7 +309,7 @@ function Window(options) {
return this._localStorage;
},
get sessionStorage() {
if (this._document.origin === "null") {
if (idlUtils.implForWrapper(this._document)._origin === "null") {
throw DOMException.create(window, [
"sessionStorage is not available for opaque origins",
"SecurityError"
Expand Down
2 changes: 1 addition & 1 deletion lib/jsdom/living/node.js
Expand Up @@ -20,7 +20,7 @@ module.exports.clone = function (node, document, cloneChildren) {
copy._encoding = node._encoding;
copy.contentType = node.contentType;
copy._URL = node._URL;
copy.origin = node.origin;
copy._origin = node._origin;
copy._parsingMode = node._parsingMode;
break;

Expand Down
2 changes: 1 addition & 1 deletion lib/jsdom/living/nodes/DOMImplementation-impl.js
Expand Up @@ -52,7 +52,7 @@ class DOMImplementationImpl {
document.appendChild(element);
}

document.origin = this._ownerDocument.origin;
document._origin = this._ownerDocument._origin;

return document;
}
Expand Down
2 changes: 1 addition & 1 deletion lib/jsdom/living/nodes/Document-impl.js
Expand Up @@ -158,7 +158,7 @@ class DocumentImpl extends NodeImpl {
}

this._URL = parsed;
this.origin = whatwgURL.serializeURLOrigin(parsed);
this._origin = whatwgURL.serializeURLOrigin(parsed);

this._location = Location.createImpl(this._globalObject, [], { relevantDocument: this });
this._history = History.createImpl(this._globalObject, [], {
Expand Down
1 change: 0 additions & 1 deletion lib/jsdom/living/nodes/Document.webidl
Expand Up @@ -6,7 +6,6 @@ interface Document : Node {
[SameObject] readonly attribute DOMImplementation implementation;
readonly attribute USVString URL;
readonly attribute USVString documentURI;
readonly attribute USVString origin;
readonly attribute DOMString compatMode;
readonly attribute DOMString characterSet;
readonly attribute DOMString charset; // historical alias of .characterSet
Expand Down
2 changes: 1 addition & 1 deletion lib/jsdom/living/nodes/HTMLFrameElement-impl.js
Expand Up @@ -147,7 +147,7 @@ function loadFrame(frame, attaching) {
contentWindow._frameElement = frame;
contentWindow._virtualConsole = parent._virtualConsole;

if (parentDoc.origin === contentDoc.origin) {
if (parentDoc._origin === contentDoc._origin) {
contentWindow._currentOriginData.windowsInSameOrigin.push(contentWindow);
}

Expand Down
3 changes: 2 additions & 1 deletion lib/jsdom/living/post-message.js
@@ -1,6 +1,7 @@
"use strict";
const DOMException = require("domexception/webidl2js-wrapper");
const MessageEvent = require("./generated/MessageEvent");
const idlUtils = require("./generated/utils");
const { isValidTargetOrigin } = require("../utils");
const { fireAnEvent } = require("./helpers/events");

Expand All @@ -23,7 +24,7 @@ module.exports = function (globalObject) {

// TODO: targetOrigin === '/' - requires reference to source window
// See https://github.com/jsdom/jsdom/pull/1140#issuecomment-111587499
if (targetOrigin !== "*" && targetOrigin !== this.location.origin) {
if (targetOrigin !== "*" && targetOrigin !== idlUtils.implForWrapper(globalObject._document)._origin) {
return;
}

Expand Down
2 changes: 1 addition & 1 deletion lib/jsdom/living/websockets/WebSocket-impl.js
Expand Up @@ -130,7 +130,7 @@ class WebSocketImpl extends EventTargetImpl {
headers: {
"user-agent": globalObject.navigator.userAgent,
cookie: this._ownerDocument._cookieJar.getCookieStringSync(nodeParsedURL, { http: true }),
origin: this._ownerDocument.origin
origin: globalObject._origin
},
rejectUnauthorized: this._ownerDocument._strictSSL
});
Expand Down
2 changes: 1 addition & 1 deletion lib/jsdom/living/xhr/XMLHttpRequest-impl.js
Expand Up @@ -118,7 +118,7 @@ class XMLHttpRequestImpl extends XMLHttpRequestEventTargetImpl {
proxy: window._resourceLoader._proxy,
cookieJar: _ownerDocument._cookieJar,
encoding: _ownerDocument._encoding,
origin: _ownerDocument.origin,
origin: window._origin,
userAgent: window.navigator.userAgent
};

Expand Down

This file was deleted.

0 comments on commit 020539e

Please sign in to comment.