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

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.
This commit is contained in:
Vyacheslav Pukhanov 2021-11-24 19:15:04 +03:00 committed by Andreas Kling
parent 58397f356f
commit 3f006d81fe
4 changed files with 21 additions and 1 deletions

View file

@ -24,6 +24,7 @@
#include <LibWeb/DOM/Node.h> #include <LibWeb/DOM/Node.h>
#include <LibWeb/DOM/ProcessingInstruction.h> #include <LibWeb/DOM/ProcessingInstruction.h>
#include <LibWeb/DOM/ShadowRoot.h> #include <LibWeb/DOM/ShadowRoot.h>
#include <LibWeb/HTML/BrowsingContextContainer.h>
#include <LibWeb/HTML/HTMLAnchorElement.h> #include <LibWeb/HTML/HTMLAnchorElement.h>
#include <LibWeb/HTML/Parser/HTMLParser.h> #include <LibWeb/HTML/Parser/HTMLParser.h>
#include <LibWeb/Layout/InitialContainingBlock.h> #include <LibWeb/Layout/InitialContainingBlock.h>
@ -702,13 +703,22 @@ void Node::serialize_tree_as_json(JsonObjectSerializer<StringBuilder>& object) c
} else if (is_element()) { } else if (is_element()) {
object.add("type", "element"); object.add("type", "element");
auto element = static_cast<DOM::Element const*>(this); auto const* element = static_cast<DOM::Element const*>(this);
if (element->has_attributes()) { if (element->has_attributes()) {
auto attributes = object.add_object("attributes"); auto attributes = object.add_object("attributes");
element->for_each_attribute([&attributes](auto& name, auto& value) { element->for_each_attribute([&attributes](auto& name, auto& value) {
attributes.add(name, value); attributes.add(name, value);
}); });
} }
if (element->is_browsing_context_container()) {
auto const* container = static_cast<HTML::BrowsingContextContainer const*>(element);
if (auto const* content_document = container->content_document()) {
auto children = object.add_array("children");
JsonObjectSerializer<StringBuilder> content_document_object = children.add_object();
content_document->serialize_tree_as_json(content_document_object);
}
}
} else if (is_text()) { } else if (is_text()) {
object.add("type", "text"); object.add("type", "text");

View file

@ -84,6 +84,7 @@ public:
virtual bool is_html_html_element() const { return false; } virtual bool is_html_html_element() const { return false; }
virtual bool is_html_template_element() const { return false; } virtual bool is_html_template_element() const { return false; }
virtual bool is_browsing_context_container() const { return false; }
ExceptionOr<NonnullRefPtr<Node>> pre_insert(NonnullRefPtr<Node>, RefPtr<Node>); ExceptionOr<NonnullRefPtr<Node>> pre_insert(NonnullRefPtr<Node>, RefPtr<Node>);
ExceptionOr<NonnullRefPtr<Node>> pre_remove(NonnullRefPtr<Node>); ExceptionOr<NonnullRefPtr<Node>> pre_remove(NonnullRefPtr<Node>);

View file

@ -116,6 +116,7 @@ class DOMRectReadOnly;
namespace Web::HTML { namespace Web::HTML {
class BrowsingContext; class BrowsingContext;
class BrowsingContextContainer;
class CanvasRenderingContext2D; class CanvasRenderingContext2D;
class CloseEvent; class CloseEvent;
class DOMParser; class DOMParser;

View file

@ -29,6 +29,14 @@ public:
protected: protected:
RefPtr<BrowsingContext> m_nested_browsing_context; RefPtr<BrowsingContext> m_nested_browsing_context;
private:
virtual bool is_browsing_context_container() const override { return true; }
}; };
} }
namespace Web::DOM {
template<>
inline bool Node::fast_is<HTML::BrowsingContextContainer>() const { return is_browsing_context_container(); }
}