Skip to content

Fast path in Node::assign_slottables_for_a_tree is wrong #35188

@simonwuelker

Description

@simonwuelker

code:

/// <https://dom.spec.whatwg.org/#assign-slotables-for-a-tree>
pub(crate) fn assign_slottables_for_a_tree(&self) {
// NOTE: This method traverses all descendants of the node and is potentially very
// expensive. If the node is not a shadow root then assigning slottables to it won't
// have any effect, so we take a fast path out.
if !self.is::<ShadowRoot>() {
return;
}

This assumption is wrong. If the root is not a shadow tree then we will never assign new slottables to it, but existing assigned slottables may be removed.

This snippet demonstrates the issue:

<body>
<div id="host">
Before
<div id="content" slot="foo">This is some Text</div>
</div>

<script defer>
let host = document.getElementById("host");
let root = host.attachShadow({mode: "closed"});
let slot = document.createElement("slot");
slot.style = "color: green;"
slot.name = "foo";
root.appendChild(slot);
slot.remove();

// Should be empty, the slot is not in the DOM tree anymore
console.log(slot.assignedNodes());
</script>

Without the fast path many tests start to fail in CI because they take too long, so we will have to come up with a new optimization instead. (At least this was the case in the past)

Also, the same optimization is present in ladybird (I originally copied it from them), so they're likely wrong too.

https://github.com/LadybirdBrowser/ladybird/blob/d4649db55e261e7fb747825f80cc6858ffe93fae/Libraries/LibWeb/DOM/Slottable.cpp#L178-L185

Part of #34318

Metadata

Metadata

Assignees

Labels

A-content/domInteracting with the DOM from web contentI-perf-slowUnnecessary performance degredation.I-wrongAn incorrect behaviour is observed.

Type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions