LayoutBlock.cpp 8.8 KB

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