Element.h 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  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. DOM::ExceptionOr<bool> matches(StringView selectors) const;
  49. int client_top() const;
  50. int client_left() const;
  51. int client_width() const;
  52. int client_height() const;
  53. template<typename Callback>
  54. void for_each_attribute(Callback callback) const
  55. {
  56. for (size_t i = 0; i < m_attributes->length(); ++i) {
  57. auto const* attribute = m_attributes->item(i);
  58. callback(attribute->name(), attribute->value());
  59. }
  60. }
  61. bool has_class(const FlyString&, CaseSensitivity = CaseSensitivity::CaseSensitive) const;
  62. const Vector<FlyString>& class_names() const { return m_classes; }
  63. virtual void apply_presentational_hints(CSS::StyleProperties&) const { }
  64. virtual void parse_attribute(const FlyString& name, const String& value);
  65. void recompute_style();
  66. Layout::NodeWithStyle* layout_node() { return static_cast<Layout::NodeWithStyle*>(Node::layout_node()); }
  67. const Layout::NodeWithStyle* layout_node() const { return static_cast<const Layout::NodeWithStyle*>(Node::layout_node()); }
  68. String name() const { return attribute(HTML::AttributeNames::name); }
  69. const CSS::StyleProperties* specified_css_values() const { return m_specified_css_values.ptr(); }
  70. NonnullRefPtr<CSS::StyleProperties> computed_style();
  71. const CSS::CSSStyleDeclaration* inline_style() const { return m_inline_style; }
  72. NonnullRefPtr<CSS::CSSStyleDeclaration> style_for_bindings();
  73. String inner_html() const;
  74. ExceptionOr<void> set_inner_html(String const&);
  75. bool is_focused() const;
  76. virtual bool is_focusable() const { return false; }
  77. bool is_active() const;
  78. NonnullRefPtr<HTMLCollection> get_elements_by_class_name(FlyString const&);
  79. ShadowRoot* shadow_root() { return m_shadow_root; }
  80. const ShadowRoot* shadow_root() const { return m_shadow_root; }
  81. void set_shadow_root(RefPtr<ShadowRoot>);
  82. Optional<CSS::StyleComputer::CustomPropertyResolutionTuple> resolve_custom_property(const String& custom_property_name) const
  83. {
  84. return m_custom_properties.get(custom_property_name);
  85. }
  86. void add_custom_property(const String& custom_property_name, CSS::StyleComputer::CustomPropertyResolutionTuple style_property)
  87. {
  88. m_custom_properties.set(custom_property_name, style_property);
  89. }
  90. void queue_an_element_task(HTML::Task::Source, Function<void()>);
  91. bool is_void_element() const;
  92. bool serializes_as_void() const;
  93. NonnullRefPtr<Geometry::DOMRect> get_bounding_client_rect() const;
  94. protected:
  95. RefPtr<Layout::Node> create_layout_node() override;
  96. virtual void children_changed() override;
  97. private:
  98. void make_html_uppercased_qualified_name();
  99. QualifiedName m_qualified_name;
  100. String m_html_uppercased_qualified_name;
  101. NonnullRefPtr<NamedNodeMap> m_attributes;
  102. RefPtr<CSS::CSSStyleDeclaration> m_inline_style;
  103. RefPtr<CSS::StyleProperties> m_specified_css_values;
  104. HashMap<String, CSS::StyleComputer::CustomPropertyResolutionTuple> m_custom_properties;
  105. Vector<FlyString> m_classes;
  106. RefPtr<ShadowRoot> m_shadow_root;
  107. };
  108. template<>
  109. inline bool Node::fast_is<Element>() const { return is_element(); }
  110. }