Element.h 2.0 KB

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