LayoutBlock.cpp 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  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. 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. if (fragment.layout_node().is_replaced())
  59. const_cast<LayoutNode&>(fragment.layout_node()).set_rect(fragment.rect());
  60. }
  61. content_height += max_height;
  62. }
  63. rect().set_height(content_height);
  64. }
  65. void LayoutBlock::compute_width()
  66. {
  67. auto& style_properties = this->style();
  68. auto auto_value = Length();
  69. auto zero_value = Length(0, Length::Type::Absolute);
  70. auto width = style_properties.length_or_fallback("width", auto_value);
  71. auto margin_left = style_properties.length_or_fallback("margin-left", zero_value);
  72. auto margin_right = style_properties.length_or_fallback("margin-right", zero_value);
  73. auto border_left = style_properties.length_or_fallback("border-left", zero_value);
  74. auto border_right = style_properties.length_or_fallback("border-right", zero_value);
  75. auto padding_left = style_properties.length_or_fallback("padding-left", zero_value);
  76. auto padding_right = style_properties.length_or_fallback("padding-right", zero_value);
  77. #ifdef HTML_DEBUG
  78. dbg() << " Left: " << margin_left << "+" << border_left << "+" << padding_left;
  79. dbg() << "Right: " << margin_right << "+" << border_right << "+" << padding_right;
  80. #endif
  81. int total_px = 0;
  82. for (auto& value : { margin_left, border_left, padding_left, width, padding_right, border_right, margin_right }) {
  83. total_px += value.to_px();
  84. }
  85. #ifdef HTML_DEBUG
  86. dbg() << "Total: " << total_px;
  87. #endif
  88. // 10.3.3 Block-level, non-replaced elements in normal flow
  89. // 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.
  90. if (width.is_auto() && total_px > containing_block()->rect().width()) {
  91. if (margin_left.is_auto())
  92. margin_left = zero_value;
  93. if (margin_right.is_auto())
  94. margin_right = zero_value;
  95. }
  96. // 10.3.3 cont'd.
  97. auto underflow_px = containing_block()->rect().width() - total_px;
  98. if (width.is_auto()) {
  99. if (margin_left.is_auto())
  100. margin_left = zero_value;
  101. if (margin_right.is_auto())
  102. margin_right = zero_value;
  103. if (underflow_px >= 0) {
  104. width = Length(underflow_px, Length::Type::Absolute);
  105. } else {
  106. width = zero_value;
  107. margin_right = Length(margin_right.to_px() + underflow_px, Length::Type::Absolute);
  108. }
  109. } else {
  110. if (!margin_left.is_auto() && !margin_right.is_auto()) {
  111. margin_right = Length(margin_right.to_px() + underflow_px, Length::Type::Absolute);
  112. } else if (!margin_left.is_auto() && margin_right.is_auto()) {
  113. margin_right = Length(underflow_px, Length::Type::Absolute);
  114. } else if (margin_left.is_auto() && !margin_right.is_auto()) {
  115. margin_left = Length(underflow_px, Length::Type::Absolute);
  116. } else { // margin_left.is_auto() && margin_right.is_auto()
  117. auto half_of_the_underflow = Length(underflow_px / 2, Length::Type::Absolute);
  118. margin_left = half_of_the_underflow;
  119. margin_right = half_of_the_underflow;
  120. }
  121. }
  122. rect().set_width(width.to_px());
  123. box_model().margin().left = margin_left;
  124. box_model().margin().right = margin_right;
  125. box_model().border().left = border_left;
  126. box_model().border().right = border_right;
  127. box_model().padding().left = padding_left;
  128. box_model().padding().right = padding_right;
  129. }
  130. void LayoutBlock::compute_position()
  131. {
  132. auto& style_properties = this->style();
  133. auto auto_value = Length();
  134. auto zero_value = Length(0, Length::Type::Absolute);
  135. auto width = style_properties.length_or_fallback("width", auto_value);
  136. box_model().margin().top = style_properties.length_or_fallback("margin-top", zero_value);
  137. box_model().margin().bottom = style_properties.length_or_fallback("margin-bottom", zero_value);
  138. box_model().border().top = style_properties.length_or_fallback("border-top", zero_value);
  139. box_model().border().bottom = style_properties.length_or_fallback("border-bottom", zero_value);
  140. box_model().padding().top = style_properties.length_or_fallback("padding-top", zero_value);
  141. box_model().padding().bottom = style_properties.length_or_fallback("padding-bottom", zero_value);
  142. 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());
  143. int top_border = -1;
  144. if (previous_sibling() != nullptr) {
  145. auto& previous_sibling_rect = previous_sibling()->rect();
  146. auto& previous_sibling_style = previous_sibling()->box_model();
  147. top_border = previous_sibling_rect.y() + previous_sibling_rect.height();
  148. top_border += previous_sibling_style.full_margin().bottom;
  149. } else {
  150. top_border = containing_block()->rect().y();
  151. }
  152. rect().set_y(top_border + box_model().full_margin().top);
  153. }
  154. void LayoutBlock::compute_height()
  155. {
  156. auto& style_properties = this->style();
  157. auto height_property = style_properties.property("height");
  158. if (!height_property.has_value())
  159. return;
  160. auto height_length = height_property.value()->to_length();
  161. if (height_length.is_absolute())
  162. rect().set_height(height_length.to_px());
  163. }
  164. void LayoutBlock::render(RenderingContext& context)
  165. {
  166. LayoutNode::render(context);
  167. // FIXME: position this properly
  168. if (style().string_or_fallback("display", "block") == "list-item") {
  169. Rect bullet_rect {
  170. rect().x() - 8,
  171. rect().y() + 4,
  172. 3,
  173. 3
  174. };
  175. context.painter().fill_rect(bullet_rect, style().color_or_fallback("color", Color::Black));
  176. }
  177. if (children_are_inline()) {
  178. for (auto& line_box : m_line_boxes) {
  179. for (auto& fragment : line_box.fragments()) {
  180. fragment.render(context);
  181. }
  182. }
  183. }
  184. }
  185. bool LayoutBlock::children_are_inline() const
  186. {
  187. return first_child() && !first_child()->is_block();
  188. }
  189. HitTestResult LayoutBlock::hit_test(const Point& position) const
  190. {
  191. if (!children_are_inline())
  192. return LayoutNode::hit_test(position);
  193. HitTestResult result;
  194. for (auto& line_box : m_line_boxes) {
  195. for (auto& fragment : line_box.fragments()) {
  196. if (fragment.rect().contains(position)) {
  197. return { fragment.layout_node() };
  198. }
  199. }
  200. }
  201. return {};
  202. }