LayoutBlock.cpp 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  1. #include <LibGUI/GPainter.h>
  2. #include <LibHTML/DOM/Element.h>
  3. #include <LibHTML/Layout/LayoutBlock.h>
  4. #include <LibHTML/Layout/LayoutInline.h>
  5. LayoutBlock::LayoutBlock(const Node* node, NonnullRefPtr<StyleProperties> style_properties)
  6. : LayoutNode(node, move(style_properties))
  7. {
  8. }
  9. LayoutBlock::~LayoutBlock()
  10. {
  11. }
  12. LayoutNode& LayoutBlock::inline_wrapper()
  13. {
  14. if (!last_child() || !last_child()->is_block() || last_child()->node() != nullptr) {
  15. append_child(adopt(*new LayoutBlock(nullptr, style())));
  16. }
  17. return *last_child();
  18. }
  19. void LayoutBlock::layout()
  20. {
  21. compute_width();
  22. compute_position();
  23. if (children_are_inline())
  24. layout_inline_children();
  25. else
  26. layout_block_children();
  27. compute_height();
  28. }
  29. void LayoutBlock::layout_block_children()
  30. {
  31. ASSERT(!children_are_inline());
  32. int content_height = 0;
  33. for_each_child([&](auto& child) {
  34. child.layout();
  35. content_height = child.rect().bottom() + child.box_model().full_margin().bottom - rect().top();
  36. });
  37. rect().set_height(content_height);
  38. }
  39. void LayoutBlock::layout_inline_children()
  40. {
  41. ASSERT(children_are_inline());
  42. m_line_boxes.clear();
  43. for_each_child([&](auto& child) {
  44. ASSERT(child.is_inline());
  45. static_cast<LayoutInline&>(child).split_into_lines(*this);
  46. });
  47. int content_height = 0;
  48. for (auto& line_box : m_line_boxes) {
  49. int max_height = 0;
  50. for (auto& fragment : line_box.fragments()) {
  51. max_height = max(max_height, fragment.rect().height());
  52. }
  53. for (auto& fragment : line_box.fragments()) {
  54. // Vertically align everyone's bottom to the line.
  55. // FIXME: Support other kinds of vertical alignment.
  56. fragment.rect().set_x(rect().x() + fragment.rect().x());
  57. fragment.rect().set_y(rect().y() + content_height + (max_height - fragment.rect().height()));
  58. }
  59. content_height += max_height;
  60. }
  61. rect().set_height(content_height);
  62. }
  63. void LayoutBlock::compute_width()
  64. {
  65. auto& style_properties = this->style();
  66. auto auto_value = Length();
  67. auto zero_value = Length(0, Length::Type::Absolute);
  68. auto width = style_properties.length_or_fallback("width", auto_value);
  69. auto margin_left = style_properties.length_or_fallback("margin-left", zero_value);
  70. auto margin_right = style_properties.length_or_fallback("margin-right", zero_value);
  71. auto border_left = style_properties.length_or_fallback("border-left", zero_value);
  72. auto border_right = style_properties.length_or_fallback("border-right", zero_value);
  73. auto padding_left = style_properties.length_or_fallback("padding-left", zero_value);
  74. auto padding_right = style_properties.length_or_fallback("padding-right", zero_value);
  75. #ifdef HTML_DEBUG
  76. dbg() << " Left: " << margin_left << "+" << border_left << "+" << padding_left;
  77. dbg() << "Right: " << margin_right << "+" << border_right << "+" << padding_right;
  78. #endif
  79. int total_px = 0;
  80. for (auto& value : { margin_left, border_left, padding_left, width, padding_right, border_right, margin_right }) {
  81. total_px += value.to_px();
  82. }
  83. #ifdef HTML_DEBUG
  84. dbg() << "Total: " << total_px;
  85. #endif
  86. // 10.3.3 Block-level, non-replaced elements in normal flow
  87. // 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.
  88. if (width.is_auto() && total_px > containing_block()->rect().width()) {
  89. if (margin_left.is_auto())
  90. margin_left = zero_value;
  91. if (margin_right.is_auto())
  92. margin_right = zero_value;
  93. }
  94. // 10.3.3 cont'd.
  95. auto underflow_px = containing_block()->rect().width() - total_px;
  96. if (width.is_auto()) {
  97. if (margin_left.is_auto())
  98. margin_left = zero_value;
  99. if (margin_right.is_auto())
  100. margin_right = zero_value;
  101. if (underflow_px >= 0) {
  102. width = Length(underflow_px, Length::Type::Absolute);
  103. } else {
  104. width = zero_value;
  105. margin_right = Length(margin_right.to_px() + underflow_px, Length::Type::Absolute);
  106. }
  107. } else {
  108. if (!margin_left.is_auto() && !margin_right.is_auto()) {
  109. margin_right = Length(margin_right.to_px() + underflow_px, Length::Type::Absolute);
  110. } else if (!margin_left.is_auto() && margin_right.is_auto()) {
  111. margin_right = Length(underflow_px, Length::Type::Absolute);
  112. } else if (margin_left.is_auto() && !margin_right.is_auto()) {
  113. margin_left = Length(underflow_px, Length::Type::Absolute);
  114. } else { // margin_left.is_auto() && margin_right.is_auto()
  115. auto half_of_the_underflow = Length(underflow_px / 2, Length::Type::Absolute);
  116. margin_left = half_of_the_underflow;
  117. margin_right = half_of_the_underflow;
  118. }
  119. }
  120. rect().set_width(width.to_px());
  121. box_model().margin().left = margin_left;
  122. box_model().margin().right = margin_right;
  123. box_model().border().left = border_left;
  124. box_model().border().right = border_right;
  125. box_model().padding().left = padding_left;
  126. box_model().padding().right = padding_right;
  127. }
  128. void LayoutBlock::compute_position()
  129. {
  130. auto& style_properties = this->style();
  131. auto auto_value = Length();
  132. auto zero_value = Length(0, Length::Type::Absolute);
  133. auto width = style_properties.length_or_fallback("width", auto_value);
  134. box_model().margin().top = style_properties.length_or_fallback("margin-top", zero_value);
  135. box_model().margin().bottom = style_properties.length_or_fallback("margin-bottom", zero_value);
  136. box_model().border().top = style_properties.length_or_fallback("border-top", zero_value);
  137. box_model().border().bottom = style_properties.length_or_fallback("border-bottom", zero_value);
  138. box_model().padding().top = style_properties.length_or_fallback("padding-top", zero_value);
  139. box_model().padding().bottom = style_properties.length_or_fallback("padding-bottom", zero_value);
  140. rect().set_x(containing_block()->rect().x() + box_model().margin().left.to_px() + box_model().border().left.to_px() + box_model().padding().left.to_px());
  141. int top_border = -1;
  142. if (previous_sibling() != nullptr) {
  143. auto& previous_sibling_rect = previous_sibling()->rect();
  144. auto& previous_sibling_style = previous_sibling()->box_model();
  145. top_border = previous_sibling_rect.y() + previous_sibling_rect.height();
  146. top_border += previous_sibling_style.full_margin().bottom;
  147. } else {
  148. top_border = containing_block()->rect().y();
  149. }
  150. rect().set_y(top_border + box_model().full_margin().top);
  151. }
  152. void LayoutBlock::compute_height()
  153. {
  154. auto& style_properties = this->style();
  155. auto height_property = style_properties.property("height");
  156. if (!height_property.has_value())
  157. return;
  158. auto height_length = height_property.value()->to_length();
  159. if (height_length.is_absolute())
  160. rect().set_height(height_length.to_px());
  161. }
  162. void LayoutBlock::render(RenderingContext& context)
  163. {
  164. LayoutNode::render(context);
  165. // FIXME: position this properly
  166. if (style().string_or_fallback("display", "block") == "list-item") {
  167. Rect bullet_rect {
  168. rect().x() - 8,
  169. rect().y() + 4,
  170. 3,
  171. 3
  172. };
  173. context.painter().fill_rect(bullet_rect, Color::Black);
  174. }
  175. if (children_are_inline()) {
  176. for (auto& line_box : m_line_boxes) {
  177. for (auto& fragment : line_box.fragments()) {
  178. fragment.render(context);
  179. }
  180. }
  181. }
  182. }
  183. bool LayoutBlock::children_are_inline() const
  184. {
  185. return first_child() && !first_child()->is_block();
  186. }
  187. HitTestResult LayoutBlock::hit_test(const Point& position) const
  188. {
  189. if (!children_are_inline())
  190. return LayoutNode::hit_test(position);
  191. HitTestResult result;
  192. for (auto& line_box : m_line_boxes) {
  193. for (auto& fragment : line_box.fragments()) {
  194. if (fragment.rect().contains(position)) {
  195. return { fragment.layout_node() };
  196. }
  197. }
  198. }
  199. return {};
  200. }