Element.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299
  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/Bindings/WindowGlobalMixin.h>
  13. #include <LibWeb/CSS/CSSStyleDeclaration.h>
  14. #include <LibWeb/CSS/StyleComputer.h>
  15. #include <LibWeb/DOM/Attr.h>
  16. #include <LibWeb/DOM/ChildNode.h>
  17. #include <LibWeb/DOM/NamedNodeMap.h>
  18. #include <LibWeb/DOM/NonDocumentTypeChildNode.h>
  19. #include <LibWeb/DOM/ParentNode.h>
  20. #include <LibWeb/DOM/QualifiedName.h>
  21. #include <LibWeb/HTML/AttributeNames.h>
  22. #include <LibWeb/HTML/EventLoop/Task.h>
  23. #include <LibWeb/HTML/TagNames.h>
  24. #include <LibWeb/HTML/Window.h>
  25. #include <LibWeb/Layout/Node.h>
  26. #include <LibWeb/Layout/TreeBuilder.h>
  27. #include <LibWeb/WebIDL/ExceptionOr.h>
  28. namespace Web::DOM {
  29. struct ShadowRootInit {
  30. Bindings::ShadowRootMode mode;
  31. bool delegates_focus = false;
  32. };
  33. // https://w3c.github.io/csswg-drafts/cssom-view-1/#dictdef-scrollintoviewoptions
  34. struct ScrollIntoViewOptions : public HTML::ScrollOptions {
  35. Bindings::ScrollLogicalPosition block { Bindings::ScrollLogicalPosition::Start };
  36. Bindings::ScrollLogicalPosition inline_ { Bindings::ScrollLogicalPosition::Nearest };
  37. };
  38. class Element
  39. : public ParentNode
  40. , public ChildNode<Element>
  41. , public NonDocumentTypeChildNode<Element>
  42. , public ARIA::ARIAMixin {
  43. WEB_PLATFORM_OBJECT(Element, ParentNode);
  44. public:
  45. virtual ~Element() override;
  46. DeprecatedString const& qualified_name() const { return m_qualified_name.as_string(); }
  47. DeprecatedString const& html_uppercased_qualified_name() const { return m_html_uppercased_qualified_name; }
  48. virtual DeprecatedFlyString node_name() const final { return html_uppercased_qualified_name(); }
  49. DeprecatedFlyString const& local_name() const { return m_qualified_name.local_name(); }
  50. // NOTE: This is for the JS bindings
  51. DeprecatedString const& tag_name() const { return html_uppercased_qualified_name(); }
  52. DeprecatedFlyString const& prefix() const { return m_qualified_name.prefix(); }
  53. DeprecatedFlyString const& namespace_() const { return m_qualified_name.namespace_(); }
  54. // NOTE: This is for the JS bindings
  55. DeprecatedFlyString const& namespace_uri() const { return namespace_(); }
  56. bool has_attribute(DeprecatedFlyString const& name) const;
  57. bool has_attributes() const { return !m_attributes->is_empty(); }
  58. DeprecatedString attribute(DeprecatedFlyString const& name) const { return get_attribute(name); }
  59. DeprecatedString get_attribute(DeprecatedFlyString const& name) const;
  60. WebIDL::ExceptionOr<void> set_attribute(DeprecatedFlyString const& name, DeprecatedString const& value);
  61. WebIDL::ExceptionOr<void> set_attribute_ns(DeprecatedFlyString const& namespace_, DeprecatedFlyString const& qualified_name, DeprecatedString const& value);
  62. WebIDL::ExceptionOr<JS::GCPtr<Attr>> set_attribute_node(Attr&);
  63. WebIDL::ExceptionOr<JS::GCPtr<Attr>> set_attribute_node_ns(Attr&);
  64. void remove_attribute(DeprecatedFlyString const& name);
  65. WebIDL::ExceptionOr<bool> toggle_attribute(DeprecatedFlyString const& name, Optional<bool> force);
  66. size_t attribute_list_size() const { return m_attributes->length(); }
  67. NamedNodeMap const* attributes() const { return m_attributes.ptr(); }
  68. Vector<DeprecatedString> get_attribute_names() const;
  69. JS::GCPtr<Attr> get_attribute_node(DeprecatedFlyString const& name) const;
  70. DOMTokenList* class_list();
  71. WebIDL::ExceptionOr<JS::NonnullGCPtr<ShadowRoot>> attach_shadow(ShadowRootInit init);
  72. JS::GCPtr<ShadowRoot> shadow_root() const;
  73. WebIDL::ExceptionOr<bool> matches(StringView selectors) const;
  74. WebIDL::ExceptionOr<DOM::Element const*> closest(StringView selectors) const;
  75. int client_top() const;
  76. int client_left() const;
  77. int client_width() const;
  78. int client_height() const;
  79. template<typename Callback>
  80. void for_each_attribute(Callback callback) const
  81. {
  82. for (size_t i = 0; i < m_attributes->length(); ++i) {
  83. auto const* attribute = m_attributes->item(i);
  84. callback(attribute->name(), attribute->value());
  85. }
  86. }
  87. bool has_class(FlyString const&, CaseSensitivity = CaseSensitivity::CaseSensitive) const;
  88. Vector<FlyString> const& class_names() const { return m_classes; }
  89. virtual void apply_presentational_hints(CSS::StyleProperties&) const { }
  90. virtual void parse_attribute(DeprecatedFlyString const& name, DeprecatedString const& value);
  91. virtual void did_remove_attribute(DeprecatedFlyString const&);
  92. enum class NeedsRelayout {
  93. No = 0,
  94. Yes = 1,
  95. };
  96. NeedsRelayout recompute_style();
  97. Layout::NodeWithStyle* layout_node() { return static_cast<Layout::NodeWithStyle*>(Node::layout_node()); }
  98. Layout::NodeWithStyle const* layout_node() const { return static_cast<Layout::NodeWithStyle const*>(Node::layout_node()); }
  99. DeprecatedString name() const { return attribute(HTML::AttributeNames::name); }
  100. CSS::StyleProperties* computed_css_values() { return m_computed_css_values.ptr(); }
  101. CSS::StyleProperties const* computed_css_values() const { return m_computed_css_values.ptr(); }
  102. void set_computed_css_values(RefPtr<CSS::StyleProperties> style) { m_computed_css_values = move(style); }
  103. NonnullRefPtr<CSS::StyleProperties> resolved_css_values();
  104. CSS::CSSStyleDeclaration const* inline_style() const;
  105. CSS::CSSStyleDeclaration* style_for_bindings();
  106. WebIDL::ExceptionOr<DeprecatedString> inner_html() const;
  107. WebIDL::ExceptionOr<void> set_inner_html(DeprecatedString const&);
  108. WebIDL::ExceptionOr<void> insert_adjacent_html(DeprecatedString position, DeprecatedString text);
  109. bool is_focused() const;
  110. bool is_active() const;
  111. JS::NonnullGCPtr<HTMLCollection> get_elements_by_class_name(DeprecatedFlyString const&);
  112. bool is_shadow_host() const;
  113. ShadowRoot* shadow_root_internal() { return m_shadow_root.ptr(); }
  114. ShadowRoot const* shadow_root_internal() const { return m_shadow_root.ptr(); }
  115. void set_shadow_root(JS::GCPtr<ShadowRoot>);
  116. void set_custom_properties(HashMap<DeprecatedFlyString, CSS::StyleProperty> custom_properties) { m_custom_properties = move(custom_properties); }
  117. HashMap<DeprecatedFlyString, CSS::StyleProperty> const& custom_properties() const { return m_custom_properties; }
  118. void queue_an_element_task(HTML::Task::Source, JS::SafeFunction<void()>);
  119. bool is_void_element() const;
  120. bool serializes_as_void() const;
  121. JS::NonnullGCPtr<Geometry::DOMRect> get_bounding_client_rect() const;
  122. JS::NonnullGCPtr<Geometry::DOMRectList> get_client_rects() const;
  123. virtual JS::GCPtr<Layout::Node> create_layout_node(NonnullRefPtr<CSS::StyleProperties>);
  124. virtual void did_receive_focus() { }
  125. virtual void did_lose_focus() { }
  126. static JS::GCPtr<Layout::Node> create_layout_node_for_display_type(DOM::Document&, CSS::Display const&, NonnullRefPtr<CSS::StyleProperties>, Element*);
  127. void set_pseudo_element_node(Badge<Layout::TreeBuilder>, CSS::Selector::PseudoElement, JS::GCPtr<Layout::Node>);
  128. JS::GCPtr<Layout::Node> get_pseudo_element_node(CSS::Selector::PseudoElement) const;
  129. void clear_pseudo_element_nodes(Badge<Layout::TreeBuilder>);
  130. void serialize_pseudo_elements_as_json(JsonArraySerializer<StringBuilder>& children_array) const;
  131. i32 tab_index() const;
  132. void set_tab_index(i32 tab_index);
  133. bool is_potentially_scrollable() const;
  134. double scroll_top() const;
  135. double scroll_left() const;
  136. void set_scroll_top(double y);
  137. void set_scroll_left(double x);
  138. int scroll_width() const;
  139. int scroll_height() const;
  140. bool is_actually_disabled() const;
  141. WebIDL::ExceptionOr<JS::GCPtr<Element>> insert_adjacent_element(DeprecatedString const& where, JS::NonnullGCPtr<Element> element);
  142. WebIDL::ExceptionOr<void> insert_adjacent_text(DeprecatedString const& where, DeprecatedString const& data);
  143. // https://w3c.github.io/csswg-drafts/cssom-view-1/#dom-element-scrollintoview
  144. ErrorOr<void> scroll_into_view(Optional<Variant<bool, ScrollIntoViewOptions>> = {});
  145. // https://www.w3.org/TR/wai-aria-1.2/#ARIAMixin
  146. #define ARIA_IMPL(name, attribute) \
  147. DeprecatedString name() const override \
  148. { \
  149. return get_attribute(attribute); \
  150. } \
  151. \
  152. WebIDL::ExceptionOr<void> set_##name(DeprecatedString const& value) override \
  153. { \
  154. TRY(set_attribute(attribute, value)); \
  155. return {}; \
  156. }
  157. // https://www.w3.org/TR/wai-aria-1.2/#accessibilityroleandproperties-correspondence
  158. ARIA_IMPL(role, "role");
  159. ARIA_IMPL(aria_active_descendant, "aria-activedescendant");
  160. ARIA_IMPL(aria_atomic, "aria-atomic");
  161. ARIA_IMPL(aria_auto_complete, "aria-autocomplete");
  162. ARIA_IMPL(aria_busy, "aria-busy");
  163. ARIA_IMPL(aria_checked, "aria-checked");
  164. ARIA_IMPL(aria_col_count, "aria-colcount");
  165. ARIA_IMPL(aria_col_index, "aria-colindex");
  166. ARIA_IMPL(aria_col_span, "aria-colspan");
  167. ARIA_IMPL(aria_controls, "aria-controls");
  168. ARIA_IMPL(aria_current, "aria-current");
  169. ARIA_IMPL(aria_described_by, "aria-describedby");
  170. ARIA_IMPL(aria_details, "aria-details");
  171. ARIA_IMPL(aria_drop_effect, "aria-dropeffect");
  172. ARIA_IMPL(aria_error_message, "aria-errormessage");
  173. ARIA_IMPL(aria_disabled, "aria-disabled");
  174. ARIA_IMPL(aria_expanded, "aria-expanded");
  175. ARIA_IMPL(aria_flow_to, "aria-flowto");
  176. ARIA_IMPL(aria_grabbed, "aria-grabbed");
  177. ARIA_IMPL(aria_has_popup, "aria-haspopup");
  178. ARIA_IMPL(aria_hidden, "aria-hidden");
  179. ARIA_IMPL(aria_invalid, "aria-invalid");
  180. ARIA_IMPL(aria_key_shortcuts, "aria-keyshortcuts");
  181. ARIA_IMPL(aria_label, "aria-label");
  182. ARIA_IMPL(aria_labelled_by, "aria-labelledby");
  183. ARIA_IMPL(aria_level, "aria-level");
  184. ARIA_IMPL(aria_live, "aria-live");
  185. ARIA_IMPL(aria_modal, "aria-modal");
  186. ARIA_IMPL(aria_multi_line, "aria-multiline");
  187. ARIA_IMPL(aria_multi_selectable, "aria-multiselectable");
  188. ARIA_IMPL(aria_orientation, "aria-orientation");
  189. ARIA_IMPL(aria_owns, "aria-owns");
  190. ARIA_IMPL(aria_placeholder, "aria-placeholder");
  191. ARIA_IMPL(aria_pos_in_set, "aria-posinset");
  192. ARIA_IMPL(aria_pressed, "aria-pressed");
  193. ARIA_IMPL(aria_read_only, "aria-readonly");
  194. ARIA_IMPL(aria_relevant, "aria-relevant");
  195. ARIA_IMPL(aria_required, "aria-required");
  196. ARIA_IMPL(aria_role_description, "aria-roledescription");
  197. ARIA_IMPL(aria_row_count, "aria-rowcount");
  198. ARIA_IMPL(aria_row_index, "aria-rowindex");
  199. ARIA_IMPL(aria_row_span, "aria-rowspan");
  200. ARIA_IMPL(aria_selected, "aria-selected");
  201. ARIA_IMPL(aria_set_size, "aria-setsize");
  202. ARIA_IMPL(aria_sort, "aria-sort");
  203. ARIA_IMPL(aria_value_max, "aria-valuemax");
  204. ARIA_IMPL(aria_value_min, "aria-valuemin");
  205. ARIA_IMPL(aria_value_now, "aria-valuenow");
  206. ARIA_IMPL(aria_value_text, "aria-valuetext");
  207. #undef ARIA_IMPL
  208. virtual bool exclude_from_accessibility_tree() const override;
  209. virtual bool include_in_accessibility_tree() const override;
  210. protected:
  211. Element(Document&, DOM::QualifiedName);
  212. virtual JS::ThrowCompletionOr<void> initialize(JS::Realm&) override;
  213. virtual void children_changed() override;
  214. virtual i32 default_tab_index_value() const;
  215. virtual void visit_edges(Cell::Visitor&) override;
  216. private:
  217. void make_html_uppercased_qualified_name();
  218. void invalidate_style_after_attribute_change(DeprecatedFlyString const& attribute_name);
  219. WebIDL::ExceptionOr<JS::GCPtr<Node>> insert_adjacent(DeprecatedString const& where, JS::NonnullGCPtr<Node> node);
  220. QualifiedName m_qualified_name;
  221. DeprecatedString m_html_uppercased_qualified_name;
  222. JS::GCPtr<NamedNodeMap> m_attributes;
  223. JS::GCPtr<CSS::ElementInlineCSSStyleDeclaration> m_inline_style;
  224. JS::GCPtr<DOMTokenList> m_class_list;
  225. JS::GCPtr<ShadowRoot> m_shadow_root;
  226. RefPtr<CSS::StyleProperties> m_computed_css_values;
  227. HashMap<DeprecatedFlyString, CSS::StyleProperty> m_custom_properties;
  228. Vector<FlyString> m_classes;
  229. Array<JS::GCPtr<Layout::Node>, to_underlying(CSS::Selector::PseudoElement::PseudoElementCount)> m_pseudo_element_nodes;
  230. };
  231. template<>
  232. inline bool Node::fast_is<Element>() const { return is_element(); }
  233. WebIDL::ExceptionOr<QualifiedName> validate_and_extract(JS::Realm&, DeprecatedFlyString namespace_, DeprecatedFlyString qualified_name);
  234. }