1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-28 05:47:34 +00:00

LibWeb: Wrap PseudoElements stored in SimpleSelector in a class

No functional impact intended. This is just a more complicated way of
writing what we have now.

The goal of this commit is so that we are able to store the 'name' of a
pseudo element for use in serializing 'unknown -webkit-
pseudo-elements', see:

https://www.w3.org/TR/selectors-4/#compat

This is quite awkward, as in pretty much all cases just the selector
type enum is enough, but we will need to cache the name for serializing
these unknown selectors. I can't figure out any reason why we would need
this name anywhere else in the engine, so pretty much everywhere is
still just passing around this raw enum. But this change will allow us
to easily store the name inside of this new struct for when it is needed
for serialization, once those webkit unknown elements are supported by
our engine.
This commit is contained in:
Shannon Booth 2023-12-10 21:00:03 +13:00 committed by Alexander Kalenik
parent 08920b7a34
commit 83758d4cdd
32 changed files with 196 additions and 174 deletions

View file

@ -1124,7 +1124,7 @@ Layout::Viewport* Document::layout_node()
return static_cast<Layout::Viewport*>(Node::layout_node());
}
void Document::set_inspected_node(Node* node, Optional<CSS::Selector::PseudoElement> pseudo_element)
void Document::set_inspected_node(Node* node, Optional<CSS::Selector::PseudoElement::Type> pseudo_element)
{
if (m_inspected_node.ptr() == node && m_inspected_pseudo_element == pseudo_element)
return;

View file

@ -137,7 +137,7 @@ public:
Node* hovered_node() { return m_hovered_node.ptr(); }
Node const* hovered_node() const { return m_hovered_node.ptr(); }
void set_inspected_node(Node*, Optional<CSS::Selector::PseudoElement>);
void set_inspected_node(Node*, Optional<CSS::Selector::PseudoElement::Type>);
Node* inspected_node() { return m_inspected_node.ptr(); }
Node const* inspected_node() const { return m_inspected_node.ptr(); }
Layout::Node* inspected_layout_node();
@ -576,7 +576,7 @@ private:
JS::GCPtr<CSS::StyleSheetList> m_style_sheets;
JS::GCPtr<Node> m_hovered_node;
JS::GCPtr<Node> m_inspected_node;
Optional<CSS::Selector::PseudoElement> m_inspected_pseudo_element;
Optional<CSS::Selector::PseudoElement::Type> m_inspected_pseudo_element;
JS::GCPtr<Node> m_active_favicon;
WeakPtr<HTML::BrowsingContext> m_browsing_context;
AK::URL m_url;

View file

@ -960,7 +960,7 @@ void Element::children_changed()
set_needs_style_update(true);
}
void Element::set_pseudo_element_node(Badge<Layout::TreeBuilder>, CSS::Selector::PseudoElement pseudo_element, JS::GCPtr<Layout::Node> pseudo_element_node)
void Element::set_pseudo_element_node(Badge<Layout::TreeBuilder>, CSS::Selector::PseudoElement::Type pseudo_element, JS::GCPtr<Layout::Node> pseudo_element_node)
{
if (!m_pseudo_element_nodes) {
if (!pseudo_element_node)
@ -971,7 +971,7 @@ void Element::set_pseudo_element_node(Badge<Layout::TreeBuilder>, CSS::Selector:
(*m_pseudo_element_nodes)[to_underlying(pseudo_element)] = pseudo_element_node;
}
JS::GCPtr<Layout::Node> Element::get_pseudo_element_node(CSS::Selector::PseudoElement pseudo_element) const
JS::GCPtr<Layout::Node> Element::get_pseudo_element_node(CSS::Selector::PseudoElement::Type pseudo_element) const
{
if (!m_pseudo_element_nodes)
return nullptr;
@ -992,7 +992,7 @@ void Element::serialize_pseudo_elements_as_json(JsonArraySerializer<StringBuilde
if (!pseudo_element_node)
continue;
auto object = MUST(children_array.add_object());
MUST(object.add("name"sv, DeprecatedString::formatted("::{}", CSS::pseudo_element_name(static_cast<CSS::Selector::PseudoElement>(i)))));
MUST(object.add("name"sv, MUST(String::formatted("::{}", CSS::Selector::PseudoElement::name(static_cast<CSS::Selector::PseudoElement::Type>(i))))));
MUST(object.add("type"sv, "pseudo-element"));
MUST(object.add("parent-id"sv, unique_id()));
MUST(object.add("pseudo-element"sv, i));
@ -2057,7 +2057,7 @@ auto Element::pseudo_element_custom_properties() const -> PseudoElementCustomPro
return *m_pseudo_element_custom_properties;
}
void Element::set_custom_properties(Optional<CSS::Selector::PseudoElement> pseudo_element, HashMap<FlyString, CSS::StyleProperty> custom_properties)
void Element::set_custom_properties(Optional<CSS::Selector::PseudoElement::Type> pseudo_element, HashMap<FlyString, CSS::StyleProperty> custom_properties)
{
if (!pseudo_element.has_value()) {
m_custom_properties = move(custom_properties);
@ -2066,7 +2066,7 @@ void Element::set_custom_properties(Optional<CSS::Selector::PseudoElement> pseud
pseudo_element_custom_properties()[to_underlying(pseudo_element.value())] = move(custom_properties);
}
HashMap<FlyString, CSS::StyleProperty> const& Element::custom_properties(Optional<CSS::Selector::PseudoElement> pseudo_element) const
HashMap<FlyString, CSS::StyleProperty> const& Element::custom_properties(Optional<CSS::Selector::PseudoElement::Type> pseudo_element) const
{
if (!pseudo_element.has_value())
return m_custom_properties;

View file

@ -173,8 +173,8 @@ public:
RequiredInvalidationAfterStyleChange recompute_style();
Optional<CSS::Selector::PseudoElement> use_pseudo_element() const { return m_use_pseudo_element; }
void set_use_pseudo_element(Optional<CSS::Selector::PseudoElement> use_pseudo_element) { m_use_pseudo_element = use_pseudo_element; }
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); }
Layout::NodeWithStyle* layout_node();
Layout::NodeWithStyle const* layout_node() const;
@ -207,8 +207,8 @@ public:
ShadowRoot const* shadow_root_internal() const { return m_shadow_root.ptr(); }
void set_shadow_root(JS::GCPtr<ShadowRoot>);
void set_custom_properties(Optional<CSS::Selector::PseudoElement>, HashMap<FlyString, CSS::StyleProperty> custom_properties);
[[nodiscard]] HashMap<FlyString, CSS::StyleProperty> const& custom_properties(Optional<CSS::Selector::PseudoElement>) const;
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;
int queue_an_element_task(HTML::Task::Source, JS::SafeFunction<void()>);
@ -225,8 +225,8 @@ public:
static JS::GCPtr<Layout::Node> create_layout_node_for_display_type(DOM::Document&, CSS::Display const&, NonnullRefPtr<CSS::StyleProperties>, Element*);
void set_pseudo_element_node(Badge<Layout::TreeBuilder>, CSS::Selector::PseudoElement, JS::GCPtr<Layout::Node>);
JS::GCPtr<Layout::Node> get_pseudo_element_node(CSS::Selector::PseudoElement) const;
void set_pseudo_element_node(Badge<Layout::TreeBuilder>, CSS::Selector::PseudoElement::Type, JS::GCPtr<Layout::Node>);
JS::GCPtr<Layout::Node> get_pseudo_element_node(CSS::Selector::PseudoElement::Type) const;
void clear_pseudo_element_nodes(Badge<Layout::TreeBuilder>);
void serialize_pseudo_elements_as_json(JsonArraySerializer<StringBuilder>& children_array) const;
@ -410,18 +410,18 @@ private:
RefPtr<CSS::StyleProperties> m_computed_css_values;
HashMap<FlyString, CSS::StyleProperty> m_custom_properties;
using PseudoElementCustomProperties = Array<HashMap<FlyString, CSS::StyleProperty>, to_underlying(CSS::Selector::PseudoElement::PseudoElementCount)>;
using PseudoElementCustomProperties = Array<HashMap<FlyString, CSS::StyleProperty>, to_underlying(CSS::Selector::PseudoElement::Type::PseudoElementCount)>;
mutable OwnPtr<PseudoElementCustomProperties> m_pseudo_element_custom_properties;
PseudoElementCustomProperties& pseudo_element_custom_properties() const;
Optional<CSS::Selector::PseudoElement> m_use_pseudo_element {};
Optional<CSS::Selector::PseudoElement::Type> m_use_pseudo_element;
Vector<FlyString> m_classes;
Optional<Dir> m_dir;
Optional<FlyString> m_id;
using PseudoElementLayoutNodes = Array<JS::GCPtr<Layout::Node>, to_underlying(CSS::Selector::PseudoElement::PseudoElementCount)>;
using PseudoElementLayoutNodes = Array<JS::GCPtr<Layout::Node>, to_underlying(CSS::Selector::PseudoElement::Type::PseudoElementCount)>;
OwnPtr<PseudoElementLayoutNodes> m_pseudo_element_nodes;
// https://html.spec.whatwg.org/multipage/custom-elements.html#custom-element-reaction-queue

View file

@ -1838,8 +1838,8 @@ ErrorOr<String> Node::name_or_description(NameOrDescription target, Document con
// i. Set the accumulated text to the empty string.
total_accumulated_text.clear();
// ii. Check for CSS generated textual content associated with the current node and include it in the accumulated text. The CSS :before and :after pseudo elements [CSS2] can provide textual content for elements that have a content model.
auto before = element->get_pseudo_element_node(CSS::Selector::PseudoElement::Before);
auto after = element->get_pseudo_element_node(CSS::Selector::PseudoElement::After);
auto before = element->get_pseudo_element_node(CSS::Selector::PseudoElement::Type::Before);
auto after = element->get_pseudo_element_node(CSS::Selector::PseudoElement::Type::After);
// - For :before pseudo elements, User agents MUST prepend CSS textual content, without a space, to the textual content of the current node.
if (before)
TRY(Node::prepend_without_space(total_accumulated_text, before->computed_values().content().data));