From f381f8d63e7935e629a13dcdc9dd689467323018 Mon Sep 17 00:00:00 2001 From: Sam Atkins Date: Fri, 27 Aug 2021 12:31:55 +0100 Subject: [PATCH] LibWeb+WebContent: Add inspect_dom_node() IPC call This is the IPC version of `Document::set_inspected_node()`, using a node ID. We return the inspected node's style properties as JSON, so that the DOM Inspector can immediately display them. --- .../Libraries/LibWeb/OutOfProcessWebView.cpp | 16 ++++++++ .../Libraries/LibWeb/OutOfProcessWebView.h | 8 ++++ .../Services/WebContent/ClientConnection.cpp | 38 +++++++++++++++++++ .../Services/WebContent/ClientConnection.h | 1 + .../Services/WebContent/WebContentServer.ipc | 1 + 5 files changed, 64 insertions(+) diff --git a/Userland/Libraries/LibWeb/OutOfProcessWebView.cpp b/Userland/Libraries/LibWeb/OutOfProcessWebView.cpp index 8cc8984801..05755945a8 100644 --- a/Userland/Libraries/LibWeb/OutOfProcessWebView.cpp +++ b/Userland/Libraries/LibWeb/OutOfProcessWebView.cpp @@ -402,6 +402,22 @@ void OutOfProcessWebView::inspect_dom_tree() client().async_inspect_dom_tree(); } +Optional OutOfProcessWebView::inspect_dom_node(i32 node_id) +{ + auto response = client().inspect_dom_node(node_id); + if (!response.has_style()) + return {}; + return DOMNodeProperties { + .specified_values_json = response.specified_style(), + .computed_values_json = response.computed_style() + }; +} + +void OutOfProcessWebView::clear_inspected_dom_node() +{ + client().inspect_dom_node(0); +} + void OutOfProcessWebView::js_console_initialize() { client().async_js_console_initialize(); diff --git a/Userland/Libraries/LibWeb/OutOfProcessWebView.h b/Userland/Libraries/LibWeb/OutOfProcessWebView.h index fb705a006d..1eb360cdec 100644 --- a/Userland/Libraries/LibWeb/OutOfProcessWebView.h +++ b/Userland/Libraries/LibWeb/OutOfProcessWebView.h @@ -31,7 +31,15 @@ public: void debug_request(const String& request, const String& argument = {}); void get_source(); + void inspect_dom_tree(); + struct DOMNodeProperties { + String specified_values_json; + String computed_values_json; + }; + Optional inspect_dom_node(i32 node_id); + void clear_inspected_dom_node(); + void js_console_initialize(); void js_console_input(const String& js_source); diff --git a/Userland/Services/WebContent/ClientConnection.cpp b/Userland/Services/WebContent/ClientConnection.cpp index 670329fe88..85a5b55384 100644 --- a/Userland/Services/WebContent/ClientConnection.cpp +++ b/Userland/Services/WebContent/ClientConnection.cpp @@ -1,5 +1,6 @@ /* * Copyright (c) 2020-2021, Andreas Kling + * Copyright (c) 2021, Sam Atkins * * SPDX-License-Identifier: BSD-2-Clause */ @@ -223,6 +224,43 @@ void ClientConnection::inspect_dom_tree() } } +Messages::WebContentServer::InspectDomNodeResponse ClientConnection::inspect_dom_node(i32 node_id) +{ + if (auto* doc = page().top_level_browsing_context().document()) { + Web::DOM::Node* node = Web::DOM::Node::from_id(node_id); + if (!node || (&node->document() != doc)) { + doc->set_inspected_node(nullptr); + return { false, "", "" }; + } + + doc->set_inspected_node(node); + + if (node->is_element()) { + auto& element = verify_cast(*node); + if (!element.specified_css_values()) + return { false, "", "" }; + + auto serialize_json = [](Web::CSS::StyleProperties const& properties) -> String { + StringBuilder builder; + + JsonObjectSerializer serializer(builder); + properties.for_each_property([&](auto property_id, auto& value) { + serializer.add(Web::CSS::string_from_property_id(property_id), value.to_string()); + }); + serializer.finish(); + + return builder.to_string(); + }; + + String specified_values_json = serialize_json(*element.specified_css_values()); + String computed_values_json = serialize_json(element.computed_style()); + return { true, specified_values_json, computed_values_json }; + } + } + + return { false, "", "" }; +} + void ClientConnection::js_console_initialize() { if (auto* document = page().top_level_browsing_context().document()) { diff --git a/Userland/Services/WebContent/ClientConnection.h b/Userland/Services/WebContent/ClientConnection.h index 64c2404ad2..99d39e8277 100644 --- a/Userland/Services/WebContent/ClientConnection.h +++ b/Userland/Services/WebContent/ClientConnection.h @@ -49,6 +49,7 @@ private: virtual void debug_request(String const&, String const&) override; virtual void get_source() override; virtual void inspect_dom_tree() override; + virtual Messages::WebContentServer::InspectDomNodeResponse inspect_dom_node(i32) override; virtual void js_console_initialize() override; virtual void js_console_input(String const&) override; virtual void run_javascript(String const&) override; diff --git a/Userland/Services/WebContent/WebContentServer.ipc b/Userland/Services/WebContent/WebContentServer.ipc index 39db7ff713..729287f001 100644 --- a/Userland/Services/WebContent/WebContentServer.ipc +++ b/Userland/Services/WebContent/WebContentServer.ipc @@ -27,6 +27,7 @@ endpoint WebContentServer debug_request(String request, String argument) =| get_source() =| inspect_dom_tree() =| + inspect_dom_node(i32 node_id) => (bool has_style, String specified_style, String computed_style) js_console_initialize() =| js_console_input(String js_source) =|