Element.h 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. /*
  2. * Copyright (c) 2018-2020, 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/AttributeNames.h>
  31. #include <LibWeb/DOM/ParentNode.h>
  32. #include <LibWeb/DOM/TagNames.h>
  33. #include <LibWeb/Layout/LayoutNode.h>
  34. namespace Web {
  35. class LayoutNodeWithStyle;
  36. class Element : public ParentNode {
  37. public:
  38. using WrapperType = Bindings::ElementWrapper;
  39. Element(Document&, const FlyString& tag_name);
  40. virtual ~Element() override;
  41. virtual FlyString tag_name() const final { return m_tag_name; }
  42. bool has_attribute(const FlyString& name) const { return !attribute(name).is_null(); }
  43. String attribute(const FlyString& name) const;
  44. void set_attribute(const FlyString& name, const String& value);
  45. void set_attributes(Vector<Attribute>&&);
  46. template<typename Callback>
  47. void for_each_attribute(Callback callback) const
  48. {
  49. for (auto& attribute : m_attributes)
  50. callback(attribute.name(), attribute.value());
  51. }
  52. bool has_class(const FlyString&) const;
  53. const Vector<FlyString>& class_names() const { return m_classes; }
  54. virtual void apply_presentational_hints(StyleProperties&) const { }
  55. virtual void parse_attribute(const FlyString& name, const String& value);
  56. void recompute_style();
  57. LayoutNodeWithStyle* layout_node() { return static_cast<LayoutNodeWithStyle*>(Node::layout_node()); }
  58. const LayoutNodeWithStyle* layout_node() const { return static_cast<const LayoutNodeWithStyle*>(Node::layout_node()); }
  59. String name() const { return attribute(HTML::AttributeNames::name); }
  60. const StyleProperties* resolved_style() const { return m_resolved_style.ptr(); }
  61. NonnullRefPtr<StyleProperties> computed_style();
  62. String inner_html() const;
  63. void set_inner_html(StringView);
  64. protected:
  65. RefPtr<LayoutNode> create_layout_node(const StyleProperties* parent_style) const override;
  66. private:
  67. Attribute* find_attribute(const FlyString& name);
  68. const Attribute* find_attribute(const FlyString& name) const;
  69. FlyString m_tag_name;
  70. Vector<Attribute> m_attributes;
  71. RefPtr<StyleProperties> m_resolved_style;
  72. Vector<FlyString> m_classes;
  73. };
  74. template<>
  75. inline bool is<Element>(const Node& node)
  76. {
  77. return node.is_element();
  78. }
  79. }