Element.h 12 KB

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