LibWebView+WebConent: Add an IPC to get an element's absolute rect

This commit is contained in:
Timothy Flynn 2022-11-03 12:51:44 -04:00 committed by Linus Groh
parent 77b6d0bf16
commit 995f3db384
Notes: sideshowbarker 2024-07-17 08:25:15 +09:00
5 changed files with 40 additions and 0 deletions

View file

@ -560,6 +560,11 @@ String OutOfProcessWebView::get_element_tag_name(i32 element_id)
return client().get_element_tag_name(element_id);
}
Gfx::IntRect OutOfProcessWebView::get_element_rect(i32 element_id)
{
return client().get_element_rect(element_id);
}
void OutOfProcessWebView::set_content_filters(Vector<String> filters)
{
client().async_set_content_filters(filters);

View file

@ -71,6 +71,7 @@ public:
String get_computed_value_for_element(i32 element_id, String const& property_name);
String get_element_text(i32 element_id);
String get_element_tag_name(i32 element_id);
Gfx::IntRect get_element_rect(i32 element_id);
void set_content_filters(Vector<String>);
void set_proxy_mappings(Vector<String> proxies, HashMap<String, size_t> mappings);

View file

@ -24,6 +24,7 @@
#include <LibWeb/DOM/Document.h>
#include <LibWeb/DOM/NodeList.h>
#include <LibWeb/Dump.h>
#include <LibWeb/Geometry/DOMRect.h>
#include <LibWeb/HTML/BrowsingContext.h>
#include <LibWeb/HTML/Scripting/ClassicScript.h>
#include <LibWeb/HTML/Storage.h>
@ -585,6 +586,36 @@ Messages::WebContentServer::GetElementTagNameResponse ConnectionFromClient::get_
return { element->tag_name() };
}
// https://w3c.github.io/webdriver/#dfn-calculate-the-absolute-position
static Gfx::IntPoint calculate_absolute_position_of_element(Web::Page const& page, JS::NonnullGCPtr<Web::Geometry::DOMRect> rect)
{
// 1. Let rect be the value returned by calling getBoundingClientRect().
// 2. Let window be the associated window of current top-level browsing context.
auto const* window = page.top_level_browsing_context().active_window();
// 3. Let x be (scrollX of window + rects x coordinate).
auto x = (window ? static_cast<int>(window->scroll_x()) : 0) + static_cast<int>(rect->x());
// 4. Let y be (scrollY of window + rects y coordinate).
auto y = (window ? static_cast<int>(window->scroll_y()) : 0) + static_cast<int>(rect->y());
// 5. Return a pair of (x, y).
return { x, y };
}
Messages::WebContentServer::GetElementRectResponse ConnectionFromClient::get_element_rect(i32 element_id)
{
auto element = find_element_by_id(element_id);
if (!element.has_value())
return { {} };
auto bounding_rect = element->get_bounding_client_rect();
auto coordinates = calculate_absolute_position_of_element(page(), bounding_rect);
return { { coordinates.x(), coordinates.y(), static_cast<int>(bounding_rect->width()), static_cast<int>(bounding_rect->height()) } };
}
Messages::WebContentServer::GetSelectedTextResponse ConnectionFromClient::get_selected_text()
{
return page().focused_context().selected_text();

View file

@ -90,6 +90,7 @@ private:
virtual Messages::WebContentServer::GetComputedValueForElementResponse get_computed_value_for_element(i32 element_id, String const& property_name) override;
virtual Messages::WebContentServer::GetElementTextResponse get_element_text(i32 element_id) override;
virtual Messages::WebContentServer::GetElementTagNameResponse get_element_tag_name(i32 element_id) override;
virtual Messages::WebContentServer::GetElementRectResponse get_element_rect(i32 element_id) override;
virtual Messages::WebContentServer::GetLocalStorageEntriesResponse get_local_storage_entries() override;
virtual Messages::WebContentServer::GetSessionStorageEntriesResponse get_session_storage_entries() override;

View file

@ -1,6 +1,7 @@
#include <AK/URL.h>
#include <LibIPC/File.h>
#include <LibCore/AnonymousBuffer.h>
#include <LibGfx/Rect.h>
#include <LibGfx/ShareableBitmap.h>
#include <LibWeb/CSS/PreferredColorScheme.h>
#include <LibWeb/CSS/Selector.h>
@ -47,6 +48,7 @@ endpoint WebContentServer
get_computed_value_for_element(i32 element_id, String property_name) => (String computed_value)
get_element_text(i32 element_id) => (String text)
get_element_tag_name(i32 element_id) => (String tag_name)
get_element_rect(i32 element_id) => (Gfx::IntRect rect)
webdriver_execute_script(String body, Vector<String> json_arguments, Optional<u64> timeout, bool async) => (Web::WebDriver::ExecuteScriptResultType result_type, String json_result)