Element.h 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  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. private:
  42. RefPtr<LayoutNode> create_layout_node(const StyleProperties* parent_style) const override;
  43. Attribute* find_attribute(const String& name);
  44. const Attribute* find_attribute(const String& name) const;
  45. String m_tag_name;
  46. Vector<Attribute> m_attributes;
  47. };
  48. template<>
  49. inline bool is<Element>(const Node& node)
  50. {
  51. return node.is_element();
  52. }