Skip to content

Commit

Permalink
DOM: Support disconnected->disconnected moveBefore()
Browse files Browse the repository at this point in the history
Per discussion in whatwg/dom#1307, we've
decided to support `moveBefore()` in the disconnected->disconnected
scenario. This CL enables and tests that.

R=nrosenthal@chromium.org

Bug: 40150299
Change-Id: Ia0cf64a1a623c53ed5d9ae01b50b0e04f7028da2
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/6037586
Reviewed-by: Noam Rosenthal <nrosenthal@chromium.org>
Commit-Queue: Dominic Farolino <dom@chromium.org>
Cr-Commit-Position: refs/heads/main@{#1386266}
  • Loading branch information
domfarolino authored and chromium-wpt-export-bot committed Nov 21, 2024
1 parent bf56e27 commit cf651ad
Showing 1 changed file with 54 additions and 0 deletions.
54 changes: 54 additions & 0 deletions dom/nodes/moveBefore/tentative/Node-moveBefore.html
Original file line number Diff line number Diff line change
Expand Up @@ -237,4 +237,58 @@
assert_equals(a.moveBefore(c, c), c);
assert_array_equals(a.childNodes, [b, c]);
}, "Inserting a node before itself should not move the node");

test(() => {
const disconnectedOrigin = document.createElement('div');
const disconnectedDestination = document.createElement('div');
const p = disconnectedOrigin.appendChild(document.createElement('p'));

disconnectedDestination.moveBefore(p, null);

assert_equals(disconnectedDestination.firstChild, p, "<p> Was successfully moved");
}, "Moving a node from a disconnected container to a disconnected new parent succeeds");

test(() => {
const disconnectedOrigin = document.createElement('div');
const connectedDestination = document.body.appendChild(document.createElement('div'));
const p = disconnectedOrigin.appendChild(document.createElement('p'));

assert_throws_dom("HIERARCHY_REQUEST_ERR", () => connectedDestination.moveBefore(p, null));
}, "Moving a node from disconnected->connected throws a HIERARCHY_REQUEST_ERR");

test(() => {
const connectedOrigin = document.body.appendChild(document.createElement('div'));
const disconnectedDestination = document.createElement('div');
const p = connectedOrigin.appendChild(document.createElement('p'));

assert_throws_dom("HIERARCHY_REQUEST_ERR", () => disconnectedDestination.moveBefore(p, null));
}, "Moving a node from connected->disconnected throws a HIERARCHY_REQUEST_ERR");

promise_test(async t => {
let reactions = [];
const element_name = `ce-${performance.now()}`;
customElements.define(element_name,
class MockCustomElement extends HTMLElement {
connectedMoveCallback() { reactions.push("connectedMove"); }
connectedCallback() { reactions.push("connected"); }
disconnectedCallback() { reactions.push("disconnected"); }
});

const oldParent = document.createElement('div');
const newParent = document.createElement('div');
const element = oldParent.appendChild(document.createElement(element_name));
t.add_cleanup(() => {
element.remove();
newParent.remove();
oldParent.remove();
});

// Wait a microtask to let any custom element reactions run (should be none,
// since the initial parent is disconnected).
await Promise.resolve();

newParent.moveBefore(element, null);
await Promise.resolve();
assert_array_equals(reactions, []);
}, "No custom element callbacks are run during disconnected moveBefore()");
</script>

0 comments on commit cf651ad

Please sign in to comment.