Element.h 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  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. String attribute(const String& name) const;
  26. void set_attribute(const String& name, const String& value);
  27. void set_attributes(Vector<Attribute>&&);
  28. template<typename Callback>
  29. void for_each_attribute(Callback callback) const
  30. {
  31. for (auto& attribute : m_attributes)
  32. callback(attribute.name(), attribute.value());
  33. }
  34. bool has_class(const StringView&) const;
  35. virtual void apply_presentational_hints(StyleProperties&) const {}
  36. virtual void parse_attribute(const String& name, const String& value);
  37. void recompute_style();
  38. LayoutNodeWithStyle* layout_node() { return static_cast<LayoutNodeWithStyle*>(Node::layout_node()); }
  39. const LayoutNodeWithStyle* layout_node() const { return static_cast<const LayoutNodeWithStyle*>(Node::layout_node()); }
  40. private:
  41. RefPtr<LayoutNode> create_layout_node(const StyleProperties* parent_style) const override;
  42. Attribute* find_attribute(const String& name);
  43. const Attribute* find_attribute(const String& name) const;
  44. String m_tag_name;
  45. Vector<Attribute> m_attributes;
  46. };
  47. template<>
  48. inline bool is<Element>(const Node& node)
  49. {
  50. return node.is_element();
  51. }