Element.h 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. /*
  2. * Copyright (c) 2018-2021, Andreas Kling <kling@serenityos.org>
  3. * All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. *
  8. * 1. Redistributions of source code must retain the above copyright notice, this
  9. * list of conditions and the following disclaimer.
  10. *
  11. * 2. Redistributions in binary form must reproduce the above copyright notice,
  12. * this list of conditions and the following disclaimer in the documentation
  13. * and/or other materials provided with the distribution.
  14. *
  15. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  16. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  17. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  18. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  19. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  20. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  21. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  22. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  23. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  24. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  25. */
  26. #pragma once
  27. #include <AK/FlyString.h>
  28. #include <AK/String.h>
  29. #include <LibWeb/DOM/Attribute.h>
  30. #include <LibWeb/DOM/ExceptionOr.h>
  31. #include <LibWeb/DOM/NonDocumentTypeChildNode.h>
  32. #include <LibWeb/DOM/ParentNode.h>
  33. #include <LibWeb/HTML/AttributeNames.h>
  34. #include <LibWeb/HTML/TagNames.h>
  35. #include <LibWeb/Layout/Node.h>
  36. #include <LibWeb/QualifiedName.h>
  37. namespace Web::DOM {
  38. class Element
  39. : public ParentNode
  40. , public NonDocumentTypeChildNode<Element> {
  41. public:
  42. using WrapperType = Bindings::ElementWrapper;
  43. Element(Document&, QualifiedName);
  44. virtual ~Element() override;
  45. virtual FlyString node_name() const final { return m_qualified_name.local_name(); }
  46. const FlyString& local_name() const { return m_qualified_name.local_name(); }
  47. // NOTE: This is for the JS bindings
  48. const FlyString& tag_name() const { return local_name(); }
  49. const FlyString& namespace_() const { return m_qualified_name.namespace_(); }
  50. // NOTE: This is for the JS bindings
  51. const FlyString& namespace_uri() const { return namespace_(); }
  52. bool has_attribute(const FlyString& name) const { return !attribute(name).is_null(); }
  53. bool has_attributes() const { return !m_attributes.is_empty(); }
  54. String attribute(const FlyString& name) const;
  55. String get_attribute(const FlyString& name) const { return attribute(name); }
  56. ExceptionOr<void> set_attribute(const FlyString& name, const String& value);
  57. void remove_attribute(const FlyString& name);
  58. template<typename Callback>
  59. void for_each_attribute(Callback callback) const
  60. {
  61. for (auto& attribute : m_attributes)
  62. callback(attribute.name(), attribute.value());
  63. }
  64. bool has_class(const FlyString&, CaseSensitivity = CaseSensitivity::CaseSensitive) const;
  65. const Vector<FlyString>& class_names() const { return m_classes; }
  66. virtual void apply_presentational_hints(CSS::StyleProperties&) const { }
  67. virtual void parse_attribute(const FlyString& name, const String& value);
  68. void recompute_style();
  69. Layout::NodeWithStyle* layout_node() { return static_cast<Layout::NodeWithStyle*>(Node::layout_node()); }
  70. const Layout::NodeWithStyle* layout_node() const { return static_cast<const Layout::NodeWithStyle*>(Node::layout_node()); }
  71. String name() const { return attribute(HTML::AttributeNames::name); }
  72. const CSS::StyleProperties* specified_css_values() const { return m_specified_css_values.ptr(); }
  73. NonnullRefPtr<CSS::StyleProperties> computed_style();
  74. const CSS::CSSStyleDeclaration* inline_style() const { return m_inline_style; }
  75. NonnullRefPtr<CSS::CSSStyleDeclaration> style_for_bindings();
  76. // FIXME: innerHTML also appears on shadow roots. https://w3c.github.io/DOM-Parsing/#dom-innerhtml
  77. String inner_html() const;
  78. void set_inner_html(StringView);
  79. bool is_focused() const;
  80. virtual bool is_focusable() const { return false; }
  81. NonnullRefPtrVector<Element> get_elements_by_tag_name(const FlyString&) const;
  82. NonnullRefPtrVector<Element> get_elements_by_class_name(const FlyString&) const;
  83. ShadowRoot* shadow_root() { return m_shadow_root; }
  84. const ShadowRoot* shadow_root() const { return m_shadow_root; }
  85. void set_shadow_root(RefPtr<ShadowRoot>);
  86. protected:
  87. RefPtr<Layout::Node> create_layout_node() override;
  88. private:
  89. Attribute* find_attribute(const FlyString& name);
  90. const Attribute* find_attribute(const FlyString& name) const;
  91. QualifiedName m_qualified_name;
  92. Vector<Attribute> m_attributes;
  93. RefPtr<CSS::CSSStyleDeclaration> m_inline_style;
  94. RefPtr<CSS::StyleProperties> m_specified_css_values;
  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. }