2025-06-18 06:19:56 -03:00
|
|
|
/*
|
2026-04-01 10:19:00 -03:00
|
|
|
* Copyright (c) 2024-2026, Sam Atkins <sam@ladybird.org>
|
2025-06-18 06:19:56 -03:00
|
|
|
*
|
|
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
|
|
|
|
*/
|
|
|
|
|
|
2026-06-06 09:03:41 -03:00
|
|
|
#include <LibWeb/CSS/ComputedProperties.h>
|
2026-06-19 11:49:20 -03:00
|
|
|
#include <LibWeb/CSS/CustomPropertyData.h>
|
2025-06-18 06:19:56 -03:00
|
|
|
#include <LibWeb/DOM/AbstractElement.h>
|
2026-02-08 12:42:02 -03:00
|
|
|
#include <LibWeb/DOM/Document.h>
|
2025-06-17 10:07:50 -03:00
|
|
|
#include <LibWeb/DOM/Element.h>
|
2026-05-16 07:47:04 -03:00
|
|
|
#include <LibWeb/DOM/PseudoElement.h>
|
2026-02-08 12:42:02 -03:00
|
|
|
#include <LibWeb/DOM/ShadowRoot.h>
|
2025-06-17 12:33:23 -03:00
|
|
|
#include <LibWeb/Layout/Node.h>
|
2025-06-18 06:19:56 -03:00
|
|
|
|
|
|
|
|
namespace Web::DOM {
|
|
|
|
|
|
|
|
|
|
AbstractElement::AbstractElement(GC::Ref<Element> element, Optional<CSS::PseudoElement> pseudo_element)
|
|
|
|
|
: m_element(element)
|
|
|
|
|
, m_pseudo_element(move(pseudo_element))
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
2026-04-01 10:19:00 -03:00
|
|
|
AbstractElement::AbstractElement(Element const& element, Optional<CSS::PseudoElement> pseudo_element)
|
|
|
|
|
: m_element(const_cast<Element&>(element))
|
|
|
|
|
, m_pseudo_element(move(pseudo_element))
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
2025-06-18 06:19:56 -03:00
|
|
|
void AbstractElement::visit(GC::Cell::Visitor& visitor) const
|
|
|
|
|
{
|
|
|
|
|
visitor.visit(m_element);
|
2025-10-20 06:21:55 -03:00
|
|
|
visitor.visit(m_inheritance_override);
|
2025-06-18 06:19:56 -03:00
|
|
|
}
|
|
|
|
|
|
2025-07-10 08:19:28 -03:00
|
|
|
Document& AbstractElement::document() const
|
|
|
|
|
{
|
|
|
|
|
return m_element->document();
|
|
|
|
|
}
|
|
|
|
|
|
2025-10-21 08:06:38 -03:00
|
|
|
AbstractElement::TreeCountingFunctionResolutionContext AbstractElement::tree_counting_function_resolution_context() const
|
2025-09-30 00:45:37 -03:00
|
|
|
{
|
|
|
|
|
// FIXME: When used on an element-backed pseudo-element which is also a real element, the tree counting functions
|
|
|
|
|
// resolve for that real element. For other pseudo elements, they resolve as if they were resolved against
|
|
|
|
|
// the originating element. It follows that for nested pseudo elements the resolution will recursively walk
|
|
|
|
|
// the originating elements until a real element is found.
|
|
|
|
|
|
|
|
|
|
// FIXME: A tree counting function is a tree-scoped reference where it references an implicit tree-scoped name for
|
|
|
|
|
// the element it resolves against. This is done to not leak tree information to an outer tree. A tree
|
|
|
|
|
// counting function that is scoped to an outer tree relative to the element it resolves against, will alway
|
|
|
|
|
// resolve to 0.
|
|
|
|
|
auto const& element_to_resolve_tree_counting_function_against = element();
|
|
|
|
|
|
|
|
|
|
// The sibling-count() functional notation represents, as an <integer>, the total number of child elements in the
|
|
|
|
|
// parent of the element on which the notation is used.
|
|
|
|
|
auto const& parent = element_to_resolve_tree_counting_function_against.parent_element();
|
|
|
|
|
|
|
|
|
|
// If there is no parent we are the root node
|
|
|
|
|
if (!parent)
|
|
|
|
|
return { .sibling_count = 1, .sibling_index = 1 };
|
|
|
|
|
|
|
|
|
|
size_t count = 0;
|
|
|
|
|
size_t index = 0;
|
|
|
|
|
|
|
|
|
|
for (auto const* child = parent->first_child_of_type<DOM::Element>(); child; child = child->next_element_sibling()) {
|
|
|
|
|
++count;
|
|
|
|
|
if (child == &element_to_resolve_tree_counting_function_against)
|
|
|
|
|
index = count;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return {
|
|
|
|
|
.sibling_count = count,
|
|
|
|
|
.sibling_index = index
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
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* AbstractElement::layout_node()
|
2025-06-17 12:33:23 -03:00
|
|
|
{
|
|
|
|
|
if (m_pseudo_element.has_value())
|
2026-05-15 04:33:12 -03:00
|
|
|
return m_element->pseudo_element_layout_node(*m_pseudo_element);
|
2025-06-17 12:33:23 -03:00
|
|
|
return m_element->layout_node();
|
|
|
|
|
}
|
|
|
|
|
|
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* AbstractElement::unsafe_layout_node()
|
2026-02-26 07:57:29 -03:00
|
|
|
{
|
|
|
|
|
if (m_pseudo_element.has_value())
|
2026-05-15 04:31:27 -03:00
|
|
|
return m_element->pseudo_element_unsafe_layout_node(*m_pseudo_element);
|
2026-02-26 07:57:29 -03:00
|
|
|
return m_element->unsafe_layout_node();
|
|
|
|
|
}
|
|
|
|
|
|
2025-06-18 06:19:56 -03:00
|
|
|
GC::Ptr<Element const> AbstractElement::parent_element() const
|
|
|
|
|
{
|
|
|
|
|
if (m_pseudo_element.has_value())
|
|
|
|
|
return m_element;
|
|
|
|
|
return m_element->parent_element();
|
|
|
|
|
}
|
|
|
|
|
|
2025-08-21 01:38:17 -03:00
|
|
|
Optional<AbstractElement> AbstractElement::element_to_inherit_style_from() const
|
2025-08-16 09:08:35 -03:00
|
|
|
{
|
2025-10-20 06:21:55 -03:00
|
|
|
if (m_inheritance_override)
|
|
|
|
|
return AbstractElement { *m_inheritance_override };
|
|
|
|
|
|
2025-08-21 01:38:17 -03:00
|
|
|
GC::Ptr<Element const> element = m_element->element_to_inherit_style_from(m_pseudo_element);
|
|
|
|
|
|
|
|
|
|
if (!element)
|
|
|
|
|
return OptionalNone {};
|
|
|
|
|
|
|
|
|
|
return AbstractElement { const_cast<DOM::Element&>(*element) };
|
2025-08-16 09:08:35 -03:00
|
|
|
}
|
|
|
|
|
|
2025-06-17 12:33:23 -03:00
|
|
|
Optional<AbstractElement> AbstractElement::walk_layout_tree(WalkMethod walk_method)
|
|
|
|
|
{
|
2026-02-26 07:57:29 -03:00
|
|
|
// NB: Called during style recalculation.
|
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::Node* node = unsafe_layout_node();
|
2025-06-17 12:33:23 -03:00
|
|
|
if (!node)
|
|
|
|
|
return OptionalNone {};
|
|
|
|
|
|
|
|
|
|
while (true) {
|
|
|
|
|
switch (walk_method) {
|
|
|
|
|
case WalkMethod::Previous:
|
|
|
|
|
node = node->previous_in_pre_order();
|
|
|
|
|
break;
|
|
|
|
|
case WalkMethod::PreviousSibling:
|
|
|
|
|
node = node->previous_sibling();
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
if (!node)
|
|
|
|
|
return OptionalNone {};
|
|
|
|
|
|
|
|
|
|
if (auto* previous_element = as_if<Element>(node->dom_node()))
|
|
|
|
|
return AbstractElement { *previous_element };
|
|
|
|
|
|
2025-07-25 23:40:34 -03:00
|
|
|
if (node->is_generated_for_pseudo_element())
|
2025-06-17 12:33:23 -03:00
|
|
|
return AbstractElement { *node->pseudo_element_generator(), node->generated_for_pseudo_element() };
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool AbstractElement::is_before(AbstractElement const& other) const
|
|
|
|
|
{
|
2026-02-26 07:57:29 -03:00
|
|
|
// NB: Called during style recalculation.
|
|
|
|
|
auto this_node = unsafe_layout_node();
|
|
|
|
|
auto other_node = other.unsafe_layout_node();
|
2025-06-17 12:33:23 -03:00
|
|
|
return this_node && other_node && this_node->is_before(*other_node);
|
|
|
|
|
}
|
|
|
|
|
|
2026-06-06 09:03:41 -03:00
|
|
|
CSS::ComputedProperties const* AbstractElement::computed_properties() const
|
2025-06-18 06:19:56 -03:00
|
|
|
{
|
2025-07-18 14:34:58 -03:00
|
|
|
return m_element->computed_properties(m_pseudo_element);
|
2025-06-18 06:19:56 -03:00
|
|
|
}
|
|
|
|
|
|
2026-05-16 07:47:04 -03:00
|
|
|
GC::Ptr<CSS::CSSStyleProperties const> AbstractElement::inline_style() const
|
|
|
|
|
{
|
|
|
|
|
if (!m_pseudo_element.has_value())
|
|
|
|
|
return m_element->inline_style();
|
|
|
|
|
|
|
|
|
|
if (!CSS::is_element_reference_pseudo_element(*m_pseudo_element))
|
|
|
|
|
return nullptr;
|
|
|
|
|
|
|
|
|
|
auto pseudo_element = m_element->get_pseudo_element(*m_pseudo_element);
|
|
|
|
|
|
|
|
|
|
if (!pseudo_element.has_value())
|
|
|
|
|
return nullptr;
|
|
|
|
|
|
|
|
|
|
return as<ElementReferencePseudoElement>(*pseudo_element).referenced_element()->inline_style();
|
|
|
|
|
}
|
|
|
|
|
|
2026-02-13 06:19:02 -03:00
|
|
|
RefPtr<CSS::CustomPropertyData const> AbstractElement::custom_property_data() const
|
2025-06-19 11:03:50 -03:00
|
|
|
{
|
2026-02-13 06:19:02 -03:00
|
|
|
return m_element->custom_property_data(m_pseudo_element);
|
2025-06-19 11:03:50 -03:00
|
|
|
}
|
|
|
|
|
|
2026-02-13 06:19:02 -03:00
|
|
|
void AbstractElement::set_custom_property_data(RefPtr<CSS::CustomPropertyData const> data)
|
2025-06-19 11:03:50 -03:00
|
|
|
{
|
2026-02-13 06:19:02 -03:00
|
|
|
m_element->set_custom_property_data(m_pseudo_element, move(data));
|
2025-06-19 11:03:50 -03:00
|
|
|
}
|
|
|
|
|
|
2026-06-08 15:31:27 -03:00
|
|
|
RefPtr<CSS::StyleValue const> AbstractElement::get_custom_property(Utf16FlyString const& name) const
|
2025-06-19 11:03:50 -03:00
|
|
|
{
|
2026-02-13 06:19:02 -03:00
|
|
|
auto data = custom_property_data();
|
|
|
|
|
if (!data)
|
|
|
|
|
return nullptr;
|
|
|
|
|
if (auto const* property = data->get(name))
|
|
|
|
|
return property->value;
|
2025-06-19 11:03:50 -03:00
|
|
|
return nullptr;
|
|
|
|
|
}
|
|
|
|
|
|
2025-06-17 12:33:23 -03:00
|
|
|
bool AbstractElement::has_non_empty_counters_set() const
|
|
|
|
|
{
|
|
|
|
|
if (m_pseudo_element.has_value())
|
2026-05-15 01:08:04 -03:00
|
|
|
return m_element->get_synthetic_pseudo_element(*m_pseudo_element)->has_non_empty_counters_set();
|
2025-06-17 12:33:23 -03:00
|
|
|
return m_element->has_non_empty_counters_set();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Optional<CSS::CountersSet const&> AbstractElement::counters_set() const
|
|
|
|
|
{
|
|
|
|
|
if (m_pseudo_element.has_value())
|
2026-05-15 01:08:04 -03:00
|
|
|
return m_element->get_synthetic_pseudo_element(*m_pseudo_element)->counters_set();
|
2025-06-17 12:33:23 -03:00
|
|
|
return m_element->counters_set();
|
|
|
|
|
}
|
|
|
|
|
|
2025-06-18 06:19:56 -03:00
|
|
|
CSS::CountersSet& AbstractElement::ensure_counters_set()
|
|
|
|
|
{
|
2025-06-17 12:33:23 -03:00
|
|
|
if (m_pseudo_element.has_value())
|
2026-05-15 01:08:04 -03:00
|
|
|
return m_element->get_synthetic_pseudo_element(*m_pseudo_element)->ensure_counters_set();
|
2025-06-18 06:19:56 -03:00
|
|
|
return m_element->ensure_counters_set();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void AbstractElement::set_counters_set(OwnPtr<CSS::CountersSet>&& counters_set)
|
|
|
|
|
{
|
2025-06-17 12:33:23 -03:00
|
|
|
if (m_pseudo_element.has_value()) {
|
2026-05-15 01:08:04 -03:00
|
|
|
m_element->get_synthetic_pseudo_element(*m_pseudo_element)->set_counters_set(move(counters_set));
|
2025-06-17 12:33:23 -03:00
|
|
|
} else {
|
|
|
|
|
m_element->set_counters_set(move(counters_set));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
String AbstractElement::debug_description() const
|
|
|
|
|
{
|
|
|
|
|
if (m_pseudo_element.has_value()) {
|
|
|
|
|
StringBuilder builder;
|
|
|
|
|
builder.append(m_element->debug_description());
|
|
|
|
|
builder.append("::"sv);
|
|
|
|
|
builder.append(CSS::pseudo_element_name(*m_pseudo_element));
|
|
|
|
|
return builder.to_string_without_validation();
|
|
|
|
|
}
|
|
|
|
|
return m_element->debug_description();
|
2025-06-18 06:19:56 -03:00
|
|
|
}
|
|
|
|
|
|
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& AbstractElement::style_scope() const
|
|
|
|
|
{
|
2026-04-25 12:27:06 -03:00
|
|
|
return m_element->style_scope();
|
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
|
|
|
}
|
|
|
|
|
|
2026-02-10 01:14:10 -03:00
|
|
|
HashMap<FlyString, GC::Ref<CSS::CSSAnimation>>* AbstractElement::css_defined_animations() const
|
2025-12-02 08:03:07 -03:00
|
|
|
{
|
|
|
|
|
return m_element->css_defined_animations(m_pseudo_element);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void AbstractElement::set_has_css_defined_animations()
|
|
|
|
|
{
|
|
|
|
|
m_element->set_has_css_defined_animations();
|
|
|
|
|
}
|
|
|
|
|
|
2025-06-18 06:19:56 -03:00
|
|
|
}
|