Element.cpp 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  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.empend(name, value);
  40. }
  41. void Element::set_attributes(Vector<Attribute>&& attributes)
  42. {
  43. m_attributes = move(attributes);
  44. }
  45. bool Element::has_class(const StringView& class_name) const
  46. {
  47. auto value = attribute("class");
  48. if (value.is_empty())
  49. return false;
  50. auto parts = value.split_view(' ');
  51. for (auto& part : parts) {
  52. if (part == class_name)
  53. return true;
  54. }
  55. return false;
  56. }