ElementReference.cpp 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. /*
  2. * Copyright (c) 2024, Tim Flynn <trflynn89@serenityos.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/WebDriver/ElementReference.h>
  11. namespace Web::WebDriver {
  12. // https://w3c.github.io/webdriver/#dfn-web-element-identifier
  13. static ByteString const web_element_identifier = "element-6066-11e4-a52e-4f735466cecf"sv;
  14. // https://w3c.github.io/webdriver/#dfn-shadow-root-identifier
  15. static ByteString const shadow_root_identifier = "shadow-6066-11e4-a52e-4f735466cecf"sv;
  16. // https://w3c.github.io/webdriver/#dfn-get-or-create-a-web-element-reference
  17. ByteString get_or_create_a_web_element_reference(Web::DOM::Node const& element)
  18. {
  19. // FIXME: 1. For each known element of the current browsing context’s list of known elements:
  20. // FIXME: 1. If known element equals element, return success with known element’s web element reference.
  21. // FIXME: 2. Add element to the list of known elements of the current browsing context.
  22. // FIXME: 3. Return success with the element’s web element reference.
  23. return ByteString::number(element.unique_id());
  24. }
  25. // https://w3c.github.io/webdriver/#dfn-web-element-reference-object
  26. JsonObject web_element_reference_object(Web::DOM::Node const& element)
  27. {
  28. // 1. Let identifier be the web element identifier.
  29. auto identifier = web_element_identifier;
  30. // 2. Let reference be the result of get or create a web element reference given element.
  31. auto reference = get_or_create_a_web_element_reference(element);
  32. // 3. Return a JSON Object initialized with a property with name identifier and value reference.
  33. JsonObject object;
  34. object.set(identifier, reference);
  35. return object;
  36. }
  37. ByteString extract_web_element_reference(JsonObject const& object)
  38. {
  39. return object.get_byte_string(web_element_identifier).release_value();
  40. }
  41. // https://w3c.github.io/webdriver/#dfn-represents-a-web-element
  42. bool represents_a_web_element(JsonValue const& value)
  43. {
  44. // An ECMAScript Object represents a web element if it has a web element identifier own property.
  45. if (!value.is_object())
  46. return false;
  47. return value.as_object().has_string(web_element_identifier);
  48. }
  49. // https://w3c.github.io/webdriver/#dfn-get-a-known-connected-element
  50. ErrorOr<Web::DOM::Element*, Web::WebDriver::Error> get_known_connected_element(StringView element_id)
  51. {
  52. // NOTE: The whole concept of "connected elements" is not implemented yet. See get_or_create_a_web_element_reference().
  53. // For now the element is only represented by its ID.
  54. auto element = element_id.to_number<int>();
  55. if (!element.has_value())
  56. return Web::WebDriver::Error::from_code(Web::WebDriver::ErrorCode::InvalidArgument, "Element ID is not an integer");
  57. auto* node = Web::DOM::Node::from_unique_id(*element);
  58. if (!node || !node->is_element())
  59. return Web::WebDriver::Error::from_code(Web::WebDriver::ErrorCode::NoSuchElement, ByteString::formatted("Could not find element with ID: {}", element_id));
  60. return static_cast<Web::DOM::Element*>(node);
  61. }
  62. // https://w3c.github.io/webdriver/#dfn-is-stale
  63. bool is_element_stale(Web::DOM::Node const& element)
  64. {
  65. // An element is stale if its node document is not the active document or if it is not connected.
  66. return !element.document().is_active() || !element.is_connected();
  67. }
  68. // https://w3c.github.io/webdriver/#dfn-get-or-create-a-shadow-root-reference
  69. ByteString get_or_create_a_shadow_root_reference(Web::DOM::ShadowRoot const& shadow_root)
  70. {
  71. // FIXME: 1. For each known shadow root of the current browsing context’s list of known shadow roots:
  72. // FIXME: 1. If known shadow root equals shadow root, return success with known shadow root’s shadow root reference.
  73. // FIXME: 2. Add shadow to the list of known shadow roots of the current browsing context.
  74. // FIXME: 3. Return success with the shadow’s shadow root reference.
  75. return ByteString::number(shadow_root.unique_id());
  76. }
  77. // https://w3c.github.io/webdriver/#dfn-shadow-root-reference-object
  78. JsonObject shadow_root_reference_object(Web::DOM::ShadowRoot const& shadow_root)
  79. {
  80. // 1. Let identifier be the shadow root identifier.
  81. auto identifier = shadow_root_identifier;
  82. // 2. Let reference be the result of get or create a shadow root reference given shadow root.
  83. auto reference = get_or_create_a_shadow_root_reference(shadow_root);
  84. // 3. Return a JSON Object initialized with a property with name identifier and value reference.
  85. JsonObject object;
  86. object.set(identifier, reference);
  87. return object;
  88. }
  89. // https://w3c.github.io/webdriver/#dfn-get-a-known-shadow-root
  90. ErrorOr<Web::DOM::ShadowRoot*, Web::WebDriver::Error> get_known_shadow_root(StringView shadow_id)
  91. {
  92. // NOTE: The whole concept of "known shadow roots" is not implemented yet. See get_or_create_a_shadow_root_reference().
  93. // For now the shadow root is only represented by its ID.
  94. auto shadow_root = shadow_id.to_number<int>();
  95. if (!shadow_root.has_value())
  96. return Web::WebDriver::Error::from_code(Web::WebDriver::ErrorCode::InvalidArgument, "Shadow ID is not an integer");
  97. auto* node = Web::DOM::Node::from_unique_id(*shadow_root);
  98. if (!node || !node->is_shadow_root())
  99. return Web::WebDriver::Error::from_code(Web::WebDriver::ErrorCode::NoSuchElement, ByteString::formatted("Could not find shadow root with ID: {}", shadow_id));
  100. return static_cast<Web::DOM::ShadowRoot*>(node);
  101. }
  102. }