Element.cpp 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  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. auto display = style->string_or_fallback(CSS::PropertyID::Display, "inline");
  71. if (display == "none")
  72. return nullptr;
  73. if (display == "block")
  74. return adopt(*new LayoutBlock(this, move(style)));
  75. if (display == "inline")
  76. return adopt(*new LayoutInline(*this, move(style)));
  77. if (display == "list-item")
  78. return adopt(*new LayoutListItem(*this, move(style)));
  79. if (display == "table")
  80. return adopt(*new LayoutTable(*this, move(style)));
  81. if (display == "table-row")
  82. return adopt(*new LayoutTableRow(*this, move(style)));
  83. if (display == "table-cell")
  84. return adopt(*new LayoutTableCell(*this, move(style)));
  85. if (display == "inline-block")
  86. return adopt(*new LayoutBlock(this, move(style)));
  87. ASSERT_NOT_REACHED();
  88. }
  89. void Element::parse_attribute(const String&, const String&)
  90. {
  91. }
  92. enum class StyleDifference {
  93. None,
  94. NeedsRepaint,
  95. NeedsRelayout,
  96. };
  97. static StyleDifference compute_style_difference(const StyleProperties& old_style, const StyleProperties& new_style, const Document& document)
  98. {
  99. if (old_style == new_style)
  100. return StyleDifference::None;
  101. bool needs_repaint = false;
  102. bool needs_relayout = false;
  103. if (new_style.color_or_fallback(CSS::PropertyID::Color, document, Color::Black) != old_style.color_or_fallback(CSS::PropertyID::Color, document, Color::Black))
  104. needs_repaint = true;
  105. else if (new_style.color_or_fallback(CSS::PropertyID::BackgroundColor, document, Color::Black) != old_style.color_or_fallback(CSS::PropertyID::BackgroundColor, document, Color::Black))
  106. needs_repaint = true;
  107. if (needs_relayout)
  108. return StyleDifference::NeedsRelayout;
  109. if (needs_repaint)
  110. return StyleDifference::NeedsRepaint;
  111. return StyleDifference::None;
  112. }
  113. void Element::recompute_style()
  114. {
  115. set_needs_style_update(false);
  116. ASSERT(parent());
  117. auto* parent_layout_node = parent()->layout_node();
  118. if (!parent_layout_node)
  119. return;
  120. ASSERT(parent_layout_node);
  121. auto style = document().style_resolver().resolve_style(*this, &parent_layout_node->style());
  122. if (!layout_node()) {
  123. if (style->string_or_fallback(CSS::PropertyID::Display, "inline") == "none")
  124. return;
  125. // We need a new layout tree here!
  126. LayoutTreeBuilder tree_builder;
  127. tree_builder.build(*this);
  128. return;
  129. }
  130. auto diff = compute_style_difference(layout_node()->style(), *style, document());
  131. if (diff == StyleDifference::None)
  132. return;
  133. layout_node()->set_style(*style);
  134. if (diff == StyleDifference::NeedsRelayout) {
  135. ASSERT_NOT_REACHED();
  136. }
  137. if (diff == StyleDifference::NeedsRepaint) {
  138. layout_node()->set_needs_display();
  139. }
  140. }