From 097b5e4803dff84b97821a0b5ce5f5ac0eb6b6ff Mon Sep 17 00:00:00 2001 From: Andreas Kling Date: Sun, 28 May 2023 20:54:33 +0200 Subject: [PATCH] WebContent+headless-browser: Use document.body.innerText for text tests This should be less fickle than the "select all & copy selected text" trick we were doing earlier. --- Userland/Services/WebContent/ConnectionFromClient.cpp | 10 ++++++++++ Userland/Services/WebContent/ConnectionFromClient.h | 1 + Userland/Services/WebContent/WebContentServer.ipc | 1 + Userland/Utilities/headless-browser.cpp | 11 +++++++---- 4 files changed, 19 insertions(+), 4 deletions(-) diff --git a/Userland/Services/WebContent/ConnectionFromClient.cpp b/Userland/Services/WebContent/ConnectionFromClient.cpp index e56628039c..e93fa55e2e 100644 --- a/Userland/Services/WebContent/ConnectionFromClient.cpp +++ b/Userland/Services/WebContent/ConnectionFromClient.cpp @@ -636,6 +636,16 @@ Messages::WebContentServer::DumpLayoutTreeResponse ConnectionFromClient::dump_la return builder.to_deprecated_string(); } +Messages::WebContentServer::DumpTextResponse ConnectionFromClient::dump_text() +{ + auto* document = page().top_level_browsing_context().active_document(); + if (!document) + return DeprecatedString { "(no DOM tree)" }; + if (!document->body()) + return DeprecatedString { "(no body)" }; + return document->body()->inner_text(); +} + void ConnectionFromClient::set_content_filters(Vector const& filters) { Web::ContentFilter::the().set_patterns(filters).release_value_but_fixme_should_propagate_errors(); diff --git a/Userland/Services/WebContent/ConnectionFromClient.h b/Userland/Services/WebContent/ConnectionFromClient.h index d2ed7a58bc..a8d66c5ed6 100644 --- a/Userland/Services/WebContent/ConnectionFromClient.h +++ b/Userland/Services/WebContent/ConnectionFromClient.h @@ -74,6 +74,7 @@ private: virtual void inspect_accessibility_tree() override; virtual Messages::WebContentServer::GetHoveredNodeIdResponse get_hovered_node_id() override; virtual Messages::WebContentServer::DumpLayoutTreeResponse dump_layout_tree() override; + virtual Messages::WebContentServer::DumpTextResponse dump_text() override; virtual void set_content_filters(Vector const&) override; virtual void set_autoplay_allowed_on_all_websites() override; virtual void set_autoplay_allowlist(Vector const& allowlist) override; diff --git a/Userland/Services/WebContent/WebContentServer.ipc b/Userland/Services/WebContent/WebContentServer.ipc index 748b6deb87..ceea4f0375 100644 --- a/Userland/Services/WebContent/WebContentServer.ipc +++ b/Userland/Services/WebContent/WebContentServer.ipc @@ -50,6 +50,7 @@ endpoint WebContentServer run_javascript(DeprecatedString js_source) =| dump_layout_tree() => (DeprecatedString dump) + dump_text() => (DeprecatedString dump) get_selected_text() => (DeprecatedString selection) select_all() =| diff --git a/Userland/Utilities/headless-browser.cpp b/Userland/Utilities/headless-browser.cpp index bdb74dc881..c9e055a19a 100644 --- a/Userland/Utilities/headless-browser.cpp +++ b/Userland/Utilities/headless-browser.cpp @@ -80,6 +80,11 @@ public: return String::from_deprecated_string(client().dump_layout_tree()); } + ErrorOr dump_text() + { + return String::from_deprecated_string(client().dump_text()); + } + private: HeadlessWebContentView() = default; @@ -191,8 +196,7 @@ static ErrorOr run_one_test(HeadlessWebContentView& view, StringView inp }; } else if (mode == TestMode::Text) { view.on_load_finish = [&](auto const&) { - view.select_all(); - result = String::from_utf8(view.selected_text()).release_value_but_fixme_should_propagate_errors(); + result = view.dump_text().release_value_but_fixme_should_propagate_errors(); loop.quit(0); }; } @@ -384,8 +388,7 @@ ErrorOr serenity_main(Main::Arguments arguments) }; } else if (dump_text) { view->on_load_finish = [&](auto const&) { - view->select_all(); - auto text = view->selected_text(); + auto text = view->dump_text().release_value_but_fixme_should_propagate_errors(); out("{}", text); fflush(stdout);