From f597b80270067cc69300479b85beadf7a71aed23 Mon Sep 17 00:00:00 2001 From: Domenic Denicola Date: Mon, 10 Oct 2022 19:21:56 +0900 Subject: [PATCH] Test that History is associated with Document, not Window This is done by expanding the object-association.js helper and using it. See https://github.com/whatwg/html/issues/2566. --- common/object-association.js | 58 ++++++++++++------- ...history-associated-with-document.window.js | 6 ++ 2 files changed, 42 insertions(+), 22 deletions(-) create mode 100644 html/browsers/history/the-history-interface/history-associated-with-document.window.js diff --git a/common/object-association.js b/common/object-association.js index 458aae67db0cef..4b119703c1c199 100644 --- a/common/object-association.js +++ b/common/object-association.js @@ -1,26 +1,24 @@ "use strict"; -// For now this only has per-Window tests, but we could expand it to also test per-Document +// This is for testing whether an object (e.g., a global property) is associated with Window, or +// with Document. Recall that Window and Document are 1:1 except when doing a same-origin navigation +// away from the initial about:blank. In that case the Window object gets reused for the new +// Document. +// +// So: +// - If something is per-Window, then it should maintain its identity across an about:blank +// navigation. +// - If something is per-Document, then it should be recreated across an about:blank navigation. -/** - * Run tests for window[propertyName] after discarding the browsing context, navigating, etc. - * @param {string} propertyName - */ window.testIsPerWindow = propertyName => { - test(t => { - const iframe = document.createElement("iframe"); - document.body.appendChild(iframe); - const frame = iframe.contentWindow; - - const before = frame[propertyName]; - assert_true(before !== undefined && before !== null, `window.${propertyName} must be implemented`); - - iframe.remove(); + runTests(propertyName, assert_equals, "must not"); +}; - const after = frame[propertyName]; - assert_equals(after, before, `window.${propertyName} should not change after iframe.remove()`); - }, `Discarding the browsing context must not change window.${propertyName}`); +window.testIsPerDocument = propertyName => { + runTests(propertyName, assert_not_equals, "must"); +}; +function runTests(propertyName, equalityOrInequalityAsserter, mustOrMustNotReplace) { async_test(t => { const iframe = document.createElement("iframe"); document.body.appendChild(iframe); @@ -38,14 +36,30 @@ window.testIsPerWindow = propertyName => { } const after = frame[propertyName]; - assert_equals(after, before); + equalityOrInequalityAsserter(after, before); t.done(); }); iframe.src = "/common/blank.html"; - }, `Navigating from the initial about:blank must not replace window.${propertyName}`); + }, `Navigating from the initial about:blank ${mustOrMustNotReplace} replace window.${propertyName}`); - // Per spec, document.open() should not change any of the Window state. + // Per spec, discarding a browsing context should not change any of the global objects. + test(() => { + const iframe = document.createElement("iframe"); + document.body.appendChild(iframe); + const frame = iframe.contentWindow; + + const before = frame[propertyName]; + assert_true(before !== undefined && before !== null, `window.${propertyName} must be implemented`); + + iframe.remove(); + + const after = frame[propertyName]; + assert_equals(after, before, `window.${propertyName} should not change after iframe.remove()`); + }, `Discarding the browsing context must not change window.${propertyName}`); + + // Per spec, document.open() should not change any of the global objects. In historical versions + // of the spec, it did, so we test here. async_test(t => { const iframe = document.createElement("iframe"); @@ -64,5 +78,5 @@ window.testIsPerWindow = propertyName => { iframe.src = "/common/blank.html"; document.body.appendChild(iframe); - }, `document.open() must replace window.${propertyName}`); -}; + }, `document.open() must not replace window.${propertyName}`); +} diff --git a/html/browsers/history/the-history-interface/history-associated-with-document.window.js b/html/browsers/history/the-history-interface/history-associated-with-document.window.js new file mode 100644 index 00000000000000..94c1b2cf6b0a4b --- /dev/null +++ b/html/browsers/history/the-history-interface/history-associated-with-document.window.js @@ -0,0 +1,6 @@ +// META: title=the History object must be associated with the Document object, not the Window object +// META: script=/common/object-association.js + +// See https://github.com/whatwg/html/issues/2566. + +testIsPerDocument("history");