StackingContext.cpp 27 KB

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