/* * Copyright (c) 2024, Tim Flynn * * SPDX-License-Identifier: BSD-2-Clause */ #include #include #include #include namespace Web::WebDriver { // https://w3c.github.io/webdriver/#dfn-web-element-identifier static ByteString const web_element_identifier = "element-6066-11e4-a52e-4f735466cecf"sv; // https://w3c.github.io/webdriver/#dfn-shadow-root-identifier static ByteString const shadow_root_identifier = "shadow-6066-11e4-a52e-4f735466cecf"sv; // https://w3c.github.io/webdriver/#dfn-get-or-create-a-web-element-reference ByteString get_or_create_a_web_element_reference(Web::DOM::Node const& element) { // FIXME: 1. For each known element of the current browsing context’s list of known elements: // FIXME: 1. If known element equals element, return success with known element’s web element reference. // FIXME: 2. Add element to the list of known elements of the current browsing context. // FIXME: 3. Return success with the element’s web element reference. return ByteString::number(element.unique_id()); } // https://w3c.github.io/webdriver/#dfn-web-element-reference-object JsonObject web_element_reference_object(Web::DOM::Node const& element) { // 1. Let identifier be the web element identifier. auto identifier = web_element_identifier; // 2. Let reference be the result of get or create a web element reference given element. auto reference = get_or_create_a_web_element_reference(element); // 3. Return a JSON Object initialized with a property with name identifier and value reference. JsonObject object; object.set("name"sv, identifier); object.set("value"sv, reference); return object; } // https://w3c.github.io/webdriver/#dfn-get-a-known-connected-element ErrorOr get_known_connected_element(StringView element_id) { // NOTE: The whole concept of "connected elements" is not implemented yet. See get_or_create_a_web_element_reference(). // For now the element is only represented by its ID. auto element = element_id.to_number(); if (!element.has_value()) return Web::WebDriver::Error::from_code(Web::WebDriver::ErrorCode::InvalidArgument, "Element ID is not an integer"); auto* node = Web::DOM::Node::from_unique_id(*element); if (!node || !node->is_element()) return Web::WebDriver::Error::from_code(Web::WebDriver::ErrorCode::NoSuchElement, ByteString::formatted("Could not find element with ID: {}", element_id)); return static_cast(node); } // https://w3c.github.io/webdriver/#dfn-get-or-create-a-shadow-root-reference ByteString 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 ByteString::number(shadow_root.unique_id()); } // https://w3c.github.io/webdriver/#dfn-shadow-root-reference-object JsonObject shadow_root_reference_object(Web::DOM::ShadowRoot const& shadow_root) { // 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, identifier); object.set("value"sv, reference); return object; } // https://w3c.github.io/webdriver/#dfn-get-a-known-shadow-root ErrorOr get_known_shadow_root(StringView shadow_id) { // NOTE: The whole concept of "known shadow roots" is not implemented yet. See get_or_create_a_shadow_root_reference(). // For now the shadow root is only represented by its ID. auto shadow_root = shadow_id.to_number(); if (!shadow_root.has_value()) return Web::WebDriver::Error::from_code(Web::WebDriver::ErrorCode::InvalidArgument, "Shadow ID is not an integer"); auto* node = Web::DOM::Node::from_unique_id(*shadow_root); if (!node || !node->is_shadow_root()) return Web::WebDriver::Error::from_code(Web::WebDriver::ErrorCode::NoSuchElement, ByteString::formatted("Could not find shadow root with ID: {}", shadow_id)); return static_cast(node); } }