2025-03-17 16:00:12 +00:00
|
|
|
/*
|
|
|
|
|
* Copyright (c) 2025, Sam Atkins <sam@ladybird.org>
|
|
|
|
|
*
|
|
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
#pragma once
|
|
|
|
|
|
|
|
|
|
#include <LibGC/Cell.h>
|
2025-09-09 13:37:30 +01:00
|
|
|
#include <LibWeb/CSS/PseudoElement.h>
|
|
|
|
|
#include <LibWeb/CSS/StyleProperty.h>
|
2025-06-17 14:07:50 +01:00
|
|
|
#include <LibWeb/Forward.h>
|
2025-03-17 16:00:12 +00:00
|
|
|
|
|
|
|
|
namespace Web::DOM {
|
|
|
|
|
|
2025-06-18 10:19:56 +01:00
|
|
|
// Either an Element or a PseudoElement
|
2025-09-07 14:27:04 +01:00
|
|
|
class WEB_API AbstractElement {
|
2025-03-17 16:00:12 +00:00
|
|
|
public:
|
2025-06-18 10:19:56 +01:00
|
|
|
AbstractElement(GC::Ref<Element>, Optional<CSS::PseudoElement> = {});
|
2025-03-17 16:00:12 +00:00
|
|
|
|
2025-07-10 12:19:28 +01:00
|
|
|
Document& document() const;
|
|
|
|
|
|
2025-03-17 16:00:12 +00:00
|
|
|
Element& element() { return m_element; }
|
|
|
|
|
Element const& element() const { return m_element; }
|
2025-03-20 16:56:46 +00:00
|
|
|
Optional<CSS::PseudoElement> pseudo_element() const { return m_pseudo_element; }
|
2025-03-17 16:00:12 +00:00
|
|
|
|
2025-06-17 16:33:23 +01:00
|
|
|
GC::Ptr<Layout::NodeWithStyle> layout_node();
|
|
|
|
|
GC::Ptr<Layout::NodeWithStyle const> layout_node() const { return const_cast<AbstractElement*>(this)->layout_node(); }
|
|
|
|
|
|
2025-10-22 00:06:38 +13:00
|
|
|
struct TreeCountingFunctionResolutionContext {
|
|
|
|
|
size_t sibling_count;
|
|
|
|
|
size_t sibling_index;
|
|
|
|
|
};
|
|
|
|
|
TreeCountingFunctionResolutionContext tree_counting_function_resolution_context() const;
|
2025-09-30 16:45:37 +13:00
|
|
|
|
2025-06-18 10:19:56 +01:00
|
|
|
GC::Ptr<Element const> parent_element() const;
|
2025-08-21 16:38:17 +12:00
|
|
|
Optional<AbstractElement> element_to_inherit_style_from() const;
|
2025-06-17 16:33:23 +01:00
|
|
|
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;
|
|
|
|
|
|
2025-10-20 11:21:55 +02:00
|
|
|
void set_inheritance_override(GC::Ref<Element> element) { m_inheritance_override = element; }
|
|
|
|
|
|
2025-06-18 10:19:56 +01:00
|
|
|
GC::Ptr<CSS::ComputedProperties const> computed_properties() const;
|
|
|
|
|
|
2025-09-18 11:44:56 +01:00
|
|
|
void set_custom_properties(OrderedHashMap<FlyString, CSS::StyleProperty>&& custom_properties);
|
|
|
|
|
[[nodiscard]] OrderedHashMap<FlyString, CSS::StyleProperty> const& custom_properties() const;
|
2025-08-08 10:11:51 +01:00
|
|
|
RefPtr<CSS::StyleValue const> get_custom_property(FlyString const& name) const;
|
2025-06-19 15:03:50 +01:00
|
|
|
|
2025-09-09 12:09:50 +01:00
|
|
|
GC::Ptr<CSS::CascadedProperties> cascaded_properties() const;
|
|
|
|
|
void set_cascaded_properties(GC::Ptr<CSS::CascadedProperties>);
|
|
|
|
|
|
2025-06-17 16:33:23 +01:00
|
|
|
bool has_non_empty_counters_set() const;
|
|
|
|
|
Optional<CSS::CountersSet const&> counters_set() const;
|
2025-06-18 10:19:56 +01:00
|
|
|
CSS::CountersSet& ensure_counters_set();
|
|
|
|
|
void set_counters_set(OwnPtr<CSS::CountersSet>&&);
|
|
|
|
|
|
|
|
|
|
void visit(GC::Cell::Visitor& visitor) const;
|
2025-03-17 16:00:12 +00:00
|
|
|
|
2025-06-17 16:33:23 +01:00
|
|
|
String debug_description() const;
|
|
|
|
|
bool operator==(AbstractElement const&) const = default;
|
|
|
|
|
|
LibWeb: Add StyleScope to keep style caches per Document/ShadowRoot
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.
2025-11-13 19:08:08 +01:00
|
|
|
CSS::StyleScope const& style_scope() const;
|
|
|
|
|
|
2025-03-17 16:00:12 +00:00
|
|
|
private:
|
2025-06-17 16:33:23 +01:00
|
|
|
enum class WalkMethod : u8 {
|
|
|
|
|
Previous,
|
|
|
|
|
PreviousSibling,
|
|
|
|
|
};
|
|
|
|
|
Optional<AbstractElement> walk_layout_tree(WalkMethod);
|
|
|
|
|
|
2025-03-17 16:00:12 +00:00
|
|
|
GC::Ref<Element> m_element;
|
2025-03-20 16:56:46 +00:00
|
|
|
Optional<CSS::PseudoElement> m_pseudo_element;
|
2025-10-20 11:21:55 +02:00
|
|
|
|
|
|
|
|
GC::Ptr<Element> m_inheritance_override;
|
2025-03-17 16:00:12 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
}
|
2025-07-18 11:56:00 +02:00
|
|
|
|
|
|
|
|
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);
|
|
|
|
|
}
|
|
|
|
|
};
|