Element.cpp 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. #include <LibHTML/CSS/StyleResolver.h>
  2. #include <LibHTML/DOM/Document.h>
  3. #include <LibHTML/DOM/Element.h>
  4. #include <LibHTML/Layout/LayoutBlock.h>
  5. #include <LibHTML/Layout/LayoutInline.h>
  6. #include <LibHTML/Layout/LayoutListItem.h>
  7. Element::Element(Document& document, const String& tag_name)
  8. : ParentNode(document, NodeType::ELEMENT_NODE)
  9. , m_tag_name(tag_name)
  10. {
  11. }
  12. Element::~Element()
  13. {
  14. }
  15. Attribute* Element::find_attribute(const String& name)
  16. {
  17. for (auto& attribute : m_attributes) {
  18. if (attribute.name() == name)
  19. return &attribute;
  20. }
  21. return nullptr;
  22. }
  23. const Attribute* Element::find_attribute(const String& name) const
  24. {
  25. for (auto& attribute : m_attributes) {
  26. if (attribute.name() == name)
  27. return &attribute;
  28. }
  29. return nullptr;
  30. }
  31. String Element::attribute(const String& name) const
  32. {
  33. if (auto* attribute = find_attribute(name))
  34. return attribute->value();
  35. return {};
  36. }
  37. void Element::set_attribute(const String& name, const String& value)
  38. {
  39. if (auto* attribute = find_attribute(name))
  40. attribute->set_value(value);
  41. else
  42. m_attributes.empend(name, value);
  43. parse_attribute(name, value);
  44. }
  45. void Element::set_attributes(Vector<Attribute>&& attributes)
  46. {
  47. m_attributes = move(attributes);
  48. for (auto& attribute : m_attributes)
  49. parse_attribute(attribute.name(), attribute.value());
  50. }
  51. bool Element::has_class(const StringView& class_name) const
  52. {
  53. auto value = attribute("class");
  54. if (value.is_empty())
  55. return false;
  56. auto parts = value.split_view(' ');
  57. for (auto& part : parts) {
  58. if (part == class_name)
  59. return true;
  60. }
  61. return false;
  62. }
  63. RefPtr<LayoutNode> Element::create_layout_node(const StyleProperties* parent_style) const
  64. {
  65. auto style = document().style_resolver().resolve_style(*this, parent_style);
  66. auto display = style->string_or_fallback(CSS::PropertyID::Display, "inline");
  67. if (display == "none")
  68. return nullptr;
  69. if (display == "block")
  70. return adopt(*new LayoutBlock(this, move(style)));
  71. if (display == "inline")
  72. return adopt(*new LayoutInline(*this, move(style)));
  73. if (display == "list-item")
  74. return adopt(*new LayoutListItem(*this, move(style)));
  75. ASSERT_NOT_REACHED();
  76. }
  77. void Element::parse_attribute(const String&, const String&)
  78. {
  79. }
  80. enum class StyleDifference {
  81. None,
  82. NeedsRepaint,
  83. NeedsRelayout,
  84. };
  85. static StyleDifference compute_style_difference(const StyleProperties& old_style, const StyleProperties& new_style, const Document& document)
  86. {
  87. if (old_style == new_style)
  88. return StyleDifference::None;
  89. bool needs_repaint = false;
  90. bool needs_relayout = false;
  91. if (new_style.color_or_fallback(CSS::PropertyID::Color, document, Color::Black) != old_style.color_or_fallback(CSS::PropertyID::Color, document, Color::Black))
  92. needs_repaint = true;
  93. else if (new_style.color_or_fallback(CSS::PropertyID::BackgroundColor, document, Color::Black) != old_style.color_or_fallback(CSS::PropertyID::BackgroundColor, document, Color::Black))
  94. needs_repaint = true;
  95. if (needs_relayout)
  96. return StyleDifference::NeedsRelayout;
  97. if (needs_repaint)
  98. return StyleDifference::NeedsRepaint;
  99. return StyleDifference::None;
  100. }
  101. void Element::recompute_style()
  102. {
  103. ASSERT(parent());
  104. auto* parent_layout_node = parent()->layout_node();
  105. ASSERT(parent_layout_node);
  106. auto style = document().style_resolver().resolve_style(*this, &parent_layout_node->style());
  107. ASSERT(layout_node());
  108. auto diff = compute_style_difference(layout_node()->style(), *style, document());
  109. if (diff == StyleDifference::None)
  110. return;
  111. layout_node()->set_style(*style);
  112. if (diff == StyleDifference::NeedsRelayout) {
  113. ASSERT_NOT_REACHED();
  114. }
  115. if (diff == StyleDifference::NeedsRepaint) {
  116. layout_node()->set_needs_display();
  117. }
  118. }