ElementReference.cpp 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. /*
  2. * Copyright (c) 2024, Tim Flynn <trflynn89@ladybird.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <LibWeb/DOM/Document.h>
  7. #include <LibWeb/DOM/Element.h>
  8. #include <LibWeb/DOM/Node.h>
  9. #include <LibWeb/DOM/ShadowRoot.h>
  10. #include <LibWeb/Geometry/DOMRect.h>
  11. #include <LibWeb/Geometry/DOMRectList.h>
  12. #include <LibWeb/WebDriver/ElementReference.h>
  13. namespace Web::WebDriver {
  14. // https://w3c.github.io/webdriver/#dfn-web-element-identifier
  15. static ByteString const web_element_identifier = "element-6066-11e4-a52e-4f735466cecf"sv;
  16. // https://w3c.github.io/webdriver/#dfn-shadow-root-identifier
  17. static ByteString const shadow_root_identifier = "shadow-6066-11e4-a52e-4f735466cecf"sv;
  18. // https://w3c.github.io/webdriver/#dfn-get-or-create-a-web-element-reference
  19. ByteString get_or_create_a_web_element_reference(Web::DOM::Node const& element)
  20. {
  21. // FIXME: 1. For each known element of the current browsing context’s list of known elements:
  22. // FIXME: 1. If known element equals element, return success with known element’s web element reference.
  23. // FIXME: 2. Add element to the list of known elements of the current browsing context.
  24. // FIXME: 3. Return success with the element’s web element reference.
  25. return ByteString::number(element.unique_id());
  26. }
  27. // https://w3c.github.io/webdriver/#dfn-web-element-reference-object
  28. JsonObject web_element_reference_object(Web::DOM::Node const& element)
  29. {
  30. // 1. Let identifier be the web element identifier.
  31. auto identifier = web_element_identifier;
  32. // 2. Let reference be the result of get or create a web element reference given element.
  33. auto reference = get_or_create_a_web_element_reference(element);
  34. // 3. Return a JSON Object initialized with a property with name identifier and value reference.
  35. JsonObject object;
  36. object.set(identifier, reference);
  37. return object;
  38. }
  39. // https://w3c.github.io/webdriver/#dfn-deserialize-a-web-element
  40. ErrorOr<JS::NonnullGCPtr<Web::DOM::Element>, WebDriver::Error> deserialize_web_element(JsonObject const& object)
  41. {
  42. // 1. If object has no own property web element identifier, return error with error code invalid argument.
  43. if (!object.has_string(web_element_identifier))
  44. return WebDriver::Error::from_code(WebDriver::ErrorCode::InvalidArgument, "Object is not a web element");
  45. // 2. Let reference be the result of getting the web element identifier property from object.
  46. auto reference = extract_web_element_reference(object);
  47. // 3. Let element be the result of trying to get a known element with session and reference.
  48. auto* element = TRY(get_known_connected_element(reference));
  49. // 4. Return success with data element.
  50. return *element;
  51. }
  52. ByteString extract_web_element_reference(JsonObject const& object)
  53. {
  54. return object.get_byte_string(web_element_identifier).release_value();
  55. }
  56. // https://w3c.github.io/webdriver/#dfn-represents-a-web-element
  57. bool represents_a_web_element(JsonValue const& value)
  58. {
  59. // An ECMAScript Object represents a web element if it has a web element identifier own property.
  60. if (!value.is_object())
  61. return false;
  62. return value.as_object().has_string(web_element_identifier);
  63. }
  64. // https://w3c.github.io/webdriver/#dfn-get-a-webelement-origin
  65. ErrorOr<JS::NonnullGCPtr<Web::DOM::Element>, Web::WebDriver::Error> get_web_element_origin(StringView origin)
  66. {
  67. // 1. Assert: browsing context is the current browsing context.
  68. // 2. Let element be equal to the result of trying to get a known element with session and origin.
  69. auto* element = TRY(get_known_connected_element(origin));
  70. // 3. Return success with data element.
  71. return *element;
  72. }
  73. // https://w3c.github.io/webdriver/#dfn-get-a-known-element
  74. ErrorOr<Web::DOM::Element*, Web::WebDriver::Error> get_known_connected_element(StringView element_id)
  75. {
  76. // NOTE: The whole concept of "connected elements" is not implemented yet. See get_or_create_a_web_element_reference().
  77. // For now the element is only represented by its ID.
  78. // 1. If not node reference is known with session, session's current browsing context, and reference return error
  79. // with error code no such element.
  80. auto element = element_id.to_number<int>();
  81. if (!element.has_value())
  82. return Web::WebDriver::Error::from_code(Web::WebDriver::ErrorCode::NoSuchElement, "Element ID is not an integer");
  83. // 2. Let node be the result of get a node with session, session's current browsing context, and reference.
  84. auto* node = Web::DOM::Node::from_unique_id(*element);
  85. // 3. If node is not null and node does not implement Element return error with error code no such element.
  86. if (node && !node->is_element())
  87. return Web::WebDriver::Error::from_code(Web::WebDriver::ErrorCode::NoSuchElement, ByteString::formatted("Could not find element with ID: {}", element_id));
  88. // 4. If node is null or node is stale return error with error code stale element reference.
  89. if (!node || is_element_stale(*node))
  90. return Web::WebDriver::Error::from_code(Web::WebDriver::ErrorCode::StaleElementReference, ByteString::formatted("Element with ID: {} is stale", element_id));
  91. // 5. Return success with data node.
  92. return static_cast<Web::DOM::Element*>(node);
  93. }
  94. // https://w3c.github.io/webdriver/#dfn-is-stale
  95. bool is_element_stale(Web::DOM::Node const& element)
  96. {
  97. // An element is stale if its node document is not the active document or if it is not connected.
  98. return !element.document().is_active() || !element.is_connected();
  99. }
  100. // https://w3c.github.io/webdriver/#dfn-get-or-create-a-shadow-root-reference
  101. ByteString get_or_create_a_shadow_root_reference(Web::DOM::ShadowRoot const& shadow_root)
  102. {
  103. // FIXME: 1. For each known shadow root of the current browsing context’s list of known shadow roots:
  104. // FIXME: 1. If known shadow root equals shadow root, return success with known shadow root’s shadow root reference.
  105. // FIXME: 2. Add shadow to the list of known shadow roots of the current browsing context.
  106. // FIXME: 3. Return success with the shadow’s shadow root reference.
  107. return ByteString::number(shadow_root.unique_id());
  108. }
  109. // https://w3c.github.io/webdriver/#dfn-shadow-root-reference-object
  110. JsonObject shadow_root_reference_object(Web::DOM::ShadowRoot const& shadow_root)
  111. {
  112. // 1. Let identifier be the shadow root identifier.
  113. auto identifier = shadow_root_identifier;
  114. // 2. Let reference be the result of get or create a shadow root reference given shadow root.
  115. auto reference = get_or_create_a_shadow_root_reference(shadow_root);
  116. // 3. Return a JSON Object initialized with a property with name identifier and value reference.
  117. JsonObject object;
  118. object.set(identifier, reference);
  119. return object;
  120. }
  121. // https://w3c.github.io/webdriver/#dfn-get-a-known-shadow-root
  122. ErrorOr<Web::DOM::ShadowRoot*, Web::WebDriver::Error> get_known_shadow_root(StringView shadow_id)
  123. {
  124. // NOTE: The whole concept of "known shadow roots" is not implemented yet. See get_or_create_a_shadow_root_reference().
  125. // For now the shadow root is only represented by its ID.
  126. auto shadow_root = shadow_id.to_number<int>();
  127. if (!shadow_root.has_value())
  128. return Web::WebDriver::Error::from_code(Web::WebDriver::ErrorCode::InvalidArgument, "Shadow ID is not an integer");
  129. auto* node = Web::DOM::Node::from_unique_id(*shadow_root);
  130. if (!node || !node->is_shadow_root())
  131. return Web::WebDriver::Error::from_code(Web::WebDriver::ErrorCode::NoSuchElement, ByteString::formatted("Could not find shadow root with ID: {}", shadow_id));
  132. return static_cast<Web::DOM::ShadowRoot*>(node);
  133. }
  134. // https://w3c.github.io/webdriver/#dfn-center-point
  135. CSSPixelPoint in_view_center_point(DOM::Element const& element, CSSPixelRect viewport)
  136. {
  137. // 1. Let rectangle be the first element of the DOMRect sequence returned by calling getClientRects() on element.
  138. auto const* rectangle = element.get_client_rects()->item(0);
  139. VERIFY(rectangle);
  140. // 2. Let left be max(0, min(x coordinate, x coordinate + width dimension)).
  141. auto left = max(0.0, min(rectangle->x(), rectangle->x() + rectangle->width()));
  142. // 3. Let right be min(innerWidth, max(x coordinate, x coordinate + width dimension)).
  143. auto right = min(viewport.width().to_double(), max(rectangle->x(), rectangle->x() + rectangle->width()));
  144. // 4. Let top be max(0, min(y coordinate, y coordinate + height dimension)).
  145. auto top = max(0.0, min(rectangle->y(), rectangle->y() + rectangle->height()));
  146. // 5. Let bottom be min(innerHeight, max(y coordinate, y coordinate + height dimension)).
  147. auto bottom = min(viewport.height().to_double(), max(rectangle->y(), rectangle->y() + rectangle->height()));
  148. // 6. Let x be floor((left + right) ÷ 2.0).
  149. auto x = floor((left + right) / 2.0);
  150. // 7. Let y be floor((top + bottom) ÷ 2.0).
  151. auto y = floor((top + bottom) / 2.0);
  152. // 8. Return the pair of (x, y).
  153. return { x, y };
  154. }
  155. }