ladybird/Libraries/LibWeb/CSS/SelectorEngine.h
Andreas Kling 223ca14abc LibWeb: Support :host::part() selectors within shadow DOM stylesheets
The :host::part() pattern allows a shadow DOM's own stylesheet to
style its internal elements that have been exposed via the part
attribute. Previously, ::part() rules were only collected from
ancestor shadow roots (for external part styling), but never from the
element's own containing shadow root.

Fix this by also collecting ::part() rules from the element's own
shadow root in collect_matching_rules(). Additionally, fix the
selector engine's ::part() compound matching to preserve the
shadow_host when the rule originates from the part element's own
shadow root, allowing :host to match correctly in the same compound
selector.

This fixes 2 previously failing WPT tests:
- css/css-shadow-parts/host-part-002.html
- css/css-shadow-parts/host-part-nesting.html
2026-03-21 21:42:44 -05:00

59 lines
1.8 KiB
C++

/*
* Copyright (c) 2018-2024, Andreas Kling <andreas@ladybird.org>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#pragma once
#include <AK/HashMap.h>
#include <LibWeb/CSS/Selector.h>
#include <LibWeb/DOM/Element.h>
namespace Web::SelectorEngine {
enum class SelectorKind {
Normal,
Relative,
};
enum class HasMatchResult : u8 {
Matched,
NotMatched,
};
struct HasResultCacheKey {
CSS::Selector const* selector;
GC::Ptr<DOM::Element const> element;
void visit_edges(GC::Cell::Visitor& visitor)
{
visitor.visit(element);
}
bool operator==(HasResultCacheKey const&) const = default;
};
struct HasResultCacheKeyTraits : Traits<HasResultCacheKey> {
static unsigned hash(HasResultCacheKey const& key)
{
return pair_int_hash(ptr_hash(key.selector), ptr_hash(key.element.ptr()));
}
};
using HasResultCache = HashMap<HasResultCacheKey, HasMatchResult, HasResultCacheKeyTraits>;
struct MatchContext {
GC::Ptr<CSS::CSSStyleSheet const> style_sheet_for_rule {};
GC::Ptr<DOM::Element const> subject {};
GC::Ptr<DOM::Element const> slotted_element {}; // Only set when matching a ::slotted() pseudo-element
GC::Ptr<DOM::Element const> part_owning_parent {}; // Only set temporarily when matching a ::part() pseudo-element
GC::Ptr<DOM::ShadowRoot const> rule_shadow_root {}; // Shadow root the matched rule belongs to
bool collect_per_element_selector_involvement_metadata { false };
CSS::PseudoClassBitmap attempted_pseudo_class_matches {};
HasResultCache* has_result_cache { nullptr };
};
bool matches(CSS::Selector const&, DOM::Element const&, GC::Ptr<DOM::Element const> shadow_host, MatchContext& context, Optional<CSS::PseudoElement> = {}, GC::Ptr<DOM::ParentNode const> scope = {}, SelectorKind selector_kind = SelectorKind::Normal, GC::Ptr<DOM::Element const> anchor = nullptr);
}