LayoutBlock.cpp 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285
  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. // FIXME: This should be done by the CSS parser!
  54. CSS::ValueID text_align = CSS::ValueID::Left;
  55. auto text_align_string = style().string_or_fallback(CSS::PropertyID::TextAlign, "left");
  56. if (text_align_string == "center")
  57. text_align = CSS::ValueID::Center;
  58. else if (text_align_string == "left")
  59. text_align = CSS::ValueID::Left;
  60. else if (text_align_string == "right")
  61. text_align = CSS::ValueID::Right;
  62. for (auto& line_box : m_line_boxes) {
  63. int max_height = min_line_height;
  64. for (auto& fragment : line_box.fragments()) {
  65. max_height = max(max_height, fragment.rect().height());
  66. }
  67. int x_offset = x();
  68. switch (text_align) {
  69. case CSS::ValueID::Center:
  70. x_offset += (width() - line_box.width()) / 2;
  71. break;
  72. case CSS::ValueID::Right:
  73. x_offset += (width() - line_box.width());
  74. break;
  75. case CSS::ValueID::Left:
  76. default:
  77. break;
  78. }
  79. for (auto& fragment : line_box.fragments()) {
  80. // Vertically align everyone's bottom to the line.
  81. // FIXME: Support other kinds of vertical alignment.
  82. fragment.rect().set_x(x_offset + fragment.rect().x());
  83. fragment.rect().set_y(y() + content_height + (max_height - fragment.rect().height()));
  84. if (is<LayoutReplaced>(fragment.layout_node()))
  85. const_cast<LayoutReplaced&>(to<LayoutReplaced>(fragment.layout_node())).set_rect(fragment.rect());
  86. }
  87. content_height += max_height;
  88. }
  89. rect().set_height(content_height);
  90. }
  91. void LayoutBlock::compute_width()
  92. {
  93. auto& style = this->style();
  94. auto auto_value = Length();
  95. auto zero_value = Length(0, Length::Type::Absolute);
  96. auto width = style.length_or_fallback(CSS::PropertyID::Width, auto_value);
  97. auto margin_left = style.length_or_fallback(CSS::PropertyID::MarginLeft, zero_value);
  98. auto margin_right = style.length_or_fallback(CSS::PropertyID::MarginRight, zero_value);
  99. auto border_left = style.length_or_fallback(CSS::PropertyID::BorderLeftWidth, zero_value);
  100. auto border_right = style.length_or_fallback(CSS::PropertyID::BorderRightWidth, zero_value);
  101. auto padding_left = style.length_or_fallback(CSS::PropertyID::PaddingLeft, zero_value);
  102. auto padding_right = style.length_or_fallback(CSS::PropertyID::PaddingRight, zero_value);
  103. #ifdef HTML_DEBUG
  104. dbg() << " Left: " << margin_left << "+" << border_left << "+" << padding_left;
  105. dbg() << "Right: " << margin_right << "+" << border_right << "+" << padding_right;
  106. #endif
  107. int total_px = 0;
  108. for (auto& value : { margin_left, border_left, padding_left, width, padding_right, border_right, margin_right }) {
  109. total_px += value.to_px();
  110. }
  111. #ifdef HTML_DEBUG
  112. dbg() << "Total: " << total_px;
  113. #endif
  114. // 10.3.3 Block-level, non-replaced elements in normal flow
  115. // 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.
  116. if (width.is_auto() && total_px > containing_block()->width()) {
  117. if (margin_left.is_auto())
  118. margin_left = zero_value;
  119. if (margin_right.is_auto())
  120. margin_right = zero_value;
  121. }
  122. // 10.3.3 cont'd.
  123. auto underflow_px = containing_block()->width() - total_px;
  124. if (width.is_auto()) {
  125. if (margin_left.is_auto())
  126. margin_left = zero_value;
  127. if (margin_right.is_auto())
  128. margin_right = zero_value;
  129. if (underflow_px >= 0) {
  130. width = Length(underflow_px, Length::Type::Absolute);
  131. } else {
  132. width = zero_value;
  133. margin_right = Length(margin_right.to_px() + underflow_px, Length::Type::Absolute);
  134. }
  135. } else {
  136. if (!margin_left.is_auto() && !margin_right.is_auto()) {
  137. margin_right = Length(margin_right.to_px() + underflow_px, Length::Type::Absolute);
  138. } else if (!margin_left.is_auto() && margin_right.is_auto()) {
  139. margin_right = Length(underflow_px, Length::Type::Absolute);
  140. } else if (margin_left.is_auto() && !margin_right.is_auto()) {
  141. margin_left = Length(underflow_px, Length::Type::Absolute);
  142. } else { // margin_left.is_auto() && margin_right.is_auto()
  143. auto half_of_the_underflow = Length(underflow_px / 2, Length::Type::Absolute);
  144. margin_left = half_of_the_underflow;
  145. margin_right = half_of_the_underflow;
  146. }
  147. }
  148. rect().set_width(width.to_px());
  149. box_model().margin().left = margin_left;
  150. box_model().margin().right = margin_right;
  151. box_model().border().left = border_left;
  152. box_model().border().right = border_right;
  153. box_model().padding().left = padding_left;
  154. box_model().padding().right = padding_right;
  155. }
  156. void LayoutBlock::compute_position()
  157. {
  158. auto& style = this->style();
  159. auto auto_value = Length();
  160. auto zero_value = Length(0, Length::Type::Absolute);
  161. auto width = style.length_or_fallback(CSS::PropertyID::Width, auto_value);
  162. box_model().margin().top = style.length_or_fallback(CSS::PropertyID::MarginTop, zero_value);
  163. box_model().margin().bottom = style.length_or_fallback(CSS::PropertyID::MarginBottom, zero_value);
  164. box_model().border().top = style.length_or_fallback(CSS::PropertyID::BorderTopWidth, zero_value);
  165. box_model().border().bottom = style.length_or_fallback(CSS::PropertyID::BorderBottomWidth, zero_value);
  166. box_model().padding().top = style.length_or_fallback(CSS::PropertyID::PaddingTop, zero_value);
  167. box_model().padding().bottom = style.length_or_fallback(CSS::PropertyID::PaddingBottom, zero_value);
  168. rect().set_x(containing_block()->x() + box_model().margin().left.to_px() + box_model().border().left.to_px() + box_model().padding().left.to_px());
  169. int top_border = -1;
  170. if (previous_sibling() != nullptr) {
  171. auto& previous_sibling_rect = previous_sibling()->rect();
  172. auto& previous_sibling_style = previous_sibling()->box_model();
  173. top_border = previous_sibling_rect.y() + previous_sibling_rect.height();
  174. top_border += previous_sibling_style.full_margin().bottom;
  175. } else {
  176. top_border = containing_block()->y();
  177. }
  178. rect().set_y(top_border + box_model().full_margin().top);
  179. }
  180. void LayoutBlock::compute_height()
  181. {
  182. auto& style = this->style();
  183. auto height_property = style.property(CSS::PropertyID::Height);
  184. if (!height_property.has_value())
  185. return;
  186. auto height_length = height_property.value()->to_length();
  187. if (height_length.is_absolute())
  188. rect().set_height(height_length.to_px());
  189. }
  190. void LayoutBlock::render(RenderingContext& context)
  191. {
  192. if (!is_visible())
  193. return;
  194. LayoutBox::render(context);
  195. if (children_are_inline()) {
  196. for (auto& line_box : m_line_boxes) {
  197. for (auto& fragment : line_box.fragments()) {
  198. if (context.should_show_line_box_borders())
  199. context.painter().draw_rect(fragment.rect(), Color::Green);
  200. fragment.render(context);
  201. }
  202. }
  203. }
  204. }
  205. bool LayoutBlock::children_are_inline() const
  206. {
  207. return first_child() && first_child()->is_inline();
  208. }
  209. HitTestResult LayoutBlock::hit_test(const Point& position) const
  210. {
  211. if (!children_are_inline())
  212. return LayoutBox::hit_test(position);
  213. HitTestResult result;
  214. for (auto& line_box : m_line_boxes) {
  215. for (auto& fragment : line_box.fragments()) {
  216. if (fragment.rect().contains(position)) {
  217. return { fragment.layout_node() };
  218. }
  219. }
  220. }
  221. return {};
  222. }
  223. NonnullRefPtr<StyleProperties> LayoutBlock::style_for_anonymous_block() const
  224. {
  225. auto new_style = StyleProperties::create();
  226. style().for_each_property([&](auto property_id, auto& value) {
  227. if (StyleResolver::is_inherited_property(property_id))
  228. new_style->set_property(property_id, value);
  229. });
  230. return new_style;
  231. }
  232. LineBox& LayoutBlock::ensure_last_line_box()
  233. {
  234. if (m_line_boxes.is_empty())
  235. m_line_boxes.append(LineBox());
  236. return m_line_boxes.last();
  237. }
  238. LineBox& LayoutBlock::add_line_box()
  239. {
  240. m_line_boxes.append(LineBox());
  241. return m_line_boxes.last();
  242. }