Element.cpp 4.3 KB

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