Bladeren bron

LibWebView+WebContent: Add a WebContent IPC to get a DOM node's HTML

Timothy Flynn 1 jaar geleden
bovenliggende
commit
bea11f6d99

+ 5 - 0
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<String> 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);

+ 1 - 0
Userland/Libraries/LibWebView/ViewImplementation.h

@@ -68,6 +68,7 @@ public:
     void add_dom_node_attributes(i32 node_id, Vector<Attribute> attributes);
     void replace_dom_node_attribute(i32 node_id, String name, Vector<Attribute> replacement_attributes);
     void remove_dom_node(i32 node_id);
+    Optional<String> get_dom_node_html(i32 node_id);
 
     void debug_request(DeprecatedString const& request, DeprecatedString const& argument = {});
 

+ 14 - 0
Userland/Services/WebContent/ConnectionFromClient.cpp

@@ -38,6 +38,7 @@
 #include <LibWeb/Loader/ContentFilter.h>
 #include <LibWeb/Loader/ProxyMappings.h>
 #include <LibWeb/Loader/ResourceLoader.h>
+#include <LibWeb/Namespace.h>
 #include <LibWeb/Painting/StackingContext.h>
 #include <LibWeb/Painting/ViewportPaintable.h>
 #include <LibWeb/PermissionsPolicy/AutoplayAllowlist.h>
@@ -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<PageClient>, Web::DOM::Document& document)
 {
     auto& realm = document.realm();

+ 1 - 0
Userland/Services/WebContent/ConnectionFromClient.h

@@ -81,6 +81,7 @@ private:
     virtual void add_dom_node_attributes(i32 node_id, Vector<WebView::Attribute> const& attributes) override;
     virtual void replace_dom_node_attribute(i32 node_id, String const& name, Vector<WebView::Attribute> 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;

+ 1 - 0
Userland/Services/WebContent/WebContentServer.ipc

@@ -51,6 +51,7 @@ endpoint WebContentServer
     add_dom_node_attributes(i32 node_id, Vector<WebView::Attribute> attributes) =|
     replace_dom_node_attribute(i32 node_id, String name, Vector<WebView::Attribute> replacement_attributes) =|
     remove_dom_node(i32 node_id) =|
+    get_dom_node_html(i32 node_id) => (Optional<String> html)
 
     take_document_screenshot() => (Gfx::ShareableBitmap data)