diff --git a/Userland/Libraries/LibWeb/DOM/Element.cpp b/Userland/Libraries/LibWeb/DOM/Element.cpp index 6d0ae6e0f86..832300893cf 100644 --- a/Userland/Libraries/LibWeb/DOM/Element.cpp +++ b/Userland/Libraries/LibWeb/DOM/Element.cpp @@ -1141,6 +1141,17 @@ JS::GCPtr Element::get_pseudo_element_node(CSS::Selector: return nullptr; } +bool Element::has_pseudo_elements() const +{ + if (m_pseudo_element_data) { + for (auto& pseudo_element : *m_pseudo_element_data) { + if (pseudo_element.layout_node) + return true; + } + } + return false; +} + void Element::clear_pseudo_element_nodes(Badge) { if (m_pseudo_element_data) { diff --git a/Userland/Libraries/LibWeb/DOM/Element.h b/Userland/Libraries/LibWeb/DOM/Element.h index f1b5fc599f6..f0d8d451ed5 100644 --- a/Userland/Libraries/LibWeb/DOM/Element.h +++ b/Userland/Libraries/LibWeb/DOM/Element.h @@ -245,6 +245,7 @@ public: void set_pseudo_element_node(Badge, CSS::Selector::PseudoElement::Type, JS::GCPtr); JS::GCPtr get_pseudo_element_node(CSS::Selector::PseudoElement::Type) const; + bool has_pseudo_elements() const; void clear_pseudo_element_nodes(Badge); void serialize_pseudo_elements_as_json(JsonArraySerializer& children_array) const; diff --git a/Userland/Libraries/LibWeb/DOM/Node.cpp b/Userland/Libraries/LibWeb/DOM/Node.cpp index 828917dfa33..0af8a722296 100644 --- a/Userland/Libraries/LibWeb/DOM/Node.cpp +++ b/Userland/Libraries/LibWeb/DOM/Node.cpp @@ -1362,7 +1362,10 @@ void Node::serialize_tree_as_json(JsonObjectSerializer& object) c MUST((object.add("visible"sv, !!layout_node()))); - if (has_child_nodes() || (is_element() && static_cast(this)->is_shadow_host())) { + auto const* element = is_element() ? static_cast(this) : nullptr; + + if (has_child_nodes() + || (element && (element->is_shadow_host() || element->has_pseudo_elements()))) { auto children = MUST(object.add_array("children"sv)); auto add_child = [&children](DOM::Node const& child) { if (child.is_uninteresting_whitespace_node()) @@ -1374,9 +1377,7 @@ void Node::serialize_tree_as_json(JsonObjectSerializer& object) c }; for_each_child(add_child); - if (is_element()) { - auto const* element = static_cast(this); - + if (element) { // Pseudo-elements don't have DOM nodes,so we have to add them separately. element->serialize_pseudo_elements_as_json(children);