LibWeb: Allow to pass pseudo-element type in computed properties getter

...and setter. We had lots of places where we check if pseudo-element
type is specified and then use `pseudo_element_computed_properties()` or
`computed_properties()`. This change moves these checks from caller side
to the getter and setter.
This commit is contained in:
Aliaksandr Kalenik 2025-07-18 19:34:58 +02:00 committed by Andreas Kling
commit 2fc405f1b2
Notes: github-actions[bot] 2025-07-18 19:20:46 +00:00
7 changed files with 48 additions and 70 deletions

View file

@ -946,27 +946,17 @@ void KeyframeEffect::update_computed_properties()
return; return;
} }
GC::Ptr<CSS::ComputedProperties> style = {}; auto computed_properties = target->computed_properties(pseudo_element_type());
if (!pseudo_element_type().has_value()) if (!computed_properties)
style = target->computed_properties();
else
style = target->pseudo_element_computed_properties(pseudo_element_type().value());
if (!style)
return; return;
auto animated_properties_before_update = style->animated_property_values(); auto animated_properties_before_update = computed_properties->animated_property_values();
if (!pseudo_element_type().has_value()) { computed_properties->reset_animated_properties({});
if (auto computed_properties = target->computed_properties())
computed_properties->reset_animated_properties({});
} else if (auto computed_properties = target->pseudo_element_computed_properties(pseudo_element_type().value())) {
computed_properties->reset_animated_properties({});
}
auto& document = target->document(); auto& document = target->document();
document.style_computer().collect_animation_into(*target, pseudo_element_type(), *this, *style, CSS::StyleComputer::AnimationRefresh::Yes); document.style_computer().collect_animation_into(*target, pseudo_element_type(), *this, *computed_properties, CSS::StyleComputer::AnimationRefresh::Yes);
auto invalidation = compute_required_invalidation(animated_properties_before_update, style->animated_property_values()); auto invalidation = compute_required_invalidation(animated_properties_before_update, computed_properties->animated_property_values());
if (invalidation.is_none()) if (invalidation.is_none())
return; return;
@ -982,11 +972,11 @@ void KeyframeEffect::update_computed_properties()
if (!pseudo_element_type().has_value()) { if (!pseudo_element_type().has_value()) {
if (target->layout_node()) if (target->layout_node())
target->layout_node()->apply_style(*style); target->layout_node()->apply_style(*computed_properties);
} else { } else {
auto pseudo_element_node = target->get_pseudo_element_node(pseudo_element_type().value()); auto pseudo_element_node = target->get_pseudo_element_node(pseudo_element_type().value());
if (auto* node_with_style = dynamic_cast<Layout::NodeWithStyle*>(pseudo_element_node.ptr())) { if (auto* node_with_style = dynamic_cast<Layout::NodeWithStyle*>(pseudo_element_node.ptr())) {
node_with_style->apply_style(*style); node_with_style->apply_style(*computed_properties);
} }
} }

View file

@ -518,19 +518,11 @@ RefPtr<CSSStyleValue const> CSSStyleProperties::style_value_for_computed_propert
}; };
auto get_computed_value = [&element, pseudo_element](PropertyID property_id) -> auto const& { auto get_computed_value = [&element, pseudo_element](PropertyID property_id) -> auto const& {
if (pseudo_element.has_value()) return element.computed_properties(pseudo_element)->property(property_id);
return element.pseudo_element_computed_properties(pseudo_element.value())->property(property_id);
return element.computed_properties()->property(property_id);
}; };
if (property_is_logical_alias(property_id)) { if (property_is_logical_alias(property_id)) {
GC::Ptr<ComputedProperties> computed_properties; auto computed_properties = element.computed_properties(pseudo_element);
if (pseudo_element.has_value())
computed_properties = element.pseudo_element_computed_properties(pseudo_element.value());
else
computed_properties = element.computed_properties();
return style_value_for_computed_property( return style_value_for_computed_property(
layout_node, layout_node,
map_logical_alias_to_physical_property(property_id, LogicalAliasMappingContext { computed_properties->writing_mode(), computed_properties->direction() })); map_logical_alias_to_physical_property(property_id, LogicalAliasMappingContext { computed_properties->writing_mode(), computed_properties->direction() }));

View file

@ -75,9 +75,7 @@ bool AbstractElement::is_before(AbstractElement const& other) const
GC::Ptr<CSS::ComputedProperties const> AbstractElement::computed_properties() const GC::Ptr<CSS::ComputedProperties const> AbstractElement::computed_properties() const
{ {
if (m_pseudo_element.has_value()) return m_element->computed_properties(m_pseudo_element);
return m_element->pseudo_element_computed_properties(*m_pseudo_element);
return m_element->computed_properties();
} }
HashMap<FlyString, CSS::StyleProperty> const& AbstractElement::custom_properties() const HashMap<FlyString, CSS::StyleProperty> const& AbstractElement::custom_properties() const

View file

@ -714,7 +714,7 @@ CSS::RequiredInvalidationAfterStyleChange Element::recompute_style()
auto old_display_is_none = m_computed_properties ? m_computed_properties->display().is_none() : true; auto old_display_is_none = m_computed_properties ? m_computed_properties->display().is_none() : true;
auto new_display_is_none = new_computed_properties->display().is_none(); auto new_display_is_none = new_computed_properties->display().is_none();
set_computed_properties(move(new_computed_properties)); set_computed_properties({}, move(new_computed_properties));
if (old_display_is_none != new_display_is_none) { if (old_display_is_none != new_display_is_none) {
for_each_shadow_including_inclusive_descendant([&](auto& node) { for_each_shadow_including_inclusive_descendant([&](auto& node) {
@ -730,7 +730,7 @@ CSS::RequiredInvalidationAfterStyleChange Element::recompute_style()
auto recompute_pseudo_element_style = [&](CSS::PseudoElement pseudo_element) { auto recompute_pseudo_element_style = [&](CSS::PseudoElement pseudo_element) {
style_computer.push_ancestor(*this); style_computer.push_ancestor(*this);
auto pseudo_element_style = pseudo_element_computed_properties(pseudo_element); auto pseudo_element_style = computed_properties(pseudo_element);
auto new_pseudo_element_style = style_computer.compute_pseudo_element_style_if_needed(*this, pseudo_element); auto new_pseudo_element_style = style_computer.compute_pseudo_element_style_if_needed(*this, pseudo_element);
// TODO: Can we be smarter about invalidation? // TODO: Can we be smarter about invalidation?
@ -740,7 +740,7 @@ CSS::RequiredInvalidationAfterStyleChange Element::recompute_style()
invalidation = CSS::RequiredInvalidationAfterStyleChange::full(); invalidation = CSS::RequiredInvalidationAfterStyleChange::full();
} }
set_pseudo_element_computed_properties(pseudo_element, move(new_pseudo_element_style)); set_computed_properties(pseudo_element, move(new_pseudo_element_style));
style_computer.pop_ancestor(*this); style_computer.pop_ancestor(*this);
}; };
@ -770,7 +770,7 @@ CSS::RequiredInvalidationAfterStyleChange Element::recompute_style()
if (!pseudo_element.has_value() || !pseudo_element->layout_node()) if (!pseudo_element.has_value() || !pseudo_element->layout_node())
continue; continue;
auto pseudo_element_style = pseudo_element_computed_properties(pseudo_element_type); auto pseudo_element_style = computed_properties(pseudo_element_type);
if (!pseudo_element_style) if (!pseudo_element_style)
continue; continue;
@ -2970,31 +2970,38 @@ void Element::set_cascaded_properties(Optional<CSS::PseudoElement> pseudo_elemen
} }
} }
void Element::set_computed_properties(GC::Ptr<CSS::ComputedProperties> style) GC::Ptr<CSS::ComputedProperties> Element::computed_properties(Optional<CSS::PseudoElement> pseudo_element_type)
{ {
if (pseudo_element_type.has_value()) {
if (auto pseudo_element = get_pseudo_element(*pseudo_element_type); pseudo_element.has_value())
return pseudo_element->computed_properties();
return {};
}
return m_computed_properties;
}
GC::Ptr<CSS::ComputedProperties const> Element::computed_properties(Optional<CSS::PseudoElement> pseudo_element_type) const
{
if (pseudo_element_type.has_value()) {
if (auto pseudo_element = get_pseudo_element(*pseudo_element_type); pseudo_element.has_value())
return pseudo_element->computed_properties();
return {};
}
return m_computed_properties;
}
void Element::set_computed_properties(Optional<CSS::PseudoElement> pseudo_element_type, GC::Ptr<CSS::ComputedProperties> style)
{
if (pseudo_element_type.has_value()) {
if (!CSS::Selector::PseudoElementSelector::is_known_pseudo_element_type(*pseudo_element_type))
return;
ensure_pseudo_element(*pseudo_element_type).set_computed_properties(style);
return;
}
m_computed_properties = style; m_computed_properties = style;
computed_properties_changed(); computed_properties_changed();
} }
void Element::set_pseudo_element_computed_properties(CSS::PseudoElement pseudo_element, GC::Ptr<CSS::ComputedProperties> style)
{
if (!m_pseudo_element_data && !style)
return;
if (!CSS::Selector::PseudoElementSelector::is_known_pseudo_element_type(pseudo_element))
return;
ensure_pseudo_element(pseudo_element).set_computed_properties(style);
}
GC::Ptr<CSS::ComputedProperties> Element::pseudo_element_computed_properties(CSS::PseudoElement type)
{
auto pseudo_element = get_pseudo_element(type);
if (pseudo_element.has_value())
return pseudo_element->computed_properties();
return {};
}
Optional<PseudoElement&> Element::get_pseudo_element(CSS::PseudoElement type) const Optional<PseudoElement&> Element::get_pseudo_element(CSS::PseudoElement type) const
{ {
if (!m_pseudo_element_data) if (!m_pseudo_element_data)

View file

@ -209,17 +209,14 @@ public:
GC::Ptr<Layout::NodeWithStyle> layout_node(); GC::Ptr<Layout::NodeWithStyle> layout_node();
GC::Ptr<Layout::NodeWithStyle const> layout_node() const; GC::Ptr<Layout::NodeWithStyle const> layout_node() const;
GC::Ptr<CSS::ComputedProperties> computed_properties() { return m_computed_properties; } GC::Ptr<CSS::ComputedProperties> computed_properties(Optional<CSS::PseudoElement> = {});
GC::Ptr<CSS::ComputedProperties const> computed_properties() const { return m_computed_properties; } GC::Ptr<CSS::ComputedProperties const> computed_properties(Optional<CSS::PseudoElement> = {}) const;
void set_computed_properties(GC::Ptr<CSS::ComputedProperties>); void set_computed_properties(Optional<CSS::PseudoElement>, GC::Ptr<CSS::ComputedProperties>);
GC::Ref<CSS::ComputedProperties> resolved_css_values(Optional<CSS::PseudoElement> = {}); GC::Ref<CSS::ComputedProperties> resolved_css_values(Optional<CSS::PseudoElement> = {});
[[nodiscard]] GC::Ptr<CSS::CascadedProperties> cascaded_properties(Optional<CSS::PseudoElement>) const; [[nodiscard]] GC::Ptr<CSS::CascadedProperties> cascaded_properties(Optional<CSS::PseudoElement>) const;
void set_cascaded_properties(Optional<CSS::PseudoElement>, GC::Ptr<CSS::CascadedProperties>); void set_cascaded_properties(Optional<CSS::PseudoElement>, GC::Ptr<CSS::CascadedProperties>);
void set_pseudo_element_computed_properties(CSS::PseudoElement, GC::Ptr<CSS::ComputedProperties>);
GC::Ptr<CSS::ComputedProperties> pseudo_element_computed_properties(CSS::PseudoElement);
Optional<PseudoElement&> get_pseudo_element(CSS::PseudoElement) const; Optional<PseudoElement&> get_pseudo_element(CSS::PseudoElement) const;
GC::Ptr<CSS::CSSStyleProperties> inline_style() { return m_inline_style; } GC::Ptr<CSS::CSSStyleProperties> inline_style() { return m_inline_style; }

View file

@ -187,7 +187,7 @@ void TreeBuilder::create_pseudo_element_if_needed(DOM::Element& element, CSS::Ps
{ {
auto& document = element.document(); auto& document = element.document();
auto pseudo_element_style = element.pseudo_element_computed_properties(pseudo_element); auto pseudo_element_style = element.computed_properties(pseudo_element);
if (!pseudo_element_style) if (!pseudo_element_style)
return; return;
@ -726,7 +726,7 @@ void TreeBuilder::update_layout_tree_after_children(DOM::Node& dom_node, GC::Ref
auto marker_style = style_computer.compute_style(element, CSS::PseudoElement::Marker); auto marker_style = style_computer.compute_style(element, CSS::PseudoElement::Marker);
auto list_item_marker = document.heap().allocate<ListItemMarkerBox>(document, layout_node->computed_values().list_style_type(), layout_node->computed_values().list_style_position(), element, marker_style); auto list_item_marker = document.heap().allocate<ListItemMarkerBox>(document, layout_node->computed_values().list_style_type(), layout_node->computed_values().list_style_position(), element, marker_style);
static_cast<ListItemBox&>(*layout_node).set_marker(list_item_marker); static_cast<ListItemBox&>(*layout_node).set_marker(list_item_marker);
element.set_pseudo_element_computed_properties(CSS::PseudoElement::Marker, marker_style); element.set_computed_properties(CSS::PseudoElement::Marker, marker_style);
element.set_pseudo_element_node({}, CSS::PseudoElement::Marker, list_item_marker); element.set_pseudo_element_node({}, CSS::PseudoElement::Marker, list_item_marker);
layout_node->prepend_child(*list_item_marker); layout_node->prepend_child(*list_item_marker);
DOM::AbstractElement marker_reference { element, CSS::PseudoElement::Marker }; DOM::AbstractElement marker_reference { element, CSS::PseudoElement::Marker };

View file

@ -461,13 +461,7 @@ void ConnectionFromClient::inspect_dom_node(u64 page_id, WebView::DOMNodePropert
auto& element = as<Web::DOM::Element>(*node); auto& element = as<Web::DOM::Element>(*node);
node->document().set_inspected_node(node); node->document().set_inspected_node(node);
GC::Ptr<Web::CSS::ComputedProperties> properties; auto properties = element.computed_properties(pseudo_element);
if (pseudo_element.has_value()) {
if (auto pseudo_element_node = element.get_pseudo_element_node(*pseudo_element))
properties = element.pseudo_element_computed_properties(*pseudo_element);
} else {
properties = element.computed_properties();
}
if (!properties) { if (!properties) {
async_did_inspect_dom_node(page_id, { property_type, {} }); async_did_inspect_dom_node(page_id, { property_type, {} });