diff --git a/Userland/Applications/Browser/BrowserWindow.cpp b/Userland/Applications/Browser/BrowserWindow.cpp index e802f4e83f..987afc4013 100644 --- a/Userland/Applications/Browser/BrowserWindow.cpp +++ b/Userland/Applications/Browser/BrowserWindow.cpp @@ -611,10 +611,6 @@ void BrowserWindow::create_new_tab(URL url, bool activate) active_tab().view().scroll_element_into_view(element_id); }; - new_tab.webdriver_endpoints().on_get_element_rect = [this](i32 element_id) { - return active_tab().view().get_element_rect(element_id); - }; - new_tab.webdriver_endpoints().on_is_element_enabled = [this](i32 element_id) { return active_tab().view().is_element_enabled(element_id); }; diff --git a/Userland/Applications/Browser/WebDriverConnection.cpp b/Userland/Applications/Browser/WebDriverConnection.cpp index 16408f607a..541f3f1f88 100644 --- a/Userland/Applications/Browser/WebDriverConnection.cpp +++ b/Userland/Applications/Browser/WebDriverConnection.cpp @@ -146,17 +146,6 @@ void WebDriverConnection::scroll_element_into_view(i32 element_id) } } -Messages::WebDriverSessionClient::GetElementRectResponse WebDriverConnection::get_element_rect(i32 element_id) -{ - dbgln("WebDriverConnection: get_element_rect {}", element_id); - if (auto browser_window = m_browser_window.strong_ref()) { - auto& tab = browser_window->active_tab(); - if (tab.webdriver_endpoints().on_get_element_rect) - return { tab.webdriver_endpoints().on_get_element_rect(element_id) }; - } - return { {} }; -} - Messages::WebDriverSessionClient::IsElementEnabledResponse WebDriverConnection::is_element_enabled(i32 element_id) { dbgln("WebDriverConnection: is_element_enabled {}", element_id); diff --git a/Userland/Applications/Browser/WebDriverConnection.h b/Userland/Applications/Browser/WebDriverConnection.h index 12800a65bf..7e5007ecae 100644 --- a/Userland/Applications/Browser/WebDriverConnection.h +++ b/Userland/Applications/Browser/WebDriverConnection.h @@ -49,7 +49,6 @@ public: virtual void add_cookie(Web::Cookie::ParsedCookie const&) override; virtual void update_cookie(Web::Cookie::Cookie const&) override; virtual void scroll_element_into_view(i32 element_id) override; - virtual Messages::WebDriverSessionClient::GetElementRectResponse get_element_rect(i32 element_id) override; virtual Messages::WebDriverSessionClient::IsElementEnabledResponse is_element_enabled(i32 element_id) override; virtual Messages::WebDriverSessionClient::TakeScreenshotResponse take_screenshot() override; virtual Messages::WebDriverSessionClient::TakeElementScreenshotResponse take_element_screenshot(i32 element_id) override; diff --git a/Userland/Applications/Browser/WebDriverEndpoints.h b/Userland/Applications/Browser/WebDriverEndpoints.h index 387a56dce0..7aed04734d 100644 --- a/Userland/Applications/Browser/WebDriverEndpoints.h +++ b/Userland/Applications/Browser/WebDriverEndpoints.h @@ -24,7 +24,6 @@ public: ~WebDriverEndpoints() = default; Function on_scroll_element_into_view; - Function on_get_element_rect; Function on_is_element_enabled; Function on_take_element_screenshot; Function on_serialize_source; diff --git a/Userland/Applications/Browser/WebDriverSessionClient.ipc b/Userland/Applications/Browser/WebDriverSessionClient.ipc index eae41548ef..5a358b9533 100644 --- a/Userland/Applications/Browser/WebDriverSessionClient.ipc +++ b/Userland/Applications/Browser/WebDriverSessionClient.ipc @@ -25,7 +25,6 @@ endpoint WebDriverSessionClient { add_cookie(Web::Cookie::ParsedCookie cookie) =| update_cookie(Web::Cookie::Cookie cookie) =| scroll_element_into_view(i32 element_id) => () - get_element_rect(i32 element_id) => (Gfx::IntRect rect) is_element_enabled(i32 element_id) => (bool enabled) take_screenshot() => (Gfx::ShareableBitmap data) take_element_screenshot(i32 element_id) => (Gfx::ShareableBitmap data) diff --git a/Userland/Libraries/LibWebView/OutOfProcessWebView.cpp b/Userland/Libraries/LibWebView/OutOfProcessWebView.cpp index 2b0dd8e312..899294f7d4 100644 --- a/Userland/Libraries/LibWebView/OutOfProcessWebView.cpp +++ b/Userland/Libraries/LibWebView/OutOfProcessWebView.cpp @@ -559,11 +559,6 @@ void OutOfProcessWebView::scroll_element_into_view(i32 element_id) return client().scroll_element_into_view(element_id); } -Gfx::IntRect OutOfProcessWebView::get_element_rect(i32 element_id) -{ - return client().get_element_rect(element_id); -} - bool OutOfProcessWebView::is_element_enabled(i32 element_id) { return client().is_element_enabled(element_id); diff --git a/Userland/Libraries/LibWebView/OutOfProcessWebView.h b/Userland/Libraries/LibWebView/OutOfProcessWebView.h index 24fdad5f21..926e6cc7ae 100644 --- a/Userland/Libraries/LibWebView/OutOfProcessWebView.h +++ b/Userland/Libraries/LibWebView/OutOfProcessWebView.h @@ -64,7 +64,6 @@ public: OrderedHashMap get_session_storage_entries(); void scroll_element_into_view(i32 element_id); - Gfx::IntRect get_element_rect(i32 element_id); bool is_element_enabled(i32 element_id); void set_content_filters(Vector); diff --git a/Userland/Services/WebContent/ConnectionFromClient.cpp b/Userland/Services/WebContent/ConnectionFromClient.cpp index 273306bf24..23c55f1165 100644 --- a/Userland/Services/WebContent/ConnectionFromClient.cpp +++ b/Userland/Services/WebContent/ConnectionFromClient.cpp @@ -525,15 +525,6 @@ static Gfx::IntRect calculate_absolute_rect_of_element(Web::Page const& page, We }; } -Messages::WebContentServer::GetElementRectResponse ConnectionFromClient::get_element_rect(i32 element_id) -{ - auto element = find_element_by_id(element_id); - if (!element.has_value()) - return { {} }; - - return { calculate_absolute_rect_of_element(page(), *element) }; -} - Messages::WebContentServer::IsElementEnabledResponse ConnectionFromClient::is_element_enabled(i32 element_id) { auto element = find_element_by_id(element_id); diff --git a/Userland/Services/WebContent/ConnectionFromClient.h b/Userland/Services/WebContent/ConnectionFromClient.h index a5ed091d47..51b37d17b3 100644 --- a/Userland/Services/WebContent/ConnectionFromClient.h +++ b/Userland/Services/WebContent/ConnectionFromClient.h @@ -84,7 +84,6 @@ private: virtual void js_console_request_messages(i32) override; virtual void scroll_element_into_view(i32 element_id) override; - virtual Messages::WebContentServer::GetElementRectResponse get_element_rect(i32 element_id) override; virtual Messages::WebContentServer::IsElementEnabledResponse is_element_enabled(i32 element_id) override; virtual Messages::WebContentServer::TakeElementScreenshotResponse take_element_screenshot(i32 element_id) override; virtual Messages::WebContentServer::TakeDocumentScreenshotResponse take_document_screenshot() override; diff --git a/Userland/Services/WebContent/WebContentServer.ipc b/Userland/Services/WebContent/WebContentServer.ipc index 636b6402b0..219b6c580d 100644 --- a/Userland/Services/WebContent/WebContentServer.ipc +++ b/Userland/Services/WebContent/WebContentServer.ipc @@ -43,7 +43,6 @@ endpoint WebContentServer js_console_request_messages(i32 start_index) =| scroll_element_into_view(i32 element_id) => () - get_element_rect(i32 element_id) => (Gfx::IntRect rect) is_element_enabled(i32 element_id) => (bool enabled) take_element_screenshot(i32 element_id) => (Gfx::ShareableBitmap data) take_document_screenshot() => (Gfx::ShareableBitmap data) diff --git a/Userland/Services/WebContent/WebDriverClient.ipc b/Userland/Services/WebContent/WebDriverClient.ipc index 72eba84e39..f4b146fc13 100644 --- a/Userland/Services/WebContent/WebDriverClient.ipc +++ b/Userland/Services/WebContent/WebDriverClient.ipc @@ -19,4 +19,5 @@ endpoint WebDriverClient { get_element_css_value(String element_id, String name) => (Web::WebDriver::Response response) get_element_text(String element_id) => (Web::WebDriver::Response response) get_element_tag_name(String element_id) => (Web::WebDriver::Response response) + get_element_rect(String element_id) => (Web::WebDriver::Response response) } diff --git a/Userland/Services/WebContent/WebDriverConnection.cpp b/Userland/Services/WebContent/WebDriverConnection.cpp index 559842f9d7..cfbe77124b 100644 --- a/Userland/Services/WebContent/WebDriverConnection.cpp +++ b/Userland/Services/WebContent/WebDriverConnection.cpp @@ -17,6 +17,7 @@ #include #include #include +#include #include #include #include @@ -58,6 +59,37 @@ static Gfx::IntRect compute_window_rect(Web::Page const& page) }; } +// https://w3c.github.io/webdriver/#dfn-calculate-the-absolute-position +static Gfx::IntPoint calculate_absolute_position_of_element(Web::Page const& page, JS::NonnullGCPtr 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 + rect’s x coordinate). + auto x = (window ? static_cast(window->scroll_x()) : 0) + static_cast(rect->x()); + + // 4. Let y be (scrollY of window + rect’s y coordinate). + auto y = (window ? static_cast(window->scroll_y()) : 0) + static_cast(rect->y()); + + // 5. Return a pair of (x, y). + return { x, y }; +} + +static Gfx::IntRect calculate_absolute_rect_of_element(Web::Page const& page, Web::DOM::Element const& element) +{ + 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(bounding_rect->width()), + static_cast(bounding_rect->height()) + }; +} + // https://w3c.github.io/webdriver/#dfn-get-or-create-a-web-element-reference static String get_or_create_a_web_element_reference(Web::DOM::Node const& element) { @@ -626,6 +658,36 @@ Messages::WebDriverClient::GetElementTagNameResponse WebDriverConnection::get_el return make_success_response(move(qualified_name)); } +// 12.4.7 Get Element Rect, https://w3c.github.io/webdriver/#dfn-get-element-rect +Messages::WebDriverClient::GetElementRectResponse WebDriverConnection::get_element_rect(String const& element_id) +{ + // 1. If the current browsing context is no longer open, return error with error code no such window. + TRY(ensure_open_top_level_browsing_context()); + + // FIXME: 2. Handle any user prompts and return its value if it is an error. + + // 3. Let element be the result of trying to get a known connected element with url variable element id. + auto* element = TRY(get_known_connected_element(element_id)); + + // 4. Calculate the absolute position of element and let it be coordinates. + // 5. Let rect be element’s bounding rectangle. + auto rect = calculate_absolute_rect_of_element(m_page_host.page(), *element); + + // 6. Let body be a new JSON Object initialized with: + // "x" + // The first value of coordinates. + // "y" + // The second value of coordinates. + // "width" + // Value of rect’s width dimension. + // "height" + // Value of rect’s height dimension. + auto body = serialize_rect(rect); + + // 7. Return success with data body. + return body; +} + // https://w3c.github.io/webdriver/#dfn-no-longer-open ErrorOr WebDriverConnection::ensure_open_top_level_browsing_context() { diff --git a/Userland/Services/WebContent/WebDriverConnection.h b/Userland/Services/WebContent/WebDriverConnection.h index 5aad0115a9..8675993775 100644 --- a/Userland/Services/WebContent/WebDriverConnection.h +++ b/Userland/Services/WebContent/WebDriverConnection.h @@ -49,6 +49,7 @@ private: virtual Messages::WebDriverClient::GetElementCssValueResponse get_element_css_value(String const& element_id, String const& name) override; virtual Messages::WebDriverClient::GetElementTextResponse get_element_text(String const& element_id) override; virtual Messages::WebDriverClient::GetElementTagNameResponse get_element_tag_name(String const& element_id) override; + virtual Messages::WebDriverClient::GetElementRectResponse get_element_rect(String const& element_id) override; ErrorOr ensure_open_top_level_browsing_context(); void restore_the_window(); diff --git a/Userland/Services/WebDriver/Client.cpp b/Userland/Services/WebDriver/Client.cpp index b72975532c..287cec4b27 100644 --- a/Userland/Services/WebDriver/Client.cpp +++ b/Userland/Services/WebDriver/Client.cpp @@ -708,8 +708,7 @@ Web::WebDriver::Response Client::handle_get_element_rect(Vector cons { dbgln_if(WEBDRIVER_DEBUG, "Handling GET /session//element//rect"); auto* session = TRY(find_session_with_id(parameters[0])); - auto result = TRY(session->get_element_rect(parameters[1])); - return make_json_value(result); + return session->web_content_connection().get_element_rect(parameters[1]); } // 12.4.8 Is Element Enabled, https://w3c.github.io/webdriver/#dfn-is-element-enabled diff --git a/Userland/Services/WebDriver/Session.cpp b/Userland/Services/WebDriver/Session.cpp index a1e5814b3d..e4b6b3fb25 100644 --- a/Userland/Services/WebDriver/Session.cpp +++ b/Userland/Services/WebDriver/Session.cpp @@ -300,17 +300,6 @@ Web::WebDriver::Response Session::get_window_handles() const return JsonValue { handles }; } -static JsonValue serialize_rect(Gfx::IntRect const& rect) -{ - JsonObject serialized_rect = {}; - serialized_rect.set("x", rect.x()); - serialized_rect.set("y", rect.y()); - serialized_rect.set("width", rect.width()); - serialized_rect.set("height", rect.height()); - - return serialized_rect; -} - // https://w3c.github.io/webdriver/#dfn-get-a-known-connected-element static ErrorOr get_known_connected_element(StringView element_id) { @@ -323,36 +312,6 @@ static ErrorOr get_known_connected_element(StringVie return maybe_element_id.release_value(); } -// 12.4.7 Get Element Rect, https://w3c.github.io/webdriver/#dfn-get-element-rect -Web::WebDriver::Response Session::get_element_rect(StringView parameter_element_id) -{ - // 1. If the current browsing context is no longer open, return error with error code no such window. - TRY(check_for_open_top_level_browsing_context_or_return_error()); - - // FIXME: 2. Handle any user prompts and return its value if it is an error. - - // 3. Let element be the result of trying to get a known connected element with url variable element id. - auto element_id = TRY(get_known_connected_element(parameter_element_id)); - - // 4. Calculate the absolute position of element and let it be coordinates. - // 5. Let rect be element’s bounding rectangle. - auto rect = m_browser_connection->get_element_rect(element_id); - - // 6. Let body be a new JSON Object initialized with: - // "x" - // The first value of coordinates. - // "y" - // The second value of coordinates. - // "width" - // Value of rect’s width dimension. - // "height" - // Value of rect’s height dimension. - auto body = serialize_rect(rect); - - // 7. Return success with data body. - return body; -} - // 12.4.8 Is Element Enabled, https://w3c.github.io/webdriver/#dfn-is-element-enabled Web::WebDriver::Response Session::is_element_enabled(StringView parameter_element_id) { diff --git a/Userland/Services/WebDriver/Session.h b/Userland/Services/WebDriver/Session.h index fc99d610fa..b20ffab75a 100644 --- a/Userland/Services/WebDriver/Session.h +++ b/Userland/Services/WebDriver/Session.h @@ -58,7 +58,6 @@ public: Web::WebDriver::Response get_window_handle(); ErrorOr> close_window(); Web::WebDriver::Response get_window_handles() const; - Web::WebDriver::Response get_element_rect(StringView element_id); Web::WebDriver::Response is_element_enabled(StringView element_id); Web::WebDriver::Response get_source(); Web::WebDriver::Response execute_script(JsonValue const& payload);