Element.h 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  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/FlyString.h>
  8. #include <AK/String.h>
  9. #include <LibWeb/Bindings/ElementPrototype.h>
  10. #include <LibWeb/CSS/CSSStyleDeclaration.h>
  11. #include <LibWeb/CSS/StyleComputer.h>
  12. #include <LibWeb/DOM/Attr.h>
  13. #include <LibWeb/DOM/ChildNode.h>
  14. #include <LibWeb/DOM/NamedNodeMap.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/TagNames.h>
  21. #include <LibWeb/Layout/Node.h>
  22. #include <LibWeb/Layout/TreeBuilder.h>
  23. #include <LibWeb/WebIDL/ExceptionOr.h>
  24. namespace Web::DOM {
  25. // https://w3c.github.io/csswg-drafts/cssom-view-1/#dictdef-scrolloptions
  26. struct ScrollOptions {
  27. Bindings::ScrollBehavior behavior { Bindings::ScrollBehavior::Auto };
  28. };
  29. // https://w3c.github.io/csswg-drafts/cssom-view-1/#dictdef-scrollintoviewoptions
  30. struct ScrollIntoViewOptions : public ScrollOptions {
  31. Bindings::ScrollLogicalPosition block { Bindings::ScrollLogicalPosition::Start };
  32. Bindings::ScrollLogicalPosition inline_ { Bindings::ScrollLogicalPosition::Nearest };
  33. };
  34. class Element
  35. : public ParentNode
  36. , public ChildNode<Element>
  37. , public NonDocumentTypeChildNode<Element> {
  38. WEB_PLATFORM_OBJECT(Element, ParentNode);
  39. public:
  40. virtual ~Element() override;
  41. String const& qualified_name() const { return m_qualified_name.as_string(); }
  42. String const& html_uppercased_qualified_name() const { return m_html_uppercased_qualified_name; }
  43. virtual FlyString node_name() const final { return html_uppercased_qualified_name(); }
  44. FlyString const& local_name() const { return m_qualified_name.local_name(); }
  45. // NOTE: This is for the JS bindings
  46. String const& tag_name() const { return html_uppercased_qualified_name(); }
  47. FlyString const& prefix() const { return m_qualified_name.prefix(); }
  48. FlyString const& namespace_() const { return m_qualified_name.namespace_(); }
  49. // NOTE: This is for the JS bindings
  50. FlyString const& namespace_uri() const { return namespace_(); }
  51. bool has_attribute(FlyString const& name) const;
  52. bool has_attributes() const { return !m_attributes->is_empty(); }
  53. String attribute(FlyString const& name) const { return get_attribute(name); }
  54. String get_attribute(FlyString const& name) const;
  55. WebIDL::ExceptionOr<void> set_attribute(FlyString const& name, String const& value);
  56. WebIDL::ExceptionOr<void> set_attribute_ns(FlyString const& namespace_, FlyString const& qualified_name, String const& value);
  57. void remove_attribute(FlyString const& name);
  58. WebIDL::ExceptionOr<bool> toggle_attribute(FlyString const& name, Optional<bool> force);
  59. size_t attribute_list_size() const { return m_attributes->length(); }
  60. NamedNodeMap const* attributes() const { return m_attributes.ptr(); }
  61. Vector<String> get_attribute_names() const;
  62. DOMTokenList* class_list();
  63. WebIDL::ExceptionOr<bool> matches(StringView selectors) const;
  64. WebIDL::ExceptionOr<DOM::Element const*> closest(StringView selectors) const;
  65. int client_top() const;
  66. int client_left() const;
  67. int client_width() const;
  68. int client_height() const;
  69. template<typename Callback>
  70. void for_each_attribute(Callback callback) const
  71. {
  72. for (size_t i = 0; i < m_attributes->length(); ++i) {
  73. auto const* attribute = m_attributes->item(i);
  74. callback(attribute->name(), attribute->value());
  75. }
  76. }
  77. bool has_class(FlyString const&, CaseSensitivity = CaseSensitivity::CaseSensitive) const;
  78. Vector<FlyString> const& class_names() const { return m_classes; }
  79. virtual void apply_presentational_hints(CSS::StyleProperties&) const { }
  80. virtual void parse_attribute(FlyString const& name, String const& value);
  81. virtual void did_remove_attribute(FlyString const&);
  82. enum class NeedsRelayout {
  83. No = 0,
  84. Yes = 1,
  85. };
  86. NeedsRelayout recompute_style();
  87. Layout::NodeWithStyle* layout_node() { return static_cast<Layout::NodeWithStyle*>(Node::layout_node()); }
  88. Layout::NodeWithStyle const* layout_node() const { return static_cast<Layout::NodeWithStyle const*>(Node::layout_node()); }
  89. String name() const { return attribute(HTML::AttributeNames::name); }
  90. CSS::StyleProperties const* computed_css_values() const { return m_computed_css_values.ptr(); }
  91. void set_computed_css_values(RefPtr<CSS::StyleProperties> style) { m_computed_css_values = move(style); }
  92. NonnullRefPtr<CSS::StyleProperties> resolved_css_values();
  93. CSS::CSSStyleDeclaration const* inline_style() const;
  94. CSS::CSSStyleDeclaration* style_for_bindings();
  95. String inner_html() const;
  96. WebIDL::ExceptionOr<void> set_inner_html(String const&);
  97. WebIDL::ExceptionOr<void> insert_adjacent_html(String position, String text);
  98. bool is_focused() const;
  99. bool is_active() const;
  100. JS::NonnullGCPtr<HTMLCollection> get_elements_by_class_name(FlyString const&);
  101. ShadowRoot* shadow_root() { return m_shadow_root.ptr(); }
  102. ShadowRoot const* shadow_root() const { return m_shadow_root.ptr(); }
  103. void set_shadow_root(JS::GCPtr<ShadowRoot>);
  104. void set_custom_properties(HashMap<FlyString, CSS::StyleProperty> custom_properties) { m_custom_properties = move(custom_properties); }
  105. HashMap<FlyString, CSS::StyleProperty> const& custom_properties() const { return m_custom_properties; }
  106. void queue_an_element_task(HTML::Task::Source, JS::SafeFunction<void()>);
  107. bool is_void_element() const;
  108. bool serializes_as_void() const;
  109. JS::NonnullGCPtr<Geometry::DOMRect> get_bounding_client_rect() const;
  110. JS::NonnullGCPtr<Geometry::DOMRectList> get_client_rects() const;
  111. virtual RefPtr<Layout::Node> create_layout_node(NonnullRefPtr<CSS::StyleProperties>);
  112. virtual void did_receive_focus() { }
  113. virtual void did_lose_focus() { }
  114. static RefPtr<Layout::Node> create_layout_node_for_display_type(DOM::Document&, CSS::Display const&, NonnullRefPtr<CSS::StyleProperties>, Element*);
  115. void set_pseudo_element_node(Badge<Layout::TreeBuilder>, CSS::Selector::PseudoElement, RefPtr<Layout::Node>);
  116. RefPtr<Layout::Node> get_pseudo_element_node(CSS::Selector::PseudoElement) const;
  117. void clear_pseudo_element_nodes(Badge<Layout::TreeBuilder>);
  118. void serialize_pseudo_elements_as_json(JsonArraySerializer<StringBuilder>& children_array) const;
  119. bool is_actually_disabled() const;
  120. WebIDL::ExceptionOr<JS::GCPtr<Element>> insert_adjacent_element(String const& where, JS::NonnullGCPtr<Element> element);
  121. WebIDL::ExceptionOr<void> insert_adjacent_text(String const& where, String const& data);
  122. // https://w3c.github.io/csswg-drafts/cssom-view-1/#dom-element-scrollintoview
  123. void scroll_into_view(Optional<Variant<bool, ScrollIntoViewOptions>>);
  124. protected:
  125. Element(Document&, DOM::QualifiedName);
  126. virtual void initialize(JS::Realm&) override;
  127. virtual void children_changed() override;
  128. virtual void visit_edges(Cell::Visitor&) override;
  129. private:
  130. void make_html_uppercased_qualified_name();
  131. WebIDL::ExceptionOr<JS::GCPtr<Node>> insert_adjacent(String const& where, JS::NonnullGCPtr<Node> node);
  132. QualifiedName m_qualified_name;
  133. String m_html_uppercased_qualified_name;
  134. JS::GCPtr<NamedNodeMap> m_attributes;
  135. JS::GCPtr<CSS::ElementInlineCSSStyleDeclaration> m_inline_style;
  136. JS::GCPtr<DOMTokenList> m_class_list;
  137. JS::GCPtr<ShadowRoot> m_shadow_root;
  138. RefPtr<CSS::StyleProperties> m_computed_css_values;
  139. HashMap<FlyString, CSS::StyleProperty> m_custom_properties;
  140. Vector<FlyString> m_classes;
  141. Array<RefPtr<Layout::Node>, CSS::Selector::PseudoElementCount> m_pseudo_element_nodes;
  142. };
  143. template<>
  144. inline bool Node::fast_is<Element>() const { return is_element(); }
  145. WebIDL::ExceptionOr<QualifiedName> validate_and_extract(JS::Realm&, FlyString namespace_, FlyString qualified_name);
  146. }