Lightpanda: the headless browser designed for AI and automation
webapi: spec-exact replaceChild records: fragment removal record, sibling capture
Karl Seguinc0ac3afecdcc
diff --git a/src/browser/Frame.zig b/src/browser/Frame.zigindex 1638a4f33..87ab3131c 100644--- a/src/browser/Frame.zig+++ b/src/browser/Frame.zig@@ -2494,17 +2494,19 @@ pub fn insertAllChildrenBefore(self: *Frame, fragment: *Node, parent: *Node, refreturn self.moveAllChildren(fragment, parent, ref_node, .records);}-pub const MoveChildrenNotify = enum { records, silent };+pub const MoveChildrenNotify = enum { records, silent_parent };// Moves every child of `source` into `parent` (before `ref_node`, or// appended). Per the DOM insert algorithm for fragments, observers get one// removal record on the source and one addition record on the parent, not-// one record per child. `.silent` suppresses even those (replaceChild queues-// its own combined record).+// one record per child. `.silent_parent` suppresses only the parent record+// (replaceChild queues its own combined record); the source record is queued+// regardless — the spec's insert algorithm notes it "intentionally does not+// pay attention to suppressObservers".pub fn moveAllChildren(self: *Frame, source: *Node, parent: *Node, ref_node: ?*Node, notify_mode: MoveChildrenNotify) !void {self.domChanged();const dest_connected = parent.isConnected();- const notify = notify_mode == .records and observers.hasMutationObservers(self);+ const notify = observers.hasMutationObservers(self);var moved: std.ArrayList(*Node) = .empty;const previous_sibling = if (ref_node) |ref| ref.previousSibling() else parent.lastChild();@@ -2530,7 +2532,9 @@ pub fn moveAllChildren(self: *Frame, source: *Node, parent: *Node, ref_node: ?*Nif (notify and moved.items.len > 0) {observers.notifyChildListChange(self, source, &.{}, moved.items, null, null);- observers.notifyChildListChange(self, parent, moved.items, &.{}, previous_sibling, ref_node);+ if (notify_mode == .records) {+ observers.notifyChildListChange(self, parent, moved.items, &.{}, previous_sibling, ref_node);+ }}}diff --git a/src/browser/tests/mutation_observer/childlist.html b/src/browser/tests/mutation_observer/childlist.htmlindex 30757c918..558861a1c 100644--- a/src/browser/tests/mutation_observer/childlist.html+++ b/src/browser/tests/mutation_observer/childlist.html@@ -198,6 +198,98 @@});</script>+<div id="replace-frag-parent"><div id="replace-frag-old">Old</div></div>++<script id="childlist_replace_child_fragment" type=module>+ const state = await testing.async();+ const parent = document.getElementById('replace-frag-parent');+ const oldChild = document.getElementById('replace-frag-old');++ const fragment = document.createDocumentFragment();+ const frag1 = document.createElement('div');+ const frag2 = document.createElement('div');+ fragment.append(frag1, frag2);++ const observer = new MutationObserver((records) => {+ observer.disconnect();+ state.resolve(records);+ });+ observer.observe(parent, { childList: true });+ observer.observe(fragment, { childList: true });++ parent.replaceChild(fragment, oldChild);++ await state.done((mutations) => {+ // The fragment's removal record is queued even though the replacement+ // itself suppresses observers (the spec's insert algorithm notes the+ // fragment record "intentionally does not pay attention to+ // suppressObservers"), followed by the parent's combined record.+ testing.expectEqual(2, mutations.length);++ testing.expectEqual('childList', mutations[0].type);+ testing.expectEqual(fragment, mutations[0].target);+ testing.expectEqual(0, mutations[0].addedNodes.length);+ testing.expectEqual(2, mutations[0].removedNodes.length);+ testing.expectEqual(frag1, mutations[0].removedNodes[0]);+ testing.expectEqual(frag2, mutations[0].removedNodes[1]);+ testing.expectEqual(null, mutations[0].previousSibling);+ testing.expectEqual(null, mutations[0].nextSibling);++ testing.expectEqual('childList', mutations[1].type);+ testing.expectEqual(parent, mutations[1].target);+ testing.expectEqual(2, mutations[1].addedNodes.length);+ testing.expectEqual(frag1, mutations[1].addedNodes[0]);+ testing.expectEqual(frag2, mutations[1].addedNodes[1]);+ testing.expectEqual(1, mutations[1].removedNodes.length);+ testing.expectEqual(oldChild, mutations[1].removedNodes[0]);+ testing.expectEqual(null, mutations[1].previousSibling);+ testing.expectEqual(null, mutations[1].nextSibling);+ });+</script>++<div id="replace-adjacent-parent"><div id="replace-adjacent-a">A</div><div id="replace-adjacent-new">New</div><div id="replace-adjacent-old">Old</div><div id="replace-adjacent-b">B</div></div>++<script id="childlist_replace_child_adjacent" type=module>+ const state = await testing.async();+ const parent = document.getElementById('replace-adjacent-parent');+ const a = document.getElementById('replace-adjacent-a');+ const newChild = document.getElementById('replace-adjacent-new');+ const oldChild = document.getElementById('replace-adjacent-old');+ const b = document.getElementById('replace-adjacent-b');++ const observer = new MutationObserver((records) => {+ observer.disconnect();+ state.resolve(records);+ });+ observer.observe(parent, { childList: true });++ // Replacing a child with its immediate previous sibling: the combined+ // record's previousSibling is captured before newChild is removed from+ // its old position, so it is newChild itself.+ parent.replaceChild(newChild, oldChild);++ await state.done((mutations) => {+ testing.expectEqual(2, mutations.length);++ testing.expectEqual('childList', mutations[0].type);+ testing.expectEqual(parent, mutations[0].target);+ testing.expectEqual(0, mutations[0].addedNodes.length);+ testing.expectEqual(1, mutations[0].removedNodes.length);+ testing.expectEqual(newChild, mutations[0].removedNodes[0]);+ testing.expectEqual(a, mutations[0].previousSibling);+ testing.expectEqual(oldChild, mutations[0].nextSibling);++ testing.expectEqual('childList', mutations[1].type);+ testing.expectEqual(parent, mutations[1].target);+ testing.expectEqual(1, mutations[1].addedNodes.length);+ testing.expectEqual(newChild, mutations[1].addedNodes[0]);+ testing.expectEqual(1, mutations[1].removedNodes.length);+ testing.expectEqual(oldChild, mutations[1].removedNodes[0]);+ testing.expectEqual(newChild, mutations[1].previousSibling);+ testing.expectEqual(b, mutations[1].nextSibling);+ });+</script>+<div id="multiple-parent"></div><script id="childlist_multiple_mutations" type=module>diff --git a/src/browser/webapi/Node.zig b/src/browser/webapi/Node.zigindex 999c19abe..0a40bdb5f 100644--- a/src/browser/webapi/Node.zig+++ b/src/browser/webapi/Node.zig@@ -276,7 +276,9 @@ fn ensurePreInsertValidity(parent: *Node, node: *Node, child: ?*Node, comptime mswitch (frag_child._type) {.element => element_count += 1,.cdata => |cd| {- if (cd._type == .text) {+ // A Text node (CDATASection included) cannot be a+ // child of a document.+ if (cd._type == .text or cd._type == .cdata_section) {return error.HierarchyError;}},@@ -840,6 +842,17 @@ pub fn replaceChild(self: *Node, new_child: *Node, old_child: *Node, frame: *Frareturn old_child;}+ // The combined record's siblings are captured before new_child is+ // removed from its old position (spec: referenceChild and+ // previousSibling are set before the adopt step), so new_child itself+ // can be the record's previousSibling. A referenceChild that is+ // new_child becomes new_child's next sibling.+ const prev = old_child.previousSibling();+ var next = old_child.nextSibling();+ if (next == new_child) {+ next = new_child.nextSibling();+ }+// Removing new_child from its current position (internal replacement)// notifies normally; the replacement itself queues one combined record// with both the added and the removed node.@@ -858,9 +871,6 @@ pub fn replaceChild(self: *Node, new_child: *Node, old_child: *Node, frame: *Fra}}- const prev = old_child.previousSibling();- const next = old_child.nextSibling();-var added: std.ArrayList(*Node) = .empty;if (new_child.is(DocumentFragment)) |_| {if (notify) {@@ -869,7 +879,7 @@ pub fn replaceChild(self: *Node, new_child: *Node, old_child: *Node, frame: *Fratry added.append(frame.call_arena, fragment_child);}}- try frame.moveAllChildren(new_child, self, old_child, .silent);+ try frame.moveAllChildren(new_child, self, old_child, .silent_parent);} else {if (notify) {try added.append(frame.call_arena, new_child);