From 5db51d85a3d8503766f84a092252439510986357 Mon Sep 17 00:00:00 2001 From: Andreas Kling Date: Tue, 2 Nov 2021 19:20:57 +0100 Subject: [PATCH] LibWeb: Hide uninteresting whitespace text nodes from DOM inspector Use a simple heuristic to exclude uninteresting whitespace and de-clutter the inspector's DOM tree. Uninteresting whitespace is currently one of these: - Non-rendered whitespace-only text nodes - Rendered whitespace-only text nodes between block-level elements --- Userland/Libraries/LibWeb/DOM/Node.cpp | 19 +++++++++++++++++++ Userland/Libraries/LibWeb/DOM/Node.h | 2 ++ 2 files changed, 21 insertions(+) diff --git a/Userland/Libraries/LibWeb/DOM/Node.cpp b/Userland/Libraries/LibWeb/DOM/Node.cpp index 29f3cfc7aa..08196e5352 100644 --- a/Userland/Libraries/LibWeb/DOM/Node.cpp +++ b/Userland/Libraries/LibWeb/DOM/Node.cpp @@ -676,6 +676,23 @@ RefPtr Node::owner_document() const return m_document; } +// This function tells us whether a node is interesting enough to show up +// in the DOM inspector. This hides two things: +// - Non-rendered whitespace +// - Rendered whitespace between block-level elements +bool Node::is_uninteresting_whitespace_node() const +{ + if (!is(*this)) + return false; + if (!static_cast(*this).data().is_whitespace()) + return false; + if (!layout_node()) + return true; + if (layout_node()->parent()->is_anonymous()) + return true; + return false; +} + void Node::serialize_tree_as_json(JsonObjectSerializer& object) const { object.add("name", node_name().view()); @@ -702,6 +719,8 @@ void Node::serialize_tree_as_json(JsonObjectSerializer& object) c if (has_child_nodes()) { auto children = object.add_array("children"); for_each_child([&children](DOM::Node& child) { + if (child.is_uninteresting_whitespace_node()) + return; JsonObjectSerializer child_object = children.add_object(); child.serialize_tree_as_json(child_object); }); diff --git a/Userland/Libraries/LibWeb/DOM/Node.h b/Userland/Libraries/LibWeb/DOM/Node.h index cc4fa09e84..c8a85b88a6 100644 --- a/Userland/Libraries/LibWeb/DOM/Node.h +++ b/Userland/Libraries/LibWeb/DOM/Node.h @@ -202,6 +202,8 @@ public: NonnullRefPtr get_root_node(GetRootNodeOptions const& options = {}); + bool is_uninteresting_whitespace_node() const; + protected: Node(Document&, NodeType);