2025-03-17 13:00:12 -03:00
|
|
|
/*
|
2026-04-01 10:19:00 -03:00
|
|
|
* Copyright (c) 2025-2026, Sam Atkins <sam@ladybird.org>
|
2025-03-17 13:00:12 -03:00
|
|
|
*
|
|
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
#pragma once
|
|
|
|
|
|
|
|
|
|
#include <LibGC/Cell.h>
|
2025-09-09 09:37:30 -03:00
|
|
|
#include <LibWeb/CSS/PseudoElement.h>
|
2025-06-17 10:07:50 -03:00
|
|
|
#include <LibWeb/Forward.h>
|
2025-03-17 13:00:12 -03:00
|
|
|
|
|
|
|
|
namespace Web::DOM {
|
|
|
|
|
|
2025-06-18 06:19:56 -03:00
|
|
|
// Either an Element or a PseudoElement
|
2026-04-01 10:19:00 -03:00
|
|
|
// FIXME: Figure out const-correctness for the Element.
|
2025-09-07 10:27:04 -03:00
|
|
|
class WEB_API AbstractElement {
|
2025-03-17 13:00:12 -03:00
|
|
|
public:
|
2025-06-18 06:19:56 -03:00
|
|
|
AbstractElement(GC::Ref<Element>, Optional<CSS::PseudoElement> = {});
|
2026-04-01 10:19:00 -03:00
|
|
|
AbstractElement(Element const&, Optional<CSS::PseudoElement> = {});
|
2025-03-17 13:00:12 -03:00
|
|
|
|
2025-07-10 08:19:28 -03:00
|
|
|
Document& document() const;
|
|
|
|
|
|
2025-03-17 13:00:12 -03:00
|
|
|
Element& element() { return m_element; }
|
|
|
|
|
Element const& element() const { return m_element; }
|
2025-03-20 13:56:46 -03:00
|
|
|
Optional<CSS::PseudoElement> pseudo_element() const { return m_pseudo_element; }
|
2025-03-17 13:00:12 -03:00
|
|
|
|
LibWeb: Make layout nodes refcounted
Move the layout tree from GC allocation to refcounted ownership so
removed layout and paint subtrees are destroyed synchronously instead
of waiting for the next GC sweep. This dramatically reduces GC memory
usage peaks after layout tree churn and makes it easier for memory use
to fall back after large document updates.
Update layout factories, tree traversal, SVG layout node creation,
paintable back-pointers, and pseudo-element layout links to use RefPtr
ownership.
Make display: contents follow the same shape as Blink and WebKit: the
element itself does not create a layout node, and its children are
flattened into the nearest layout parent. Wrap direct non-whitespace
text in an anonymous inline node when the boxless element contributes
inherited style to that text.
Use an internal inline wrapper for display: contents pseudo-elements
so generated content can still participate in layout, painting, hit
testing, and pseudo-element queries. Keep CSSOM reporting the computed
display value from the pseudo style, not the internal wrapper.
Remove the retained out-of-tree layout node list and its testing hook,
since the flattened model does not need a side owner for boxless
elements. Add coverage for inherited text style, dynamic insertion
order, pseudo-element hit testing, and computed style queries.
2026-06-07 12:50:33 -03:00
|
|
|
Layout::NodeWithStyle* layout_node();
|
|
|
|
|
Layout::NodeWithStyle const* layout_node() const { return const_cast<AbstractElement*>(this)->layout_node(); }
|
2025-06-17 12:33:23 -03:00
|
|
|
|
LibWeb: Make layout nodes refcounted
Move the layout tree from GC allocation to refcounted ownership so
removed layout and paint subtrees are destroyed synchronously instead
of waiting for the next GC sweep. This dramatically reduces GC memory
usage peaks after layout tree churn and makes it easier for memory use
to fall back after large document updates.
Update layout factories, tree traversal, SVG layout node creation,
paintable back-pointers, and pseudo-element layout links to use RefPtr
ownership.
Make display: contents follow the same shape as Blink and WebKit: the
element itself does not create a layout node, and its children are
flattened into the nearest layout parent. Wrap direct non-whitespace
text in an anonymous inline node when the boxless element contributes
inherited style to that text.
Use an internal inline wrapper for display: contents pseudo-elements
so generated content can still participate in layout, painting, hit
testing, and pseudo-element queries. Keep CSSOM reporting the computed
display value from the pseudo style, not the internal wrapper.
Remove the retained out-of-tree layout node list and its testing hook,
since the flattened model does not need a side owner for boxless
elements. Add coverage for inherited text style, dynamic insertion
order, pseudo-element hit testing, and computed style queries.
2026-06-07 12:50:33 -03:00
|
|
|
Layout::NodeWithStyle* unsafe_layout_node();
|
|
|
|
|
Layout::NodeWithStyle const* unsafe_layout_node() const { return const_cast<AbstractElement*>(this)->unsafe_layout_node(); }
|
2026-02-26 07:57:29 -03:00
|
|
|
|
2025-10-21 08:06:38 -03:00
|
|
|
struct TreeCountingFunctionResolutionContext {
|
|
|
|
|
size_t sibling_count;
|
|
|
|
|
size_t sibling_index;
|
|
|
|
|
};
|
|
|
|
|
TreeCountingFunctionResolutionContext tree_counting_function_resolution_context() const;
|
2025-09-30 00:45:37 -03:00
|
|
|
|
2025-06-18 06:19:56 -03:00
|
|
|
GC::Ptr<Element const> parent_element() const;
|
2025-08-21 01:38:17 -03:00
|
|
|
Optional<AbstractElement> element_to_inherit_style_from() const;
|
2025-06-17 12:33:23 -03: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 06:21:55 -03:00
|
|
|
void set_inheritance_override(GC::Ref<Element> element) { m_inheritance_override = element; }
|
|
|
|
|
|
2026-06-06 09:03:41 -03:00
|
|
|
CSS::ComputedProperties const* computed_properties() const;
|
2026-05-16 07:47:04 -03:00
|
|
|
GC::Ptr<CSS::CSSStyleProperties const> inline_style() const;
|
2025-06-18 06:19:56 -03:00
|
|
|
|
2026-02-13 06:19:02 -03:00
|
|
|
void set_custom_property_data(RefPtr<CSS::CustomPropertyData const>);
|
|
|
|
|
[[nodiscard]] RefPtr<CSS::CustomPropertyData const> custom_property_data() const;
|
2026-06-08 15:31:27 -03:00
|
|
|
RefPtr<CSS::StyleValue const> get_custom_property(Utf16FlyString const& name) const;
|
2025-06-19 11:03:50 -03:00
|
|
|
|
2025-06-17 12:33:23 -03:00
|
|
|
bool has_non_empty_counters_set() const;
|
|
|
|
|
Optional<CSS::CountersSet const&> counters_set() const;
|
2025-06-18 06:19:56 -03:00
|
|
|
CSS::CountersSet& ensure_counters_set();
|
|
|
|
|
void set_counters_set(OwnPtr<CSS::CountersSet>&&);
|
|
|
|
|
|
2026-02-10 01:14:10 -03:00
|
|
|
HashMap<FlyString, GC::Ref<CSS::CSSAnimation>>* css_defined_animations() const;
|
2025-12-02 08:03:07 -03:00
|
|
|
void set_has_css_defined_animations();
|
|
|
|
|
|
2025-06-18 06:19:56 -03:00
|
|
|
void visit(GC::Cell::Visitor& visitor) const;
|
2025-03-17 13:00:12 -03:00
|
|
|
|
2025-06-17 12:33:23 -03: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 15:08:08 -03:00
|
|
|
CSS::StyleScope const& style_scope() const;
|
|
|
|
|
|
2025-03-17 13:00:12 -03:00
|
|
|
private:
|
2025-06-17 12:33:23 -03:00
|
|
|
enum class WalkMethod : u8 {
|
|
|
|
|
Previous,
|
|
|
|
|
PreviousSibling,
|
|
|
|
|
};
|
|
|
|
|
Optional<AbstractElement> walk_layout_tree(WalkMethod);
|
|
|
|
|
|
2025-03-17 13:00:12 -03:00
|
|
|
GC::Ref<Element> m_element;
|
2025-03-20 13:56:46 -03:00
|
|
|
Optional<CSS::PseudoElement> m_pseudo_element;
|
2025-10-20 06:21:55 -03:00
|
|
|
|
|
|
|
|
GC::Ptr<Element> m_inheritance_override;
|
2025-03-17 13:00:12 -03:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
}
|
2025-07-18 06:56:00 -03: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);
|
|
|
|
|
}
|
|
|
|
|
};
|