LayoutBlock.cpp 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. #include <LibHTML/DOM/Element.h>
  2. #include <LibHTML/Layout/LayoutBlock.h>
  3. LayoutBlock::LayoutBlock(const Node* node, StyleProperties&& style_properties)
  4. : LayoutNode(node, move(style_properties))
  5. {
  6. }
  7. LayoutBlock::~LayoutBlock()
  8. {
  9. }
  10. LayoutNode& LayoutBlock::inline_wrapper()
  11. {
  12. if (!last_child() || !last_child()->is_block() || last_child()->node() != nullptr) {
  13. append_child(adopt(*new LayoutBlock(nullptr, {})));
  14. }
  15. return *last_child();
  16. }
  17. void LayoutBlock::layout()
  18. {
  19. compute_width();
  20. compute_position();
  21. int content_height = 0;
  22. for_each_child([&](auto& child) {
  23. child.layout();
  24. content_height = child.rect().bottom() + child.style().full_margin().bottom - rect().top();
  25. });
  26. rect().set_height(content_height);
  27. compute_height();
  28. }
  29. void LayoutBlock::compute_width()
  30. {
  31. auto& style_properties = this->style_properties();
  32. auto auto_value = Length();
  33. auto zero_value = Length(0, Length::Type::Absolute);
  34. auto width = style_properties.length_or_fallback("width", auto_value);
  35. auto margin_left = style_properties.length_or_fallback("margin-left", zero_value);
  36. auto margin_right = style_properties.length_or_fallback("margin-right", zero_value);
  37. auto border_left = style_properties.length_or_fallback("border-left", zero_value);
  38. auto border_right = style_properties.length_or_fallback("border-right", zero_value);
  39. auto padding_left = style_properties.length_or_fallback("padding-left", zero_value);
  40. auto padding_right = style_properties.length_or_fallback("padding-right", zero_value);
  41. dbg() << " Left: " << margin_left << "+" << border_left << "+" << padding_left;
  42. dbg() << "Right: " << margin_right << "+" << border_right << "+" << padding_right;
  43. int total_px = 0;
  44. for (auto& value : { margin_left, border_left, padding_left, width, padding_right, border_right, margin_right }) {
  45. total_px += value.to_px();
  46. }
  47. dbg() << "Total: " << total_px;
  48. // 10.3.3 Block-level, non-replaced elements in normal flow
  49. // If 'width' is not 'auto' and 'border-left-width' + 'padding-left' + 'width' + 'padding-right' + 'border-right-width' (plus any of 'margin-left' or 'margin-right' that are not 'auto') is larger than the width of the containing block, then any 'auto' values for 'margin-left' or 'margin-right' are, for the following rules, treated as zero.
  50. if (width.is_auto() && total_px > containing_block()->rect().width()) {
  51. if (margin_left.is_auto())
  52. margin_left = zero_value;
  53. if (margin_right.is_auto())
  54. margin_right = zero_value;
  55. }
  56. // 10.3.3 cont'd.
  57. auto underflow_px = containing_block()->rect().width() - total_px;
  58. if (width.is_auto()) {
  59. if (margin_left.is_auto())
  60. margin_left = zero_value;
  61. if (margin_right.is_auto())
  62. margin_right = zero_value;
  63. if (underflow_px >= 0) {
  64. width = Length(underflow_px, Length::Type::Absolute);
  65. } else {
  66. width = zero_value;
  67. margin_right = Length(margin_right.to_px() + underflow_px, Length::Type::Absolute);
  68. }
  69. } else {
  70. if (!margin_left.is_auto() && !margin_right.is_auto()) {
  71. margin_right = Length(margin_right.to_px() + underflow_px, Length::Type::Absolute);
  72. } else if (!margin_left.is_auto() && margin_right.is_auto()) {
  73. margin_right = Length(underflow_px, Length::Type::Absolute);
  74. } else if (margin_left.is_auto() && !margin_right.is_auto()) {
  75. margin_left = Length(underflow_px, Length::Type::Absolute);
  76. } else { // margin_left.is_auto() && margin_right.is_auto()
  77. auto half_of_the_underflow = Length(underflow_px / 2, Length::Type::Absolute);
  78. margin_left = half_of_the_underflow;
  79. margin_right = half_of_the_underflow;
  80. }
  81. }
  82. rect().set_width(width.to_px());
  83. style().margin().left = margin_left;
  84. style().margin().right = margin_right;
  85. style().border().left = border_left;
  86. style().border().right = border_right;
  87. style().padding().left = padding_left;
  88. style().padding().right = padding_right;
  89. }
  90. void LayoutBlock::compute_position()
  91. {
  92. auto& style_properties = this->style_properties();
  93. auto auto_value = Length();
  94. auto zero_value = Length(0, Length::Type::Absolute);
  95. auto width = style_properties.length_or_fallback("width", auto_value);
  96. style().margin().top = style_properties.length_or_fallback("margin-top", zero_value);
  97. style().margin().bottom = style_properties.length_or_fallback("margin-bottom", zero_value);
  98. style().border().top = style_properties.length_or_fallback("border-top", zero_value);
  99. style().border().bottom = style_properties.length_or_fallback("border-bottom", zero_value);
  100. style().padding().top = style_properties.length_or_fallback("padding-top", zero_value);
  101. style().padding().bottom = style_properties.length_or_fallback("padding-bottom", zero_value);
  102. rect().set_x(containing_block()->rect().x() + style().margin().left.to_px() + style().border().left.to_px() + style().padding().left.to_px());
  103. int top_border = -1;
  104. if (previous_sibling() != nullptr) {
  105. auto& previous_sibling_rect = previous_sibling()->rect();
  106. auto& previous_sibling_style = previous_sibling()->style();
  107. top_border = previous_sibling_rect.y() + previous_sibling_rect.height();
  108. top_border += previous_sibling_style.full_margin().bottom;
  109. } else {
  110. top_border = containing_block()->rect().y();
  111. }
  112. rect().set_y(top_border + style().full_margin().top);
  113. }
  114. void LayoutBlock::compute_height()
  115. {
  116. auto& style_properties = this->style_properties();
  117. auto height_property = style_properties.property("height");
  118. if (!height_property.has_value())
  119. return;
  120. auto height_length = height_property.value()->to_length();
  121. if (height_length.is_absolute())
  122. rect().set_height(height_length.to_px());
  123. }