From 3f006d81feacbdf088f554087ad4705712f8e871 Mon Sep 17 00:00:00 2001 From: Vyacheslav Pukhanov Date: Wed, 24 Nov 2021 19:15:04 +0300 Subject: [PATCH] LibWeb: Add JSON serialization for nested browsing contexts This changes allows for nested browser contexts to be embedded in the serialized JSON of their container element (like `iframe`) and enables their inspection in the DOM Inspector. --- Userland/Libraries/LibWeb/DOM/Node.cpp | 12 +++++++++++- Userland/Libraries/LibWeb/DOM/Node.h | 1 + Userland/Libraries/LibWeb/Forward.h | 1 + .../Libraries/LibWeb/HTML/BrowsingContextContainer.h | 8 ++++++++ 4 files changed, 21 insertions(+), 1 deletion(-) diff --git a/Userland/Libraries/LibWeb/DOM/Node.cpp b/Userland/Libraries/LibWeb/DOM/Node.cpp index 52c2213303..2b8b2b88eb 100644 --- a/Userland/Libraries/LibWeb/DOM/Node.cpp +++ b/Userland/Libraries/LibWeb/DOM/Node.cpp @@ -24,6 +24,7 @@ #include #include #include +#include #include #include #include @@ -702,13 +703,22 @@ void Node::serialize_tree_as_json(JsonObjectSerializer& object) c } else if (is_element()) { object.add("type", "element"); - auto element = static_cast(this); + auto const* element = static_cast(this); if (element->has_attributes()) { auto attributes = object.add_object("attributes"); element->for_each_attribute([&attributes](auto& name, auto& value) { attributes.add(name, value); }); } + + if (element->is_browsing_context_container()) { + auto const* container = static_cast(element); + if (auto const* content_document = container->content_document()) { + auto children = object.add_array("children"); + JsonObjectSerializer content_document_object = children.add_object(); + content_document->serialize_tree_as_json(content_document_object); + } + } } else if (is_text()) { object.add("type", "text"); diff --git a/Userland/Libraries/LibWeb/DOM/Node.h b/Userland/Libraries/LibWeb/DOM/Node.h index c8a85b88a6..d3be7b5b13 100644 --- a/Userland/Libraries/LibWeb/DOM/Node.h +++ b/Userland/Libraries/LibWeb/DOM/Node.h @@ -84,6 +84,7 @@ public: virtual bool is_html_html_element() const { return false; } virtual bool is_html_template_element() const { return false; } + virtual bool is_browsing_context_container() const { return false; } ExceptionOr> pre_insert(NonnullRefPtr, RefPtr); ExceptionOr> pre_remove(NonnullRefPtr); diff --git a/Userland/Libraries/LibWeb/Forward.h b/Userland/Libraries/LibWeb/Forward.h index 87b6f13ab5..7112cae86b 100644 --- a/Userland/Libraries/LibWeb/Forward.h +++ b/Userland/Libraries/LibWeb/Forward.h @@ -116,6 +116,7 @@ class DOMRectReadOnly; namespace Web::HTML { class BrowsingContext; +class BrowsingContextContainer; class CanvasRenderingContext2D; class CloseEvent; class DOMParser; diff --git a/Userland/Libraries/LibWeb/HTML/BrowsingContextContainer.h b/Userland/Libraries/LibWeb/HTML/BrowsingContextContainer.h index 6dc69e497a..3d0c2db372 100644 --- a/Userland/Libraries/LibWeb/HTML/BrowsingContextContainer.h +++ b/Userland/Libraries/LibWeb/HTML/BrowsingContextContainer.h @@ -29,6 +29,14 @@ public: protected: RefPtr m_nested_browsing_context; + +private: + virtual bool is_browsing_context_container() const override { return true; } }; } + +namespace Web::DOM { +template<> +inline bool Node::fast_is() const { return is_browsing_context_container(); } +}