mirror of
https://github.com/LadybirdBrowser/ladybird
synced 2026-04-26 01:35:08 +02:00
A DOM mutation under a document that uses any :has() rule currently walks every ancestor up to the root, invoking invalidate_style_if_ affected_by_has() on each. Most of those ancestors have nothing to do with :has(), so the work scales linearly with DOM depth. Introduce an in_has_scope flag on Element, set while evaluating :has() arguments for invalidation metadata. StyleScope's upward invalidation walk now terminates at the first element that is neither in :has() scope nor a :has() anchor, so it only traverses the region where some :has() rule might actually care about the change. Keep the existing fast :has() matching paths for normal selector matching, but bypass them while collecting per-element metadata so the scope markers still get populated. Node insertion also schedules the parent for the :has() walk so newly inserted nodes still reach the real anchor. The css-has-invalidation suite adds focused coverage for these shapes and updates the expected counters to reflect the shorter walks.
67 lines
2.3 KiB
C++
67 lines
2.3 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 };
|
|
bool for_host_part_matching { false };
|
|
// True while we are evaluating the argument of a :has() pseudo-class.
|
|
// Elements visited by selector walks (descendants, siblings, etc.) while
|
|
// this is set get marked as in_has_scope so the invalidation walker can
|
|
// later terminate once it leaves the scope. Transparent to callers; set
|
|
// by matches_has_pseudo_class with a ScopeGuard.
|
|
bool inside_has_argument_match { false };
|
|
CSS::PseudoClassBitmap attempted_pseudo_class_matches {};
|
|
HasResultCache* has_result_cache { nullptr };
|
|
};
|
|
|
|
bool matches(CSS::Selector const&, DOM::AbstractElement const&, GC::Ptr<DOM::Element const> shadow_host, MatchContext& context, GC::Ptr<DOM::ParentNode const> scope = {}, SelectorKind selector_kind = SelectorKind::Normal, GC::Ptr<DOM::Element const> anchor = nullptr);
|
|
|
|
}
|