Skip to content

Commit

Permalink
Fix postMessage referenced as a bare property
Browse files Browse the repository at this point in the history
Closes #3755.

Also inlines it into Window.js since that's the general pattern for the mess of global functions we currently have.
  • Loading branch information
domenic committed Aug 25, 2024
1 parent a241df6 commit 46d5d5c
Show file tree
Hide file tree
Showing 6 changed files with 55 additions and 73 deletions.
40 changes: 38 additions & 2 deletions lib/jsdom/browser/Window.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
const vm = require("vm");
const webIDLConversions = require("webidl-conversions");
const { CSSStyleDeclaration } = require("cssstyle");
const whatwgURL = require("whatwg-url");
const notImplemented = require("./not-implemented");
const { installInterfaces } = require("../living/interfaces");
const { define, mixin } = require("../utils");
Expand All @@ -13,7 +14,6 @@ const OnBeforeUnloadEventHandlerNonNull = require("../living/generated/OnBeforeU
const OnErrorEventHandlerNonNull = require("../living/generated/OnErrorEventHandlerNonNull");
const { fireAPageTransitionEvent } = require("../living/helpers/page-transition-event");
const namedPropertiesWindow = require("../living/named-properties-window");
const postMessage = require("../living/post-message");
const DOMException = require("../living/generated/DOMException");
const idlUtils = require("../living/generated/utils");
const WebSocketImpl = require("../living/websockets/WebSocket-impl").implementation;
Expand All @@ -33,6 +33,7 @@ const SessionHistory = require("../living/window/SessionHistory");
const { getDeclarationForElement, getResolvedValue, propertiesWithResolvedValueImplemented,
SHADOW_DOM_PSEUDO_REGEXP } = require("../living/helpers/style-rules.js");
const CustomElementRegistry = require("../living/generated/CustomElementRegistry");
const MessageEvent = require("../living/generated/MessageEvent");
const jsGlobals = require("./js-globals.json");

const GlobalEventHandlersImpl = require("../living/nodes/GlobalEventHandlers-impl").implementation;
Expand Down Expand Up @@ -781,7 +782,42 @@ function installOwnProperties(window, options) {
writable: true
});

window.postMessage = postMessage(window);
window.postMessage = function (message, targetOrigin) {
if (arguments.length < 2) {
throw new TypeError("'postMessage' requires 2 arguments: 'message' and 'targetOrigin'");
}

targetOrigin = webIDLConversions.DOMString(targetOrigin);

if (targetOrigin === "/") {
// TODO: targetOrigin === "/" requires getting incumbent settings object.
// Maybe could be done with Error stack traces??
return;
} else if (targetOrigin !== "*") {
const parsedURL = whatwgURL.parseURL(targetOrigin);
if (parsedURL === null) {
throw DOMException.create(window, [
"Failed to execute 'postMessage' on 'Window': " +
"Invalid target origin '" + targetOrigin + "' in a call to 'postMessage'.",
"SyntaxError"
]);
}
targetOrigin = whatwgURL.serializeURLOrigin(parsedURL);

if (targetOrigin !== idlUtils.implForWrapper(window._document)._origin) {
// Not implemented.
return;
}
}

// TODO: event.source - requires reference to incumbent window
// TODO: event.origin - requires reference to incumbent window
// TODO: event.ports
// TODO: event.data - requires structured cloning
setTimeout(() => {
fireAnEvent("message", window, MessageEvent, { data: message });
}, 0);
};

window.atob = function (str) {
try {
Expand Down
39 changes: 0 additions & 39 deletions lib/jsdom/living/post-message.js

This file was deleted.

9 changes: 0 additions & 9 deletions lib/jsdom/utils.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
"use strict";
const whatwgURL = require("whatwg-url");
const { domSymbolTree } = require("./living/helpers/internal-constants");
const SYMBOL_TREE_POSITION = require("symbol-tree").TreePosition;

Expand Down Expand Up @@ -68,14 +67,6 @@ exports.memoizeQuery = function memoizeQuery(fn) {
};
};

function isValidAbsoluteURL(str) {
return whatwgURL.parseURL(str) !== null;
}

exports.isValidTargetOrigin = function (str) {
return str === "*" || str === "/" || isValidAbsoluteURL(str);
};

exports.simultaneousIterators = function* (first, second) {
for (;;) {
const firstResult = first.next();
Expand Down
22 changes: 0 additions & 22 deletions test/helper-unit-tests/utils.js

This file was deleted.

1 change: 0 additions & 1 deletion test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ require("./api/resources.js");
require("./api/virtual-console.js");

require("./helper-unit-tests/named-properties-tracker.js");
require("./helper-unit-tests/utils.js");

require("./to-port-to-wpts/class-list.js");
require("./to-port-to-wpts/current-script.js");
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<!DOCTYPE html>
<meta charset="utf-8">
<title>Using the global postMessage() to post a message to oneself</title>
<script src="/resources/testharness.js"></script>
<script src="/resources/testharnessreport.js"></script>
<!-- Regression test for https://github.com/jsdom/jsdom/issues/3755 -->

<script>
"use strict";
async_test(t => {
onmessage = t.step_func_done(e => {
assert_equals(e.data, "Hello");
});

postMessage("Hello", "*");
});
</script>

0 comments on commit 46d5d5c

Please sign in to comment.