diff --git a/Libraries/LibWeb/DOM/Slottable.cpp b/Libraries/LibWeb/DOM/Slottable.cpp index 10d648c606b..0ec372e6f7e 100644 --- a/Libraries/LibWeb/DOM/Slottable.cpp +++ b/Libraries/LibWeb/DOM/Slottable.cpp @@ -1,5 +1,6 @@ /* * Copyright (c) 2023, Tim Flynn + * Copyright (c) 2025, Shannon Booth * * SPDX-License-Identifier: BSD-2-Clause */ @@ -154,6 +155,54 @@ Vector find_slottables(GC::Ref slot) return result; } +// https://dom.spec.whatwg.org/#find-flattened-slotables +Vector find_flattened_slottables(GC::Ref slot) +{ + // 1. Let result be « ». + Vector result; + + // 2. If slot’s root is not a shadow root, then return result. + if (!slot->root().is_shadow_root()) + return result; + + // 3. Let slottables be the result of finding slottables given slot. + auto slottables = find_slottables(slot); + + // 4. If slottables is the empty list, then append each slottable child of slot, in tree order, to slottables. + if (slottables.is_empty()) { + slot->for_each_child([&](auto& node) { + if (!node.is_slottable()) + return IterationDecision::Continue; + + slottables.append(node.as_slottable()); + return IterationDecision::Continue; + }); + } + + // 5. For each node of slottables: + for (auto& node : slottables) { + // 1. If node is a slot whose root is a shadow root: + // 1. Let temporaryResult be the result of finding flattened slottables given node. + // 2. Append each slottable in temporaryResult, in order, to result. + // 2. Otherwise, append node to result. + auto* maybe_slot = node.get_pointer>(); + if (!maybe_slot) { + result.append(node); + continue; + } + + if (auto* slot = as_if(maybe_slot->ptr()); slot && slot->root().is_shadow_root()) { + auto temporary_result = find_flattened_slottables(*slot); + result.extend(temporary_result); + } else { + result.append(node); + } + } + + // 6. Return result. + return result; +} + // https://dom.spec.whatwg.org/#assign-slotables void assign_slottables(GC::Ref slot) { diff --git a/Libraries/LibWeb/DOM/Slottable.h b/Libraries/LibWeb/DOM/Slottable.h index 46a2bfaeb84..e538752740e 100644 --- a/Libraries/LibWeb/DOM/Slottable.h +++ b/Libraries/LibWeb/DOM/Slottable.h @@ -57,6 +57,7 @@ bool is_an_assigned_slottable(GC::Ref); GC::Ptr find_a_slot(Slottable const&, OpenFlag = OpenFlag::Unset); Vector find_slottables(GC::Ref); +Vector find_flattened_slottables(GC::Ref); void assign_slottables(GC::Ref); void assign_slottables_for_a_tree(GC::Ref); void assign_a_slot(Slottable const&);