ElementReference.cpp 26 KB

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