PaintableBox.cpp 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570
  1. /*
  2. * Copyright (c) 2022, Andreas Kling <kling@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <LibWeb/DOM/Document.h>
  7. #include <LibWeb/HTML/HTMLHtmlElement.h>
  8. #include <LibWeb/Layout/BlockContainer.h>
  9. #include <LibWeb/Painting/BackgroundPainting.h>
  10. #include <LibWeb/Painting/PaintableBox.h>
  11. #include <LibWeb/Painting/ShadowPainting.h>
  12. #include <LibWeb/Painting/StackingContext.h>
  13. namespace Web::Painting {
  14. NonnullRefPtr<PaintableBox> PaintableBox::create(Layout::Box const& layout_box)
  15. {
  16. return adopt_ref(*new PaintableBox(layout_box));
  17. }
  18. PaintableBox::PaintableBox(Layout::Box const& layout_box)
  19. : Paintable(layout_box)
  20. {
  21. }
  22. PaintableBox::~PaintableBox()
  23. {
  24. }
  25. PaintableWithLines::PaintableWithLines(Layout::BlockContainer const& layout_box)
  26. : PaintableBox(layout_box)
  27. {
  28. }
  29. PaintableWithLines::~PaintableWithLines()
  30. {
  31. }
  32. void PaintableBox::set_offset(const Gfx::FloatPoint& offset)
  33. {
  34. if (m_offset == offset)
  35. return;
  36. m_offset = offset;
  37. // FIXME: This const_cast is gross.
  38. const_cast<Layout::Box&>(layout_box()).did_set_rect();
  39. }
  40. void PaintableBox::set_content_size(Gfx::FloatSize const& size)
  41. {
  42. if (m_content_size == size)
  43. return;
  44. m_content_size = size;
  45. // FIXME: This const_cast is gross.
  46. const_cast<Layout::Box&>(layout_box()).did_set_rect();
  47. }
  48. Gfx::FloatPoint PaintableBox::effective_offset() const
  49. {
  50. if (m_containing_line_box_fragment.has_value()) {
  51. auto const& fragment = containing_block()->paint_box()->line_boxes()[m_containing_line_box_fragment->line_box_index].fragments()[m_containing_line_box_fragment->fragment_index];
  52. return fragment.offset();
  53. }
  54. return m_offset;
  55. }
  56. Gfx::FloatRect PaintableBox::absolute_rect() const
  57. {
  58. if (!m_absolute_rect.has_value()) {
  59. Gfx::FloatRect rect { effective_offset(), content_size() };
  60. for (auto const* block = containing_block(); block && block->paintable(); block = block->paintable()->containing_block())
  61. rect.translate_by(block->paint_box()->effective_offset());
  62. m_absolute_rect = rect;
  63. }
  64. return *m_absolute_rect;
  65. }
  66. void PaintableBox::set_containing_line_box_fragment(Optional<Layout::LineBoxFragmentCoordinate> fragment_coordinate)
  67. {
  68. m_containing_line_box_fragment = fragment_coordinate;
  69. }
  70. Painting::StackingContext* PaintableBox::enclosing_stacking_context()
  71. {
  72. for (auto* ancestor = layout_box().parent(); ancestor; ancestor = ancestor->parent()) {
  73. if (!is<Layout::Box>(ancestor))
  74. continue;
  75. auto& ancestor_box = static_cast<Layout::Box&>(const_cast<Layout::NodeWithStyle&>(*ancestor));
  76. if (auto* ancestor_paint_box = ancestor_box.paint_box(); ancestor_paint_box && ancestor_paint_box->stacking_context())
  77. return const_cast<StackingContext*>(ancestor_paint_box->stacking_context());
  78. }
  79. // We should always reach the Layout::InitialContainingBlock stacking context.
  80. VERIFY_NOT_REACHED();
  81. }
  82. void PaintableBox::paint(PaintContext& context, PaintPhase phase) const
  83. {
  84. if (!is_visible())
  85. return;
  86. if (phase == PaintPhase::Background) {
  87. paint_background(context);
  88. paint_box_shadow(context);
  89. }
  90. if (phase == PaintPhase::Border) {
  91. paint_border(context);
  92. }
  93. if (phase == PaintPhase::Overlay && layout_box().dom_node() && layout_box().document().inspected_node() == layout_box().dom_node()) {
  94. auto content_rect = absolute_rect();
  95. auto margin_box = box_model().margin_box();
  96. Gfx::FloatRect margin_rect;
  97. margin_rect.set_x(absolute_x() - margin_box.left);
  98. margin_rect.set_width(content_width() + margin_box.left + margin_box.right);
  99. margin_rect.set_y(absolute_y() - margin_box.top);
  100. margin_rect.set_height(content_height() + margin_box.top + margin_box.bottom);
  101. auto border_rect = absolute_border_box_rect();
  102. auto padding_rect = absolute_padding_box_rect();
  103. auto paint_inspector_rect = [&](Gfx::FloatRect const& rect, Color color) {
  104. context.painter().fill_rect(enclosing_int_rect(rect), Color(color).with_alpha(100));
  105. context.painter().draw_rect(enclosing_int_rect(rect), Color(color));
  106. };
  107. paint_inspector_rect(margin_rect, Color::Yellow);
  108. paint_inspector_rect(padding_rect, Color::Cyan);
  109. paint_inspector_rect(border_rect, Color::Green);
  110. paint_inspector_rect(content_rect, Color::Magenta);
  111. StringBuilder builder;
  112. if (layout_box().dom_node())
  113. builder.append(layout_box().dom_node()->debug_description());
  114. else
  115. builder.append(layout_box().debug_description());
  116. builder.appendff(" {}x{} @ {},{}", border_rect.width(), border_rect.height(), border_rect.x(), border_rect.y());
  117. auto size_text = builder.to_string();
  118. auto size_text_rect = border_rect;
  119. size_text_rect.set_y(border_rect.y() + border_rect.height());
  120. size_text_rect.set_top(size_text_rect.top());
  121. size_text_rect.set_width((float)context.painter().font().width(size_text) + 4);
  122. size_text_rect.set_height(context.painter().font().glyph_height() + 4);
  123. context.painter().fill_rect(enclosing_int_rect(size_text_rect), context.palette().color(Gfx::ColorRole::Tooltip));
  124. context.painter().draw_rect(enclosing_int_rect(size_text_rect), context.palette().threed_shadow1());
  125. context.painter().draw_text(enclosing_int_rect(size_text_rect), size_text, Gfx::TextAlignment::Center, context.palette().color(Gfx::ColorRole::TooltipText));
  126. }
  127. if (phase == PaintPhase::FocusOutline && layout_box().dom_node() && layout_box().dom_node()->is_element() && verify_cast<DOM::Element>(*layout_box().dom_node()).is_focused()) {
  128. context.painter().draw_rect(enclosing_int_rect(absolute_rect()), context.palette().focus_outline());
  129. }
  130. }
  131. void PaintableBox::paint_border(PaintContext& context) const
  132. {
  133. auto borders_data = BordersData {
  134. .top = computed_values().border_top(),
  135. .right = computed_values().border_right(),
  136. .bottom = computed_values().border_bottom(),
  137. .left = computed_values().border_left(),
  138. };
  139. paint_all_borders(context, absolute_border_box_rect(), normalized_border_radius_data(), borders_data);
  140. }
  141. void PaintableBox::paint_background(PaintContext& context) const
  142. {
  143. // If the body's background properties were propagated to the root element, do no re-paint the body's background.
  144. if (layout_box().is_body() && document().html_element()->should_use_body_background_properties())
  145. return;
  146. Gfx::IntRect background_rect;
  147. Color background_color = computed_values().background_color();
  148. auto* background_layers = &computed_values().background_layers();
  149. if (layout_box().is_root_element()) {
  150. // CSS 2.1 Appendix E.2: If the element is a root element, paint the background over the entire canvas.
  151. background_rect = context.viewport_rect();
  152. // Section 2.11.2: If the computed value of background-image on the root element is none and its background-color is transparent,
  153. // user agents must instead propagate the computed values of the background properties from that element’s first HTML BODY child element.
  154. if (document().html_element()->should_use_body_background_properties()) {
  155. background_layers = document().background_layers();
  156. background_color = document().background_color(context.palette());
  157. }
  158. } else {
  159. background_rect = enclosing_int_rect(absolute_padding_box_rect());
  160. }
  161. // HACK: If the Box has a border, use the bordered_rect to paint the background.
  162. // This way if we have a border-radius there will be no gap between the filling and actual border.
  163. if (computed_values().border_top().width || computed_values().border_right().width || computed_values().border_bottom().width || computed_values().border_left().width)
  164. background_rect = enclosing_int_rect(absolute_border_box_rect());
  165. Painting::paint_background(context, layout_box(), background_rect, background_color, background_layers, normalized_border_radius_data());
  166. }
  167. void PaintableBox::paint_box_shadow(PaintContext& context) const
  168. {
  169. auto box_shadow_data = computed_values().box_shadow();
  170. if (box_shadow_data.is_empty())
  171. return;
  172. Vector<BoxShadowData> resolved_box_shadow_data;
  173. resolved_box_shadow_data.ensure_capacity(box_shadow_data.size());
  174. for (auto const& layer : box_shadow_data) {
  175. resolved_box_shadow_data.empend(
  176. layer.color,
  177. static_cast<int>(layer.offset_x.to_px(layout_box())),
  178. static_cast<int>(layer.offset_y.to_px(layout_box())),
  179. static_cast<int>(layer.blur_radius.to_px(layout_box())),
  180. static_cast<int>(layer.spread_distance.to_px(layout_box())),
  181. layer.placement == CSS::BoxShadowPlacement::Outer ? BoxShadowPlacement::Outer : BoxShadowPlacement::Inner);
  182. }
  183. Painting::paint_box_shadow(context, enclosing_int_rect(absolute_border_box_rect()), resolved_box_shadow_data);
  184. }
  185. BorderRadiusData PaintableBox::normalized_border_radius_data() const
  186. {
  187. return Painting::normalized_border_radius_data(layout_box(), absolute_border_box_rect(),
  188. computed_values().border_top_left_radius(),
  189. computed_values().border_top_right_radius(),
  190. computed_values().border_bottom_right_radius(),
  191. computed_values().border_bottom_left_radius());
  192. }
  193. void PaintableBox::before_children_paint(PaintContext& context, PaintPhase) const
  194. {
  195. // FIXME: Support more overflow variations.
  196. if (computed_values().overflow_x() == CSS::Overflow::Hidden && computed_values().overflow_y() == CSS::Overflow::Hidden) {
  197. context.painter().save();
  198. context.painter().add_clip_rect(enclosing_int_rect(absolute_border_box_rect()));
  199. }
  200. }
  201. void PaintableBox::after_children_paint(PaintContext& context, PaintPhase) const
  202. {
  203. // FIXME: Support more overflow variations.
  204. if (computed_values().overflow_x() == CSS::Overflow::Hidden && computed_values().overflow_y() == CSS::Overflow::Hidden)
  205. context.painter().restore();
  206. }
  207. static void paint_cursor_if_needed(PaintContext& context, Layout::TextNode const& text_node, Layout::LineBoxFragment const& fragment)
  208. {
  209. auto const& browsing_context = text_node.browsing_context();
  210. if (!browsing_context.is_focused_context())
  211. return;
  212. if (!browsing_context.cursor_blink_state())
  213. return;
  214. if (browsing_context.cursor_position().node() != &text_node.dom_node())
  215. return;
  216. // NOTE: This checks if the cursor is before the start or after the end of the fragment. If it is at the end, after all text, it should still be painted.
  217. if (browsing_context.cursor_position().offset() < (unsigned)fragment.start() || browsing_context.cursor_position().offset() > (unsigned)(fragment.start() + fragment.length()))
  218. return;
  219. if (!fragment.layout_node().dom_node() || !fragment.layout_node().dom_node()->is_editable())
  220. return;
  221. auto fragment_rect = fragment.absolute_rect();
  222. float cursor_x = fragment_rect.x() + text_node.font().width(fragment.text().substring_view(0, text_node.browsing_context().cursor_position().offset() - fragment.start()));
  223. float cursor_top = fragment_rect.top();
  224. float cursor_height = fragment_rect.height();
  225. Gfx::IntRect cursor_rect(cursor_x, cursor_top, 1, cursor_height);
  226. context.painter().draw_rect(cursor_rect, text_node.computed_values().color());
  227. }
  228. static void paint_text_decoration(Gfx::Painter& painter, Layout::Node const& text_node, Layout::LineBoxFragment const& fragment)
  229. {
  230. Gfx::IntPoint line_start_point {};
  231. Gfx::IntPoint line_end_point {};
  232. auto& font = fragment.layout_node().font();
  233. auto fragment_box = enclosing_int_rect(fragment.absolute_rect());
  234. auto glyph_height = font.glyph_height();
  235. auto baseline = fragment_box.height() / 2 - (glyph_height + 4) / 2 + glyph_height;
  236. switch (text_node.computed_values().text_decoration_line()) {
  237. case CSS::TextDecorationLine::None:
  238. return;
  239. case CSS::TextDecorationLine::Underline:
  240. line_start_point = fragment_box.top_left().translated(0, baseline + 2);
  241. line_end_point = fragment_box.top_right().translated(0, baseline + 2);
  242. break;
  243. case CSS::TextDecorationLine::Overline:
  244. line_start_point = fragment_box.top_left().translated(0, baseline - glyph_height);
  245. line_end_point = fragment_box.top_right().translated(0, baseline - glyph_height);
  246. break;
  247. case CSS::TextDecorationLine::LineThrough: {
  248. auto x_height = font.x_height();
  249. line_start_point = fragment_box.top_left().translated(0, baseline - x_height / 2);
  250. line_end_point = fragment_box.top_right().translated(0, baseline - x_height / 2);
  251. break;
  252. }
  253. case CSS::TextDecorationLine::Blink:
  254. // Conforming user agents may simply not blink the text
  255. return;
  256. }
  257. auto line_color = text_node.computed_values().text_decoration_color();
  258. int line_thickness = [&] {
  259. CSS::Length computed_thickness = text_node.computed_values().text_decoration_thickness().resolved(text_node, CSS::Length(1, CSS::Length::Type::Em));
  260. if (computed_thickness.is_auto())
  261. return CSS::InitialValues::text_decoration_thickness().to_px(text_node);
  262. return computed_thickness.to_px(text_node);
  263. }();
  264. switch (text_node.computed_values().text_decoration_style()) {
  265. case CSS::TextDecorationStyle::Solid:
  266. painter.draw_line(line_start_point, line_end_point, line_color, line_thickness, Gfx::Painter::LineStyle::Solid);
  267. break;
  268. case CSS::TextDecorationStyle::Double:
  269. switch (text_node.computed_values().text_decoration_line()) {
  270. case CSS::TextDecorationLine::Underline:
  271. break;
  272. case CSS::TextDecorationLine::Overline:
  273. line_start_point.translate_by(0, -line_thickness - 1);
  274. line_end_point.translate_by(0, -line_thickness - 1);
  275. break;
  276. case CSS::TextDecorationLine::LineThrough:
  277. line_start_point.translate_by(0, -line_thickness / 2);
  278. line_end_point.translate_by(0, -line_thickness / 2);
  279. break;
  280. default:
  281. VERIFY_NOT_REACHED();
  282. }
  283. painter.draw_line(line_start_point, line_end_point, line_color, line_thickness);
  284. painter.draw_line(line_start_point.translated(0, line_thickness + 1), line_end_point.translated(0, line_thickness + 1), line_color, line_thickness);
  285. break;
  286. case CSS::TextDecorationStyle::Dashed:
  287. painter.draw_line(line_start_point, line_end_point, line_color, line_thickness, Gfx::Painter::LineStyle::Dashed);
  288. break;
  289. case CSS::TextDecorationStyle::Dotted:
  290. painter.draw_line(line_start_point, line_end_point, line_color, line_thickness, Gfx::Painter::LineStyle::Dotted);
  291. break;
  292. case CSS::TextDecorationStyle::Wavy:
  293. painter.draw_triangle_wave(line_start_point, line_end_point, line_color, line_thickness + 1, line_thickness);
  294. break;
  295. }
  296. }
  297. static void paint_text_fragment(PaintContext& context, Layout::TextNode const& text_node, Layout::LineBoxFragment const& fragment, Painting::PaintPhase phase)
  298. {
  299. auto& painter = context.painter();
  300. if (phase == Painting::PaintPhase::Foreground) {
  301. auto fragment_absolute_rect = fragment.absolute_rect();
  302. painter.set_font(text_node.font());
  303. if (text_node.document().inspected_node() == &text_node.dom_node())
  304. context.painter().draw_rect(enclosing_int_rect(fragment_absolute_rect), Color::Magenta);
  305. // FIXME: text-transform should be done already in layout, since uppercase glyphs may be wider than lowercase, etc.
  306. auto text = text_node.text_for_rendering();
  307. auto text_transform = text_node.computed_values().text_transform();
  308. if (text_transform == CSS::TextTransform::Uppercase)
  309. text = text_node.text_for_rendering().to_uppercase();
  310. if (text_transform == CSS::TextTransform::Lowercase)
  311. text = text_node.text_for_rendering().to_lowercase();
  312. // FIXME: This is a hack to prevent text clipping when painting a bitmap font into a too-small box.
  313. auto draw_rect = enclosing_int_rect(fragment_absolute_rect);
  314. draw_rect.set_height(max(draw_rect.height(), text_node.font().glyph_height()));
  315. painter.draw_text(draw_rect, text.substring_view(fragment.start(), fragment.length()), Gfx::TextAlignment::CenterLeft, text_node.computed_values().color());
  316. auto selection_rect = fragment.selection_rect(text_node.font());
  317. if (!selection_rect.is_empty()) {
  318. painter.fill_rect(enclosing_int_rect(selection_rect), context.palette().selection());
  319. Gfx::PainterStateSaver saver(painter);
  320. painter.add_clip_rect(enclosing_int_rect(selection_rect));
  321. painter.draw_text(enclosing_int_rect(fragment_absolute_rect), text.substring_view(fragment.start(), fragment.length()), Gfx::TextAlignment::CenterLeft, context.palette().selection_text());
  322. }
  323. paint_text_decoration(painter, text_node, fragment);
  324. paint_cursor_if_needed(context, text_node, fragment);
  325. }
  326. }
  327. void PaintableWithLines::paint(PaintContext& context, PaintPhase phase) const
  328. {
  329. if (!is_visible())
  330. return;
  331. PaintableBox::paint(context, phase);
  332. if (m_line_boxes.is_empty())
  333. return;
  334. bool should_clip_overflow = computed_values().overflow_x() != CSS::Overflow::Visible && computed_values().overflow_y() != CSS::Overflow::Visible;
  335. if (should_clip_overflow) {
  336. context.painter().save();
  337. // FIXME: Handle overflow-x and overflow-y being different values.
  338. context.painter().add_clip_rect(enclosing_int_rect(absolute_padding_box_rect()));
  339. auto scroll_offset = static_cast<Layout::BlockContainer const&>(layout_box()).scroll_offset();
  340. context.painter().translate(-scroll_offset.to_type<int>());
  341. }
  342. for (auto& line_box : m_line_boxes) {
  343. for (auto& fragment : line_box.fragments()) {
  344. if (context.should_show_line_box_borders())
  345. context.painter().draw_rect(enclosing_int_rect(fragment.absolute_rect()), Color::Green);
  346. if (is<Layout::TextNode>(fragment.layout_node()))
  347. paint_text_fragment(context, static_cast<Layout::TextNode const&>(fragment.layout_node()), fragment, phase);
  348. }
  349. }
  350. if (should_clip_overflow) {
  351. context.painter().restore();
  352. }
  353. // FIXME: Merge this loop with the above somehow..
  354. if (phase == PaintPhase::FocusOutline) {
  355. for (auto& line_box : m_line_boxes) {
  356. for (auto& fragment : line_box.fragments()) {
  357. auto* node = fragment.layout_node().dom_node();
  358. if (!node)
  359. continue;
  360. auto* parent = node->parent_element();
  361. if (!parent)
  362. continue;
  363. if (parent->is_focused())
  364. context.painter().draw_rect(enclosing_int_rect(fragment.absolute_rect()), context.palette().focus_outline());
  365. }
  366. }
  367. }
  368. }
  369. bool PaintableWithLines::handle_mousewheel(Badge<EventHandler>, Gfx::IntPoint const&, unsigned, unsigned, int wheel_delta_x, int wheel_delta_y)
  370. {
  371. if (!layout_box().is_scrollable())
  372. return false;
  373. auto new_offset = layout_box().scroll_offset();
  374. new_offset.translate_by(wheel_delta_x, wheel_delta_y);
  375. const_cast<Layout::BlockContainer&>(layout_box()).set_scroll_offset(new_offset);
  376. return true;
  377. }
  378. Layout::BlockContainer const& PaintableWithLines::layout_box() const
  379. {
  380. return static_cast<Layout::BlockContainer const&>(PaintableBox::layout_box());
  381. }
  382. Layout::BlockContainer& PaintableWithLines::layout_box()
  383. {
  384. return static_cast<Layout::BlockContainer&>(PaintableBox::layout_box());
  385. }
  386. void PaintableBox::set_stacking_context(NonnullOwnPtr<StackingContext> stacking_context)
  387. {
  388. m_stacking_context = move(stacking_context);
  389. }
  390. template<typename Callback>
  391. void PaintableBox::for_each_child_in_paint_order(Callback callback) const
  392. {
  393. // Element traversal using the order defined in https://www.w3.org/TR/CSS2/zindex.html#painting-order.
  394. // Note: Some steps are skipped because they are not relevant to node traversal.
  395. // 3. Stacking contexts formed by positioned descendants with negative z-indices (excluding 0) in z-index order
  396. // (most negative first) then tree order.
  397. // FIXME: This does not retrieve elements in the z-index order.
  398. layout_box().for_each_child([&](auto& child) {
  399. if (!child.is_positioned() || !is<Layout::Box>(child))
  400. return;
  401. auto& box_child = verify_cast<Layout::Box>(child);
  402. auto* stacking_context = box_child.paint_box()->stacking_context();
  403. if (stacking_context && box_child.computed_values().z_index().has_value() && box_child.computed_values().z_index().value() < 0)
  404. callback(child);
  405. });
  406. // 4. For all its in-flow, non-positioned, block-level descendants in tree order: If the element is a block, list-item,
  407. // or other block equivalent:
  408. layout_box().for_each_child([&](auto& child) {
  409. if (is<Layout::Box>(child) && verify_cast<Layout::Box>(child).paint_box()->stacking_context())
  410. return;
  411. if (!child.is_positioned())
  412. callback(child);
  413. });
  414. // 5. All non-positioned floating descendants, in tree order. For each one of these, treat the element as if it created
  415. // a new stacking context, but any positioned descendants and descendants which actually create a new stacking context
  416. // should be considered part of the parent stacking context, not this new one.
  417. layout_box().for_each_child([&](auto& child) {
  418. if (is<Layout::Box>(child) && verify_cast<Layout::Box>(child).paint_box()->stacking_context())
  419. return;
  420. if (child.is_positioned())
  421. callback(child);
  422. });
  423. // 8. All positioned descendants with 'z-index: auto' or 'z-index: 0', in tree order. For those with 'z-index: auto', treat
  424. // the element as if it created a new stacking context, but any positioned descendants and descendants which actually
  425. // create a new stacking context should be considered part of the parent stacking context, not this new one. For those
  426. // with 'z-index: 0', treat the stacking context generated atomically.
  427. layout_box().for_each_child([&](auto& child) {
  428. if (!child.is_positioned() || !is<Layout::Box>(child))
  429. return;
  430. auto& box_child = verify_cast<Layout::Box>(child);
  431. auto* stacking_context = box_child.paint_box()->stacking_context();
  432. if (stacking_context && box_child.computed_values().z_index().has_value() && box_child.computed_values().z_index().value() == 0)
  433. callback(child);
  434. });
  435. // 9. Stacking contexts formed by positioned descendants with z-indices greater than or equal to 1 in z-index order
  436. // (smallest first) then tree order.
  437. // FIXME: This does not retrieve elements in the z-index order.
  438. layout_box().for_each_child([&](auto& child) {
  439. if (!child.is_positioned() || !is<Layout::Box>(child))
  440. return;
  441. auto& box_child = verify_cast<Layout::Box>(child);
  442. auto* stacking_context = box_child.paint_box()->stacking_context();
  443. if (stacking_context && box_child.computed_values().z_index().has_value() && box_child.computed_values().z_index().value() > 0)
  444. callback(child);
  445. });
  446. }
  447. HitTestResult PaintableBox::hit_test(Gfx::FloatPoint const& position, HitTestType type) const
  448. {
  449. if (layout_box().is_initial_containing_block_box())
  450. return stacking_context()->hit_test(position, type);
  451. HitTestResult result { absolute_border_box_rect().contains(position.x(), position.y()) ? this : nullptr };
  452. for_each_child_in_paint_order([&](auto& child) {
  453. if (child.paintable()) {
  454. auto child_result = child.paintable()->hit_test(position, type);
  455. if (child_result.paintable)
  456. result = child_result;
  457. }
  458. });
  459. return result;
  460. }
  461. HitTestResult PaintableWithLines::hit_test(const Gfx::FloatPoint& position, HitTestType type) const
  462. {
  463. if (!layout_box().children_are_inline())
  464. return PaintableBox::hit_test(position, type);
  465. HitTestResult last_good_candidate;
  466. for (auto& line_box : m_line_boxes) {
  467. for (auto& fragment : line_box.fragments()) {
  468. if (is<Layout::Box>(fragment.layout_node()) && static_cast<Layout::Box const&>(fragment.layout_node()).paint_box()->stacking_context())
  469. continue;
  470. if (fragment.absolute_rect().contains(position)) {
  471. if (is<Layout::BlockContainer>(fragment.layout_node()) && fragment.layout_node().paintable())
  472. return fragment.layout_node().paintable()->hit_test(position, type);
  473. return { fragment.layout_node().paintable(), fragment.text_index_at(position.x()) };
  474. }
  475. if (fragment.absolute_rect().top() <= position.y())
  476. last_good_candidate = { fragment.layout_node().paintable(), fragment.text_index_at(position.x()) };
  477. }
  478. }
  479. if (type == HitTestType::TextCursor && last_good_candidate.paintable)
  480. return last_good_candidate;
  481. return { absolute_border_box_rect().contains(position.x(), position.y()) ? this : nullptr };
  482. }
  483. }