Element.h 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367
  1. /*
  2. * Copyright (c) 2018-2023, Andreas Kling <kling@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #pragma once
  7. #include <AK/DeprecatedFlyString.h>
  8. #include <AK/DeprecatedString.h>
  9. #include <LibWeb/ARIA/ARIAMixin.h>
  10. #include <LibWeb/Bindings/ElementPrototype.h>
  11. #include <LibWeb/Bindings/ShadowRootPrototype.h>
  12. #include <LibWeb/CSS/Selector.h>
  13. #include <LibWeb/CSS/StyleProperty.h>
  14. #include <LibWeb/DOM/ChildNode.h>
  15. #include <LibWeb/DOM/NonDocumentTypeChildNode.h>
  16. #include <LibWeb/DOM/ParentNode.h>
  17. #include <LibWeb/DOM/QualifiedName.h>
  18. #include <LibWeb/HTML/AttributeNames.h>
  19. #include <LibWeb/HTML/EventLoop/Task.h>
  20. #include <LibWeb/HTML/ScrollOptions.h>
  21. #include <LibWeb/HTML/TagNames.h>
  22. #include <LibWeb/HTML/Window.h>
  23. #include <LibWeb/WebIDL/ExceptionOr.h>
  24. namespace Web::DOM {
  25. struct ShadowRootInit {
  26. Bindings::ShadowRootMode mode;
  27. bool delegates_focus = false;
  28. };
  29. // https://w3c.github.io/csswg-drafts/cssom-view-1/#dictdef-scrollintoviewoptions
  30. struct ScrollIntoViewOptions : public HTML::ScrollOptions {
  31. Bindings::ScrollLogicalPosition block { Bindings::ScrollLogicalPosition::Start };
  32. Bindings::ScrollLogicalPosition inline_ { Bindings::ScrollLogicalPosition::Nearest };
  33. };
  34. // https://html.spec.whatwg.org/multipage/custom-elements.html#upgrade-reaction
  35. // An upgrade reaction, which will upgrade the custom element and contains a custom element definition; or
  36. struct CustomElementUpgradeReaction {
  37. JS::Handle<HTML::CustomElementDefinition> custom_element_definition;
  38. };
  39. // https://html.spec.whatwg.org/multipage/custom-elements.html#callback-reaction
  40. // A callback reaction, which will call a lifecycle callback, and contains a callback function as well as a list of arguments.
  41. struct CustomElementCallbackReaction {
  42. JS::Handle<WebIDL::CallbackType> callback;
  43. JS::MarkedVector<JS::Value> arguments;
  44. };
  45. // https://dom.spec.whatwg.org/#concept-element-custom-element-state
  46. // An element’s custom element state is one of "undefined", "failed", "uncustomized", "precustomized", or "custom".
  47. enum class CustomElementState {
  48. Undefined,
  49. Failed,
  50. Uncustomized,
  51. Precustomized,
  52. Custom,
  53. };
  54. class Element
  55. : public ParentNode
  56. , public ChildNode<Element>
  57. , public NonDocumentTypeChildNode<Element>
  58. , public ARIA::ARIAMixin {
  59. WEB_PLATFORM_OBJECT(Element, ParentNode);
  60. public:
  61. virtual ~Element() override;
  62. DeprecatedString const& qualified_name() const { return m_qualified_name.as_string(); }
  63. DeprecatedString const& html_uppercased_qualified_name() const { return m_html_uppercased_qualified_name; }
  64. virtual DeprecatedFlyString node_name() const final { return html_uppercased_qualified_name(); }
  65. DeprecatedFlyString const& local_name() const { return m_qualified_name.local_name(); }
  66. // NOTE: This is for the JS bindings
  67. DeprecatedString const& tag_name() const { return html_uppercased_qualified_name(); }
  68. DeprecatedFlyString const& prefix() const { return m_qualified_name.prefix(); }
  69. void set_prefix(DeprecatedFlyString const& value);
  70. DeprecatedFlyString const& namespace_() const { return m_qualified_name.namespace_(); }
  71. // NOTE: This is for the JS bindings
  72. DeprecatedFlyString const& namespace_uri() const { return namespace_(); }
  73. bool has_attribute(DeprecatedFlyString const& name) const;
  74. bool has_attributes() const;
  75. DeprecatedString attribute(DeprecatedFlyString const& name) const { return get_attribute(name); }
  76. DeprecatedString get_attribute(DeprecatedFlyString const& name) const;
  77. virtual WebIDL::ExceptionOr<void> set_attribute(DeprecatedFlyString const& name, DeprecatedString const& value);
  78. WebIDL::ExceptionOr<void> set_attribute_ns(DeprecatedFlyString const& namespace_, DeprecatedFlyString const& qualified_name, DeprecatedString const& value);
  79. WebIDL::ExceptionOr<JS::GCPtr<Attr>> set_attribute_node(Attr&);
  80. WebIDL::ExceptionOr<JS::GCPtr<Attr>> set_attribute_node_ns(Attr&);
  81. virtual void remove_attribute(DeprecatedFlyString const& name);
  82. WebIDL::ExceptionOr<bool> toggle_attribute(DeprecatedFlyString const& name, Optional<bool> force);
  83. size_t attribute_list_size() const;
  84. NamedNodeMap const* attributes() const { return m_attributes.ptr(); }
  85. Vector<DeprecatedString> get_attribute_names() const;
  86. JS::GCPtr<Attr> get_attribute_node(DeprecatedFlyString const& name) const;
  87. DOMTokenList* class_list();
  88. WebIDL::ExceptionOr<JS::NonnullGCPtr<ShadowRoot>> attach_shadow(ShadowRootInit init);
  89. JS::GCPtr<ShadowRoot> shadow_root() const;
  90. WebIDL::ExceptionOr<bool> matches(StringView selectors) const;
  91. WebIDL::ExceptionOr<DOM::Element const*> closest(StringView selectors) const;
  92. int client_top() const;
  93. int client_left() const;
  94. int client_width() const;
  95. int client_height() const;
  96. void for_each_attribute(Function<void(DeprecatedFlyString const&, DeprecatedString const&)>) const;
  97. bool has_class(FlyString const&, CaseSensitivity = CaseSensitivity::CaseSensitive) const;
  98. Vector<FlyString> const& class_names() const { return m_classes; }
  99. virtual void apply_presentational_hints(CSS::StyleProperties&) const { }
  100. virtual void parse_attribute(DeprecatedFlyString const& name, DeprecatedString const& value);
  101. virtual void did_remove_attribute(DeprecatedFlyString const&);
  102. struct [[nodiscard]] RequiredInvalidationAfterStyleChange {
  103. bool repaint { false };
  104. bool rebuild_stacking_context_tree { false };
  105. bool relayout { false };
  106. bool rebuild_layout_tree { false };
  107. void operator|=(RequiredInvalidationAfterStyleChange const& other)
  108. {
  109. repaint |= other.repaint;
  110. rebuild_stacking_context_tree |= other.rebuild_stacking_context_tree;
  111. relayout |= other.relayout;
  112. rebuild_layout_tree |= other.rebuild_layout_tree;
  113. }
  114. [[nodiscard]] bool is_none() const { return !repaint && !rebuild_stacking_context_tree && !relayout && !rebuild_layout_tree; }
  115. static RequiredInvalidationAfterStyleChange full() { return { true, true, true, true }; }
  116. };
  117. RequiredInvalidationAfterStyleChange recompute_style();
  118. virtual Optional<CSS::Selector::PseudoElement> pseudo_element() const { return {}; }
  119. Layout::NodeWithStyle* layout_node();
  120. Layout::NodeWithStyle const* layout_node() const;
  121. DeprecatedString name() const { return attribute(HTML::AttributeNames::name); }
  122. CSS::StyleProperties* computed_css_values() { return m_computed_css_values.ptr(); }
  123. CSS::StyleProperties const* computed_css_values() const { return m_computed_css_values.ptr(); }
  124. void set_computed_css_values(RefPtr<CSS::StyleProperties>);
  125. NonnullRefPtr<CSS::StyleProperties> resolved_css_values();
  126. CSS::CSSStyleDeclaration const* inline_style() const;
  127. CSS::CSSStyleDeclaration* style_for_bindings();
  128. WebIDL::ExceptionOr<DeprecatedString> inner_html() const;
  129. WebIDL::ExceptionOr<void> set_inner_html(DeprecatedString const&);
  130. WebIDL::ExceptionOr<void> insert_adjacent_html(DeprecatedString position, DeprecatedString text);
  131. bool is_focused() const;
  132. bool is_active() const;
  133. JS::NonnullGCPtr<HTMLCollection> get_elements_by_class_name(DeprecatedFlyString const&);
  134. bool is_shadow_host() const;
  135. ShadowRoot* shadow_root_internal() { return m_shadow_root.ptr(); }
  136. ShadowRoot const* shadow_root_internal() const { return m_shadow_root.ptr(); }
  137. void set_shadow_root(JS::GCPtr<ShadowRoot>);
  138. void set_custom_properties(Optional<CSS::Selector::PseudoElement>, HashMap<DeprecatedFlyString, CSS::StyleProperty> custom_properties);
  139. [[nodiscard]] HashMap<DeprecatedFlyString, CSS::StyleProperty> const& custom_properties(Optional<CSS::Selector::PseudoElement>) const;
  140. void queue_an_element_task(HTML::Task::Source, JS::SafeFunction<void()>);
  141. bool is_void_element() const;
  142. bool serializes_as_void() const;
  143. JS::NonnullGCPtr<Geometry::DOMRect> get_bounding_client_rect() const;
  144. JS::NonnullGCPtr<Geometry::DOMRectList> get_client_rects() const;
  145. virtual JS::GCPtr<Layout::Node> create_layout_node(NonnullRefPtr<CSS::StyleProperties>);
  146. virtual void did_receive_focus() { }
  147. virtual void did_lose_focus() { }
  148. static JS::GCPtr<Layout::Node> create_layout_node_for_display_type(DOM::Document&, CSS::Display const&, NonnullRefPtr<CSS::StyleProperties>, Element*);
  149. void set_pseudo_element_node(Badge<Layout::TreeBuilder>, CSS::Selector::PseudoElement, JS::GCPtr<Layout::Node>);
  150. JS::GCPtr<Layout::Node> get_pseudo_element_node(CSS::Selector::PseudoElement) const;
  151. void clear_pseudo_element_nodes(Badge<Layout::TreeBuilder>);
  152. void serialize_pseudo_elements_as_json(JsonArraySerializer<StringBuilder>& children_array) const;
  153. i32 tab_index() const;
  154. void set_tab_index(i32 tab_index);
  155. bool is_potentially_scrollable() const;
  156. double scroll_top() const;
  157. double scroll_left() const;
  158. void set_scroll_top(double y);
  159. void set_scroll_left(double x);
  160. int scroll_width() const;
  161. int scroll_height() const;
  162. bool is_actually_disabled() const;
  163. WebIDL::ExceptionOr<JS::GCPtr<Element>> insert_adjacent_element(DeprecatedString const& where, JS::NonnullGCPtr<Element> element);
  164. WebIDL::ExceptionOr<void> insert_adjacent_text(DeprecatedString const& where, DeprecatedString const& data);
  165. // https://w3c.github.io/csswg-drafts/cssom-view-1/#dom-element-scrollintoview
  166. ErrorOr<void> scroll_into_view(Optional<Variant<bool, ScrollIntoViewOptions>> = {});
  167. // https://www.w3.org/TR/wai-aria-1.2/#ARIAMixin
  168. #define ARIA_IMPL(name, attribute) \
  169. DeprecatedString name() const override \
  170. { \
  171. return get_attribute(attribute); \
  172. } \
  173. \
  174. WebIDL::ExceptionOr<void> set_##name(DeprecatedString const& value) override \
  175. { \
  176. TRY(set_attribute(attribute, value)); \
  177. return {}; \
  178. }
  179. // https://www.w3.org/TR/wai-aria-1.2/#accessibilityroleandproperties-correspondence
  180. ARIA_IMPL(role, "role");
  181. ARIA_IMPL(aria_active_descendant, "aria-activedescendant");
  182. ARIA_IMPL(aria_atomic, "aria-atomic");
  183. ARIA_IMPL(aria_auto_complete, "aria-autocomplete");
  184. ARIA_IMPL(aria_busy, "aria-busy");
  185. ARIA_IMPL(aria_checked, "aria-checked");
  186. ARIA_IMPL(aria_col_count, "aria-colcount");
  187. ARIA_IMPL(aria_col_index, "aria-colindex");
  188. ARIA_IMPL(aria_col_span, "aria-colspan");
  189. ARIA_IMPL(aria_controls, "aria-controls");
  190. ARIA_IMPL(aria_current, "aria-current");
  191. ARIA_IMPL(aria_described_by, "aria-describedby");
  192. ARIA_IMPL(aria_details, "aria-details");
  193. ARIA_IMPL(aria_drop_effect, "aria-dropeffect");
  194. ARIA_IMPL(aria_error_message, "aria-errormessage");
  195. ARIA_IMPL(aria_disabled, "aria-disabled");
  196. ARIA_IMPL(aria_expanded, "aria-expanded");
  197. ARIA_IMPL(aria_flow_to, "aria-flowto");
  198. ARIA_IMPL(aria_grabbed, "aria-grabbed");
  199. ARIA_IMPL(aria_has_popup, "aria-haspopup");
  200. ARIA_IMPL(aria_hidden, "aria-hidden");
  201. ARIA_IMPL(aria_invalid, "aria-invalid");
  202. ARIA_IMPL(aria_key_shortcuts, "aria-keyshortcuts");
  203. ARIA_IMPL(aria_label, "aria-label");
  204. ARIA_IMPL(aria_labelled_by, "aria-labelledby");
  205. ARIA_IMPL(aria_level, "aria-level");
  206. ARIA_IMPL(aria_live, "aria-live");
  207. ARIA_IMPL(aria_modal, "aria-modal");
  208. ARIA_IMPL(aria_multi_line, "aria-multiline");
  209. ARIA_IMPL(aria_multi_selectable, "aria-multiselectable");
  210. ARIA_IMPL(aria_orientation, "aria-orientation");
  211. ARIA_IMPL(aria_owns, "aria-owns");
  212. ARIA_IMPL(aria_placeholder, "aria-placeholder");
  213. ARIA_IMPL(aria_pos_in_set, "aria-posinset");
  214. ARIA_IMPL(aria_pressed, "aria-pressed");
  215. ARIA_IMPL(aria_read_only, "aria-readonly");
  216. ARIA_IMPL(aria_relevant, "aria-relevant");
  217. ARIA_IMPL(aria_required, "aria-required");
  218. ARIA_IMPL(aria_role_description, "aria-roledescription");
  219. ARIA_IMPL(aria_row_count, "aria-rowcount");
  220. ARIA_IMPL(aria_row_index, "aria-rowindex");
  221. ARIA_IMPL(aria_row_span, "aria-rowspan");
  222. ARIA_IMPL(aria_selected, "aria-selected");
  223. ARIA_IMPL(aria_set_size, "aria-setsize");
  224. ARIA_IMPL(aria_sort, "aria-sort");
  225. ARIA_IMPL(aria_value_max, "aria-valuemax");
  226. ARIA_IMPL(aria_value_min, "aria-valuemin");
  227. ARIA_IMPL(aria_value_now, "aria-valuenow");
  228. ARIA_IMPL(aria_value_text, "aria-valuetext");
  229. #undef ARIA_IMPL
  230. virtual bool exclude_from_accessibility_tree() const override;
  231. virtual bool include_in_accessibility_tree() const override;
  232. void enqueue_a_custom_element_upgrade_reaction(HTML::CustomElementDefinition& custom_element_definition);
  233. void enqueue_a_custom_element_callback_reaction(FlyString const& callback_name, JS::MarkedVector<JS::Value> arguments);
  234. Vector<Variant<CustomElementUpgradeReaction, CustomElementCallbackReaction>>& custom_element_reaction_queue() { return m_custom_element_reaction_queue; }
  235. Vector<Variant<CustomElementUpgradeReaction, CustomElementCallbackReaction>> const& custom_element_reaction_queue() const { return m_custom_element_reaction_queue; }
  236. JS::ThrowCompletionOr<void> upgrade_element(JS::NonnullGCPtr<HTML::CustomElementDefinition> custom_element_definition);
  237. void try_to_upgrade();
  238. bool is_defined() const;
  239. bool is_custom() const;
  240. Optional<String> const& is_value() const { return m_is_value; }
  241. void set_is_value(Optional<String> const& is) { m_is_value = is; }
  242. void set_custom_element_state(CustomElementState value) { m_custom_element_state = value; }
  243. void setup_custom_element_from_constructor(HTML::CustomElementDefinition& custom_element_definition, Optional<String> const& is_value);
  244. void scroll(HTML::ScrollToOptions const&);
  245. void scroll(double x, double y);
  246. protected:
  247. Element(Document&, DOM::QualifiedName);
  248. virtual JS::ThrowCompletionOr<void> initialize(JS::Realm&) override;
  249. virtual void children_changed() override;
  250. virtual i32 default_tab_index_value() const;
  251. virtual void visit_edges(Cell::Visitor&) override;
  252. private:
  253. void make_html_uppercased_qualified_name();
  254. void invalidate_style_after_attribute_change(DeprecatedFlyString const& attribute_name);
  255. WebIDL::ExceptionOr<JS::GCPtr<Node>> insert_adjacent(DeprecatedString const& where, JS::NonnullGCPtr<Node> node);
  256. void enqueue_an_element_on_the_appropriate_element_queue();
  257. QualifiedName m_qualified_name;
  258. DeprecatedString m_html_uppercased_qualified_name;
  259. JS::GCPtr<NamedNodeMap> m_attributes;
  260. JS::GCPtr<CSS::ElementInlineCSSStyleDeclaration> m_inline_style;
  261. JS::GCPtr<DOMTokenList> m_class_list;
  262. JS::GCPtr<ShadowRoot> m_shadow_root;
  263. RefPtr<CSS::StyleProperties> m_computed_css_values;
  264. HashMap<DeprecatedFlyString, CSS::StyleProperty> m_custom_properties;
  265. Array<HashMap<DeprecatedFlyString, CSS::StyleProperty>, to_underlying(CSS::Selector::PseudoElement::PseudoElementCount)> m_pseudo_element_custom_properties;
  266. Vector<FlyString> m_classes;
  267. Array<JS::GCPtr<Layout::Node>, to_underlying(CSS::Selector::PseudoElement::PseudoElementCount)> m_pseudo_element_nodes;
  268. // https://html.spec.whatwg.org/multipage/custom-elements.html#custom-element-reaction-queue
  269. // All elements have an associated custom element reaction queue, initially empty. Each item in the custom element reaction queue is of one of two types:
  270. // NOTE: See the structs at the top of this header.
  271. Vector<Variant<CustomElementUpgradeReaction, CustomElementCallbackReaction>> m_custom_element_reaction_queue;
  272. // https://dom.spec.whatwg.org/#concept-element-custom-element-state
  273. CustomElementState m_custom_element_state { CustomElementState::Undefined };
  274. // https://dom.spec.whatwg.org/#concept-element-custom-element-definition
  275. JS::GCPtr<HTML::CustomElementDefinition> m_custom_element_definition;
  276. // https://dom.spec.whatwg.org/#concept-element-is-value
  277. Optional<String> m_is_value;
  278. };
  279. template<>
  280. inline bool Node::fast_is<Element>() const { return is_element(); }
  281. WebIDL::ExceptionOr<QualifiedName> validate_and_extract(JS::Realm&, DeprecatedFlyString namespace_, DeprecatedFlyString qualified_name);
  282. }