Element.cpp 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. #include <LibHTML/CSS/StyleResolver.h>
  2. #include <LibHTML/DOM/Element.h>
  3. #include <LibHTML/Layout/LayoutBlock.h>
  4. #include <LibHTML/Layout/LayoutInline.h>
  5. Element::Element(Document& document, const String& tag_name)
  6. : ParentNode(document, NodeType::ELEMENT_NODE)
  7. , m_tag_name(tag_name)
  8. {
  9. }
  10. Element::~Element()
  11. {
  12. }
  13. Attribute* Element::find_attribute(const String& name)
  14. {
  15. for (auto& attribute : m_attributes) {
  16. if (attribute.name() == name)
  17. return &attribute;
  18. }
  19. return nullptr;
  20. }
  21. const Attribute* Element::find_attribute(const String& name) const
  22. {
  23. for (auto& attribute : m_attributes) {
  24. if (attribute.name() == name)
  25. return &attribute;
  26. }
  27. return nullptr;
  28. }
  29. String Element::attribute(const String& name) const
  30. {
  31. if (auto* attribute = find_attribute(name))
  32. return attribute->value();
  33. return {};
  34. }
  35. void Element::set_attribute(const String& name, const String& value)
  36. {
  37. if (auto* attribute = find_attribute(name))
  38. attribute->set_value(value);
  39. else
  40. m_attributes.empend(name, value);
  41. }
  42. void Element::set_attributes(Vector<Attribute>&& attributes)
  43. {
  44. m_attributes = move(attributes);
  45. }
  46. bool Element::has_class(const StringView& class_name) const
  47. {
  48. auto value = attribute("class");
  49. if (value.is_empty())
  50. return false;
  51. auto parts = value.split_view(' ');
  52. for (auto& part : parts) {
  53. if (part == class_name)
  54. return true;
  55. }
  56. return false;
  57. }
  58. RefPtr<LayoutNode> Element::create_layout_node(const StyleResolver& resolver, const StyleProperties* parent_properties) const
  59. {
  60. auto style_properties = resolver.resolve_style(*this, parent_properties);
  61. auto display_property = style_properties->property("display");
  62. String display = display_property.has_value() ? display_property.release_value()->to_string() : "inline";
  63. if (display == "none")
  64. return nullptr;
  65. if (display == "block" || display == "list-item")
  66. return adopt(*new LayoutBlock(this, move(style_properties)));
  67. if (display == "inline")
  68. return adopt(*new LayoutInline(*this, move(style_properties)));
  69. ASSERT_NOT_REACHED();
  70. }