Element.cpp 5.7 KB

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