StackingContext.cpp 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533
  1. /*
  2. * Copyright (c) 2020-2022, Andreas Kling <kling@serenityos.org>
  3. * Copyright (c) 2022, Sam Atkins <atkinssj@serenityos.org>
  4. *
  5. * SPDX-License-Identifier: BSD-2-Clause
  6. */
  7. #include <AK/Debug.h>
  8. #include <AK/QuickSort.h>
  9. #include <AK/StringBuilder.h>
  10. #include <LibGfx/AffineTransform.h>
  11. #include <LibGfx/Matrix4x4.h>
  12. #include <LibGfx/Rect.h>
  13. #include <LibWeb/CSS/ComputedValues.h>
  14. #include <LibWeb/CSS/StyleValues/TransformationStyleValue.h>
  15. #include <LibWeb/Layout/Box.h>
  16. #include <LibWeb/Layout/ReplacedBox.h>
  17. #include <LibWeb/Layout/Viewport.h>
  18. #include <LibWeb/Painting/PaintableBox.h>
  19. #include <LibWeb/Painting/SVGPaintable.h>
  20. #include <LibWeb/Painting/StackingContext.h>
  21. #include <LibWeb/Painting/TableBordersPainting.h>
  22. #include <LibWeb/SVG/SVGMaskElement.h>
  23. namespace Web::Painting {
  24. static void paint_node(Paintable const& paintable, PaintContext& context, PaintPhase phase)
  25. {
  26. paintable.before_paint(context, phase);
  27. paintable.paint(context, phase);
  28. paintable.after_paint(context, phase);
  29. }
  30. StackingContext::StackingContext(PaintableBox& paintable_box, StackingContext* parent, size_t index_in_tree_order)
  31. : m_paintable_box(paintable_box)
  32. , m_transform(combine_transformations(paintable_box.computed_values().transformations()))
  33. , m_transform_origin(compute_transform_origin())
  34. , m_parent(parent)
  35. , m_index_in_tree_order(index_in_tree_order)
  36. {
  37. VERIFY(m_parent != this);
  38. if (m_parent)
  39. m_parent->m_children.append(this);
  40. }
  41. void StackingContext::sort()
  42. {
  43. quick_sort(m_children, [](auto& a, auto& b) {
  44. auto a_z_index = a->paintable_box().computed_values().z_index().value_or(0);
  45. auto b_z_index = b->paintable_box().computed_values().z_index().value_or(0);
  46. if (a_z_index == b_z_index)
  47. return a->m_index_in_tree_order < b->m_index_in_tree_order;
  48. return a_z_index < b_z_index;
  49. });
  50. for (auto* child : m_children)
  51. child->sort();
  52. }
  53. static PaintPhase to_paint_phase(StackingContext::StackingContextPaintPhase phase)
  54. {
  55. // There are not a fully correct mapping since some stacking context phases are combined.
  56. switch (phase) {
  57. case StackingContext::StackingContextPaintPhase::Floats:
  58. case StackingContext::StackingContextPaintPhase::BackgroundAndBordersForInlineLevelAndReplaced:
  59. case StackingContext::StackingContextPaintPhase::BackgroundAndBorders:
  60. return PaintPhase::Background;
  61. case StackingContext::StackingContextPaintPhase::Foreground:
  62. return PaintPhase::Foreground;
  63. case StackingContext::StackingContextPaintPhase::FocusAndOverlay:
  64. return PaintPhase::Overlay;
  65. default:
  66. VERIFY_NOT_REACHED();
  67. }
  68. }
  69. void StackingContext::paint_node_as_stacking_context(Paintable const& paintable, PaintContext& context)
  70. {
  71. paint_node(paintable, context, PaintPhase::Background);
  72. paint_node(paintable, context, PaintPhase::Border);
  73. paint_descendants(context, paintable, StackingContextPaintPhase::BackgroundAndBorders);
  74. paint_descendants(context, paintable, StackingContextPaintPhase::Floats);
  75. paint_descendants(context, paintable, StackingContextPaintPhase::BackgroundAndBordersForInlineLevelAndReplaced);
  76. paint_node(paintable, context, PaintPhase::Foreground);
  77. paint_descendants(context, paintable, StackingContextPaintPhase::Foreground);
  78. paint_node(paintable, context, PaintPhase::Outline);
  79. paint_node(paintable, context, PaintPhase::Overlay);
  80. paint_descendants(context, paintable, StackingContextPaintPhase::FocusAndOverlay);
  81. }
  82. void StackingContext::paint_descendants(PaintContext& context, Paintable const& paintable, StackingContextPaintPhase phase)
  83. {
  84. paintable.before_children_paint(context, to_paint_phase(phase));
  85. paintable.apply_clip_overflow_rect(context, to_paint_phase(phase));
  86. paintable.for_each_child([&context, phase](auto& child) {
  87. auto* stacking_context = child.stacking_context_rooted_here();
  88. if (child.is_positioned()) {
  89. // If `child` is positioned with a z-index of `0` or `auto`, skip over it.
  90. auto const& z_index = child.computed_values().z_index();
  91. if (!z_index.has_value() || z_index.value() == 0)
  92. return;
  93. // Skip positioned children with stacking contexts, these are handled in paint_internal().
  94. if (stacking_context)
  95. return;
  96. }
  97. if (stacking_context) {
  98. // FIXME: This may not be fully correct with respect to the paint phases.
  99. if (phase == StackingContextPaintPhase::Foreground)
  100. paint_child(context, *stacking_context);
  101. // Note: Don't further recuse into descendants as paint_child() will do that.
  102. return;
  103. }
  104. // NOTE: Grid specification https://www.w3.org/TR/css-grid-2/#z-order says that grid items should be treated
  105. // the same way as CSS2 defines for inline-blocks:
  106. // "For each one of these, treat the element as if it created a new stacking context, but any positioned
  107. // descendants and descendants which actually create a new stacking context should be considered part of
  108. // the parent stacking context, not this new one."
  109. auto should_be_treated_as_stacking_context = child.layout_node().is_grid_item();
  110. if (should_be_treated_as_stacking_context) {
  111. // FIXME: This may not be fully correct with respect to the paint phases.
  112. if (phase == StackingContextPaintPhase::Foreground)
  113. paint_node_as_stacking_context(child, context);
  114. return;
  115. }
  116. bool child_is_inline_or_replaced = child.is_inline() || is<Layout::ReplacedBox>(child);
  117. switch (phase) {
  118. case StackingContextPaintPhase::BackgroundAndBorders:
  119. if (!child_is_inline_or_replaced && !child.is_floating()) {
  120. paint_node(child, context, PaintPhase::Background);
  121. bool is_table_with_collapsed_borders = child.display().is_table_inside() && child.computed_values().border_collapse() == CSS::BorderCollapse::Collapse;
  122. if (!child.display().is_table_cell() && !is_table_with_collapsed_borders)
  123. paint_node(child, context, PaintPhase::Border);
  124. paint_descendants(context, child, phase);
  125. if (child.display().is_table_inside() || child.computed_values().border_collapse() == CSS::BorderCollapse::Collapse) {
  126. paint_table_borders(context, verify_cast<PaintableBox>(child));
  127. }
  128. }
  129. break;
  130. case StackingContextPaintPhase::Floats:
  131. if (child.is_floating()) {
  132. paint_node(child, context, PaintPhase::Background);
  133. paint_node(child, context, PaintPhase::Border);
  134. paint_descendants(context, child, StackingContextPaintPhase::BackgroundAndBorders);
  135. }
  136. paint_descendants(context, child, phase);
  137. break;
  138. case StackingContextPaintPhase::BackgroundAndBordersForInlineLevelAndReplaced:
  139. if (child_is_inline_or_replaced) {
  140. paint_node(child, context, PaintPhase::Background);
  141. paint_node(child, context, PaintPhase::Border);
  142. if (child.display().is_table_inside() && child.computed_values().border_collapse() == CSS::BorderCollapse::Separate)
  143. paint_table_borders(context, verify_cast<PaintableBox>(child));
  144. paint_descendants(context, child, StackingContextPaintPhase::BackgroundAndBorders);
  145. }
  146. paint_descendants(context, child, phase);
  147. break;
  148. case StackingContextPaintPhase::Foreground:
  149. paint_node(child, context, PaintPhase::Foreground);
  150. paint_descendants(context, child, phase);
  151. break;
  152. case StackingContextPaintPhase::FocusAndOverlay:
  153. paint_node(child, context, PaintPhase::Outline);
  154. paint_node(child, context, PaintPhase::Overlay);
  155. paint_descendants(context, child, phase);
  156. break;
  157. }
  158. });
  159. paintable.clear_clip_overflow_rect(context, to_paint_phase(phase));
  160. paintable.after_children_paint(context, to_paint_phase(phase));
  161. }
  162. void StackingContext::paint_child(PaintContext& context, StackingContext const& child)
  163. {
  164. auto parent_paintable = child.paintable_box().parent();
  165. if (parent_paintable)
  166. parent_paintable->before_children_paint(context, PaintPhase::Foreground);
  167. child.paint(context);
  168. if (parent_paintable)
  169. parent_paintable->after_children_paint(context, PaintPhase::Foreground);
  170. }
  171. void StackingContext::paint_internal(PaintContext& context) const
  172. {
  173. // For a more elaborate description of the algorithm, see CSS 2.1 Appendix E
  174. // Draw the background and borders for the context root (steps 1, 2)
  175. paint_node(paintable_box(), context, PaintPhase::Background);
  176. paint_node(paintable_box(), context, PaintPhase::Border);
  177. // Stacking contexts formed by positioned descendants with negative z-indices (excluding 0) in z-index order
  178. // (most negative first) then tree order. (step 3)
  179. for (auto* child : m_children) {
  180. if (!child->paintable_box().is_positioned())
  181. continue;
  182. if (child->paintable_box().computed_values().z_index().has_value() && child->paintable_box().computed_values().z_index().value() < 0)
  183. paint_child(context, *child);
  184. }
  185. // Draw the background and borders for block-level children (step 4)
  186. paint_descendants(context, paintable_box(), StackingContextPaintPhase::BackgroundAndBorders);
  187. // Draw the non-positioned floats (step 5)
  188. paint_descendants(context, paintable_box(), StackingContextPaintPhase::Floats);
  189. // Draw inline content, replaced content, etc. (steps 6, 7)
  190. paint_descendants(context, paintable_box(), StackingContextPaintPhase::BackgroundAndBordersForInlineLevelAndReplaced);
  191. paint_node(paintable_box(), context, PaintPhase::Foreground);
  192. paint_descendants(context, paintable_box(), StackingContextPaintPhase::Foreground);
  193. // Draw positioned descendants with z-index `0` or `auto` in tree order. (step 8)
  194. // FIXME: There's more to this step that we have yet to understand and implement.
  195. paintable_box().for_each_in_subtree([&context](Paintable const& paintable) {
  196. auto const& z_index = paintable.computed_values().z_index();
  197. if (!paintable.is_positioned() || (z_index.has_value() && z_index.value() != 0)) {
  198. return paintable.stacking_context_rooted_here()
  199. ? TraversalDecision::SkipChildrenAndContinue
  200. : TraversalDecision::Continue;
  201. }
  202. // At this point, `paintable_box` is a positioned descendant with z-index: auto.
  203. // FIXME: This is basically duplicating logic found elsewhere in this same function. Find a way to make this more elegant.
  204. auto exit_decision = TraversalDecision::Continue;
  205. auto* parent_paintable = paintable.parent();
  206. if (parent_paintable)
  207. parent_paintable->before_children_paint(context, PaintPhase::Foreground);
  208. auto containing_block = paintable.containing_block();
  209. auto* containing_block_paintable = containing_block ? containing_block->paintable() : nullptr;
  210. if (containing_block_paintable)
  211. containing_block_paintable->apply_clip_overflow_rect(context, PaintPhase::Foreground);
  212. if (auto* child = paintable.stacking_context_rooted_here()) {
  213. paint_child(context, *child);
  214. exit_decision = TraversalDecision::SkipChildrenAndContinue;
  215. } else {
  216. paint_node_as_stacking_context(paintable, context);
  217. }
  218. if (parent_paintable)
  219. parent_paintable->after_children_paint(context, PaintPhase::Foreground);
  220. if (containing_block_paintable)
  221. containing_block_paintable->clear_clip_overflow_rect(context, PaintPhase::Foreground);
  222. return exit_decision;
  223. });
  224. // Stacking contexts formed by positioned descendants with z-indices greater than or equal to 1 in z-index order
  225. // (smallest first) then tree order. (Step 9)
  226. for (auto* child : m_children) {
  227. if (!child->paintable_box().is_positioned())
  228. continue;
  229. auto containing_block = child->paintable_box().containing_block();
  230. auto const* containing_block_paintable = containing_block ? containing_block->paintable() : nullptr;
  231. if (containing_block_paintable)
  232. containing_block_paintable->apply_clip_overflow_rect(context, PaintPhase::Foreground);
  233. if (child->paintable_box().computed_values().z_index().has_value() && child->paintable_box().computed_values().z_index().value() >= 1)
  234. paint_child(context, *child);
  235. if (containing_block_paintable)
  236. containing_block_paintable->clear_clip_overflow_rect(context, PaintPhase::Foreground);
  237. }
  238. paint_node(paintable_box(), context, PaintPhase::Outline);
  239. paint_node(paintable_box(), context, PaintPhase::Overlay);
  240. paint_descendants(context, paintable_box(), StackingContextPaintPhase::FocusAndOverlay);
  241. }
  242. Gfx::FloatMatrix4x4 StackingContext::combine_transformations(Vector<CSS::Transformation> const& transformations) const
  243. {
  244. auto matrix = Gfx::FloatMatrix4x4::identity();
  245. for (auto const& transform : transformations)
  246. matrix = matrix * transform.to_matrix(paintable_box());
  247. return matrix;
  248. }
  249. // FIXME: This extracts the affine 2D part of the full transformation matrix.
  250. // Use the whole matrix when we get better transformation support in LibGfx or use LibGL for drawing the bitmap
  251. Gfx::AffineTransform StackingContext::affine_transform_matrix() const
  252. {
  253. return Gfx::extract_2d_affine_transform(m_transform);
  254. }
  255. static Gfx::FloatMatrix4x4 matrix_with_scaled_translation(Gfx::FloatMatrix4x4 matrix, float scale)
  256. {
  257. auto* m = matrix.elements();
  258. m[0][3] *= scale;
  259. m[1][3] *= scale;
  260. m[2][3] *= scale;
  261. return matrix;
  262. }
  263. void StackingContext::paint(PaintContext& context) const
  264. {
  265. auto opacity = paintable_box().computed_values().opacity();
  266. if (opacity == 0.0f)
  267. return;
  268. RecordingPainterStateSaver saver(context.recording_painter());
  269. auto to_device_pixels_scale = float(context.device_pixels_per_css_pixel());
  270. RecordingPainter::PushStackingContextParams push_stacking_context_params {
  271. .opacity = opacity,
  272. .is_fixed_position = paintable_box().is_fixed_position(),
  273. .source_paintable_rect = context.enclosing_device_rect(paintable_box().absolute_paint_rect()).to_type<int>(),
  274. .image_rendering = paintable_box().computed_values().image_rendering(),
  275. .transform = {
  276. .origin = transform_origin().scaled(to_device_pixels_scale),
  277. .matrix = matrix_with_scaled_translation(transform_matrix(), to_device_pixels_scale),
  278. },
  279. };
  280. if (auto masking_area = paintable_box().get_masking_area(); masking_area.has_value()) {
  281. if (masking_area->is_empty())
  282. return;
  283. auto mask_bitmap = paintable_box().calculate_mask(context, *masking_area);
  284. push_stacking_context_params.source_paintable_rect = context.enclosing_device_rect(*masking_area).to_type<int>();
  285. push_stacking_context_params.mask = StackingContextMask {
  286. .mask_bitmap = mask_bitmap.release_nonnull(),
  287. .mask_kind = *paintable_box().get_mask_type()
  288. };
  289. }
  290. context.recording_painter().push_stacking_context(push_stacking_context_params);
  291. paint_internal(context);
  292. context.recording_painter().pop_stacking_context();
  293. }
  294. Gfx::FloatPoint StackingContext::compute_transform_origin() const
  295. {
  296. auto style_value = paintable_box().computed_values().transform_origin();
  297. // FIXME: respect transform-box property
  298. auto reference_box = paintable_box().absolute_border_box_rect();
  299. auto x = reference_box.left() + style_value.x.to_px(paintable_box().layout_node(), reference_box.width());
  300. auto y = reference_box.top() + style_value.y.to_px(paintable_box().layout_node(), reference_box.height());
  301. return { x.to_float(), y.to_float() };
  302. }
  303. template<typename U, typename Callback>
  304. static TraversalDecision for_each_in_inclusive_subtree_of_type_within_same_stacking_context_in_reverse(Paintable const& paintable, Callback callback)
  305. {
  306. if (paintable.stacking_context_rooted_here()) {
  307. // Note: Include the stacking context (so we can hit test it), but don't recurse into it.
  308. if (auto decision = callback(static_cast<U const&>(paintable)); decision != TraversalDecision::Continue)
  309. return decision;
  310. return TraversalDecision::SkipChildrenAndContinue;
  311. }
  312. for (auto* child = paintable.last_child(); child; child = child->previous_sibling()) {
  313. if (for_each_in_inclusive_subtree_of_type_within_same_stacking_context_in_reverse<U>(*child, callback) == TraversalDecision::Break)
  314. return TraversalDecision::Break;
  315. }
  316. if (is<U>(paintable)) {
  317. if (auto decision = callback(static_cast<U const&>(paintable)); decision != TraversalDecision::Continue)
  318. return decision;
  319. }
  320. return TraversalDecision::Continue;
  321. }
  322. template<typename U, typename Callback>
  323. static TraversalDecision for_each_in_subtree_of_type_within_same_stacking_context_in_reverse(Paintable const& paintable, Callback callback)
  324. {
  325. for (auto* child = paintable.last_child(); child; child = child->previous_sibling()) {
  326. if (for_each_in_inclusive_subtree_of_type_within_same_stacking_context_in_reverse<U>(*child, callback) == TraversalDecision::Break)
  327. return TraversalDecision::Break;
  328. }
  329. return TraversalDecision::Continue;
  330. }
  331. Optional<HitTestResult> StackingContext::hit_test(CSSPixelPoint position, HitTestType type) const
  332. {
  333. if (!paintable_box().is_visible())
  334. return {};
  335. auto transform_origin = this->transform_origin().to_type<CSSPixels>();
  336. // NOTE: This CSSPixels -> Float -> CSSPixels conversion is because we can't AffineTransform::map() a CSSPixelPoint.
  337. Gfx::FloatPoint offset_position {
  338. (position.x() - transform_origin.x()).to_float(),
  339. (position.y() - transform_origin.y()).to_float()
  340. };
  341. auto transformed_position = affine_transform_matrix().inverse().value_or({}).map(offset_position).to_type<CSSPixels>() + transform_origin;
  342. if (paintable_box().is_fixed_position()) {
  343. auto scroll_offset = paintable_box().document().navigable()->viewport_scroll_offset();
  344. transformed_position.translate_by(-scroll_offset);
  345. }
  346. // FIXME: Support more overflow variations.
  347. if (paintable_box().computed_values().overflow_x() == CSS::Overflow::Hidden && paintable_box().computed_values().overflow_y() == CSS::Overflow::Hidden) {
  348. if (!paintable_box().absolute_border_box_rect().contains(transformed_position.x(), transformed_position.y()))
  349. return {};
  350. }
  351. // NOTE: Hit testing basically happens in reverse painting order.
  352. // https://www.w3.org/TR/CSS22/visuren.html#z-index
  353. // 7. the child stacking contexts with positive stack levels (least positive first).
  354. // NOTE: Hit testing follows reverse painting order, that's why the conditions here are reversed.
  355. for (ssize_t i = m_children.size() - 1; i >= 0; --i) {
  356. auto const& child = *m_children[i];
  357. if (child.paintable_box().computed_values().z_index().value_or(0) <= 0)
  358. break;
  359. auto result = child.hit_test(transformed_position, type);
  360. if (result.has_value() && result->paintable->visible_for_hit_testing())
  361. return result;
  362. }
  363. // 6. the child stacking contexts with stack level 0 and the positioned descendants with stack level 0.
  364. Optional<HitTestResult> result;
  365. for_each_in_subtree_of_type_within_same_stacking_context_in_reverse<PaintableBox>(paintable_box(), [&](PaintableBox const& paintable_box) {
  366. // FIXME: Support more overflow variations.
  367. if (paintable_box.computed_values().overflow_x() == CSS::Overflow::Hidden && paintable_box.computed_values().overflow_y() == CSS::Overflow::Hidden) {
  368. if (!paintable_box.absolute_border_box_rect().contains(transformed_position.x(), transformed_position.y()))
  369. return TraversalDecision::SkipChildrenAndContinue;
  370. }
  371. auto const& z_index = paintable_box.computed_values().z_index();
  372. if (z_index.value_or(0) == 0 && paintable_box.is_positioned() && !paintable_box.stacking_context()) {
  373. auto candidate = paintable_box.hit_test(transformed_position, type);
  374. if (candidate.has_value() && candidate->paintable->visible_for_hit_testing()) {
  375. result = move(candidate);
  376. return TraversalDecision::Break;
  377. }
  378. }
  379. if (paintable_box.stacking_context()) {
  380. if (z_index.value_or(0) == 0) {
  381. auto candidate = paintable_box.stacking_context()->hit_test(transformed_position, type);
  382. if (candidate.has_value() && candidate->paintable->visible_for_hit_testing()) {
  383. result = move(candidate);
  384. return TraversalDecision::Break;
  385. }
  386. }
  387. }
  388. return TraversalDecision::Continue;
  389. });
  390. if (result.has_value())
  391. return result;
  392. // 5. the in-flow, inline-level, non-positioned descendants, including inline tables and inline blocks.
  393. if (paintable_box().layout_box().children_are_inline() && is<Layout::BlockContainer>(paintable_box().layout_box())) {
  394. auto result = paintable_box().hit_test(transformed_position, type);
  395. if (result.has_value() && result->paintable->visible_for_hit_testing())
  396. return result;
  397. }
  398. // 4. the non-positioned floats.
  399. for_each_in_subtree_of_type_within_same_stacking_context_in_reverse<PaintableBox>(paintable_box(), [&](PaintableBox const& paintable_box) {
  400. // FIXME: Support more overflow variations.
  401. if (paintable_box.computed_values().overflow_x() == CSS::Overflow::Hidden && paintable_box.computed_values().overflow_y() == CSS::Overflow::Hidden) {
  402. if (!paintable_box.absolute_border_box_rect().contains(transformed_position.x(), transformed_position.y()))
  403. return TraversalDecision::SkipChildrenAndContinue;
  404. }
  405. if (paintable_box.is_floating()) {
  406. if (auto candidate = paintable_box.hit_test(transformed_position, type); candidate.has_value()) {
  407. result = move(candidate);
  408. return TraversalDecision::Break;
  409. }
  410. }
  411. return TraversalDecision::Continue;
  412. });
  413. if (result.has_value() && result->paintable->visible_for_hit_testing())
  414. return result;
  415. // 3. the in-flow, non-inline-level, non-positioned descendants.
  416. if (!paintable_box().layout_box().children_are_inline()) {
  417. for_each_in_subtree_of_type_within_same_stacking_context_in_reverse<PaintableBox>(paintable_box(), [&](PaintableBox const& paintable_box) {
  418. // FIXME: Support more overflow variations.
  419. if (paintable_box.computed_values().overflow_x() == CSS::Overflow::Hidden && paintable_box.computed_values().overflow_y() == CSS::Overflow::Hidden) {
  420. if (!paintable_box.absolute_border_box_rect().contains(transformed_position.x(), transformed_position.y()))
  421. return TraversalDecision::SkipChildrenAndContinue;
  422. }
  423. if (!paintable_box.is_absolutely_positioned() && !paintable_box.is_floating()) {
  424. if (auto candidate = paintable_box.hit_test(transformed_position, type); candidate.has_value()) {
  425. result = move(candidate);
  426. return TraversalDecision::Break;
  427. }
  428. }
  429. return TraversalDecision::Continue;
  430. });
  431. if (result.has_value() && result->paintable->visible_for_hit_testing())
  432. return result;
  433. }
  434. // 2. the child stacking contexts with negative stack levels (most negative first).
  435. // NOTE: Hit testing follows reverse painting order, that's why the conditions here are reversed.
  436. for (ssize_t i = m_children.size() - 1; i >= 0; --i) {
  437. auto const& child = *m_children[i];
  438. if (child.paintable_box().computed_values().z_index().value_or(0) >= 0)
  439. break;
  440. auto result = child.hit_test(transformed_position, type);
  441. if (result.has_value() && result->paintable->visible_for_hit_testing())
  442. return result;
  443. }
  444. // 1. the background and borders of the element forming the stacking context.
  445. if (paintable_box().absolute_border_box_rect().contains(transformed_position.x(), transformed_position.y())) {
  446. return HitTestResult {
  447. .paintable = const_cast<PaintableBox&>(paintable_box()),
  448. };
  449. }
  450. return {};
  451. }
  452. void StackingContext::dump(int indent) const
  453. {
  454. StringBuilder builder;
  455. for (int i = 0; i < indent; ++i)
  456. builder.append(' ');
  457. builder.appendff("SC for {} {} [children: {}] (z-index: ", paintable_box().layout_box().debug_description(), paintable_box().absolute_rect(), m_children.size());
  458. if (paintable_box().computed_values().z_index().has_value())
  459. builder.appendff("{}", paintable_box().computed_values().z_index().value());
  460. else
  461. builder.append("auto"sv);
  462. builder.append(')');
  463. auto affine_transform = affine_transform_matrix();
  464. if (!affine_transform.is_identity()) {
  465. builder.appendff(", transform: {}", affine_transform);
  466. }
  467. dbgln("{}", builder.string_view());
  468. for (auto& child : m_children)
  469. child->dump(indent + 1);
  470. }
  471. }