Skip to content

Truly independent web browser

LibWeb: Cover lookup maps across state-preserving moves

Andreas Kling26cd0464aed2
diff --git a/Tests/LibWeb/Text/expected/DOM/moveBefore-lookup-scopes.txt b/Tests/LibWeb/Text/expected/DOM/moveBefore-lookup-scopes.txt
new file mode 100644
index 00000000000..96590debce6
--- /dev/null
+++ b/Tests/LibWeb/Text/expected/DOM/moveBefore-lookup-scopes.txt
@@ -0,0 +1,14 @@
+Initial document lookup: true
+Initial shadow lookup: true
+Initial nested lookup: true
+Initial form associations: true
+Form associations follow new scope: false
+Old shadow releases moved ID: false
+New shadow finds moved ID: false
+Nested shadow retains its ID: true
+Document retains its own ID: true
+Shadow controls release document form: false
+Document follows duplicate order: false
+Previous shadow releases moved ID: true
+Nested shadow stays separate: true
+Renamed nested ID stays local: true
diff --git a/Tests/LibWeb/Text/expected/wpt-import/dom/nodes/moveBefore/moveBefore-id-map.txt b/Tests/LibWeb/Text/expected/wpt-import/dom/nodes/moveBefore/moveBefore-id-map.txt
new file mode 100644
index 00000000000..3f2446970e6
--- /dev/null
+++ b/Tests/LibWeb/Text/expected/wpt-import/dom/nodes/moveBefore/moveBefore-id-map.txt
@@ -0,0 +1,9 @@
+Harness status: OK
+
+Found 4 tests
+
+4 Fail
+Fail moveBefore() correctly updates id map when moving into shadow root
+Fail moveBefore() correctly updates window map when moving id-mapped element into shadow root
+Fail moveBefore() correctly updates id map when moving between shadow roots
+Fail moveBefore() correctly updates id map when swapping elements with duplicate ids
\ No newline at end of file
diff --git a/Tests/LibWeb/Text/expected/wpt-import/dom/nodes/moveBefore/moveBefore-name-map.txt b/Tests/LibWeb/Text/expected/wpt-import/dom/nodes/moveBefore/moveBefore-name-map.txt
new file mode 100644
index 00000000000..3e155ff9b28
--- /dev/null
+++ b/Tests/LibWeb/Text/expected/wpt-import/dom/nodes/moveBefore/moveBefore-name-map.txt
@@ -0,0 +1,9 @@
+Harness status: OK
+
+Found 3 tests
+
+2 Pass
+1 Fail
+Pass moveBefore() correctly updates name map when moving into shadow root
+Fail moveBefore() correctly updates window map
+Pass moveBefore() correctly updates name map ordering in document
\ No newline at end of file
diff --git a/Tests/LibWeb/Text/expected/wpt-import/dom/nodes/moveBefore/moveBefore-shadow-boundary-id-change.txt b/Tests/LibWeb/Text/expected/wpt-import/dom/nodes/moveBefore/moveBefore-shadow-boundary-id-change.txt
new file mode 100644
index 00000000000..382a780de17
--- /dev/null
+++ b/Tests/LibWeb/Text/expected/wpt-import/dom/nodes/moveBefore/moveBefore-shadow-boundary-id-change.txt
@@ -0,0 +1,8 @@
+Harness status: OK
+
+Found 2 tests
+
+1 Pass
+1 Fail
+Pass Changing the id of an element moved into a shadow root only updates the shadow root's id map
+Fail Changing the id of an element moved out of a shadow root updates the document's id and name maps
\ No newline at end of file
diff --git a/Tests/LibWeb/Text/input/DOM/moveBefore-lookup-scopes.html b/Tests/LibWeb/Text/input/DOM/moveBefore-lookup-scopes.html
new file mode 100644
index 00000000000..1cfb5610cb0
--- /dev/null
+++ b/Tests/LibWeb/Text/input/DOM/moveBefore-lookup-scopes.html
@@ -0,0 +1,55 @@
+<!DOCTYPE html>
+<script src="../include.js"></script>
+<script>
+ test(() => {
+ const firstHost = document.createElement("div");
+ const secondHost = document.createElement("div");
+ document.body.append(firstHost, secondHost);
+ const firstRoot = firstHost.attachShadow({ mode: "open" });
+ const secondRoot = secondHost.attachShadow({ mode: "open" });
+
+ const documentElement = document.createElement("span");
+ documentElement.id = "duplicate";
+ document.body.append(documentElement);
+
+ const moved = document.createElement("section");
+ const child = document.createElement("span");
+ child.id = "duplicate";
+ const nestedHost = document.createElement("div");
+ const form = document.createElement("form");
+ form.id = "moved-form";
+ moved.append(child, nestedHost, form);
+ firstRoot.append(moved);
+ const oldControl = document.createElement("input");
+ const newControl = document.createElement("input");
+ oldControl.setAttribute("form", "moved-form");
+ newControl.setAttribute("form", "moved-form");
+ firstRoot.append(oldControl);
+ secondRoot.append(newControl);
+ const nestedRoot = nestedHost.attachShadow({ mode: "open" });
+ const nestedChild = document.createElement("span");
+ nestedChild.id = "duplicate";
+ nestedRoot.append(nestedChild);
+
+ println(`Initial document lookup: ${document.getElementById("duplicate") === documentElement}`);
+ println(`Initial shadow lookup: ${firstRoot.getElementById("duplicate") === child}`);
+ println(`Initial nested lookup: ${nestedRoot.getElementById("duplicate") === nestedChild}`);
+
+ println(`Initial form associations: ${oldControl.form === form && newControl.form === null}`);
+ secondRoot.moveBefore(moved, null);
+ println(`Form associations follow new scope: ${oldControl.form === null && newControl.form === form}`);
+ println(`Old shadow releases moved ID: ${firstRoot.getElementById("duplicate") === null}`);
+ println(`New shadow finds moved ID: ${secondRoot.getElementById("duplicate") === child}`);
+ println(`Nested shadow retains its ID: ${nestedRoot.getElementById("duplicate") === nestedChild}`);
+ println(`Document retains its own ID: ${document.getElementById("duplicate") === documentElement}`);
+
+ document.body.moveBefore(moved, documentElement);
+ println(`Shadow controls release document form: ${oldControl.form === null && newControl.form === null}`);
+ println(`Document follows duplicate order: ${document.getElementById("duplicate") === child}`);
+ println(`Previous shadow releases moved ID: ${secondRoot.getElementById("duplicate") === null}`);
+ println(`Nested shadow stays separate: ${nestedRoot.getElementById("duplicate") === nestedChild}`);
+
+ nestedChild.id = "renamed-in-nested-shadow";
+ println(`Renamed nested ID stays local: ${nestedRoot.getElementById("renamed-in-nested-shadow") === nestedChild && document.getElementById("renamed-in-nested-shadow") === null}`);
+ });
+</script>
diff --git a/Tests/LibWeb/Text/input/wpt-import/dom/nodes/moveBefore/moveBefore-id-map.html b/Tests/LibWeb/Text/input/wpt-import/dom/nodes/moveBefore/moveBefore-id-map.html
new file mode 100644
index 00000000000..e6ca4b3cf0d
--- /dev/null
+++ b/Tests/LibWeb/Text/input/wpt-import/dom/nodes/moveBefore/moveBefore-id-map.html
@@ -0,0 +1,90 @@
+<!DOCTYPE html>
+<script src="../../../resources/testharness.js"></script>
+<script src="../../../resources/testharnessreport.js"></script>
+
+<body>
+ <div id="container"></div>
+ <div id="target" data-target></div>
+ <span id="target" data-span></span>
+
+ <div id="container2"></div>
+ <div id="target2"></div>
+
+ <div id="host1"></div>
+ <div id="host2"></div>
+
+ <div id="swap-parent">
+ <div id="duplicate" data-a></div>
+ <div id="duplicate" data-b></div>
+ </div>
+</body>
+
+<script>
+ test(() => {
+ let shadowRoot = container.attachShadow({mode: "open"});
+ let div = document.createElement("div");
+ shadowRoot.appendChild(div);
+ let target = document.querySelector('[data-target]');
+
+ assert_equals(document.getElementById("target"), target);
+ assert_equals(shadowRoot.getElementById("target"), null);
+
+ div.moveBefore(target, null);
+
+ assert_equals(document.getElementById("target"), document.querySelector('[data-span]'));
+ assert_equals(shadowRoot.getElementById("target"), target);
+ }, 'moveBefore() correctly updates id map when moving into shadow root');
+
+ test(() => {
+ let shadowRoot = container2.attachShadow({mode: "open"});
+ let div = document.createElement("div");
+ shadowRoot.appendChild(div);
+ let localTarget = document.getElementById("target2");
+
+ assert_equals(window.target2, localTarget);
+
+ div.moveBefore(localTarget, null);
+
+ assert_equals(window.target2, undefined);
+ }, 'moveBefore() correctly updates window map when moving id-mapped element into shadow root');
+
+ test(() => {
+ let shadowRoot1 = host1.attachShadow({mode: "open"});
+ let shadowRoot2 = host2.attachShadow({mode: "open"});
+
+ let container1 = document.createElement("div");
+ let container2 = document.createElement("div");
+ shadowRoot1.appendChild(container1);
+ shadowRoot2.appendChild(container2);
+
+ let div = document.createElement("div");
+ div.id = "a";
+ container1.appendChild(div);
+
+ assert_equals(shadowRoot1.getElementById("a"), div);
+ assert_equals(shadowRoot2.getElementById("a"), null);
+
+ container2.moveBefore(div, null);
+
+ assert_equals(shadowRoot1.getElementById("a"), null);
+ assert_equals(shadowRoot2.getElementById("a"), div);
+ }, 'moveBefore() correctly updates id map when moving between shadow roots');
+
+ test(() => {
+ let parent = document.getElementById("swap-parent");
+ let a = parent.querySelector('[data-a]');
+ let b = parent.querySelector('[data-b]');
+
+ assert_equals(document.getElementById("duplicate"), a);
+
+ parent.moveBefore(b, a);
+
+ assert_equals(parent.firstElementChild, b);
+ assert_equals(document.getElementById("duplicate"), b);
+
+ parent.moveBefore(a, b);
+
+ assert_equals(parent.firstElementChild, a);
+ assert_equals(document.getElementById("duplicate"), a);
+ }, 'moveBefore() correctly updates id map when swapping elements with duplicate ids');
+</script>
diff --git a/Tests/LibWeb/Text/input/wpt-import/dom/nodes/moveBefore/moveBefore-name-map.html b/Tests/LibWeb/Text/input/wpt-import/dom/nodes/moveBefore/moveBefore-name-map.html
new file mode 100644
index 00000000000..3e82845a72a
--- /dev/null
+++ b/Tests/LibWeb/Text/input/wpt-import/dom/nodes/moveBefore/moveBefore-name-map.html
@@ -0,0 +1,79 @@
+<!DOCTYPE html>
+<script src="../../../resources/testharness.js"></script>
+<script src="../../../resources/testharnessreport.js"></script>
+
+<body>
+ <div id="container"></div>
+ <div id="window-container"></div>
+ <img name="target" data-target>
+ <img name="target2" data-target2>
+
+ <div id="host1"></div>
+ <div id="host2"></div>
+</body>
+
+<script>
+ test(() => {
+ let shadowRoot = container.attachShadow({mode: "open"});
+ let div = document.createElement("div");
+ shadowRoot.appendChild(div);
+
+ let target = document.querySelector('[data-target]');
+
+ let nameMap = document.getElementsByName("target");
+ assert_equals(nameMap.length, 1);
+ assert_equals(nameMap[0], target);
+
+ div.moveBefore(target, null);
+
+ assert_equals(nameMap.length, 0);
+ }, 'moveBefore() correctly updates name map when moving into shadow root');
+
+ test(() => {
+ let shadowRoot = document.getElementById("window-container").attachShadow({mode: "open"});
+ let div = document.createElement("div");
+ shadowRoot.appendChild(div);
+
+ let target = document.querySelector('[data-target2]');
+
+ assert_equals(window.target2, target);
+
+ div.moveBefore(target, null);
+
+ assert_equals(window.target2, undefined);
+ }, 'moveBefore() correctly updates window map');
+
+ test(() => {
+ let host = document.createElement("div");
+ document.body.appendChild(host);
+
+ let shadowRoot = host.attachShadow({mode: "open"});
+ let parent = document.createElement("div");
+ shadowRoot.appendChild(parent);
+
+ let a = document.createElement("img");
+ let b = document.createElement("img");
+
+ a.name = "duplicate";
+ b.name = "duplicate";
+
+ parent.appendChild(a);
+ parent.appendChild(b);
+
+ let nameMap = document.getElementsByName("duplicate");
+
+ assert_equals(nameMap.length, 0);
+
+ document.body.appendChild(a);
+ document.body.appendChild(b);
+
+ nameMap = document.getElementsByName("duplicate");
+ assert_equals(nameMap.length, 2);
+ assert_equals(nameMap[0], a);
+
+ document.body.moveBefore(b, a);
+
+ assert_equals(nameMap.length, 2);
+ assert_equals(nameMap[0], b);
+ }, 'moveBefore() correctly updates name map ordering in document');
+</script>
diff --git a/Tests/LibWeb/Text/input/wpt-import/dom/nodes/moveBefore/moveBefore-shadow-boundary-id-change.html b/Tests/LibWeb/Text/input/wpt-import/dom/nodes/moveBefore/moveBefore-shadow-boundary-id-change.html
new file mode 100644
index 00000000000..519326f24b0
--- /dev/null
+++ b/Tests/LibWeb/Text/input/wpt-import/dom/nodes/moveBefore/moveBefore-shadow-boundary-id-change.html
@@ -0,0 +1,49 @@
+<!DOCTYPE html>
+<title>Changing the id of an element that moveBefore() moved across a shadow boundary</title>
+<link rel=help href="https://dom.spec.whatwg.org/#move">
+<script src="../../../resources/testharness.js"></script>
+<script src="../../../resources/testharnessreport.js"></script>
+
+<body>
+ <div id="host"></div>
+</body>
+
+<script>
+ const host = document.getElementById("host");
+ const shadowRoot = host.attachShadow({mode: "open"});
+
+ test(t => {
+ const element = document.createElement("div");
+ element.id = "moved-into-shadow";
+ document.body.appendChild(element);
+ t.add_cleanup(() => element.remove());
+
+ shadowRoot.moveBefore(element, null);
+ element.id = "renamed-in-shadow";
+
+ assert_equals(shadowRoot.getElementById("renamed-in-shadow"), element);
+ assert_equals(document.getElementById("renamed-in-shadow"), null);
+ assert_equals(window["renamed-in-shadow"], undefined);
+ }, "Changing the id of an element moved into a shadow root only updates the shadow root's id map");
+
+ test(t => {
+ const element = document.createElement("div");
+ element.id = "moved-out-of-shadow";
+ shadowRoot.appendChild(element);
+ t.add_cleanup(() => element.remove());
+
+ document.body.moveBefore(element, null);
+
+ assert_equals(window["moved-out-of-shadow"], element);
+
+ element.id = "renamed-in-document";
+
+ assert_equals(document.getElementById("renamed-in-document"), element);
+ assert_equals(window["renamed-in-document"], element);
+ assert_equals(window["moved-out-of-shadow"], undefined);
+
+ element.remove();
+
+ assert_equals(window["renamed-in-document"], undefined);
+ }, "Changing the id of an element moved out of a shadow root updates the document's id and name maps");
+</script>