Element.cpp 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. #include <LibHTML/DOM/Element.h>
  2. #include <LibHTML/Layout/LayoutBlock.h>
  3. #include <LibHTML/Layout/LayoutInline.h>
  4. Element::Element(const String& tag_name)
  5. : ParentNode(NodeType::ELEMENT_NODE)
  6. , m_tag_name(tag_name)
  7. {
  8. }
  9. Element::~Element()
  10. {
  11. }
  12. Attribute* Element::find_attribute(const String& name)
  13. {
  14. for (auto& attribute : m_attributes) {
  15. if (attribute.name() == name)
  16. return &attribute;
  17. }
  18. return nullptr;
  19. }
  20. const Attribute* Element::find_attribute(const String& name) const
  21. {
  22. for (auto& attribute : m_attributes) {
  23. if (attribute.name() == name)
  24. return &attribute;
  25. }
  26. return nullptr;
  27. }
  28. String Element::attribute(const String& name) const
  29. {
  30. if (auto* attribute = find_attribute(name))
  31. return attribute->value();
  32. return { };
  33. }
  34. void Element::set_attribute(const String& name, const String& value)
  35. {
  36. if (auto* attribute = find_attribute(name))
  37. attribute->set_value(value);
  38. else
  39. m_attributes.append({ name, value });
  40. }
  41. void Element::set_attributes(Vector<Attribute>&& attributes)
  42. {
  43. m_attributes = move(attributes);
  44. }
  45. RetainPtr<LayoutNode> Element::create_layout_node()
  46. {
  47. if (m_tag_name == "html")
  48. return adopt(*new LayoutBlock(*this));
  49. if (m_tag_name == "body")
  50. return adopt(*new LayoutBlock(*this));
  51. if (m_tag_name == "h1")
  52. return adopt(*new LayoutBlock(*this));
  53. if (m_tag_name == "p")
  54. return adopt(*new LayoutBlock(*this));
  55. if (m_tag_name == "b")
  56. return adopt(*new LayoutInline(*this));
  57. return nullptr;
  58. }