Element.h 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  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/CSS/CSSStyleDeclaration.h>
  10. #include <LibWeb/CSS/StyleComputer.h>
  11. #include <LibWeb/DOM/Attribute.h>
  12. #include <LibWeb/DOM/ChildNode.h>
  13. #include <LibWeb/DOM/ExceptionOr.h>
  14. #include <LibWeb/DOM/NamedNodeMap.h>
  15. #include <LibWeb/DOM/NonDocumentTypeChildNode.h>
  16. #include <LibWeb/DOM/ParentNode.h>
  17. #include <LibWeb/HTML/AttributeNames.h>
  18. #include <LibWeb/HTML/EventLoop/Task.h>
  19. #include <LibWeb/HTML/TagNames.h>
  20. #include <LibWeb/Layout/Node.h>
  21. #include <LibWeb/QualifiedName.h>
  22. namespace Web::DOM {
  23. class Element
  24. : public ParentNode
  25. , public ChildNode<Element>
  26. , public NonDocumentTypeChildNode<Element> {
  27. public:
  28. using WrapperType = Bindings::ElementWrapper;
  29. Element(Document&, QualifiedName);
  30. virtual ~Element() override;
  31. const String& qualified_name() const { return m_qualified_name.as_string(); }
  32. const String& html_uppercased_qualified_name() const { return m_html_uppercased_qualified_name; }
  33. virtual FlyString node_name() const final { return html_uppercased_qualified_name(); }
  34. const FlyString& local_name() const { return m_qualified_name.local_name(); }
  35. // NOTE: This is for the JS bindings
  36. const String& tag_name() const { return html_uppercased_qualified_name(); }
  37. const FlyString& prefix() const { return m_qualified_name.prefix(); }
  38. const FlyString& namespace_() const { return m_qualified_name.namespace_(); }
  39. // NOTE: This is for the JS bindings
  40. const FlyString& namespace_uri() const { return namespace_(); }
  41. bool has_attribute(const FlyString& name) const;
  42. bool has_attributes() const { return !m_attributes->is_empty(); }
  43. String attribute(const FlyString& name) const { return get_attribute(name); }
  44. String get_attribute(const FlyString& name) const;
  45. ExceptionOr<void> set_attribute(const FlyString& name, const String& value);
  46. void remove_attribute(const FlyString& name);
  47. size_t attribute_list_size() const { return m_attributes->length(); }
  48. NonnullRefPtr<NamedNodeMap> const& attributes() const { return m_attributes; }
  49. DOM::ExceptionOr<bool> matches(StringView selectors) const;
  50. int client_top() const;
  51. int client_left() const;
  52. int client_width() const;
  53. int client_height() const;
  54. template<typename Callback>
  55. void for_each_attribute(Callback callback) const
  56. {
  57. for (size_t i = 0; i < m_attributes->length(); ++i) {
  58. auto const* attribute = m_attributes->item(i);
  59. callback(attribute->name(), attribute->value());
  60. }
  61. }
  62. bool has_class(const FlyString&, CaseSensitivity = CaseSensitivity::CaseSensitive) const;
  63. const Vector<FlyString>& class_names() const { return m_classes; }
  64. virtual void apply_presentational_hints(CSS::StyleProperties&) const { }
  65. virtual void parse_attribute(const FlyString& name, const String& value);
  66. void recompute_style();
  67. Layout::NodeWithStyle* layout_node() { return static_cast<Layout::NodeWithStyle*>(Node::layout_node()); }
  68. const Layout::NodeWithStyle* layout_node() const { return static_cast<const Layout::NodeWithStyle*>(Node::layout_node()); }
  69. String name() const { return attribute(HTML::AttributeNames::name); }
  70. const CSS::StyleProperties* specified_css_values() const { return m_specified_css_values.ptr(); }
  71. NonnullRefPtr<CSS::StyleProperties> computed_style();
  72. const CSS::CSSStyleDeclaration* inline_style() const { return m_inline_style; }
  73. NonnullRefPtr<CSS::CSSStyleDeclaration> style_for_bindings();
  74. String inner_html() const;
  75. ExceptionOr<void> set_inner_html(String const&);
  76. bool is_focused() const;
  77. virtual bool is_focusable() const { return false; }
  78. bool is_active() const;
  79. NonnullRefPtr<HTMLCollection> get_elements_by_class_name(FlyString const&);
  80. ShadowRoot* shadow_root() { return m_shadow_root; }
  81. const ShadowRoot* shadow_root() const { return m_shadow_root; }
  82. void set_shadow_root(RefPtr<ShadowRoot>);
  83. Optional<CSS::StyleComputer::CustomPropertyResolutionTuple> resolve_custom_property(const String& custom_property_name) const
  84. {
  85. return m_custom_properties.get(custom_property_name);
  86. }
  87. void add_custom_property(const String& custom_property_name, CSS::StyleComputer::CustomPropertyResolutionTuple style_property)
  88. {
  89. m_custom_properties.set(custom_property_name, style_property);
  90. }
  91. void queue_an_element_task(HTML::Task::Source, Function<void()>);
  92. bool is_void_element() const;
  93. bool serializes_as_void() const;
  94. NonnullRefPtr<Geometry::DOMRect> get_bounding_client_rect() const;
  95. protected:
  96. RefPtr<Layout::Node> create_layout_node() override;
  97. virtual void children_changed() override;
  98. private:
  99. void make_html_uppercased_qualified_name();
  100. QualifiedName m_qualified_name;
  101. String m_html_uppercased_qualified_name;
  102. NonnullRefPtr<NamedNodeMap> m_attributes;
  103. RefPtr<CSS::CSSStyleDeclaration> m_inline_style;
  104. RefPtr<CSS::StyleProperties> m_specified_css_values;
  105. HashMap<String, CSS::StyleComputer::CustomPropertyResolutionTuple> m_custom_properties;
  106. Vector<FlyString> m_classes;
  107. RefPtr<ShadowRoot> m_shadow_root;
  108. };
  109. template<>
  110. inline bool Node::fast_is<Element>() const { return is_element(); }
  111. }