Element.h 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  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/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. template<typename Callback>
  45. void for_each_attribute(Callback callback) const
  46. {
  47. for (auto& attribute : m_attributes)
  48. callback(attribute.name(), attribute.value());
  49. }
  50. bool has_class(const FlyString&, CaseSensitivity = CaseSensitivity::CaseSensitive) const;
  51. const Vector<FlyString>& class_names() const { return m_classes; }
  52. virtual void apply_presentational_hints(CSS::StyleProperties&) const { }
  53. virtual void parse_attribute(const FlyString& name, const String& value);
  54. void recompute_style();
  55. Layout::NodeWithStyle* layout_node() { return static_cast<Layout::NodeWithStyle*>(Node::layout_node()); }
  56. const Layout::NodeWithStyle* layout_node() const { return static_cast<const Layout::NodeWithStyle*>(Node::layout_node()); }
  57. String name() const { return attribute(HTML::AttributeNames::name); }
  58. const CSS::StyleProperties* specified_css_values() const { return m_specified_css_values.ptr(); }
  59. NonnullRefPtr<CSS::StyleProperties> computed_style();
  60. const CSS::CSSStyleDeclaration* inline_style() const { return m_inline_style; }
  61. NonnullRefPtr<CSS::CSSStyleDeclaration> style_for_bindings();
  62. // FIXME: innerHTML also appears on shadow roots. https://w3c.github.io/DOM-Parsing/#dom-innerhtml
  63. String inner_html() const;
  64. void set_inner_html(StringView);
  65. bool is_focused() const;
  66. virtual bool is_focusable() const { return false; }
  67. bool is_active() const;
  68. NonnullRefPtr<HTMLCollection> get_elements_by_tag_name(FlyString const&);
  69. NonnullRefPtr<HTMLCollection> get_elements_by_class_name(FlyString const&);
  70. ShadowRoot* shadow_root() { return m_shadow_root; }
  71. const ShadowRoot* shadow_root() const { return m_shadow_root; }
  72. void set_shadow_root(RefPtr<ShadowRoot>);
  73. Optional<CSS::StyleResolver::CustomPropertyResolutionTuple> resolve_custom_property(const String& custom_property_name)
  74. {
  75. return m_custom_properties.get(custom_property_name);
  76. }
  77. void add_custom_property(const String& custom_property_name, CSS::StyleResolver::CustomPropertyResolutionTuple style_property)
  78. {
  79. m_custom_properties.set(custom_property_name, style_property);
  80. }
  81. void queue_an_element_task(HTML::Task::Source, Function<void()>);
  82. protected:
  83. RefPtr<Layout::Node> create_layout_node() override;
  84. private:
  85. Attribute* find_attribute(const FlyString& name);
  86. const Attribute* find_attribute(const FlyString& name) const;
  87. void make_html_uppercased_qualified_name();
  88. QualifiedName m_qualified_name;
  89. String m_html_uppercased_qualified_name;
  90. Vector<Attribute> m_attributes;
  91. RefPtr<CSS::CSSStyleDeclaration> m_inline_style;
  92. RefPtr<CSS::StyleProperties> m_specified_css_values;
  93. HashMap<String, CSS::StyleResolver::CustomPropertyResolutionTuple> m_custom_properties;
  94. Vector<FlyString> m_classes;
  95. RefPtr<ShadowRoot> m_shadow_root;
  96. };
  97. template<>
  98. inline bool Node::fast_is<Element>() const { return is_element(); }
  99. }