Truly independent web browser
LibWeb: Leave unassigned and unused fallback nodes out of the flat tree
Jelle Raaijmakers3cf8bf91a68b
diff --git a/Libraries/LibWeb/DOM/Node.cpp b/Libraries/LibWeb/DOM/Node.cppindex 09ed85c8751..c281d93f9b9 100644--- a/Libraries/LibWeb/DOM/Node.cpp+++ b/Libraries/LibWeb/DOM/Node.cpp@@ -55,6 +55,7 @@#include <LibWeb/DOM/ProcessingInstruction.h>#include <LibWeb/DOM/Range.h>#include <LibWeb/DOM/ShadowRoot.h>+#include <LibWeb/DOM/Slottable.h>#include <LibWeb/DOM/StaticNodeList.h>#include <LibWeb/DOM/SubtreeInsertionScope.h>#include <LibWeb/DOM/XMLDocument.h>@@ -2928,19 +2929,38 @@ Element* Node::parent_or_shadow_host_element()// https://drafts.csswg.org/css-shadow-1/#flat-treeParentNode* Node::flat_tree_parent(){+ // NB: The flat tree is built top-down from the steps below. This finds the pending node that appends this node.++ // AD-HOC: A shadow root is not part of the flat tree, but some walks start at one, such as those from a range's+ // common ancestor container or boundary point, or from the parent of removed children. Continue them at the+ // host, as its shadow-including parent. Scrolling a text fragment inside a shadow tree into view relies on+ // this, which the spec leaves open: https://github.com/WICG/scroll-to-text-fragment/issues/190+ if (auto* shadow_root = as_if<ShadowRoot>(*this))+ return shadow_root->host();+// pending node is a slot// Find slottables for pending node, and append them to pending nodes, with pending node as their associated// parent.if (auto slot = assigned_slot_for_node(*this))return slot.ptr();+ // If no slottables were found for pending node, instead append its children to pending nodes, with pending+ // node as their associated parent.+ if (auto* slot = as_if<HTML::HTMLSlotElement>(parent()))+ return find_slottables(*slot).is_empty() ? slot : nullptr;+// pending node is a shadow host// Append the child nodes of the shadow root of the shadow tree it hosts to pending nodes, with pending node as// their associated parent.- auto* parent = parent_or_shadow_host();+ auto* parent = as<ParentNode>(this->parent());if (auto* shadow_root = as_if<ShadowRoot>(parent))return shadow_root->host();+ // NB: Only the first matching step applies, so a shadow host does not append its own children: a child of one that+ // no slot found above is not part of the flat tree.+ if (auto* element = as_if<Element>(parent); element && element->is_shadow_host())+ return nullptr;+// Otherwise,// Append the child nodes of pending node’s light tree to pending nodes, with pending node as their associated// parent.diff --git a/Tests/LibWeb/Text/expected/popover/popover-nesting-slot-fallback-content.txt b/Tests/LibWeb/Text/expected/popover/popover-nesting-slot-fallback-content.txtnew file mode 100644index 00000000000..ef50929e951--- /dev/null+++ b/Tests/LibWeb/Text/expected/popover/popover-nesting-slot-fallback-content.txt@@ -0,0 +1,4 @@+nothing assigned to the slot: outer stays open+right after assigning a node to the slot: outer closed+a node assigned to the slot: outer closed+right after unassigning the last node from the slot: outer stays opendiff --git a/Tests/LibWeb/Text/expected/popover/popover-nesting-unassigned-shadow-host-child.txt b/Tests/LibWeb/Text/expected/popover/popover-nesting-unassigned-shadow-host-child.txtnew file mode 100644index 00000000000..dc37815ac6a--- /dev/null+++ b/Tests/LibWeb/Text/expected/popover/popover-nesting-unassigned-shadow-host-child.txt@@ -0,0 +1,3 @@+assigned to a slot: outer stays open+not assigned to a slot: outer closed+its slot removed: outer closeddiff --git a/Tests/LibWeb/Text/input/popover/popover-nesting-slot-fallback-content.html b/Tests/LibWeb/Text/input/popover/popover-nesting-slot-fallback-content.htmlnew file mode 100644index 00000000000..1fa3b5b073c--- /dev/null+++ b/Tests/LibWeb/Text/input/popover/popover-nesting-slot-fallback-content.html@@ -0,0 +1,33 @@+<!DOCTYPE html>+<script src="../include.js"></script>++<div id="host"></div>++<script>+ test(() => {+ const shadow = host.attachShadow({ mode: "open" });+ shadow.innerHTML = `<div popover id="outer"><slot><div popover id="inner"></div></slot></div>`;+ const outer = shadow.getElementById("outer");+ const inner = shadow.getElementById("inner");++ function showBoth(description) {+ outer.showPopover();+ inner.showPopover();+ println(`${description}: outer ${outer.matches(":popover-open") ? "stays open" : "closed"}`);+ inner.hidePopover();+ outer.hidePopover();+ }++ showBoth("nothing assigned to the slot");++ const slottable = document.createElement("span");+ host.appendChild(slottable);+ showBoth("right after assigning a node to the slot");++ host.offsetWidth;+ showBoth("a node assigned to the slot");++ slottable.remove();+ showBoth("right after unassigning the last node from the slot");+ });+</script>diff --git a/Tests/LibWeb/Text/input/popover/popover-nesting-unassigned-shadow-host-child.html b/Tests/LibWeb/Text/input/popover/popover-nesting-unassigned-shadow-host-child.htmlnew file mode 100644index 00000000000..3db775ee700--- /dev/null+++ b/Tests/LibWeb/Text/input/popover/popover-nesting-unassigned-shadow-host-child.html@@ -0,0 +1,31 @@+<!DOCTYPE html>+<script src="../include.js"></script>++<div popover id="outer">+ <div id="host"><div popover id="inner"></div></div>+</div>++<script>+ test(() => {+ const shadow = host.attachShadow({ mode: "open" });+ shadow.innerHTML = `<slot name="assigned"></slot>`;++ function showBoth(description) {+ outer.showPopover();+ inner.showPopover();+ println(`${description}: outer ${outer.matches(":popover-open") ? "stays open" : "closed"}`);+ inner.hidePopover();+ outer.hidePopover();+ }++ inner.slot = "assigned";+ showBoth("assigned to a slot");++ inner.slot = "unassigned";+ showBoth("not assigned to a slot");++ inner.slot = "assigned";+ shadow.querySelector("slot").remove();+ showBoth("its slot removed");+ });+</script>