Element.h 13 KB

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