LayoutBlock.cpp 9.0 KB

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