LayoutBlock.cpp 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584
  1. /*
  2. * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
  3. * All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. *
  8. * 1. Redistributions of source code must retain the above copyright notice, this
  9. * list of conditions and the following disclaimer.
  10. *
  11. * 2. Redistributions in binary form must reproduce the above copyright notice,
  12. * this list of conditions and the following disclaimer in the documentation
  13. * and/or other materials provided with the distribution.
  14. *
  15. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  16. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  17. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  18. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  19. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  20. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  21. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  22. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  23. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  24. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  25. */
  26. #include <LibGUI/Painter.h>
  27. #include <LibWeb/CSS/StyleResolver.h>
  28. #include <LibWeb/DOM/Element.h>
  29. #include <LibWeb/Dump.h>
  30. #include <LibWeb/Layout/LayoutBlock.h>
  31. #include <LibWeb/Layout/LayoutDocument.h>
  32. #include <LibWeb/Layout/LayoutInline.h>
  33. #include <LibWeb/Layout/LayoutReplaced.h>
  34. #include <LibWeb/Layout/LayoutText.h>
  35. #include <LibWeb/Layout/LayoutWidget.h>
  36. #include <math.h>
  37. namespace Web {
  38. LayoutBlock::LayoutBlock(const Node* node, NonnullRefPtr<StyleProperties> style)
  39. : LayoutBox(node, move(style))
  40. {
  41. }
  42. LayoutBlock::~LayoutBlock()
  43. {
  44. }
  45. LayoutNode& LayoutBlock::inline_wrapper()
  46. {
  47. if (!last_child() || !last_child()->is_block() || last_child()->node() != nullptr) {
  48. append_child(adopt(*new LayoutBlock(nullptr, style_for_anonymous_block())));
  49. last_child()->set_children_are_inline(true);
  50. }
  51. return *last_child();
  52. }
  53. void LayoutBlock::layout(LayoutMode layout_mode)
  54. {
  55. compute_width();
  56. if (!is_inline())
  57. compute_position();
  58. layout_children(layout_mode);
  59. compute_height();
  60. if (layout_mode == LayoutMode::Default)
  61. layout_absolutely_positioned_descendants();
  62. }
  63. void LayoutBlock::layout_absolutely_positioned_descendants()
  64. {
  65. for (auto& box : m_absolutely_positioned_descendants) {
  66. box->layout(LayoutMode::Default);
  67. auto& box_model = box->box_model();
  68. auto& style = box->style();
  69. auto zero_value = Length(0, Length::Type::Px);
  70. auto specified_width = style.length_or_fallback(CSS::PropertyID::Width, Length(), width());
  71. box_model.margin().top = style.length_or_fallback(CSS::PropertyID::MarginTop, {}, height());
  72. box_model.margin().right = style.length_or_fallback(CSS::PropertyID::MarginRight, {}, width());
  73. box_model.margin().bottom = style.length_or_fallback(CSS::PropertyID::MarginBottom, {}, height());
  74. box_model.margin().left = style.length_or_fallback(CSS::PropertyID::MarginLeft, {}, width());
  75. box_model.offset().top = style.length_or_fallback(CSS::PropertyID::Top, {}, height());
  76. box_model.offset().right = style.length_or_fallback(CSS::PropertyID::Right, {}, width());
  77. box_model.offset().bottom = style.length_or_fallback(CSS::PropertyID::Bottom, {}, height());
  78. box_model.offset().left = style.length_or_fallback(CSS::PropertyID::Left, {}, width());
  79. if (box_model.offset().left.is_auto() && specified_width.is_auto() && box_model.offset().right.is_auto()) {
  80. if (box_model.margin().left.is_auto())
  81. box_model.margin().left = zero_value;
  82. if (box_model.margin().right.is_auto())
  83. box_model.margin().right = zero_value;
  84. }
  85. Gfx::FloatPoint used_offset;
  86. float x_offset = box_model.offset().left.to_px(*box)
  87. + box_model.border_box(*box).left
  88. - box_model.offset().right.to_px(*box)
  89. - box_model.border_box(*box).right;
  90. float y_offset = box_model.offset().top.to_px(*box)
  91. + box_model.border_box(*box).top
  92. - box_model.offset().bottom.to_px(*box)
  93. - box_model.border_box(*box).bottom;
  94. if (!box_model.offset().left.is_auto()) {
  95. used_offset.set_x(x_offset + box_model.margin().left.to_px(*box));
  96. } else if (!box_model.offset().right.is_auto()) {
  97. used_offset.set_x(width() + x_offset - box->width() - box_model.margin().right.to_px(*box));
  98. }
  99. if (!box_model.offset().top.is_auto()) {
  100. used_offset.set_y(y_offset + box_model.margin().top.to_px(*box));
  101. } else if (!box_model.offset().bottom.is_auto()) {
  102. used_offset.set_y(height() + y_offset - box->height() - box_model.margin().bottom.to_px(*box));
  103. }
  104. box->set_offset(used_offset);
  105. }
  106. }
  107. void LayoutBlock::add_absolutely_positioned_descendant(LayoutBox& box)
  108. {
  109. m_absolutely_positioned_descendants.set(box);
  110. }
  111. void LayoutBlock::layout_children(LayoutMode layout_mode)
  112. {
  113. if (children_are_inline())
  114. layout_inline_children(layout_mode);
  115. else
  116. layout_block_children(layout_mode);
  117. }
  118. void LayoutBlock::layout_block_children(LayoutMode layout_mode)
  119. {
  120. ASSERT(!children_are_inline());
  121. float content_height = 0;
  122. for_each_child([&](auto& child) {
  123. // FIXME: What should we do here? Something like a <table> might have a bunch of useless text children..
  124. if (child.is_inline())
  125. return;
  126. auto& child_block = static_cast<LayoutBlock&>(child);
  127. child_block.layout(layout_mode);
  128. if (!child_block.is_absolutely_positioned())
  129. content_height = max(content_height, child_block.effective_offset().y() + child_block.height() + child_block.box_model().margin_box(*this).bottom);
  130. });
  131. if (layout_mode != LayoutMode::Default) {
  132. float max_width = 0;
  133. for_each_child([&](auto& child) {
  134. if (child.is_box() && !child.is_absolutely_positioned())
  135. max_width = max(max_width, to<LayoutBox>(child).width());
  136. });
  137. set_width(max_width);
  138. }
  139. set_height(content_height);
  140. }
  141. void LayoutBlock::layout_inline_children(LayoutMode layout_mode)
  142. {
  143. ASSERT(children_are_inline());
  144. m_line_boxes.clear();
  145. for_each_child([&](auto& child) {
  146. ASSERT(child.is_inline());
  147. if (child.is_box() && child.is_absolutely_positioned()) {
  148. const_cast<LayoutBlock*>(child.containing_block())->add_absolutely_positioned_descendant((LayoutBox&)child);
  149. return;
  150. }
  151. child.split_into_lines(*this, layout_mode);
  152. });
  153. for (auto& line_box : m_line_boxes) {
  154. line_box.trim_trailing_whitespace();
  155. }
  156. float min_line_height = style().line_height(*this);
  157. float line_spacing = min_line_height - style().font().glyph_height();
  158. float content_height = 0;
  159. // FIXME: This should be done by the CSS parser!
  160. CSS::ValueID text_align = CSS::ValueID::Left;
  161. auto text_align_string = style().string_or_fallback(CSS::PropertyID::TextAlign, "left");
  162. if (text_align_string == "center")
  163. text_align = CSS::ValueID::Center;
  164. else if (text_align_string == "left")
  165. text_align = CSS::ValueID::Left;
  166. else if (text_align_string == "right")
  167. text_align = CSS::ValueID::Right;
  168. else if (text_align_string == "justify")
  169. text_align = CSS::ValueID::Justify;
  170. float max_linebox_width = 0;
  171. for (auto& line_box : m_line_boxes) {
  172. float max_height = min_line_height;
  173. for (auto& fragment : line_box.fragments()) {
  174. max_height = max(max_height, fragment.height());
  175. }
  176. float x_offset = 0;
  177. float excess_horizontal_space = (float)width() - line_box.width();
  178. switch (text_align) {
  179. case CSS::ValueID::Center:
  180. x_offset += excess_horizontal_space / 2;
  181. break;
  182. case CSS::ValueID::Right:
  183. x_offset += excess_horizontal_space;
  184. break;
  185. case CSS::ValueID::Left:
  186. case CSS::ValueID::Justify:
  187. default:
  188. break;
  189. }
  190. float excess_horizontal_space_including_whitespace = excess_horizontal_space;
  191. int whitespace_count = 0;
  192. if (text_align == CSS::ValueID::Justify) {
  193. for (auto& fragment : line_box.fragments()) {
  194. if (fragment.is_justifiable_whitespace()) {
  195. ++whitespace_count;
  196. excess_horizontal_space_including_whitespace += fragment.width();
  197. }
  198. }
  199. }
  200. float justified_space_width = whitespace_count ? (excess_horizontal_space_including_whitespace / (float)whitespace_count) : 0;
  201. for (size_t i = 0; i < line_box.fragments().size(); ++i) {
  202. auto& fragment = line_box.fragments()[i];
  203. // Vertically align everyone's bottom to the line.
  204. // FIXME: Support other kinds of vertical alignment.
  205. fragment.set_offset({ roundf(x_offset + fragment.offset().x()), content_height + (max_height - fragment.height()) - (line_spacing / 2) });
  206. if (text_align == CSS::ValueID::Justify) {
  207. if (fragment.is_justifiable_whitespace()) {
  208. if (fragment.width() != justified_space_width) {
  209. float diff = justified_space_width - fragment.width();
  210. fragment.set_width(justified_space_width);
  211. // Shift subsequent sibling fragments to the right to adjust for change in width.
  212. for (size_t j = i + 1; j < line_box.fragments().size(); ++j) {
  213. auto offset = line_box.fragments()[j].offset();
  214. offset.move_by(diff, 0);
  215. line_box.fragments()[j].set_offset(offset);
  216. }
  217. }
  218. }
  219. }
  220. if (fragment.layout_node().is_inline_block()) {
  221. auto& inline_block = const_cast<LayoutBlock&>(to<LayoutBlock>(fragment.layout_node()));
  222. inline_block.set_size(fragment.size());
  223. inline_block.layout(layout_mode);
  224. }
  225. float final_line_box_width = 0;
  226. for (auto& fragment : line_box.fragments())
  227. final_line_box_width += fragment.width();
  228. line_box.m_width = final_line_box_width;
  229. max_linebox_width = max(max_linebox_width, final_line_box_width);
  230. }
  231. content_height += max_height;
  232. }
  233. if (layout_mode != LayoutMode::Default) {
  234. set_width(max_linebox_width);
  235. }
  236. set_height(content_height);
  237. }
  238. void LayoutBlock::compute_width()
  239. {
  240. auto& style = this->style();
  241. auto auto_value = Length();
  242. auto zero_value = Length(0, Length::Type::Px);
  243. Length margin_left;
  244. Length margin_right;
  245. Length border_left;
  246. Length border_right;
  247. Length padding_left;
  248. Length padding_right;
  249. auto& containing_block = *this->containing_block();
  250. auto try_compute_width = [&](const auto& a_width) {
  251. Length width = a_width;
  252. #ifdef HTML_DEBUG
  253. dbg() << " Left: " << margin_left << "+" << border_left << "+" << padding_left;
  254. dbg() << "Right: " << margin_right << "+" << border_right << "+" << padding_right;
  255. #endif
  256. margin_left = style.length_or_fallback(CSS::PropertyID::MarginLeft, zero_value, containing_block.width());
  257. margin_right = style.length_or_fallback(CSS::PropertyID::MarginRight, zero_value, containing_block.width());
  258. border_left = style.length_or_fallback(CSS::PropertyID::BorderLeftWidth, zero_value);
  259. border_right = style.length_or_fallback(CSS::PropertyID::BorderRightWidth, zero_value);
  260. padding_left = style.length_or_fallback(CSS::PropertyID::PaddingLeft, zero_value, containing_block.width());
  261. padding_right = style.length_or_fallback(CSS::PropertyID::PaddingRight, zero_value, containing_block.width());
  262. float total_px = 0;
  263. for (auto& value : { margin_left, border_left, padding_left, width, padding_right, border_right, margin_right }) {
  264. total_px += value.to_px(*this);
  265. }
  266. #ifdef HTML_DEBUG
  267. dbg() << "Total: " << total_px;
  268. #endif
  269. if (!is_replaced() && !is_inline()) {
  270. // 10.3.3 Block-level, non-replaced elements in normal flow
  271. // 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.
  272. if (width.is_auto() && total_px > containing_block.width()) {
  273. if (margin_left.is_auto())
  274. margin_left = zero_value;
  275. if (margin_right.is_auto())
  276. margin_right = zero_value;
  277. }
  278. // 10.3.3 cont'd.
  279. auto underflow_px = containing_block.width() - total_px;
  280. if (width.is_auto()) {
  281. if (margin_left.is_auto())
  282. margin_left = zero_value;
  283. if (margin_right.is_auto())
  284. margin_right = zero_value;
  285. if (underflow_px >= 0) {
  286. width = Length(underflow_px, Length::Type::Px);
  287. } else {
  288. width = zero_value;
  289. margin_right = Length(margin_right.to_px(*this) + underflow_px, Length::Type::Px);
  290. }
  291. } else {
  292. if (!margin_left.is_auto() && !margin_right.is_auto()) {
  293. margin_right = Length(margin_right.to_px(*this) + underflow_px, Length::Type::Px);
  294. } else if (!margin_left.is_auto() && margin_right.is_auto()) {
  295. margin_right = Length(underflow_px, Length::Type::Px);
  296. } else if (margin_left.is_auto() && !margin_right.is_auto()) {
  297. margin_left = Length(underflow_px, Length::Type::Px);
  298. } else { // margin_left.is_auto() && margin_right.is_auto()
  299. auto half_of_the_underflow = Length(underflow_px / 2, Length::Type::Px);
  300. margin_left = half_of_the_underflow;
  301. margin_right = half_of_the_underflow;
  302. }
  303. }
  304. } else if (!is_replaced() && is_inline_block()) {
  305. // 10.3.9 'Inline-block', non-replaced elements in normal flow
  306. // A computed value of 'auto' for 'margin-left' or 'margin-right' becomes a used value of '0'.
  307. if (margin_left.is_auto())
  308. margin_left = zero_value;
  309. if (margin_right.is_auto())
  310. margin_right = zero_value;
  311. // If 'width' is 'auto', the used value is the shrink-to-fit width as for floating elements.
  312. if (width.is_auto()) {
  313. auto greatest_child_width = [&] {
  314. float max_width = 0;
  315. if (children_are_inline()) {
  316. for (auto& box : line_boxes()) {
  317. max_width = max(max_width, box.width());
  318. }
  319. } else {
  320. for_each_child([&](auto& child) {
  321. if (child.is_box())
  322. max_width = max(max_width, to<LayoutBox>(child).width());
  323. });
  324. }
  325. return max_width;
  326. };
  327. // Find the available width: in this case, this is the width of the containing
  328. // block minus the used values of 'margin-left', 'border-left-width', 'padding-left',
  329. // 'padding-right', 'border-right-width', 'margin-right', and the widths of any relevant scroll bars.
  330. float available_width = containing_block.width()
  331. - margin_left.to_px(*this) - border_left.to_px(*this) - padding_left.to_px(*this)
  332. - padding_right.to_px(*this) - border_right.to_px(*this) - margin_right.to_px(*this);
  333. // Calculate the preferred width by formatting the content without breaking lines
  334. // other than where explicit line breaks occur.
  335. layout_children(LayoutMode::OnlyRequiredLineBreaks);
  336. float preferred_width = greatest_child_width();
  337. // Also calculate the preferred minimum width, e.g., by trying all possible line breaks.
  338. // CSS 2.2 does not define the exact algorithm.
  339. layout_children(LayoutMode::AllPossibleLineBreaks);
  340. float preferred_minimum_width = greatest_child_width();
  341. // Then the shrink-to-fit width is: min(max(preferred minimum width, available width), preferred width).
  342. width = Length(min(max(preferred_minimum_width, available_width), preferred_width), Length::Type::Px);
  343. }
  344. }
  345. return width;
  346. };
  347. auto specified_width = style.length_or_fallback(CSS::PropertyID::Width, auto_value, containing_block.width());
  348. // 1. The tentative used width is calculated (without 'min-width' and 'max-width')
  349. auto used_width = try_compute_width(specified_width);
  350. // 2. The tentative used width is greater than 'max-width', the rules above are applied again,
  351. // but this time using the computed value of 'max-width' as the computed value for 'width'.
  352. auto specified_max_width = style.length_or_fallback(CSS::PropertyID::MaxWidth, auto_value, containing_block.width());
  353. if (!specified_max_width.is_auto()) {
  354. if (used_width.to_px(*this) > specified_max_width.to_px(*this)) {
  355. used_width = try_compute_width(specified_max_width);
  356. }
  357. }
  358. // 3. If the resulting width is smaller than 'min-width', the rules above are applied again,
  359. // but this time using the value of 'min-width' as the computed value for 'width'.
  360. auto specified_min_width = style.length_or_fallback(CSS::PropertyID::MinWidth, auto_value, containing_block.width());
  361. if (!specified_min_width.is_auto()) {
  362. if (used_width.to_px(*this) < specified_min_width.to_px(*this)) {
  363. used_width = try_compute_width(specified_min_width);
  364. }
  365. }
  366. set_width(used_width.to_px(*this));
  367. box_model().margin().left = margin_left;
  368. box_model().margin().right = margin_right;
  369. box_model().border().left = border_left;
  370. box_model().border().right = border_right;
  371. box_model().padding().left = padding_left;
  372. box_model().padding().right = padding_right;
  373. }
  374. void LayoutBlock::compute_position()
  375. {
  376. if (is_absolutely_positioned()) {
  377. const_cast<LayoutBlock*>(containing_block())->add_absolutely_positioned_descendant(*this);
  378. return;
  379. }
  380. auto& style = this->style();
  381. auto zero_value = Length(0, Length::Type::Px);
  382. auto& containing_block = *this->containing_block();
  383. box_model().margin().top = style.length_or_fallback(CSS::PropertyID::MarginTop, zero_value, containing_block.width());
  384. box_model().margin().bottom = style.length_or_fallback(CSS::PropertyID::MarginBottom, zero_value, containing_block.width());
  385. box_model().border().top = style.length_or_fallback(CSS::PropertyID::BorderTopWidth, zero_value);
  386. box_model().border().bottom = style.length_or_fallback(CSS::PropertyID::BorderBottomWidth, zero_value);
  387. box_model().padding().top = style.length_or_fallback(CSS::PropertyID::PaddingTop, zero_value, containing_block.width());
  388. box_model().padding().bottom = style.length_or_fallback(CSS::PropertyID::PaddingBottom, zero_value, containing_block.width());
  389. float position_x = box_model().margin().left.to_px(*this)
  390. + box_model().border().left.to_px(*this)
  391. + box_model().padding().left.to_px(*this)
  392. + box_model().offset().left.to_px(*this);
  393. float position_y = box_model().margin_box(*this).top
  394. + box_model().offset().top.to_px(*this);
  395. LayoutBlock* relevant_sibling = previous_sibling();
  396. while (relevant_sibling != nullptr) {
  397. if (relevant_sibling->style().position() != CSS::Position::Absolute)
  398. break;
  399. relevant_sibling = relevant_sibling->previous_sibling();
  400. }
  401. if (relevant_sibling) {
  402. auto& previous_sibling_style = relevant_sibling->box_model();
  403. position_y += relevant_sibling->effective_offset().y() + relevant_sibling->height();
  404. // Collapse top margin with bottom margin of previous sibling if necessary
  405. float previous_sibling_margin_bottom = previous_sibling_style.margin().bottom.to_px(*relevant_sibling);
  406. float my_margin_top = box_model().margin().top.to_px(*this);
  407. if (previous_sibling_margin_bottom > my_margin_top) {
  408. // Sibling's margin is larger than mine, adjust so we use sibling's.
  409. position_y += previous_sibling_margin_bottom - my_margin_top;
  410. }
  411. }
  412. set_offset({ position_x, position_y });
  413. }
  414. void LayoutBlock::compute_height()
  415. {
  416. auto& style = this->style();
  417. auto specified_height = style.length_or_fallback(CSS::PropertyID::Height, Length(), containing_block()->height());
  418. auto specified_max_height = style.length_or_fallback(CSS::PropertyID::MaxHeight, Length(), containing_block()->height());
  419. if (!specified_height.is_auto()) {
  420. float used_height = specified_height.to_px(*this);
  421. if (!specified_max_height.is_auto())
  422. used_height = min(used_height, specified_max_height.to_px(*this));
  423. set_height(used_height);
  424. }
  425. }
  426. void LayoutBlock::render(RenderingContext& context)
  427. {
  428. if (!is_visible())
  429. return;
  430. LayoutBox::render(context);
  431. if (children_are_inline()) {
  432. for (auto& line_box : m_line_boxes) {
  433. for (auto& fragment : line_box.fragments()) {
  434. if (context.should_show_line_box_borders())
  435. context.painter().draw_rect(enclosing_int_rect(fragment.absolute_rect()), Color::Green);
  436. fragment.render(context);
  437. }
  438. }
  439. }
  440. }
  441. HitTestResult LayoutBlock::hit_test(const Gfx::IntPoint& position) const
  442. {
  443. if (!children_are_inline())
  444. return LayoutBox::hit_test(position);
  445. HitTestResult result;
  446. for (auto& line_box : m_line_boxes) {
  447. for (auto& fragment : line_box.fragments()) {
  448. if (enclosing_int_rect(fragment.absolute_rect()).contains(position)) {
  449. if (fragment.layout_node().is_block())
  450. return to<LayoutBlock>(fragment.layout_node()).hit_test(position);
  451. return { fragment.layout_node(), fragment.text_index_at(position.x()) };
  452. }
  453. }
  454. }
  455. // FIXME: This should be smarter about the text position if we're hitting a block
  456. // that has text inside it, but `position` is to the right of the text box.
  457. return { absolute_rect().contains(position.x(), position.y()) ? this : nullptr };
  458. }
  459. NonnullRefPtr<StyleProperties> LayoutBlock::style_for_anonymous_block() const
  460. {
  461. auto new_style = StyleProperties::create();
  462. style().for_each_property([&](auto property_id, auto& value) {
  463. if (StyleResolver::is_inherited_property(property_id))
  464. new_style->set_property(property_id, value);
  465. });
  466. return new_style;
  467. }
  468. LineBox& LayoutBlock::ensure_last_line_box()
  469. {
  470. if (m_line_boxes.is_empty())
  471. m_line_boxes.append(LineBox());
  472. return m_line_boxes.last();
  473. }
  474. LineBox& LayoutBlock::add_line_box()
  475. {
  476. m_line_boxes.append(LineBox());
  477. return m_line_boxes.last();
  478. }
  479. void LayoutBlock::split_into_lines(LayoutBlock& container, LayoutMode layout_mode)
  480. {
  481. ASSERT(is_inline());
  482. layout(layout_mode);
  483. auto* line_box = &container.ensure_last_line_box();
  484. if (line_box->width() > 0 && line_box->width() + width() > container.width())
  485. line_box = &container.add_line_box();
  486. line_box->add_fragment(*this, 0, 0, width(), height());
  487. }
  488. }