LibWeb/CSS: Move and rename PseudoElement types to prep for code gen

The upcoming generated types will match those for pseudo-classes: A
PseudoElementSelector type, that then holds a PseudoElement enum
defining what it is. That enum will be at the top level in the Web::CSS
namespace.

In order to keep the diffs clearer, this commit renames and moves the
types, and then a following one will replace the handwritten enum with
a generated one.
This commit is contained in:
Sam Atkins 2025-03-20 16:56:46 +00:00
commit 0ed2e71801
Notes: github-actions[bot] 2025-03-24 09:51:36 +00:00
42 changed files with 270 additions and 271 deletions

View file

@ -1685,7 +1685,7 @@ void Document::set_inspected_node(GC::Ptr<Node> node)
m_inspected_node = node;
}
void Document::set_highlighted_node(GC::Ptr<Node> node, Optional<CSS::Selector::PseudoElement::Type> pseudo_element)
void Document::set_highlighted_node(GC::Ptr<Node> node, Optional<CSS::PseudoElement> pseudo_element)
{
if (m_highlighted_node == node && m_highlighted_pseudo_element == pseudo_element)
return;
@ -1797,12 +1797,12 @@ void Document::invalidate_style_for_elements_affected_by_hover_change(Node& old_
SelectorEngine::MatchContext context;
if (SelectorEngine::matches(selector, element, {}, context, {}))
return true;
if (element.has_pseudo_element(CSS::Selector::PseudoElement::Type::Before)) {
if (SelectorEngine::matches(selector, element, {}, context, CSS::Selector::PseudoElement::Type::Before))
if (element.has_pseudo_element(CSS::PseudoElement::Before)) {
if (SelectorEngine::matches(selector, element, {}, context, CSS::PseudoElement::Before))
return true;
}
if (element.has_pseudo_element(CSS::Selector::PseudoElement::Type::After)) {
if (SelectorEngine::matches(selector, element, {}, context, CSS::Selector::PseudoElement::Type::After))
if (element.has_pseudo_element(CSS::PseudoElement::After)) {
if (SelectorEngine::matches(selector, element, {}, context, CSS::PseudoElement::After))
return true;
}
return false;

View file

@ -265,7 +265,7 @@ public:
void set_inspected_node(GC::Ptr<Node>);
GC::Ptr<Node const> inspected_node() const { return m_inspected_node; }
void set_highlighted_node(GC::Ptr<Node>, Optional<CSS::Selector::PseudoElement::Type>);
void set_highlighted_node(GC::Ptr<Node>, Optional<CSS::PseudoElement>);
GC::Ptr<Node const> highlighted_node() const { return m_highlighted_node; }
GC::Ptr<Layout::Node> highlighted_layout_node();
GC::Ptr<Layout::Node const> highlighted_layout_node() const { return const_cast<Document*>(this)->highlighted_layout_node(); }
@ -963,7 +963,7 @@ private:
GC::Ptr<Node> m_hovered_node;
GC::Ptr<Node> m_inspected_node;
GC::Ptr<Node> m_highlighted_node;
Optional<CSS::Selector::PseudoElement::Type> m_highlighted_pseudo_element;
Optional<CSS::PseudoElement> m_highlighted_pseudo_element;
Optional<Color> m_normal_link_color;
Optional<Color> m_active_link_color;

View file

@ -547,7 +547,7 @@ CSS::RequiredInvalidationAfterStyleChange Element::recompute_style()
}
// Any document change that can cause this element's style to change, could also affect its pseudo-elements.
auto recompute_pseudo_element_style = [&](CSS::Selector::PseudoElement::Type pseudo_element) {
auto recompute_pseudo_element_style = [&](CSS::PseudoElement pseudo_element) {
style_computer.push_ancestor(*this);
auto pseudo_element_style = pseudo_element_computed_properties(pseudo_element);
@ -564,10 +564,10 @@ CSS::RequiredInvalidationAfterStyleChange Element::recompute_style()
style_computer.pop_ancestor(*this);
};
recompute_pseudo_element_style(CSS::Selector::PseudoElement::Type::Before);
recompute_pseudo_element_style(CSS::Selector::PseudoElement::Type::After);
recompute_pseudo_element_style(CSS::PseudoElement::Before);
recompute_pseudo_element_style(CSS::PseudoElement::After);
if (had_list_marker || m_computed_properties->display().is_list_item())
recompute_pseudo_element_style(CSS::Selector::PseudoElement::Type::Marker);
recompute_pseudo_element_style(CSS::PseudoElement::Marker);
if (invalidation.is_none())
return invalidation;
@ -582,8 +582,8 @@ CSS::RequiredInvalidationAfterStyleChange Element::recompute_style()
paintable()->set_needs_display();
// Do the same for pseudo-elements.
for (auto i = 0; i < to_underlying(CSS::Selector::PseudoElement::Type::KnownPseudoElementCount); i++) {
auto pseudo_element_type = static_cast<CSS::Selector::PseudoElement::Type>(i);
for (auto i = 0; i < to_underlying(CSS::PseudoElement::KnownPseudoElementCount); i++) {
auto pseudo_element_type = static_cast<CSS::PseudoElement>(i);
auto pseudo_element = get_pseudo_element(pseudo_element_type);
if (!pseudo_element.has_value() || !pseudo_element->layout_node)
continue;
@ -647,7 +647,7 @@ CSS::RequiredInvalidationAfterStyleChange Element::recompute_inherited_style()
return invalidation;
}
GC::Ref<CSS::ComputedProperties> Element::resolved_css_values(Optional<CSS::Selector::PseudoElement::Type> type)
GC::Ref<CSS::ComputedProperties> Element::resolved_css_values(Optional<CSS::PseudoElement> type)
{
auto element_computed_style = CSS::CSSStyleProperties::create_resolved_style({ *this, type });
auto properties = heap().allocate<CSS::ComputedProperties>();
@ -1180,20 +1180,20 @@ void Element::children_changed(ChildrenChangedMetadata const* metadata)
set_needs_style_update(true);
}
void Element::set_pseudo_element_node(Badge<Layout::TreeBuilder>, CSS::Selector::PseudoElement::Type pseudo_element, GC::Ptr<Layout::NodeWithStyle> pseudo_element_node)
void Element::set_pseudo_element_node(Badge<Layout::TreeBuilder>, CSS::PseudoElement pseudo_element, GC::Ptr<Layout::NodeWithStyle> pseudo_element_node)
{
auto existing_pseudo_element = get_pseudo_element(pseudo_element);
if (!existing_pseudo_element.has_value() && !pseudo_element_node)
return;
if (!CSS::Selector::PseudoElement::is_known_pseudo_element_type(pseudo_element)) {
if (!CSS::Selector::PseudoElementSelector::is_known_pseudo_element_type(pseudo_element)) {
return;
}
ensure_pseudo_element(pseudo_element).layout_node = move(pseudo_element_node);
}
GC::Ptr<Layout::NodeWithStyle> Element::get_pseudo_element_node(CSS::Selector::PseudoElement::Type pseudo_element) const
GC::Ptr<Layout::NodeWithStyle> Element::get_pseudo_element_node(CSS::PseudoElement pseudo_element) const
{
if (auto element_data = get_pseudo_element(pseudo_element); element_data.has_value())
return element_data->layout_node;
@ -1401,7 +1401,7 @@ void Element::serialize_pseudo_elements_as_json(JsonArraySerializer<StringBuilde
if (!pseudo_element)
continue;
auto object = MUST(children_array.add_object());
MUST(object.add("name"sv, MUST(String::formatted("::{}", CSS::Selector::PseudoElement::name(static_cast<CSS::Selector::PseudoElement::Type>(i))))));
MUST(object.add("name"sv, MUST(String::formatted("::{}", CSS::Selector::PseudoElementSelector::name(static_cast<CSS::PseudoElement>(i))))));
MUST(object.add("type"sv, "pseudo-element"));
MUST(object.add("parent-id"sv, unique_id().value()));
MUST(object.add("pseudo-element"sv, i));
@ -2661,7 +2661,7 @@ size_t Element::attribute_list_size() const
return m_attributes->length();
}
GC::Ptr<CSS::CascadedProperties> Element::cascaded_properties(Optional<CSS::Selector::PseudoElement::Type> pseudo_element) const
GC::Ptr<CSS::CascadedProperties> Element::cascaded_properties(Optional<CSS::PseudoElement> pseudo_element) const
{
if (pseudo_element.has_value()) {
auto pseudo_element_data = get_pseudo_element(pseudo_element.value());
@ -2672,10 +2672,10 @@ GC::Ptr<CSS::CascadedProperties> Element::cascaded_properties(Optional<CSS::Sele
return m_cascaded_properties;
}
void Element::set_cascaded_properties(Optional<CSS::Selector::PseudoElement::Type> pseudo_element, GC::Ptr<CSS::CascadedProperties> cascaded_properties)
void Element::set_cascaded_properties(Optional<CSS::PseudoElement> pseudo_element, GC::Ptr<CSS::CascadedProperties> cascaded_properties)
{
if (pseudo_element.has_value()) {
if (pseudo_element.value() >= CSS::Selector::PseudoElement::Type::KnownPseudoElementCount)
if (pseudo_element.value() >= CSS::PseudoElement::KnownPseudoElementCount)
return;
ensure_pseudo_element(pseudo_element.value()).cascaded_properties = cascaded_properties;
} else {
@ -2689,19 +2689,19 @@ void Element::set_computed_properties(GC::Ptr<CSS::ComputedProperties> style)
computed_properties_changed();
}
void Element::set_pseudo_element_computed_properties(CSS::Selector::PseudoElement::Type pseudo_element, GC::Ptr<CSS::ComputedProperties> style)
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::PseudoElement::is_known_pseudo_element_type(pseudo_element)) {
if (!CSS::Selector::PseudoElementSelector::is_known_pseudo_element_type(pseudo_element)) {
return;
}
ensure_pseudo_element(pseudo_element).computed_properties = style;
}
GC::Ptr<CSS::ComputedProperties> Element::pseudo_element_computed_properties(CSS::Selector::PseudoElement::Type type)
GC::Ptr<CSS::ComputedProperties> Element::pseudo_element_computed_properties(CSS::PseudoElement type)
{
auto pseudo_element = get_pseudo_element(type);
if (pseudo_element.has_value())
@ -2709,48 +2709,48 @@ GC::Ptr<CSS::ComputedProperties> Element::pseudo_element_computed_properties(CSS
return {};
}
Optional<Element::PseudoElement&> Element::get_pseudo_element(CSS::Selector::PseudoElement::Type type) const
Optional<Element::PseudoElement&> Element::get_pseudo_element(CSS::PseudoElement type) const
{
if (!m_pseudo_element_data)
return {};
if (!CSS::Selector::PseudoElement::is_known_pseudo_element_type(type)) {
if (!CSS::Selector::PseudoElementSelector::is_known_pseudo_element_type(type)) {
return {};
}
return m_pseudo_element_data->at(to_underlying(type));
}
Element::PseudoElement& Element::ensure_pseudo_element(CSS::Selector::PseudoElement::Type type) const
Element::PseudoElement& Element::ensure_pseudo_element(CSS::PseudoElement type) const
{
if (!m_pseudo_element_data)
m_pseudo_element_data = make<PseudoElementData>();
VERIFY(CSS::Selector::PseudoElement::is_known_pseudo_element_type(type));
VERIFY(CSS::Selector::PseudoElementSelector::is_known_pseudo_element_type(type));
return m_pseudo_element_data->at(to_underlying(type));
}
void Element::set_custom_properties(Optional<CSS::Selector::PseudoElement::Type> pseudo_element, HashMap<FlyString, CSS::StyleProperty> custom_properties)
void Element::set_custom_properties(Optional<CSS::PseudoElement> pseudo_element, HashMap<FlyString, CSS::StyleProperty> custom_properties)
{
if (!pseudo_element.has_value()) {
m_custom_properties = move(custom_properties);
return;
}
if (!CSS::Selector::PseudoElement::is_known_pseudo_element_type(pseudo_element.value())) {
if (!CSS::Selector::PseudoElementSelector::is_known_pseudo_element_type(pseudo_element.value())) {
return;
}
ensure_pseudo_element(pseudo_element.value()).custom_properties = move(custom_properties);
}
HashMap<FlyString, CSS::StyleProperty> const& Element::custom_properties(Optional<CSS::Selector::PseudoElement::Type> pseudo_element) const
HashMap<FlyString, CSS::StyleProperty> const& Element::custom_properties(Optional<CSS::PseudoElement> pseudo_element) const
{
if (!pseudo_element.has_value())
return m_custom_properties;
VERIFY(CSS::Selector::PseudoElement::is_known_pseudo_element_type(pseudo_element.value()));
VERIFY(CSS::Selector::PseudoElementSelector::is_known_pseudo_element_type(pseudo_element.value()));
return ensure_pseudo_element(pseudo_element.value()).custom_properties;
}

View file

@ -192,8 +192,8 @@ public:
CSS::RequiredInvalidationAfterStyleChange recompute_style();
CSS::RequiredInvalidationAfterStyleChange recompute_inherited_style();
Optional<CSS::Selector::PseudoElement::Type> use_pseudo_element() const { return m_use_pseudo_element; }
void set_use_pseudo_element(Optional<CSS::Selector::PseudoElement::Type> use_pseudo_element) { m_use_pseudo_element = move(use_pseudo_element); }
Optional<CSS::PseudoElement> use_pseudo_element() const { return m_use_pseudo_element; }
void set_use_pseudo_element(Optional<CSS::PseudoElement> use_pseudo_element) { m_use_pseudo_element = move(use_pseudo_element); }
GC::Ptr<Layout::NodeWithStyle> layout_node();
GC::Ptr<Layout::NodeWithStyle const> layout_node() const;
@ -201,13 +201,13 @@ public:
GC::Ptr<CSS::ComputedProperties> computed_properties() { return m_computed_properties; }
GC::Ptr<CSS::ComputedProperties const> computed_properties() const { return m_computed_properties; }
void set_computed_properties(GC::Ptr<CSS::ComputedProperties>);
GC::Ref<CSS::ComputedProperties> resolved_css_values(Optional<CSS::Selector::PseudoElement::Type> = {});
GC::Ref<CSS::ComputedProperties> resolved_css_values(Optional<CSS::PseudoElement> = {});
[[nodiscard]] GC::Ptr<CSS::CascadedProperties> cascaded_properties(Optional<CSS::Selector::PseudoElement::Type>) const;
void set_cascaded_properties(Optional<CSS::Selector::PseudoElement::Type>, GC::Ptr<CSS::CascadedProperties>);
[[nodiscard]] GC::Ptr<CSS::CascadedProperties> cascaded_properties(Optional<CSS::PseudoElement>) const;
void set_cascaded_properties(Optional<CSS::PseudoElement>, GC::Ptr<CSS::CascadedProperties>);
void set_pseudo_element_computed_properties(CSS::Selector::PseudoElement::Type, GC::Ptr<CSS::ComputedProperties>);
GC::Ptr<CSS::ComputedProperties> pseudo_element_computed_properties(CSS::Selector::PseudoElement::Type);
void set_pseudo_element_computed_properties(CSS::PseudoElement, GC::Ptr<CSS::ComputedProperties>);
GC::Ptr<CSS::ComputedProperties> pseudo_element_computed_properties(CSS::PseudoElement);
void reset_animated_css_properties();
@ -242,8 +242,8 @@ public:
GC::Ptr<ShadowRoot const> shadow_root() const { return m_shadow_root; }
void set_shadow_root(GC::Ptr<ShadowRoot>);
void set_custom_properties(Optional<CSS::Selector::PseudoElement::Type>, HashMap<FlyString, CSS::StyleProperty> custom_properties);
[[nodiscard]] HashMap<FlyString, CSS::StyleProperty> const& custom_properties(Optional<CSS::Selector::PseudoElement::Type>) const;
void set_custom_properties(Optional<CSS::PseudoElement>, HashMap<FlyString, CSS::StyleProperty> custom_properties);
[[nodiscard]] HashMap<FlyString, CSS::StyleProperty> const& custom_properties(Optional<CSS::PseudoElement>) const;
bool style_uses_css_custom_properties() const { return m_style_uses_css_custom_properties; }
void set_style_uses_css_custom_properties(bool value) { m_style_uses_css_custom_properties = value; }
@ -271,9 +271,9 @@ public:
bool affected_by_hover() const;
bool includes_properties_from_invalidation_set(CSS::InvalidationSet const&) const;
void set_pseudo_element_node(Badge<Layout::TreeBuilder>, CSS::Selector::PseudoElement::Type, GC::Ptr<Layout::NodeWithStyle>);
GC::Ptr<Layout::NodeWithStyle> get_pseudo_element_node(CSS::Selector::PseudoElement::Type) const;
bool has_pseudo_element(CSS::Selector::PseudoElement::Type) const;
void set_pseudo_element_node(Badge<Layout::TreeBuilder>, CSS::PseudoElement, GC::Ptr<Layout::NodeWithStyle>);
GC::Ptr<Layout::NodeWithStyle> get_pseudo_element_node(CSS::PseudoElement) const;
bool has_pseudo_element(CSS::PseudoElement) const;
bool has_pseudo_elements() const;
void clear_pseudo_element_nodes(Badge<Layout::TreeBuilder>);
void serialize_pseudo_elements_as_json(JsonArraySerializer<StringBuilder>& children_array) const;
@ -516,14 +516,14 @@ private:
GC::Ptr<CSS::ComputedProperties> computed_properties;
HashMap<FlyString, CSS::StyleProperty> custom_properties;
};
// TODO: CSS::Selector::PseudoElement::Type includes a lot of pseudo-elements that exist in shadow trees,
// TODO: CSS::Selector::PseudoElement includes a lot of pseudo-elements that exist in shadow trees,
// and so we don't want to include data for them here.
using PseudoElementData = Array<PseudoElement, to_underlying(CSS::Selector::PseudoElement::Type::KnownPseudoElementCount)>;
using PseudoElementData = Array<PseudoElement, to_underlying(CSS::PseudoElement::KnownPseudoElementCount)>;
mutable OwnPtr<PseudoElementData> m_pseudo_element_data;
Optional<PseudoElement&> get_pseudo_element(CSS::Selector::PseudoElement::Type) const;
PseudoElement& ensure_pseudo_element(CSS::Selector::PseudoElement::Type) const;
Optional<PseudoElement&> get_pseudo_element(CSS::PseudoElement) const;
PseudoElement& ensure_pseudo_element(CSS::PseudoElement) const;
Optional<CSS::Selector::PseudoElement::Type> m_use_pseudo_element;
Optional<CSS::PseudoElement> m_use_pseudo_element;
Vector<FlyString> m_classes;
Optional<Dir> m_dir;
@ -603,11 +603,11 @@ inline bool Element::has_class(FlyString const& class_name, CaseSensitivity case
});
}
inline bool Element::has_pseudo_element(CSS::Selector::PseudoElement::Type type) const
inline bool Element::has_pseudo_element(CSS::PseudoElement type) const
{
if (!m_pseudo_element_data)
return false;
if (!CSS::Selector::PseudoElement::is_known_pseudo_element_type(type))
if (!CSS::Selector::PseudoElementSelector::is_known_pseudo_element_type(type))
return false;
return m_pseudo_element_data->at(to_underlying(type)).layout_node;
}

View file

@ -14,7 +14,7 @@ namespace Web::DOM {
class ElementReference {
public:
ElementReference(GC::Ref<Element> element, Optional<CSS::Selector::PseudoElement::Type> pseudo_element = {})
ElementReference(GC::Ref<Element> element, Optional<CSS::PseudoElement> pseudo_element = {})
: m_element(element)
, m_pseudo_element(move(pseudo_element))
{
@ -22,7 +22,7 @@ public:
Element& element() { return m_element; }
Element const& element() const { return m_element; }
Optional<CSS::Selector::PseudoElement::Type> pseudo_element() const { return m_pseudo_element; }
Optional<CSS::PseudoElement> pseudo_element() const { return m_pseudo_element; }
void visit(GC::Cell::Visitor& visitor) const
{
@ -31,7 +31,7 @@ public:
private:
GC::Ref<Element> m_element;
Optional<CSS::Selector::PseudoElement::Type> m_pseudo_element;
Optional<CSS::PseudoElement> m_pseudo_element;
};
}

View file

@ -2750,7 +2750,7 @@ ErrorOr<String> Node::name_or_description(NameOrDescription target, Document con
// b. For ::after pseudo elements, User agents MUST append CSS textual content, without a space, to the textual
// content of the current node. NOTE: The code for handling the ::after pseudo elements case is further below,
// following the “iii. For each child node of the current node” code.
if (auto before = element->get_pseudo_element_node(CSS::Selector::PseudoElement::Type::Before)) {
if (auto before = element->get_pseudo_element_node(CSS::PseudoElement::Before)) {
if (before->computed_values().content().alt_text.has_value())
total_accumulated_text.append(before->computed_values().content().alt_text.release_value());
else
@ -2806,7 +2806,7 @@ ErrorOr<String> Node::name_or_description(NameOrDescription target, Document con
}
// NOTE: See step ii.b above.
if (auto after = element->get_pseudo_element_node(CSS::Selector::PseudoElement::Type::After)) {
if (auto after = element->get_pseudo_element_node(CSS::PseudoElement::After)) {
if (after->computed_values().content().alt_text.has_value())
total_accumulated_text.append(after->computed_values().content().alt_text.release_value());
else
@ -2960,8 +2960,8 @@ void Node::play_or_cancel_animations_after_display_property_change()
auto const& element = static_cast<Element const&>(node);
if (auto animation = element.cached_animation_name_animation({}))
play_or_cancel_depending_on_display(*animation);
for (auto i = 0; i < to_underlying(CSS::Selector::PseudoElement::Type::KnownPseudoElementCount); i++) {
auto pseudo_element = static_cast<CSS::Selector::PseudoElement::Type>(i);
for (auto i = 0; i < to_underlying(CSS::PseudoElement::KnownPseudoElementCount); i++) {
auto pseudo_element = static_cast<CSS::PseudoElement>(i);
if (auto animation = element.cached_animation_name_animation(pseudo_element))
play_or_cancel_depending_on_display(*animation);
}