Element.cpp 4.8 KB

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