LayoutBlock.cpp 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. #include <LibGUI/GPainter.h>
  2. #include <LibHTML/DOM/Element.h>
  3. #include <LibHTML/Layout/LayoutBlock.h>
  4. LayoutBlock::LayoutBlock(const Node* node, StyleProperties&& style_properties)
  5. : LayoutNode(node, move(style_properties))
  6. {
  7. }
  8. LayoutBlock::~LayoutBlock()
  9. {
  10. }
  11. LayoutNode& LayoutBlock::inline_wrapper()
  12. {
  13. if (!last_child() || !last_child()->is_block() || last_child()->node() != nullptr) {
  14. append_child(adopt(*new LayoutBlock(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().bottom() + child.style().full_margin().bottom - rect().top();
  26. });
  27. rect().set_height(content_height);
  28. compute_height();
  29. }
  30. void LayoutBlock::compute_width()
  31. {
  32. auto& style_properties = this->style_properties();
  33. auto auto_value = Length();
  34. auto zero_value = Length(0, Length::Type::Absolute);
  35. auto width = style_properties.length_or_fallback("width", auto_value);
  36. auto margin_left = style_properties.length_or_fallback("margin-left", zero_value);
  37. auto margin_right = style_properties.length_or_fallback("margin-right", zero_value);
  38. auto border_left = style_properties.length_or_fallback("border-left", zero_value);
  39. auto border_right = style_properties.length_or_fallback("border-right", zero_value);
  40. auto padding_left = style_properties.length_or_fallback("padding-left", zero_value);
  41. auto padding_right = style_properties.length_or_fallback("padding-right", zero_value);
  42. #ifdef HTML_DEBUG
  43. dbg() << " Left: " << margin_left << "+" << border_left << "+" << padding_left;
  44. dbg() << "Right: " << margin_right << "+" << border_right << "+" << padding_right;
  45. #endif
  46. int total_px = 0;
  47. for (auto& value : { margin_left, border_left, padding_left, width, padding_right, border_right, margin_right }) {
  48. total_px += value.to_px();
  49. }
  50. #ifdef HTML_DEBUG
  51. dbg() << "Total: " << total_px;
  52. #endif
  53. // 10.3.3 Block-level, non-replaced elements in normal flow
  54. // 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.
  55. if (width.is_auto() && total_px > containing_block()->rect().width()) {
  56. if (margin_left.is_auto())
  57. margin_left = zero_value;
  58. if (margin_right.is_auto())
  59. margin_right = zero_value;
  60. }
  61. // 10.3.3 cont'd.
  62. auto underflow_px = containing_block()->rect().width() - total_px;
  63. if (width.is_auto()) {
  64. if (margin_left.is_auto())
  65. margin_left = zero_value;
  66. if (margin_right.is_auto())
  67. margin_right = zero_value;
  68. if (underflow_px >= 0) {
  69. width = Length(underflow_px, Length::Type::Absolute);
  70. } else {
  71. width = zero_value;
  72. margin_right = Length(margin_right.to_px() + underflow_px, Length::Type::Absolute);
  73. }
  74. } else {
  75. if (!margin_left.is_auto() && !margin_right.is_auto()) {
  76. margin_right = Length(margin_right.to_px() + underflow_px, Length::Type::Absolute);
  77. } else if (!margin_left.is_auto() && margin_right.is_auto()) {
  78. margin_right = Length(underflow_px, Length::Type::Absolute);
  79. } else if (margin_left.is_auto() && !margin_right.is_auto()) {
  80. margin_left = Length(underflow_px, Length::Type::Absolute);
  81. } else { // margin_left.is_auto() && margin_right.is_auto()
  82. auto half_of_the_underflow = Length(underflow_px / 2, Length::Type::Absolute);
  83. margin_left = half_of_the_underflow;
  84. margin_right = half_of_the_underflow;
  85. }
  86. }
  87. rect().set_width(width.to_px());
  88. style().margin().left = margin_left;
  89. style().margin().right = margin_right;
  90. style().border().left = border_left;
  91. style().border().right = border_right;
  92. style().padding().left = padding_left;
  93. style().padding().right = padding_right;
  94. }
  95. void LayoutBlock::compute_position()
  96. {
  97. auto& style_properties = this->style_properties();
  98. auto auto_value = Length();
  99. auto zero_value = Length(0, Length::Type::Absolute);
  100. auto width = style_properties.length_or_fallback("width", auto_value);
  101. style().margin().top = style_properties.length_or_fallback("margin-top", zero_value);
  102. style().margin().bottom = style_properties.length_or_fallback("margin-bottom", zero_value);
  103. style().border().top = style_properties.length_or_fallback("border-top", zero_value);
  104. style().border().bottom = style_properties.length_or_fallback("border-bottom", zero_value);
  105. style().padding().top = style_properties.length_or_fallback("padding-top", zero_value);
  106. style().padding().bottom = style_properties.length_or_fallback("padding-bottom", zero_value);
  107. rect().set_x(containing_block()->rect().x() + style().margin().left.to_px() + style().border().left.to_px() + style().padding().left.to_px());
  108. int top_border = -1;
  109. if (previous_sibling() != nullptr) {
  110. auto& previous_sibling_rect = previous_sibling()->rect();
  111. auto& previous_sibling_style = previous_sibling()->style();
  112. top_border = previous_sibling_rect.y() + previous_sibling_rect.height();
  113. top_border += previous_sibling_style.full_margin().bottom;
  114. } else {
  115. top_border = containing_block()->rect().y();
  116. }
  117. rect().set_y(top_border + style().full_margin().top);
  118. }
  119. void LayoutBlock::compute_height()
  120. {
  121. auto& style_properties = this->style_properties();
  122. auto height_property = style_properties.property("height");
  123. if (!height_property.has_value())
  124. return;
  125. auto height_length = height_property.value()->to_length();
  126. if (height_length.is_absolute())
  127. rect().set_height(height_length.to_px());
  128. }
  129. void LayoutBlock::render(RenderingContext& context)
  130. {
  131. LayoutNode::render(context);
  132. // FIXME: position this properly
  133. if (style_properties().string_or_fallback("display", "block") == "list-item") {
  134. Rect bullet_rect {
  135. rect().x() - 8,
  136. rect().y() + 4,
  137. 3,
  138. 3
  139. };
  140. context.painter().fill_rect(bullet_rect, Color::Black);
  141. }
  142. }