diff options
author | Adam Hodgen <ant1441@gmail.com> | 2021-06-07 16:32:24 +0100 |
---|---|---|
committer | Andreas Kling <kling@serenityos.org> | 2021-06-29 23:06:48 +0200 |
commit | 4affe052b8e6a955e3c5ca06ea58accf985ae51e (patch) | |
tree | 4830ca0e54c406ead90695ddf33b0198ee2b832f /Userland/Libraries/LibWeb/DOM | |
parent | f4eab697859825d70c3a1ed47db0825d2af0f797 (diff) | |
download | serenity-4affe052b8e6a955e3c5ca06ea58accf985ae51e.zip |
LibWeb: Add JSON serialization method to DOM::Node
This method builds a JSON object representing the full state of the
DOM tree.
The JSON that is built will be used for building the DOM Inspector
widget for the OutOfProcessWebView.
Diffstat (limited to 'Userland/Libraries/LibWeb/DOM')
-rw-r--r-- | Userland/Libraries/LibWeb/DOM/Document.cpp | 10 | ||||
-rw-r--r-- | Userland/Libraries/LibWeb/DOM/Document.h | 2 | ||||
-rw-r--r-- | Userland/Libraries/LibWeb/DOM/Node.cpp | 32 | ||||
-rw-r--r-- | Userland/Libraries/LibWeb/DOM/Node.h | 4 |
4 files changed, 48 insertions, 0 deletions
diff --git a/Userland/Libraries/LibWeb/DOM/Document.cpp b/Userland/Libraries/LibWeb/DOM/Document.cpp index a97d29207f..69c07deaae 100644 --- a/Userland/Libraries/LibWeb/DOM/Document.cpp +++ b/Userland/Libraries/LibWeb/DOM/Document.cpp @@ -934,4 +934,14 @@ void Document::set_cookie(String cookie_string, Cookie::Source source) page->client().page_did_set_cookie(m_url, cookie.value(), source); } +String Document::dump_dom_tree_as_json() const +{ + StringBuilder builder; + JsonObjectSerializer json(builder); + serialize_tree_as_json(json); + + json.finish(); + return builder.to_string(); +} + } diff --git a/Userland/Libraries/LibWeb/DOM/Document.h b/Userland/Libraries/LibWeb/DOM/Document.h index 74c184e032..0d9a999d1c 100644 --- a/Userland/Libraries/LibWeb/DOM/Document.h +++ b/Userland/Libraries/LibWeb/DOM/Document.h @@ -267,6 +267,8 @@ public: virtual EventTarget* get_parent(const Event&) override; + String dump_dom_tree_as_json() const; + private: explicit Document(const URL&); diff --git a/Userland/Libraries/LibWeb/DOM/Node.cpp b/Userland/Libraries/LibWeb/DOM/Node.cpp index ea0dea14dd..afd431d628 100644 --- a/Userland/Libraries/LibWeb/DOM/Node.cpp +++ b/Userland/Libraries/LibWeb/DOM/Node.cpp @@ -606,4 +606,36 @@ RefPtr<Document> Node::owner_document() const return m_document; } +void Node::serialize_tree_as_json(JsonObjectSerializer<StringBuilder>& object) const +{ + object.add("name", node_name().view()); + object.add("internal_id", (size_t)this); + if (is_document()) + object.add("type", "document"); + else if (is_element()) { + object.add("type", "element"); + + auto element = downcast<DOM::Element>(this); + if (element->has_attributes()) { + auto attributes = object.add_object("attributes"); + element->for_each_attribute([&attributes](auto& name, auto& value) { + attributes.add(name, value); + }); + } + } else if (is_text()) { + object.add("type", "text"); + + auto text_node = downcast<DOM::Text>(this); + object.add("text", text_node->data()); + } + + if (has_child_nodes()) { + auto children = object.add_array("children"); + for_each_child([&children](DOM::Node& child) { + JsonObjectSerializer<StringBuilder> 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 4751a494f8..06c1e59b1c 100644 --- a/Userland/Libraries/LibWeb/DOM/Node.h +++ b/Userland/Libraries/LibWeb/DOM/Node.h @@ -7,6 +7,7 @@ #pragma once #include <AK/Badge.h> +#include <AK/JsonObjectSerializer.h> #include <AK/RefPtr.h> #include <AK/String.h> #include <AK/TypeCasts.h> @@ -162,6 +163,9 @@ public: bool is_host_including_inclusive_ancestor_of(const Node&) const; + // Used for dumping the DOM Tree + void serialize_tree_as_json(JsonObjectSerializer<StringBuilder>&) const; + protected: Node(Document&, NodeType); |