LayoutBlock.cpp 5.9 KB

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