LayoutBlock.cpp 9.1 KB

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