Element.cpp 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  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. ASSERT_NOT_REACHED();
  86. }
  87. void Element::parse_attribute(const String&, const String&)
  88. {
  89. }
  90. enum class StyleDifference {
  91. None,
  92. NeedsRepaint,
  93. NeedsRelayout,
  94. };
  95. static StyleDifference compute_style_difference(const StyleProperties& old_style, const StyleProperties& new_style, const Document& document)
  96. {
  97. if (old_style == new_style)
  98. return StyleDifference::None;
  99. bool needs_repaint = false;
  100. bool needs_relayout = false;
  101. if (new_style.color_or_fallback(CSS::PropertyID::Color, document, Color::Black) != old_style.color_or_fallback(CSS::PropertyID::Color, document, Color::Black))
  102. needs_repaint = true;
  103. else if (new_style.color_or_fallback(CSS::PropertyID::BackgroundColor, document, Color::Black) != old_style.color_or_fallback(CSS::PropertyID::BackgroundColor, document, Color::Black))
  104. needs_repaint = true;
  105. if (needs_relayout)
  106. return StyleDifference::NeedsRelayout;
  107. if (needs_repaint)
  108. return StyleDifference::NeedsRepaint;
  109. return StyleDifference::None;
  110. }
  111. void Element::recompute_style()
  112. {
  113. set_needs_style_update(false);
  114. ASSERT(parent());
  115. auto* parent_layout_node = parent()->layout_node();
  116. if (!parent_layout_node)
  117. return;
  118. ASSERT(parent_layout_node);
  119. auto style = document().style_resolver().resolve_style(*this, &parent_layout_node->style());
  120. if (!layout_node()) {
  121. if (style->string_or_fallback(CSS::PropertyID::Display, "inline") == "none")
  122. return;
  123. // We need a new layout tree here!
  124. LayoutTreeBuilder tree_builder;
  125. tree_builder.build(*this);
  126. return;
  127. }
  128. auto diff = compute_style_difference(layout_node()->style(), *style, document());
  129. if (diff == StyleDifference::None)
  130. return;
  131. layout_node()->set_style(*style);
  132. if (diff == StyleDifference::NeedsRelayout) {
  133. ASSERT_NOT_REACHED();
  134. }
  135. if (diff == StyleDifference::NeedsRepaint) {
  136. layout_node()->set_needs_display();
  137. }
  138. }