Element.h 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. #pragma once
  2. #include <AK/String.h>
  3. #include <LibHTML/DOM/ParentNode.h>
  4. #include <LibHTML/Layout/LayoutNode.h>
  5. class LayoutNodeWithStyle;
  6. class Attribute {
  7. public:
  8. Attribute(const String& name, const String& value)
  9. : m_name(name)
  10. , m_value(value)
  11. {
  12. }
  13. const String& name() const { return m_name; }
  14. const String& value() const { return m_value; }
  15. void set_value(const String& value) { m_value = value; }
  16. private:
  17. String m_name;
  18. String m_value;
  19. };
  20. class Element : public ParentNode {
  21. public:
  22. Element(Document&, const String& tag_name);
  23. virtual ~Element() override;
  24. virtual String tag_name() const final { return m_tag_name; }
  25. bool has_attribute(const String& name) const { return !attribute(name).is_null(); }
  26. String attribute(const String& name) const;
  27. void set_attribute(const String& name, const String& value);
  28. void set_attributes(Vector<Attribute>&&);
  29. template<typename Callback>
  30. void for_each_attribute(Callback callback) const
  31. {
  32. for (auto& attribute : m_attributes)
  33. callback(attribute.name(), attribute.value());
  34. }
  35. bool has_class(const StringView&) const;
  36. virtual void apply_presentational_hints(StyleProperties&) const {}
  37. virtual void parse_attribute(const String& name, const String& value);
  38. void recompute_style();
  39. LayoutNodeWithStyle* layout_node() { return static_cast<LayoutNodeWithStyle*>(Node::layout_node()); }
  40. const LayoutNodeWithStyle* layout_node() const { return static_cast<const LayoutNodeWithStyle*>(Node::layout_node()); }
  41. String name() const { return attribute("name"); }
  42. const StyleProperties* resolved_style() const { return m_resolved_style.ptr(); }
  43. RefPtr<StyleProperties> computed_style();
  44. private:
  45. RefPtr<LayoutNode> create_layout_node(const StyleProperties* parent_style) const override;
  46. Attribute* find_attribute(const String& name);
  47. const Attribute* find_attribute(const String& name) const;
  48. String m_tag_name;
  49. Vector<Attribute> m_attributes;
  50. RefPtr<StyleProperties> m_resolved_style;
  51. };
  52. template<>
  53. inline bool is<Element>(const Node& node)
  54. {
  55. return node.is_element();
  56. }