LayoutBlock.cpp 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  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().height()
  25. + child.style().margin().top.to_px()
  26. + child.style().border().top.to_px()
  27. + child.style().padding().top.to_px()
  28. + child.style().margin().bottom.to_px()
  29. + child.style().border().bottom.to_px()
  30. + child.style().padding().bottom.to_px();
  31. });
  32. rect().set_height(content_height);
  33. compute_height();
  34. }
  35. void LayoutBlock::compute_width()
  36. {
  37. auto& style_properties = this->style_properties();
  38. auto auto_value = Length();
  39. auto zero_value = Length(0, Length::Type::Absolute);
  40. auto width = style_properties.length_or_fallback("width", auto_value);
  41. auto margin_left = style_properties.length_or_fallback("margin-left", zero_value);
  42. auto margin_right = style_properties.length_or_fallback("margin-right", zero_value);
  43. auto border_left = style_properties.length_or_fallback("border-left", zero_value);
  44. auto border_right = style_properties.length_or_fallback("border-right", zero_value);
  45. auto padding_left = style_properties.length_or_fallback("padding-left", zero_value);
  46. auto padding_right = style_properties.length_or_fallback("padding-right", zero_value);
  47. dbg() << " Left: " << margin_left << "+" << border_left << "+" << padding_left;
  48. dbg() << "Right: " << margin_right << "+" << border_right << "+" << padding_right;
  49. int total_px = 0;
  50. for (auto& value : { margin_left, border_left, padding_left, width, padding_right, border_right, margin_right }) {
  51. total_px += value.to_px();
  52. }
  53. dbg() << "Total: " << total_px;
  54. // 10.3.3 Block-level, non-replaced elements in normal flow
  55. // 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.
  56. if (width.is_auto() && total_px > containing_block()->rect().width()) {
  57. if (margin_left.is_auto())
  58. margin_left = zero_value;
  59. if (margin_right.is_auto())
  60. margin_right = zero_value;
  61. }
  62. // 10.3.3 cont'd.
  63. auto underflow_px = containing_block()->rect().width() - total_px;
  64. if (width.is_auto()) {
  65. if (margin_left.is_auto())
  66. margin_left = zero_value;
  67. if (margin_right.is_auto())
  68. margin_right = zero_value;
  69. if (underflow_px >= 0) {
  70. width = Length(underflow_px, Length::Type::Absolute);
  71. } else {
  72. width = zero_value;
  73. margin_right = Length(margin_right.to_px() + underflow_px, Length::Type::Absolute);
  74. }
  75. } else {
  76. if (!margin_left.is_auto() && !margin_right.is_auto()) {
  77. margin_right = Length(margin_right.to_px() + underflow_px, Length::Type::Absolute);
  78. } else if (!margin_left.is_auto() && margin_right.is_auto()) {
  79. margin_right = Length(underflow_px, Length::Type::Absolute);
  80. } else if (margin_left.is_auto() && !margin_right.is_auto()) {
  81. margin_left = Length(underflow_px, Length::Type::Absolute);
  82. } else { // margin_left.is_auto() && margin_right.is_auto()
  83. auto half_of_the_underflow = Length(underflow_px / 2, Length::Type::Absolute);
  84. margin_left = half_of_the_underflow;
  85. margin_right = half_of_the_underflow;
  86. }
  87. }
  88. rect().set_width(width.to_px());
  89. style().margin().left = margin_left;
  90. style().margin().right = margin_right;
  91. style().border().left = border_left;
  92. style().border().right = border_right;
  93. style().padding().left = padding_left;
  94. style().padding().right = padding_right;
  95. }
  96. void LayoutBlock::compute_position()
  97. {
  98. auto& style_properties = this->style_properties();
  99. auto auto_value = Length();
  100. auto zero_value = Length(0, Length::Type::Absolute);
  101. auto width = style_properties.length_or_fallback("width", auto_value);
  102. style().margin().top = style_properties.length_or_fallback("margin-top", zero_value);
  103. style().margin().bottom = style_properties.length_or_fallback("margin-bottom", zero_value);
  104. style().border().top = style_properties.length_or_fallback("border-top", zero_value);
  105. style().border().bottom = style_properties.length_or_fallback("border-bottom", zero_value);
  106. style().padding().top = style_properties.length_or_fallback("padding-top", zero_value);
  107. style().padding().bottom = style_properties.length_or_fallback("padding-bottom", zero_value);
  108. rect().set_x(containing_block()->rect().x() + style().margin().left.to_px() + style().border().left.to_px() + style().padding().left.to_px());
  109. rect().set_y(containing_block()->rect().y() + style().margin().top.to_px() + style().border().top.to_px() + style().padding().top.to_px());
  110. }
  111. void LayoutBlock::compute_height()
  112. {
  113. auto& style_properties = this->style_properties();
  114. auto height_property = style_properties.property("height");
  115. if (!height_property.has_value())
  116. return;
  117. auto height_length = height_property.value()->to_length();
  118. if (height_length.is_absolute())
  119. rect().set_height(height_length.to_px());
  120. }