mirror of
https://github.com/LadybirdBrowser/ladybird.git
synced 2025-12-07 21:59:54 +00:00
Before this change, we've been maintaining various StyleComputer caches at the document level. This made sense for old-school documents without shadow trees, since all the style information was document-wide anyway. However, documents with many shadow trees ended up suffering since any time you mutated a style sheet inside a shadow tree, *all* style caches for the entire document would get invalidated. This was particularly expensive on Reddit, which has tons of shadow trees with their own style elements. Every time we'd create one of their custom elements, we'd invalidate the document-level "rule cache" and have to rebuild it, taking about ~60ms each time (ouch). This commit introduces a new object called StyleScope. Every Document and ShadowRoot has its own StyleScope. Rule caches etc are moved from StyleComputer to StyleScope. Rule cache invalidation now happens at StyleScope level. As an example, rule cache rebuilds now take ~1ms on Reddit instead of ~60ms. This is largely a mechanical change, moving things around, but there's one key detail to be aware of: due to the :host selector, which works across the shadow DOM boundary and reaches from inside a shadow tree out into the light tree, there are various places where we have to check both the shadow tree's StyleScope *and* the document-level StyleScope in order to get all rules that may apply.
86 lines
3 KiB
C++
86 lines
3 KiB
C++
/*
|
|
* Copyright (c) 2025, Sam Atkins <sam@ladybird.org>
|
|
*
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
|
*/
|
|
|
|
#pragma once
|
|
|
|
#include <LibGC/Cell.h>
|
|
#include <LibWeb/CSS/PseudoElement.h>
|
|
#include <LibWeb/CSS/StyleProperty.h>
|
|
#include <LibWeb/Forward.h>
|
|
|
|
namespace Web::DOM {
|
|
|
|
// Either an Element or a PseudoElement
|
|
class WEB_API AbstractElement {
|
|
public:
|
|
AbstractElement(GC::Ref<Element>, Optional<CSS::PseudoElement> = {});
|
|
|
|
Document& document() const;
|
|
|
|
Element& element() { return m_element; }
|
|
Element const& element() const { return m_element; }
|
|
Optional<CSS::PseudoElement> pseudo_element() const { return m_pseudo_element; }
|
|
|
|
GC::Ptr<Layout::NodeWithStyle> layout_node();
|
|
GC::Ptr<Layout::NodeWithStyle const> layout_node() const { return const_cast<AbstractElement*>(this)->layout_node(); }
|
|
|
|
struct TreeCountingFunctionResolutionContext {
|
|
size_t sibling_count;
|
|
size_t sibling_index;
|
|
};
|
|
TreeCountingFunctionResolutionContext tree_counting_function_resolution_context() const;
|
|
|
|
GC::Ptr<Element const> parent_element() const;
|
|
Optional<AbstractElement> element_to_inherit_style_from() const;
|
|
Optional<AbstractElement> previous_in_tree_order() { return walk_layout_tree(WalkMethod::Previous); }
|
|
Optional<AbstractElement> previous_sibling_in_tree_order() { return walk_layout_tree(WalkMethod::PreviousSibling); }
|
|
bool is_before(AbstractElement const&) const;
|
|
|
|
void set_inheritance_override(GC::Ref<Element> element) { m_inheritance_override = element; }
|
|
|
|
GC::Ptr<CSS::ComputedProperties const> computed_properties() const;
|
|
|
|
void set_custom_properties(OrderedHashMap<FlyString, CSS::StyleProperty>&& custom_properties);
|
|
[[nodiscard]] OrderedHashMap<FlyString, CSS::StyleProperty> const& custom_properties() const;
|
|
RefPtr<CSS::StyleValue const> get_custom_property(FlyString const& name) const;
|
|
|
|
GC::Ptr<CSS::CascadedProperties> cascaded_properties() const;
|
|
void set_cascaded_properties(GC::Ptr<CSS::CascadedProperties>);
|
|
|
|
bool has_non_empty_counters_set() const;
|
|
Optional<CSS::CountersSet const&> counters_set() const;
|
|
CSS::CountersSet& ensure_counters_set();
|
|
void set_counters_set(OwnPtr<CSS::CountersSet>&&);
|
|
|
|
void visit(GC::Cell::Visitor& visitor) const;
|
|
|
|
String debug_description() const;
|
|
bool operator==(AbstractElement const&) const = default;
|
|
|
|
CSS::StyleScope const& style_scope() const;
|
|
|
|
private:
|
|
enum class WalkMethod : u8 {
|
|
Previous,
|
|
PreviousSibling,
|
|
};
|
|
Optional<AbstractElement> walk_layout_tree(WalkMethod);
|
|
|
|
GC::Ref<Element> m_element;
|
|
Optional<CSS::PseudoElement> m_pseudo_element;
|
|
|
|
GC::Ptr<Element> m_inheritance_override;
|
|
};
|
|
|
|
}
|
|
|
|
template<>
|
|
struct AK::Traits<Web::DOM::AbstractElement> : public DefaultTraits<Web::DOM::AbstractElement> {
|
|
static unsigned hash(Web::DOM::AbstractElement const& key)
|
|
{
|
|
return pair_int_hash(ptr_hash(&key.element()), key.pseudo_element().has_value() ? to_underlying(key.pseudo_element().value()) : -1);
|
|
}
|
|
};
|