Skip to content

Lightpanda: the headless browser designed for AI and automation

Fix spurious slotchange event from triggering

Karl Seguin3996204e87a1
diff --git a/src/browser/Frame.zig b/src/browser/Frame.zig
index 8d47d90e3..19f6f9d5f 100644
--- a/src/browser/Frame.zig
+++ b/src/browser/Frame.zig
@@ -3241,10 +3241,10 @@ pub fn _insertNodeRelative(self: *Frame, comptime from_parser: bool, parent: *No
if (self._element_shadow_roots.count() != 0) {
// html5ever wraps fragment parses in a temporary <html> element that
// gets unwrapped later; it must not take part in slot assignment.
- const is_fragment_wrapper = from_parser and
- self._parse_mode == .fragment and child.is(Element.Html.Html) != null;
- if (!is_fragment_wrapper) {
- self.slotInsertionSteps(parent, child);
+ const in_fragment_parse = from_parser and self._parse_mode == .fragment;
+ const is_fragment_wrapper = in_fragment_parse and child.is(Element.Html.Html) != null;
+ if (is_fragment_wrapper == false) {
+ self.slotInsertionSteps(parent, child, in_fragment_parse);
}
}
@@ -3520,13 +3520,11 @@ fn _assignSlottables(self: *Frame, slot: *Element.Html.Slot) !void {
}
}
-// DOM spec "assign a slot"
fn assignASlot(self: *Frame, slottable: *Node) void {
const slot = self.findSlotForSlottable(slottable, false) orelse return;
self.assignSlottables(slot);
}
-// DOM spec "assign slottables for a tree"
pub fn assignSlottablesForTree(self: *Frame, root: *Node) void {
var tw = @import("webapi/TreeWalker.zig").Full.Elements.init(root, .{});
while (tw.next()) |el| {
@@ -3549,8 +3547,7 @@ fn subtreeHasSlot(node: *Node) bool {
return false;
}
-// DOM spec insertion steps that affect slot assignment.
-fn slotInsertionSteps(self: *Frame, parent: *Node, child: *Node) void {
+fn slotInsertionSteps(self: *Frame, parent: *Node, child: *Node, in_fragment_parse: bool) void {
// The new child may be a slottable to assign in the parent's shadow tree.
if (parent.is(Element)) |parent_el| {
if (self._element_shadow_roots.get(parent_el) != null and isSlottable(child)) {
@@ -3559,9 +3556,14 @@ fn slotInsertionSteps(self: *Frame, parent: *Node, child: *Node) void {
}
// New fallback content in a slot that currently renders its fallback.
- if (parent.is(Element.Html.Slot)) |parent_slot| {
- if (parent_slot._assigned.items.len == 0 and parent.getRootNode(.{}).is(ShadowRoot) != null) {
- self.signalSlotChange(parent_slot);
+ // Skipped during fragment parsing: signaling would fire a spurious
+ // slotchange when the fragment's slots end up with the same (empty)
+ // assignment they were parsed with.
+ if (!in_fragment_parse == false) {
+ if (parent.is(Element.Html.Slot)) |parent_slot| {
+ if (parent_slot._assigned.items.len == 0 and parent.getRootNode(.{}).is(ShadowRoot) != null) {
+ self.signalSlotChange(parent_slot);
+ }
}
}
diff --git a/src/browser/tests/element/html/slot.html b/src/browser/tests/element/html/slot.html
index 067520a56..68475639f 100644
--- a/src/browser/tests/element/html/slot.html
+++ b/src/browser/tests/element/html/slot.html
@@ -537,6 +537,42 @@
}
</script>
+<script id="Slot#slotchange_on_fallback_content_insertion">
+{
+ // Appending fallback content to a slot that renders its fallback (no
+ // assigned nodes) fires slotchange (DOM insertion steps).
+ let calls = 0;
+ const host = document.createElement('div');
+ const shadow = host.attachShadow({ mode: 'open' });
+ const slot = document.createElement('slot');
+ shadow.appendChild(slot);
+ slot.addEventListener('slotchange', () => { calls += 1; });
+
+ slot.appendChild(document.createTextNode('fallback'));
+
+ testing.onload(() => {
+ testing.expectEqual(1, calls);
+ });
+}
+</script>
+
+<script id="Slot#no_slotchange_on_empty_slot_innerHTML">
+{
+ // Fragment-parsing a slot (with fallback content) into a shadow tree must
+ // not fire slotchange when nothing gets assigned to it (empty -> empty).
+ let calls = 0;
+ const host = document.createElement('div');
+ const shadow = host.attachShadow({ mode: 'open' });
+ shadow.addEventListener('slotchange', () => { calls += 1; });
+
+ shadow.innerHTML = '<slot name="z">fallback</slot>';
+
+ testing.onload(() => {
+ testing.expectEqual(0, calls);
+ });
+}
+</script>
+
<script id="Slot#slotchange_bubbles_to_shadow_root">
{
let listener_calls = 0;