Element.h 4.8 KB

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