Element.cpp 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  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. parse_attribute(name, value);
  42. }
  43. void Element::set_attributes(Vector<Attribute>&& attributes)
  44. {
  45. m_attributes = move(attributes);
  46. for (auto& attribute : m_attributes)
  47. parse_attribute(attribute.name(), attribute.value());
  48. }
  49. bool Element::has_class(const StringView& class_name) const
  50. {
  51. auto value = attribute("class");
  52. if (value.is_empty())
  53. return false;
  54. auto parts = value.split_view(' ');
  55. for (auto& part : parts) {
  56. if (part == class_name)
  57. return true;
  58. }
  59. return false;
  60. }
  61. RefPtr<LayoutNode> Element::create_layout_node(const StyleResolver& resolver, const StyleProperties* parent_style) const
  62. {
  63. auto style = resolver.resolve_style(*this, parent_style);
  64. auto display_property = style->property(CSS::PropertyID::Display);
  65. String display = display_property.has_value() ? display_property.release_value()->to_string() : "inline";
  66. if (display == "none")
  67. return nullptr;
  68. if (display == "block" || display == "list-item")
  69. return adopt(*new LayoutBlock(this, move(style)));
  70. if (display == "inline")
  71. return adopt(*new LayoutInline(*this, move(style)));
  72. ASSERT_NOT_REACHED();
  73. }
  74. void Element::parse_attribute(const String&, const String&)
  75. {
  76. }