From bea11f6d99868e2a95f46fd47d6a65f53eb255fa Mon Sep 17 00:00:00 2001 From: Timothy Flynn Date: Wed, 6 Dec 2023 10:36:27 -0500 Subject: [PATCH] LibWebView+WebContent: Add a WebContent IPC to get a DOM node's HTML --- .../Libraries/LibWebView/ViewImplementation.cpp | 5 +++++ Userland/Libraries/LibWebView/ViewImplementation.h | 1 + .../Services/WebContent/ConnectionFromClient.cpp | 14 ++++++++++++++ .../Services/WebContent/ConnectionFromClient.h | 1 + Userland/Services/WebContent/WebContentServer.ipc | 1 + 5 files changed, 22 insertions(+) diff --git a/Userland/Libraries/LibWebView/ViewImplementation.cpp b/Userland/Libraries/LibWebView/ViewImplementation.cpp index 8e8a71ba49..5f2a6bd1f4 100644 --- a/Userland/Libraries/LibWebView/ViewImplementation.cpp +++ b/Userland/Libraries/LibWebView/ViewImplementation.cpp @@ -206,6 +206,11 @@ void ViewImplementation::remove_dom_node(i32 node_id) client().async_remove_dom_node(node_id); } +Optional ViewImplementation::get_dom_node_html(i32 node_id) +{ + return client().get_dom_node_html(node_id); +} + void ViewImplementation::debug_request(DeprecatedString const& request, DeprecatedString const& argument) { client().async_debug_request(request, argument); diff --git a/Userland/Libraries/LibWebView/ViewImplementation.h b/Userland/Libraries/LibWebView/ViewImplementation.h index 487305506a..8078b2b2f2 100644 --- a/Userland/Libraries/LibWebView/ViewImplementation.h +++ b/Userland/Libraries/LibWebView/ViewImplementation.h @@ -68,6 +68,7 @@ public: void add_dom_node_attributes(i32 node_id, Vector attributes); void replace_dom_node_attribute(i32 node_id, String name, Vector replacement_attributes); void remove_dom_node(i32 node_id); + Optional get_dom_node_html(i32 node_id); void debug_request(DeprecatedString const& request, DeprecatedString const& argument = {}); diff --git a/Userland/Services/WebContent/ConnectionFromClient.cpp b/Userland/Services/WebContent/ConnectionFromClient.cpp index f5f7bb5acd..b92be7f29f 100644 --- a/Userland/Services/WebContent/ConnectionFromClient.cpp +++ b/Userland/Services/WebContent/ConnectionFromClient.cpp @@ -38,6 +38,7 @@ #include #include #include +#include #include #include #include @@ -737,6 +738,19 @@ void ConnectionFromClient::remove_dom_node(i32 node_id) active_document->force_layout(); } +Messages::WebContentServer::GetDomNodeHtmlResponse ConnectionFromClient::get_dom_node_html(i32 node_id) +{ + auto* dom_node = Web::DOM::Node::from_unique_id(node_id); + if (!dom_node) + return OptionalNone {}; + + // FIXME: Implement Element's outerHTML attribute. + auto container = Web::DOM::create_element(dom_node->document(), Web::HTML::TagNames::div, Web::Namespace::HTML).release_value_but_fixme_should_propagate_errors(); + container->append_child(dom_node->clone_node(nullptr, true)).release_value_but_fixme_should_propagate_errors(); + + return container->inner_html().release_value_but_fixme_should_propagate_errors(); +} + void ConnectionFromClient::initialize_js_console(Badge, Web::DOM::Document& document) { auto& realm = document.realm(); diff --git a/Userland/Services/WebContent/ConnectionFromClient.h b/Userland/Services/WebContent/ConnectionFromClient.h index e74fdaeae0..e8b9605d24 100644 --- a/Userland/Services/WebContent/ConnectionFromClient.h +++ b/Userland/Services/WebContent/ConnectionFromClient.h @@ -81,6 +81,7 @@ private: virtual void add_dom_node_attributes(i32 node_id, Vector const& attributes) override; virtual void replace_dom_node_attribute(i32 node_id, String const& name, Vector const& replacement_attributes) override; virtual void remove_dom_node(i32 node_id) override; + virtual Messages::WebContentServer::GetDomNodeHtmlResponse get_dom_node_html(i32 node_id) override; virtual Messages::WebContentServer::DumpLayoutTreeResponse dump_layout_tree() override; virtual Messages::WebContentServer::DumpPaintTreeResponse dump_paint_tree() override; diff --git a/Userland/Services/WebContent/WebContentServer.ipc b/Userland/Services/WebContent/WebContentServer.ipc index 3ef342dc48..8bc433f4b9 100644 --- a/Userland/Services/WebContent/WebContentServer.ipc +++ b/Userland/Services/WebContent/WebContentServer.ipc @@ -51,6 +51,7 @@ endpoint WebContentServer add_dom_node_attributes(i32 node_id, Vector attributes) =| replace_dom_node_attribute(i32 node_id, String name, Vector replacement_attributes) =| remove_dom_node(i32 node_id) =| + get_dom_node_html(i32 node_id) => (Optional html) take_document_screenshot() => (Gfx::ShareableBitmap data)