summaryrefslogtreecommitdiff
path: root/Userland
diff options
context:
space:
mode:
authorTimothy Flynn <trflynn89@pm.me>2022-11-14 19:27:11 -0500
committerLinus Groh <mail@linusgroh.de>2022-11-15 11:55:36 +0000
commit4d9fd5d190030faf87326f6d5d50b4d958278550 (patch)
tree13bcaa2c9cd089f39132bf82eefdf0214d14317c /Userland
parentcb91e6067cb1e527e81a432007172c1962b3a160 (diff)
downloadserenity-4d9fd5d190030faf87326f6d5d50b4d958278550.zip
LibWeb+WebContent+WebDriver: Implement Get Element Shadow Root
Diffstat (limited to 'Userland')
-rw-r--r--Userland/Libraries/LibWeb/WebDriver/Client.cpp1
-rw-r--r--Userland/Libraries/LibWeb/WebDriver/Client.h1
-rw-r--r--Userland/Services/WebContent/WebDriverClient.ipc1
-rw-r--r--Userland/Services/WebContent/WebDriverConnection.cpp56
-rw-r--r--Userland/Services/WebContent/WebDriverConnection.h1
-rw-r--r--Userland/Services/WebDriver/Client.cpp9
-rw-r--r--Userland/Services/WebDriver/Client.h1
7 files changed, 70 insertions, 0 deletions
diff --git a/Userland/Libraries/LibWeb/WebDriver/Client.cpp b/Userland/Libraries/LibWeb/WebDriver/Client.cpp
index 99bed95673..f279d7ee26 100644
--- a/Userland/Libraries/LibWeb/WebDriver/Client.cpp
+++ b/Userland/Libraries/LibWeb/WebDriver/Client.cpp
@@ -74,6 +74,7 @@ static constexpr auto s_webdriver_endpoints = Array {
ROUTE(POST, "/session/:session_id/element/:element_id/element"sv, find_element_from_element),
ROUTE(POST, "/session/:session_id/element/:element_id/elements"sv, find_elements_from_element),
ROUTE(GET, "/session/:session_id/element/active"sv, get_active_element),
+ ROUTE(GET, "/session/:session_id/element/:element_id/shadow"sv, get_element_shadow_root),
ROUTE(GET, "/session/:session_id/element/:element_id/selected"sv, is_element_selected),
ROUTE(GET, "/session/:session_id/element/:element_id/attribute/:name"sv, get_element_attribute),
ROUTE(GET, "/session/:session_id/element/:element_id/property/:name"sv, get_element_property),
diff --git a/Userland/Libraries/LibWeb/WebDriver/Client.h b/Userland/Libraries/LibWeb/WebDriver/Client.h
index bf00176930..2ede64127c 100644
--- a/Userland/Libraries/LibWeb/WebDriver/Client.h
+++ b/Userland/Libraries/LibWeb/WebDriver/Client.h
@@ -62,6 +62,7 @@ public:
virtual Response find_element_from_element(Parameters parameters, JsonValue payload) = 0;
virtual Response find_elements_from_element(Parameters parameters, JsonValue payload) = 0;
virtual Response get_active_element(Parameters parameters, JsonValue payload) = 0;
+ virtual Response get_element_shadow_root(Parameters parameters, JsonValue payload) = 0;
virtual Response is_element_selected(Parameters parameters, JsonValue payload) = 0;
virtual Response get_element_attribute(Parameters parameters, JsonValue payload) = 0;
virtual Response get_element_property(Parameters parameters, JsonValue payload) = 0;
diff --git a/Userland/Services/WebContent/WebDriverClient.ipc b/Userland/Services/WebContent/WebDriverClient.ipc
index 5ec325042b..6f0cc67409 100644
--- a/Userland/Services/WebContent/WebDriverClient.ipc
+++ b/Userland/Services/WebContent/WebDriverClient.ipc
@@ -24,6 +24,7 @@ endpoint WebDriverClient {
find_element_from_element(JsonValue payload, String element_id) => (Web::WebDriver::Response response)
find_elements_from_element(JsonValue payload, String element_id) => (Web::WebDriver::Response response)
get_active_element() => (Web::WebDriver::Response response)
+ get_element_shadow_root(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 c8ee9a66c4..acf43b4c05 100644
--- a/Userland/Services/WebContent/WebDriverConnection.cpp
+++ b/Userland/Services/WebContent/WebDriverConnection.cpp
@@ -19,6 +19,7 @@
#include <LibWeb/Cookie/Cookie.h>
#include <LibWeb/DOM/Document.h>
#include <LibWeb/DOM/Element.h>
+#include <LibWeb/DOM/ShadowRoot.h>
#include <LibWeb/Geometry/DOMRect.h>
#include <LibWeb/HTML/AttributeNames.h>
#include <LibWeb/HTML/BrowsingContext.h>
@@ -150,6 +151,36 @@ static ErrorOr<Web::DOM::Element*, Web::WebDriver::Error> get_known_connected_el
return static_cast<Web::DOM::Element*>(node);
}
+// https://w3c.github.io/webdriver/#dfn-get-or-create-a-shadow-root-reference
+static String get_or_create_a_shadow_root_reference(Web::DOM::ShadowRoot const& shadow_root)
+{
+ // FIXME: 1. For each known shadow root of the current browsing context’s list of known shadow roots:
+ // FIXME: 1. If known shadow root equals shadow root, return success with known shadow root’s shadow root reference.
+ // FIXME: 2. Add shadow to the list of known shadow roots of the current browsing context.
+ // FIXME: 3. Return success with the shadow’s shadow root reference.
+
+ return String::number(shadow_root.id());
+}
+
+// https://w3c.github.io/webdriver/#dfn-shadow-root-reference-object
+static JsonObject shadow_root_reference_object(Web::DOM::ShadowRoot const& shadow_root)
+{
+ // https://w3c.github.io/webdriver/#dfn-shadow-root-identifier
+ static String const shadow_root_identifier = "shadow-6066-11e4-a52e-4f735466cecf"sv;
+
+ // 1. Let identifier be the shadow root identifier.
+ auto identifier = shadow_root_identifier;
+
+ // 2. Let reference be the result of get or create a shadow root reference given shadow root.
+ auto reference = get_or_create_a_shadow_root_reference(shadow_root);
+
+ // 3. Return a JSON Object initialized with a property with name identifier and value reference.
+ JsonObject object;
+ object.set("name"sv, move(identifier));
+ object.set("value"sv, move(reference));
+ return object;
+}
+
// https://w3c.github.io/webdriver/#dfn-scrolls-into-view
static void scroll_element_into_view(Web::DOM::Element& element)
{
@@ -724,6 +755,31 @@ Messages::WebDriverClient::GetActiveElementResponse WebDriverConnection::get_act
return Web::WebDriver::Error::from_code(Web::WebDriver::ErrorCode::NoSuchElement, "The current document does not have an active element"sv);
}
+// 12.3.9 Get Element Shadow Root, https://w3c.github.io/webdriver/#get-element-shadow-root
+Messages::WebDriverClient::GetElementShadowRootResponse WebDriverConnection::get_element_shadow_root(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. Let shadow root be element's shadow root.
+ auto* shadow_root = element->shadow_root();
+
+ // 5. If shadow root is null, return error with error code no such shadow root.
+ if (!shadow_root)
+ return Web::WebDriver::Error::from_code(Web::WebDriver::ErrorCode::NoSuchShadowRoot, String::formatted("Element with ID '{}' does not have a shadow root", element_id));
+
+ // 6. Let serialized be the shadow root reference object for shadow root.
+ auto serialized = shadow_root_reference_object(*shadow_root);
+
+ // 7. Return success with data serialized.
+ return serialized;
+}
+
// 12.4.1 Is Element Selected, https://w3c.github.io/webdriver/#dfn-is-element-selected
Messages::WebDriverClient::IsElementSelectedResponse WebDriverConnection::is_element_selected(String const& element_id)
{
diff --git a/Userland/Services/WebContent/WebDriverConnection.h b/Userland/Services/WebContent/WebDriverConnection.h
index 712c7c5141..00540d73e0 100644
--- a/Userland/Services/WebContent/WebDriverConnection.h
+++ b/Userland/Services/WebContent/WebDriverConnection.h
@@ -59,6 +59,7 @@ private:
virtual Messages::WebDriverClient::FindElementFromElementResponse find_element_from_element(JsonValue const& payload, String const& element_id) override;
virtual Messages::WebDriverClient::FindElementsFromElementResponse find_elements_from_element(JsonValue const& payload, String const& element_id) override;
virtual Messages::WebDriverClient::GetActiveElementResponse get_active_element() override;
+ virtual Messages::WebDriverClient::GetElementShadowRootResponse get_element_shadow_root(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;
diff --git a/Userland/Services/WebDriver/Client.cpp b/Userland/Services/WebDriver/Client.cpp
index 4addb5603c..0229d72966 100644
--- a/Userland/Services/WebDriver/Client.cpp
+++ b/Userland/Services/WebDriver/Client.cpp
@@ -368,6 +368,15 @@ Web::WebDriver::Response Client::get_active_element(Web::WebDriver::Parameters p
return session->web_content_connection().get_active_element();
}
+// 12.3.9 Get Element Shadow Root, https://w3c.github.io/webdriver/#get-element-shadow-root
+// GET /session/{session id}/element/{element id}/shadow
+Web::WebDriver::Response Client::get_element_shadow_root(Web::WebDriver::Parameters parameters, JsonValue)
+{
+ dbgln_if(WEBDRIVER_DEBUG, "Handling GET /session/<session_id>/element/<element_id>/shadow");
+ auto* session = TRY(find_session_with_id(parameters[0]));
+ return session->web_content_connection().get_element_shadow_root(parameters[1]);
+}
+
// 12.4.1 Is Element Selected, https://w3c.github.io/webdriver/#dfn-is-element-selected
// GET /session/{session id}/element/{element id}/selected
Web::WebDriver::Response Client::is_element_selected(Web::WebDriver::Parameters parameters, JsonValue)
diff --git a/Userland/Services/WebDriver/Client.h b/Userland/Services/WebDriver/Client.h
index 440af902a3..a968a25314 100644
--- a/Userland/Services/WebDriver/Client.h
+++ b/Userland/Services/WebDriver/Client.h
@@ -57,6 +57,7 @@ private:
virtual Web::WebDriver::Response find_element_from_element(Web::WebDriver::Parameters parameters, JsonValue payload) override;
virtual Web::WebDriver::Response find_elements_from_element(Web::WebDriver::Parameters parameters, JsonValue payload) override;
virtual Web::WebDriver::Response get_active_element(Web::WebDriver::Parameters parameters, JsonValue payload) override;
+ virtual Web::WebDriver::Response get_element_shadow_root(Web::WebDriver::Parameters parameters, JsonValue payload) override;
virtual Web::WebDriver::Response is_element_selected(Web::WebDriver::Parameters parameters, JsonValue payload) override;
virtual Web::WebDriver::Response get_element_attribute(Web::WebDriver::Parameters parameters, JsonValue payload) override;
virtual Web::WebDriver::Response get_element_property(Web::WebDriver::Parameters parameters, JsonValue payload) override;