LayoutBlock.cpp 10.0 KB

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