ElementReference.cpp 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450
  1. /*
  2. * Copyright (c) 2024, Tim Flynn <trflynn89@ladybird.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <AK/HashMap.h>
  7. #include <LibWeb/DOM/Document.h>
  8. #include <LibWeb/DOM/Element.h>
  9. #include <LibWeb/DOM/Node.h>
  10. #include <LibWeb/DOM/ShadowRoot.h>
  11. #include <LibWeb/Geometry/DOMRect.h>
  12. #include <LibWeb/Geometry/DOMRectList.h>
  13. #include <LibWeb/HTML/BrowsingContext.h>
  14. #include <LibWeb/HTML/BrowsingContextGroup.h>
  15. #include <LibWeb/HTML/HTMLBodyElement.h>
  16. #include <LibWeb/HTML/HTMLInputElement.h>
  17. #include <LibWeb/HTML/HTMLTextAreaElement.h>
  18. #include <LibWeb/HTML/TraversableNavigable.h>
  19. #include <LibWeb/Page/Page.h>
  20. #include <LibWeb/Painting/PaintableBox.h>
  21. #include <LibWeb/WebDriver/ElementReference.h>
  22. namespace Web::WebDriver {
  23. // https://w3c.github.io/webdriver/#dfn-web-element-identifier
  24. static ByteString const web_element_identifier = "element-6066-11e4-a52e-4f735466cecf"sv;
  25. // https://w3c.github.io/webdriver/#dfn-shadow-root-identifier
  26. static ByteString const shadow_root_identifier = "shadow-6066-11e4-a52e-4f735466cecf"sv;
  27. // https://w3c.github.io/webdriver/#dfn-browsing-context-group-node-map
  28. static HashMap<JS::RawGCPtr<HTML::BrowsingContextGroup const>, HashTable<ByteString>> browsing_context_group_node_map;
  29. // https://w3c.github.io/webdriver/#dfn-navigable-seen-nodes-map
  30. static HashMap<JS::RawGCPtr<HTML::Navigable>, HashTable<ByteString>> navigable_seen_nodes_map;
  31. // https://w3c.github.io/webdriver/#dfn-get-a-node
  32. JS::GCPtr<Web::DOM::Node> get_node(HTML::BrowsingContext const& browsing_context, StringView reference)
  33. {
  34. // 1. Let browsing context group node map be session's browsing context group node map.
  35. // 2. Let browsing context group be browsing context's browsing context group.
  36. auto const* browsing_context_group = browsing_context.group();
  37. // 3. If browsing context group node map does not contain browsing context group, return null.
  38. // 4. Let node id map be browsing context group node map[browsing context group].
  39. auto node_id_map = browsing_context_group_node_map.get(browsing_context_group);
  40. if (!node_id_map.has_value())
  41. return nullptr;
  42. // 5. Let node be the entry in node id map whose value is reference, if such an entry exists, or null otherwise.
  43. JS::GCPtr<Web::DOM::Node> node;
  44. if (node_id_map->contains(reference)) {
  45. auto node_id = reference.to_number<i64>().value();
  46. node = Web::DOM::Node::from_unique_id(UniqueNodeID(node_id));
  47. }
  48. // 6. Return node.
  49. return node;
  50. }
  51. // https://w3c.github.io/webdriver/#dfn-get-or-create-a-node-reference
  52. ByteString get_or_create_a_node_reference(HTML::BrowsingContext const& browsing_context, Web::DOM::Node const& node)
  53. {
  54. // 1. Let browsing context group node map be session's browsing context group node map.
  55. // 2. Let browsing context group be browsing context's browsing context group.
  56. auto const* browsing_context_group = browsing_context.group();
  57. // 3. If browsing context group node map does not contain browsing context group, set browsing context group node
  58. // map[browsing context group] to a new weak map.
  59. // 4. Let node id map be browsing context group node map[browsing context group].
  60. auto& node_id_map = browsing_context_group_node_map.ensure(browsing_context_group);
  61. auto node_id = ByteString::number(node.unique_id().value());
  62. // 5. If node id map does not contain node:
  63. if (!node_id_map.contains(node_id)) {
  64. // 1. Let node id be a new globally unique string.
  65. // 2. Set node id map[node] to node id.
  66. node_id_map.set(node_id);
  67. // 3. Let navigable be browsing context's active document's node navigable.
  68. auto navigable = browsing_context.active_document()->navigable();
  69. // 4. Let navigable seen nodes map be session's navigable seen nodes map.
  70. // 5. If navigable seen nodes map does not contain navigable, set navigable seen nodes map[navigable] to an empty set.
  71. // 6. Append node id to navigable seen nodes map[navigable].
  72. navigable_seen_nodes_map.ensure(navigable).set(node_id);
  73. }
  74. // 6. Return node id map[node].
  75. return node_id;
  76. }
  77. // https://w3c.github.io/webdriver/#dfn-node-reference-is-known
  78. bool node_reference_is_known(HTML::BrowsingContext const& browsing_context, StringView reference)
  79. {
  80. // 1. Let navigable be browsing context's active document's node navigable.
  81. auto navigable = browsing_context.active_document()->navigable();
  82. if (!navigable)
  83. return false;
  84. // 2. Let navigable seen nodes map be session's navigable seen nodes map.
  85. // 3. If navigable seen nodes map contains navigable and navigable seen nodes map[navigable] contains reference,
  86. // return true, otherwise return false.
  87. if (auto map = navigable_seen_nodes_map.get(navigable); map.has_value())
  88. return map->contains(reference);
  89. return false;
  90. }
  91. // https://w3c.github.io/webdriver/#dfn-get-or-create-a-web-element-reference
  92. ByteString get_or_create_a_web_element_reference(HTML::BrowsingContext const& browsing_context, Web::DOM::Node const& element)
  93. {
  94. // 1. Assert: element implements Element.
  95. VERIFY(element.is_element());
  96. // 2. Return the result of trying to get or create a node reference given session, session's current browsing
  97. // context, and element.
  98. return get_or_create_a_node_reference(browsing_context, element);
  99. }
  100. // https://w3c.github.io/webdriver/#dfn-web-element-reference-object
  101. JsonObject web_element_reference_object(HTML::BrowsingContext const& browsing_context, Web::DOM::Node const& element)
  102. {
  103. // 1. Let identifier be the web element identifier.
  104. auto identifier = web_element_identifier;
  105. // 2. Let reference be the result of get or create a web element reference given element.
  106. auto reference = get_or_create_a_web_element_reference(browsing_context, element);
  107. // 3. Return a JSON Object initialized with a property with name identifier and value reference.
  108. JsonObject object;
  109. object.set(identifier, reference);
  110. return object;
  111. }
  112. // https://w3c.github.io/webdriver/#dfn-deserialize-a-web-element
  113. ErrorOr<JS::NonnullGCPtr<Web::DOM::Element>, WebDriver::Error> deserialize_web_element(Web::HTML::BrowsingContext const& browsing_context, JsonObject const& object)
  114. {
  115. // 1. If object has no own property web element identifier, return error with error code invalid argument.
  116. if (!object.has_string(web_element_identifier))
  117. return WebDriver::Error::from_code(WebDriver::ErrorCode::InvalidArgument, "Object is not a web element");
  118. // 2. Let reference be the result of getting the web element identifier property from object.
  119. auto reference = extract_web_element_reference(object);
  120. // 3. Let element be the result of trying to get a known element with session and reference.
  121. auto element = TRY(get_known_element(browsing_context, reference));
  122. // 4. Return success with data element.
  123. return element;
  124. }
  125. ByteString extract_web_element_reference(JsonObject const& object)
  126. {
  127. return object.get_byte_string(web_element_identifier).release_value();
  128. }
  129. // https://w3c.github.io/webdriver/#dfn-represents-a-web-element
  130. bool represents_a_web_element(JsonValue const& value)
  131. {
  132. // An ECMAScript Object represents a web element if it has a web element identifier own property.
  133. if (!value.is_object())
  134. return false;
  135. return value.as_object().has_string(web_element_identifier);
  136. }
  137. // https://w3c.github.io/webdriver/#dfn-get-a-webelement-origin
  138. ErrorOr<JS::NonnullGCPtr<Web::DOM::Element>, Web::WebDriver::Error> get_web_element_origin(Web::HTML::BrowsingContext const& browsing_context, StringView origin)
  139. {
  140. // 1. Assert: browsing context is the current browsing context.
  141. // 2. Let element be equal to the result of trying to get a known element with session and origin.
  142. auto element = TRY(get_known_element(browsing_context, origin));
  143. // 3. Return success with data element.
  144. return element;
  145. }
  146. // https://w3c.github.io/webdriver/#dfn-get-a-known-element
  147. ErrorOr<JS::NonnullGCPtr<Web::DOM::Element>, Web::WebDriver::Error> get_known_element(Web::HTML::BrowsingContext const& browsing_context, StringView reference)
  148. {
  149. // 1. If not node reference is known with session, session's current browsing context, and reference return error
  150. // with error code no such element.
  151. if (!node_reference_is_known(browsing_context, reference))
  152. return Web::WebDriver::Error::from_code(Web::WebDriver::ErrorCode::NoSuchElement, "Element ID is not an integer");
  153. // 2. Let node be the result of get a node with session, session's current browsing context, and reference.
  154. auto node = get_node(browsing_context, reference);
  155. // 3. If node is not null and node does not implement Element return error with error code no such element.
  156. if (node && !node->is_element())
  157. return Web::WebDriver::Error::from_code(Web::WebDriver::ErrorCode::NoSuchElement, ByteString::formatted("Could not find element with node reference: {}", reference));
  158. // 4. If node is null or node is stale return error with error code stale element reference.
  159. if (!node || is_element_stale(*node))
  160. return Web::WebDriver::Error::from_code(Web::WebDriver::ErrorCode::StaleElementReference, ByteString::formatted("Element reference {} is stale", reference));
  161. // 5. Return success with data node.
  162. return static_cast<Web::DOM::Element&>(*node);
  163. }
  164. // https://w3c.github.io/webdriver/#dfn-is-stale
  165. bool is_element_stale(Web::DOM::Node const& element)
  166. {
  167. // An element is stale if its node document is not the active document or if it is not connected.
  168. return !element.document().is_active() || !element.is_connected();
  169. }
  170. // https://w3c.github.io/webdriver/#dfn-interactable
  171. bool is_element_interactable(Web::HTML::BrowsingContext const& browsing_context, Web::DOM::Element const& element)
  172. {
  173. // An interactable element is an element which is either pointer-interactable or keyboard-interactable.
  174. return is_element_keyboard_interactable(element) || is_element_pointer_interactable(browsing_context, element);
  175. }
  176. // https://w3c.github.io/webdriver/#dfn-pointer-interactable
  177. bool is_element_pointer_interactable(Web::HTML::BrowsingContext const& browsing_context, Web::DOM::Element const& element)
  178. {
  179. // A pointer-interactable element is defined to be the first element, defined by the paint order found at the center
  180. // point of its rectangle that is inside the viewport, excluding the size of any rendered scrollbars.
  181. auto const* document = browsing_context.active_document();
  182. if (!document)
  183. return false;
  184. auto const* paint_root = document->paintable_box();
  185. if (!paint_root)
  186. return false;
  187. auto viewport = browsing_context.page().top_level_traversable()->viewport_rect();
  188. auto center_point = in_view_center_point(element, viewport);
  189. auto result = paint_root->hit_test(center_point, Painting::HitTestType::TextCursor);
  190. if (!result.has_value())
  191. return false;
  192. return result->dom_node() == &element;
  193. }
  194. // https://w3c.github.io/webdriver/#dfn-keyboard-interactable
  195. bool is_element_keyboard_interactable(Web::DOM::Element const& element)
  196. {
  197. // A keyboard-interactable element is any element that has a focusable area, is a body element, or is the document element.
  198. return element.is_focusable() || is<HTML::HTMLBodyElement>(element) || element.is_document_element();
  199. }
  200. // https://w3c.github.io/webdriver/#dfn-editable
  201. bool is_element_editable(Web::DOM::Element const& element)
  202. {
  203. // Editable elements are those that can be used for typing and clearing, and they fall into two subcategories:
  204. // "Mutable form control elements" and "Mutable elements".
  205. return is_element_mutable_form_control(element) || is_element_mutable(element);
  206. }
  207. // https://w3c.github.io/webdriver/#dfn-mutable-element
  208. bool is_element_mutable(Web::DOM::Element const& element)
  209. {
  210. // Denotes elements that are editing hosts or content editable.
  211. if (!is<HTML::HTMLElement>(element))
  212. return false;
  213. auto const& html_element = static_cast<HTML::HTMLElement const&>(element);
  214. return html_element.is_editable();
  215. }
  216. // https://w3c.github.io/webdriver/#dfn-mutable-form-control-element
  217. bool is_element_mutable_form_control(Web::DOM::Element const& element)
  218. {
  219. // Denotes input elements that are mutable (e.g. that are not read only or disabled) and whose type attribute is
  220. // in one of the following states:
  221. if (is<HTML::HTMLInputElement>(element)) {
  222. auto const& input_element = static_cast<HTML::HTMLInputElement const&>(element);
  223. if (!input_element.is_mutable() || !input_element.enabled())
  224. return false;
  225. // Text and Search, URL, Telephone, Email, Password, Date, Month, Week, Time, Local Date and Time, Number,
  226. // Range, Color, File Upload
  227. switch (input_element.type_state()) {
  228. case HTML::HTMLInputElement::TypeAttributeState::Text:
  229. case HTML::HTMLInputElement::TypeAttributeState::Search:
  230. case HTML::HTMLInputElement::TypeAttributeState::URL:
  231. case HTML::HTMLInputElement::TypeAttributeState::Telephone:
  232. case HTML::HTMLInputElement::TypeAttributeState::Email:
  233. case HTML::HTMLInputElement::TypeAttributeState::Password:
  234. case HTML::HTMLInputElement::TypeAttributeState::Date:
  235. case HTML::HTMLInputElement::TypeAttributeState::Month:
  236. case HTML::HTMLInputElement::TypeAttributeState::Week:
  237. case HTML::HTMLInputElement::TypeAttributeState::Time:
  238. case HTML::HTMLInputElement::TypeAttributeState::LocalDateAndTime:
  239. case HTML::HTMLInputElement::TypeAttributeState::Number:
  240. case HTML::HTMLInputElement::TypeAttributeState::Range:
  241. case HTML::HTMLInputElement::TypeAttributeState::Color:
  242. case HTML::HTMLInputElement::TypeAttributeState::FileUpload:
  243. return true;
  244. default:
  245. return false;
  246. }
  247. }
  248. // And the textarea element.
  249. if (is<HTML::HTMLTextAreaElement>(element)) {
  250. auto const& text_area = static_cast<HTML::HTMLTextAreaElement const&>(element);
  251. return text_area.enabled();
  252. }
  253. return false;
  254. }
  255. // https://w3c.github.io/webdriver/#dfn-non-typeable-form-control
  256. bool is_element_non_typeable_form_control(Web::DOM::Element const& element)
  257. {
  258. // A non-typeable form control is an input element whose type attribute state causes the primary input mechanism not
  259. // to be through means of a keyboard, whether virtual or physical.
  260. if (!is<HTML::HTMLInputElement>(element))
  261. return false;
  262. auto const& input_element = static_cast<HTML::HTMLInputElement const&>(element);
  263. switch (input_element.type_state()) {
  264. case HTML::HTMLInputElement::TypeAttributeState::Hidden:
  265. case HTML::HTMLInputElement::TypeAttributeState::Range:
  266. case HTML::HTMLInputElement::TypeAttributeState::Color:
  267. case HTML::HTMLInputElement::TypeAttributeState::Checkbox:
  268. case HTML::HTMLInputElement::TypeAttributeState::RadioButton:
  269. case HTML::HTMLInputElement::TypeAttributeState::FileUpload:
  270. case HTML::HTMLInputElement::TypeAttributeState::SubmitButton:
  271. case HTML::HTMLInputElement::TypeAttributeState::ImageButton:
  272. case HTML::HTMLInputElement::TypeAttributeState::ResetButton:
  273. case HTML::HTMLInputElement::TypeAttributeState::Button:
  274. return true;
  275. default:
  276. return false;
  277. }
  278. }
  279. // https://w3c.github.io/webdriver/#dfn-in-view
  280. bool is_element_in_view(ReadonlySpan<JS::NonnullGCPtr<Web::DOM::Element>> paint_tree, Web::DOM::Element& element)
  281. {
  282. // An element is in view if it is a member of its own pointer-interactable paint tree, given the pretense that its
  283. // pointer events are not disabled.
  284. if (!element.paintable() || !element.paintable()->is_visible() || !element.paintable()->visible_for_hit_testing())
  285. return false;
  286. return paint_tree.contains_slow(JS::NonnullGCPtr { element });
  287. }
  288. // https://w3c.github.io/webdriver/#dfn-in-view
  289. bool is_element_obscured(ReadonlySpan<JS::NonnullGCPtr<Web::DOM::Element>> paint_tree, Web::DOM::Element& element)
  290. {
  291. // An element is obscured if the pointer-interactable paint tree at its center point is empty, or the first element
  292. // in this tree is not an inclusive descendant of itself.
  293. return paint_tree.is_empty() || !paint_tree.first()->is_shadow_including_inclusive_descendant_of(element);
  294. }
  295. // https://w3c.github.io/webdriver/#dfn-pointer-interactable-paint-tree
  296. JS::MarkedVector<JS::NonnullGCPtr<Web::DOM::Element>> pointer_interactable_tree(Web::HTML::BrowsingContext& browsing_context, Web::DOM::Element& element)
  297. {
  298. // 1. If element is not in the same tree as session's current browsing context's active document, return an empty sequence.
  299. if (!browsing_context.active_document()->contains(element))
  300. return JS::MarkedVector<JS::NonnullGCPtr<Web::DOM::Element>>(browsing_context.heap());
  301. // 2. Let rectangles be the DOMRect sequence returned by calling getClientRects().
  302. auto rectangles = element.get_client_rects();
  303. // 3. If rectangles has the length of 0, return an empty sequence.
  304. if (rectangles->length() == 0)
  305. return JS::MarkedVector<JS::NonnullGCPtr<Web::DOM::Element>>(browsing_context.heap());
  306. // 4. Let center point be the in-view center point of the first indexed element in rectangles.
  307. auto viewport = browsing_context.page().top_level_traversable()->viewport_rect();
  308. auto center_point = Web::WebDriver::in_view_center_point(element, viewport);
  309. // 5. Return the elements from point given the coordinates center point.
  310. return browsing_context.active_document()->elements_from_point(center_point.x().to_double(), center_point.y().to_double());
  311. }
  312. // https://w3c.github.io/webdriver/#dfn-get-or-create-a-shadow-root-reference
  313. ByteString get_or_create_a_shadow_root_reference(Web::DOM::ShadowRoot const& shadow_root)
  314. {
  315. // FIXME: 1. For each known shadow root of the current browsing context’s list of known shadow roots:
  316. // FIXME: 1. If known shadow root equals shadow root, return success with known shadow root’s shadow root reference.
  317. // FIXME: 2. Add shadow to the list of known shadow roots of the current browsing context.
  318. // FIXME: 3. Return success with the shadow’s shadow root reference.
  319. return ByteString::number(shadow_root.unique_id().value());
  320. }
  321. // https://w3c.github.io/webdriver/#dfn-shadow-root-reference-object
  322. JsonObject shadow_root_reference_object(Web::DOM::ShadowRoot const& shadow_root)
  323. {
  324. // 1. Let identifier be the shadow root identifier.
  325. auto identifier = shadow_root_identifier;
  326. // 2. Let reference be the result of get or create a shadow root reference given shadow root.
  327. auto reference = get_or_create_a_shadow_root_reference(shadow_root);
  328. // 3. Return a JSON Object initialized with a property with name identifier and value reference.
  329. JsonObject object;
  330. object.set(identifier, reference);
  331. return object;
  332. }
  333. // https://w3c.github.io/webdriver/#dfn-get-a-known-shadow-root
  334. ErrorOr<JS::NonnullGCPtr<Web::DOM::ShadowRoot>, Web::WebDriver::Error> get_known_shadow_root(StringView shadow_id)
  335. {
  336. // NOTE: The whole concept of "known shadow roots" is not implemented yet. See get_or_create_a_shadow_root_reference().
  337. // For now the shadow root is only represented by its ID.
  338. auto shadow_root = shadow_id.to_number<i64>();
  339. if (!shadow_root.has_value())
  340. return Web::WebDriver::Error::from_code(Web::WebDriver::ErrorCode::InvalidArgument, "Shadow ID is not an integer");
  341. auto* node = Web::DOM::Node::from_unique_id(UniqueNodeID(*shadow_root));
  342. if (!node || !node->is_shadow_root())
  343. return Web::WebDriver::Error::from_code(Web::WebDriver::ErrorCode::NoSuchElement, ByteString::formatted("Could not find shadow root with ID: {}", shadow_id));
  344. return static_cast<Web::DOM::ShadowRoot&>(*node);
  345. }
  346. // https://w3c.github.io/webdriver/#dfn-center-point
  347. CSSPixelPoint in_view_center_point(DOM::Element const& element, CSSPixelRect viewport)
  348. {
  349. // 1. Let rectangle be the first element of the DOMRect sequence returned by calling getClientRects() on element.
  350. auto const* rectangle = element.get_client_rects()->item(0);
  351. VERIFY(rectangle);
  352. // 2. Let left be max(0, min(x coordinate, x coordinate + width dimension)).
  353. auto left = max(0.0, min(rectangle->x(), rectangle->x() + rectangle->width()));
  354. // 3. Let right be min(innerWidth, max(x coordinate, x coordinate + width dimension)).
  355. auto right = min(viewport.width().to_double(), max(rectangle->x(), rectangle->x() + rectangle->width()));
  356. // 4. Let top be max(0, min(y coordinate, y coordinate + height dimension)).
  357. auto top = max(0.0, min(rectangle->y(), rectangle->y() + rectangle->height()));
  358. // 5. Let bottom be min(innerHeight, max(y coordinate, y coordinate + height dimension)).
  359. auto bottom = min(viewport.height().to_double(), max(rectangle->y(), rectangle->y() + rectangle->height()));
  360. // 6. Let x be floor((left + right) ÷ 2.0).
  361. auto x = floor((left + right) / 2.0);
  362. // 7. Let y be floor((top + bottom) ÷ 2.0).
  363. auto y = floor((top + bottom) / 2.0);
  364. // 8. Return the pair of (x, y).
  365. return { x, y };
  366. }
  367. }