ladybird/Libraries/LibWeb/DOM/PseudoElement.h
Andreas Kling 9340d2d1a3 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 20:52:49 +02:00

105 lines
3.8 KiB
C++

/*
* Copyright (c) 2025, Sam Atkins <sam@ladybird.org>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#pragma once
#include <AK/OwnPtr.h>
#include <AK/WeakPtr.h>
#include <LibGC/CellAllocator.h>
#include <LibJS/Heap/Cell.h>
#include <LibWeb/CSS/ComputedProperties.h>
#include <LibWeb/CSS/CustomPropertyData.h>
#include <LibWeb/Export.h>
#include <LibWeb/Forward.h>
#include <LibWeb/PixelUnits.h>
#include <LibWeb/TreeNode.h>
namespace Web::DOM {
class WEB_API PseudoElement : public JS::Cell {
GC_CELL(PseudoElement, JS::Cell);
GC_DECLARE_ALLOCATOR(PseudoElement);
public:
virtual Layout::NodeWithStyle* layout_node() const = 0;
virtual Layout::NodeWithStyle* unsafe_layout_node() const = 0;
virtual RefPtr<CSS::ComputedProperties> computed_properties() const = 0;
virtual RefPtr<CSS::CustomPropertyData const> custom_property_data() const = 0;
virtual void set_custom_property_data(RefPtr<CSS::CustomPropertyData const> value) = 0;
};
class WEB_API SyntheticPseudoElement : public PseudoElement {
GC_CELL(SyntheticPseudoElement, PseudoElement);
GC_DECLARE_ALLOCATOR(SyntheticPseudoElement);
Layout::NodeWithStyle* layout_node() const override { return m_layout_node.ptr(); }
Layout::NodeWithStyle* unsafe_layout_node() const override { return m_layout_node.ptr(); }
void set_layout_node(Layout::NodeWithStyle*);
RefPtr<CSS::ComputedProperties> computed_properties() const override { return m_computed_properties; }
void set_computed_properties(RefPtr<CSS::ComputedProperties> value) { m_computed_properties = value; }
RefPtr<CSS::CustomPropertyData const> custom_property_data() const override { return m_custom_property_data; }
void set_custom_property_data(RefPtr<CSS::CustomPropertyData const> value) override { m_custom_property_data = move(value); }
bool has_non_empty_counters_set() const { return m_counters_set; }
Optional<CSS::CountersSet const&> counters_set() const;
CSS::CountersSet& ensure_counters_set();
void set_counters_set(OwnPtr<CSS::CountersSet>&&);
CSSPixelPoint scroll_offset() const { return m_scroll_offset; }
void set_scroll_offset(CSSPixelPoint value) { m_scroll_offset = value; }
virtual void visit_edges(JS::Cell::Visitor&) override;
private:
WeakPtr<Layout::NodeWithStyle> m_layout_node;
RefPtr<CSS::ComputedProperties> m_computed_properties;
RefPtr<CSS::CustomPropertyData const> m_custom_property_data;
OwnPtr<CSS::CountersSet> m_counters_set;
CSSPixelPoint m_scroll_offset {};
};
// https://drafts.csswg.org/css-view-transitions/#pseudo-element-tree
class SyntheticPseudoElementTreeNode
: public SyntheticPseudoElement
, public TreeNode<SyntheticPseudoElementTreeNode> {
GC_CELL(SyntheticPseudoElementTreeNode, SyntheticPseudoElement);
GC_DECLARE_ALLOCATOR(SyntheticPseudoElementTreeNode);
protected:
virtual void visit_edges(JS::Cell::Visitor& visitor) override;
};
class WEB_API ElementReferencePseudoElement : public PseudoElement {
GC_CELL(ElementReferencePseudoElement, PseudoElement);
GC_DECLARE_ALLOCATOR(ElementReferencePseudoElement);
ElementReferencePseudoElement(GC::Ref<Element> referenced_element)
: m_referenced_element(referenced_element)
{
}
Layout::NodeWithStyle* layout_node() const override;
Layout::NodeWithStyle* unsafe_layout_node() const override;
RefPtr<CSS::ComputedProperties> computed_properties() const override;
RefPtr<CSS::CustomPropertyData const> custom_property_data() const override;
void set_custom_property_data(RefPtr<CSS::CustomPropertyData const> value) override;
GC::Ref<Element> const& referenced_element() const { return m_referenced_element; }
protected:
virtual void visit_edges(JS::Cell::Visitor& visitor) override;
private:
GC::Ref<Element> m_referenced_element;
};
}