diff options
author | Timothy Flynn <trflynn89@pm.me> | 2022-11-10 09:25:53 -0500 |
---|---|---|
committer | Linus Groh <mail@linusgroh.de> | 2022-11-10 17:02:11 +0000 |
commit | 3c00d0e92b070e8336ea5be97ee044e0a2fda443 (patch) | |
tree | 15a4ae7dea03bc4d827a0630aa77af8a666fa267 /Userland/Services/WebContent | |
parent | 1bc94e135ff03cc8e949842c4b73442da9696164 (diff) | |
download | serenity-3c00d0e92b070e8336ea5be97ee044e0a2fda443.zip |
Browser+WebContent+WebDriver: Move Get Element Property to WebContent
Diffstat (limited to 'Userland/Services/WebContent')
6 files changed, 33 insertions, 24 deletions
diff --git a/Userland/Services/WebContent/ConnectionFromClient.cpp b/Userland/Services/WebContent/ConnectionFromClient.cpp index 082d1886dd..6dcf480da2 100644 --- a/Userland/Services/WebContent/ConnectionFromClient.cpp +++ b/Userland/Services/WebContent/ConnectionFromClient.cpp @@ -495,28 +495,6 @@ void ConnectionFromClient::scroll_element_into_view(i32 element_id) element->scroll_into_view(options); } -Messages::WebContentServer::GetElementPropertyResponse ConnectionFromClient::get_element_property(i32 element_id, String const& name) -{ - auto element = find_element_by_id(element_id); - if (!element.has_value()) - return Optional<String> {}; - - auto property_or_error = element->get(name); - if (property_or_error.is_throw_completion()) - return Optional<String> {}; - - auto property = property_or_error.release_value(); - - if (property.is_undefined()) - return Optional<String> {}; - - auto string_or_error = property.to_string(element->vm()); - if (string_or_error.is_error()) - return Optional<String> {}; - - return { string_or_error.release_value() }; -} - Messages::WebContentServer::GetActiveDocumentsTypeResponse ConnectionFromClient::get_active_documents_type() { auto* active_document = page().top_level_browsing_context().active_document(); diff --git a/Userland/Services/WebContent/ConnectionFromClient.h b/Userland/Services/WebContent/ConnectionFromClient.h index 6434c5b7b3..b4d6f306fb 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::GetElementPropertyResponse get_element_property(i32 element_id, String const& name) override; virtual Messages::WebContentServer::GetActiveDocumentsTypeResponse get_active_documents_type() override; 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; diff --git a/Userland/Services/WebContent/WebContentServer.ipc b/Userland/Services/WebContent/WebContentServer.ipc index a089e2abdd..fcc6340d13 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_property(i32 element_id, String name) => (Optional<String> property) get_active_documents_type() => (String type) get_computed_value_for_element(i32 element_id, String property_name) => (String computed_value) get_element_text(i32 element_id) => (String text) diff --git a/Userland/Services/WebContent/WebDriverClient.ipc b/Userland/Services/WebContent/WebDriverClient.ipc index 1cf93db5b2..7db76b59d7 100644 --- a/Userland/Services/WebContent/WebDriverClient.ipc +++ b/Userland/Services/WebContent/WebDriverClient.ipc @@ -15,4 +15,5 @@ endpoint WebDriverClient { find_elements_from_element(JsonValue payload, String element_id) => (Web::WebDriver::Response response) is_element_selected(String element_id) => (Web::WebDriver::Response response) get_element_attribute(String element_id, String name) => (Web::WebDriver::Response response) + get_element_property(String element_id, String name) => (Web::WebDriver::Response response) } diff --git a/Userland/Services/WebContent/WebDriverConnection.cpp b/Userland/Services/WebContent/WebDriverConnection.cpp index ec06b2f0a5..b39420a4e9 100644 --- a/Userland/Services/WebContent/WebDriverConnection.cpp +++ b/Userland/Services/WebContent/WebDriverConnection.cpp @@ -11,6 +11,7 @@ #include <AK/JsonObject.h> #include <AK/JsonValue.h> #include <AK/Vector.h> +#include <LibJS/Runtime/Value.h> #include <LibWeb/DOM/Document.h> #include <LibWeb/DOM/Element.h> #include <LibWeb/HTML/AttributeNames.h> @@ -524,6 +525,36 @@ Messages::WebDriverClient::GetElementAttributeResponse WebDriverConnection::get_ return make_success_response({}); } +// 12.4.3 Get Element Property, https://w3c.github.io/webdriver/#dfn-get-element-property +Messages::WebDriverClient::GetElementPropertyResponse WebDriverConnection::get_element_property(String const& element_id, String const& name) +{ + // 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)); + + Optional<String> result; + + // 4. Let property be the result of calling the Object.[[GetProperty]](name) on element. + if (auto property_or_error = element->get(name); !property_or_error.is_throw_completion()) { + auto property = property_or_error.release_value(); + + // 5. Let result be the value of property if not undefined, or null. + if (!property.is_undefined()) { + if (auto string_or_error = property.to_string(element->vm()); !string_or_error.is_error()) + result = string_or_error.release_value(); + } + } + + // 6. Return success with data result. + if (result.has_value()) + return make_success_response(result.release_value()); + return make_success_response({}); +} + // https://w3c.github.io/webdriver/#dfn-no-longer-open ErrorOr<void, Web::WebDriver::Error> WebDriverConnection::ensure_open_top_level_browsing_context() { diff --git a/Userland/Services/WebContent/WebDriverConnection.h b/Userland/Services/WebContent/WebDriverConnection.h index 8182705572..79000352a4 100644 --- a/Userland/Services/WebContent/WebDriverConnection.h +++ b/Userland/Services/WebContent/WebDriverConnection.h @@ -45,6 +45,7 @@ private: virtual Messages::WebDriverClient::FindElementsFromElementResponse find_elements_from_element(JsonValue const& payload, String const& element_id) override; virtual Messages::WebDriverClient::IsElementSelectedResponse is_element_selected(String const& element_id) override; virtual Messages::WebDriverClient::GetElementAttributeResponse get_element_attribute(String const& element_id, String const& name) override; + virtual Messages::WebDriverClient::GetElementPropertyResponse get_element_property(String const& element_id, String const& name) override; ErrorOr<void, Web::WebDriver::Error> ensure_open_top_level_browsing_context(); void restore_the_window(); |