Element.h 4.9 KB

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