StackingContext.cpp 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544
  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/Painter.h>
  14. #include <LibGfx/Rect.h>
  15. #include <LibWeb/Layout/Box.h>
  16. #include <LibWeb/Layout/InitialContainingBlock.h>
  17. #include <LibWeb/Layout/ReplacedBox.h>
  18. #include <LibWeb/Painting/PaintableBox.h>
  19. #include <LibWeb/Painting/StackingContext.h>
  20. namespace Web::Painting {
  21. static void paint_node(Layout::Node const& layout_node, PaintContext& context, PaintPhase phase)
  22. {
  23. if (auto const* paintable = layout_node.paintable())
  24. paintable->paint(context, phase);
  25. }
  26. StackingContext::StackingContext(Layout::Box& box, StackingContext* parent)
  27. : m_box(box)
  28. , m_transform(combine_transformations(m_box.computed_values().transformations()))
  29. , m_transform_origin(compute_transform_origin())
  30. , m_parent(parent)
  31. {
  32. VERIFY(m_parent != this);
  33. if (m_parent)
  34. m_parent->m_children.append(this);
  35. }
  36. void StackingContext::sort()
  37. {
  38. quick_sort(m_children, [](auto& a, auto& b) {
  39. auto a_z_index = a->m_box.computed_values().z_index().value_or(0);
  40. auto b_z_index = b->m_box.computed_values().z_index().value_or(0);
  41. if (a_z_index == b_z_index)
  42. return a->m_box.is_before(b->m_box);
  43. return a_z_index < b_z_index;
  44. });
  45. for (auto* child : m_children)
  46. child->sort();
  47. }
  48. static PaintPhase to_paint_phase(StackingContext::StackingContextPaintPhase phase)
  49. {
  50. // There are not a fully correct mapping since some stacking context phases are combined.
  51. switch (phase) {
  52. case StackingContext::StackingContextPaintPhase::Floats:
  53. case StackingContext::StackingContextPaintPhase::BackgroundAndBordersForInlineLevelAndReplaced:
  54. case StackingContext::StackingContextPaintPhase::BackgroundAndBorders:
  55. return PaintPhase::Background;
  56. case StackingContext::StackingContextPaintPhase::Foreground:
  57. return PaintPhase::Foreground;
  58. case StackingContext::StackingContextPaintPhase::FocusAndOverlay:
  59. return PaintPhase::Overlay;
  60. default:
  61. VERIFY_NOT_REACHED();
  62. }
  63. }
  64. void StackingContext::paint_descendants(PaintContext& context, Layout::Node const& box, StackingContextPaintPhase phase) const
  65. {
  66. if (auto* paintable = box.paintable())
  67. paintable->before_children_paint(context, to_paint_phase(phase), Paintable::ShouldClipOverflow::Yes);
  68. box.for_each_child([&](auto& child) {
  69. // If `child` establishes its own stacking context, skip over it.
  70. if (is<Layout::Box>(child) && child.paintable() && static_cast<Layout::Box const&>(child).paint_box()->stacking_context())
  71. return;
  72. // If `child` is positioned with a z-index of `0` or `auto`, skip over it.
  73. if (child.is_positioned()) {
  74. auto const& z_index = child.computed_values().z_index();
  75. if (!z_index.has_value() || z_index.value() == 0)
  76. return;
  77. }
  78. bool child_is_inline_or_replaced = child.is_inline() || is<Layout::ReplacedBox>(child);
  79. switch (phase) {
  80. case StackingContextPaintPhase::BackgroundAndBorders:
  81. if (!child_is_inline_or_replaced && !child.is_floating()) {
  82. paint_node(child, context, PaintPhase::Background);
  83. paint_node(child, context, PaintPhase::Border);
  84. paint_descendants(context, child, phase);
  85. }
  86. break;
  87. case StackingContextPaintPhase::Floats:
  88. if (child.is_floating()) {
  89. paint_node(child, context, PaintPhase::Background);
  90. paint_node(child, context, PaintPhase::Border);
  91. paint_descendants(context, child, StackingContextPaintPhase::BackgroundAndBorders);
  92. }
  93. paint_descendants(context, child, phase);
  94. break;
  95. case StackingContextPaintPhase::BackgroundAndBordersForInlineLevelAndReplaced:
  96. if (child_is_inline_or_replaced) {
  97. paint_node(child, context, PaintPhase::Background);
  98. paint_node(child, context, PaintPhase::Border);
  99. paint_descendants(context, child, StackingContextPaintPhase::BackgroundAndBorders);
  100. }
  101. paint_descendants(context, child, phase);
  102. break;
  103. case StackingContextPaintPhase::Foreground:
  104. paint_node(child, context, PaintPhase::Foreground);
  105. paint_descendants(context, child, phase);
  106. break;
  107. case StackingContextPaintPhase::FocusAndOverlay:
  108. if (context.has_focus()) {
  109. paint_node(child, context, PaintPhase::FocusOutline);
  110. }
  111. paint_node(child, context, PaintPhase::Overlay);
  112. paint_descendants(context, child, phase);
  113. break;
  114. }
  115. });
  116. if (auto* paintable = box.paintable())
  117. paintable->after_children_paint(context, to_paint_phase(phase), Paintable::ShouldClipOverflow::Yes);
  118. }
  119. void StackingContext::paint_internal(PaintContext& context) const
  120. {
  121. // For a more elaborate description of the algorithm, see CSS 2.1 Appendix E
  122. // Draw the background and borders for the context root (steps 1, 2)
  123. paint_node(m_box, context, PaintPhase::Background);
  124. paint_node(m_box, context, PaintPhase::Border);
  125. auto paint_child = [&](auto* child) {
  126. auto parent = child->m_box.parent();
  127. auto should_clip_overflow = child->m_box.is_absolutely_positioned() ? Paintable::ShouldClipOverflow::No : Paintable::ShouldClipOverflow::Yes;
  128. auto* paintable = parent ? parent->paintable() : nullptr;
  129. if (paintable)
  130. paintable->before_children_paint(context, PaintPhase::Foreground, should_clip_overflow);
  131. child->paint(context);
  132. if (paintable)
  133. paintable->after_children_paint(context, PaintPhase::Foreground, should_clip_overflow);
  134. };
  135. // Draw positioned descendants with negative z-indices (step 3)
  136. for (auto* child : m_children) {
  137. if (child->m_box.computed_values().z_index().has_value() && child->m_box.computed_values().z_index().value() < 0)
  138. paint_child(child);
  139. }
  140. // Draw the background and borders for block-level children (step 4)
  141. paint_descendants(context, m_box, StackingContextPaintPhase::BackgroundAndBorders);
  142. // Draw the non-positioned floats (step 5)
  143. paint_descendants(context, m_box, StackingContextPaintPhase::Floats);
  144. // Draw inline content, replaced content, etc. (steps 6, 7)
  145. paint_descendants(context, m_box, StackingContextPaintPhase::BackgroundAndBordersForInlineLevelAndReplaced);
  146. paint_node(m_box, context, PaintPhase::Foreground);
  147. paint_descendants(context, m_box, StackingContextPaintPhase::Foreground);
  148. // Draw positioned descendants with z-index `0` or `auto` in tree order. (step 8)
  149. // NOTE: Non-positioned descendants that establish stacking contexts with z-index `0` or `auto` are also painted here.
  150. // FIXME: There's more to this step that we have yet to understand and implement.
  151. m_box.paint_box()->for_each_in_subtree_of_type<PaintableBox>([&](PaintableBox const& paint_box) {
  152. auto const& z_index = paint_box.computed_values().z_index();
  153. if (auto* child = paint_box.stacking_context()) {
  154. if (!z_index.has_value() || z_index.value() == 0)
  155. paint_child(child);
  156. return TraversalDecision::SkipChildrenAndContinue;
  157. }
  158. if (z_index.has_value() && z_index.value() != 0)
  159. return TraversalDecision::Continue;
  160. if (!paint_box.layout_box().is_positioned())
  161. return TraversalDecision::Continue;
  162. // At this point, `paint_box` is a positioned descendant with z-index: auto
  163. // but no stacking context of its own.
  164. // FIXME: This is basically duplicating logic found elsewhere in this same function. Find a way to make this more elegant.
  165. paint_node(paint_box.layout_box(), context, PaintPhase::Background);
  166. paint_node(paint_box.layout_box(), context, PaintPhase::Border);
  167. paint_descendants(context, paint_box.layout_box(), StackingContextPaintPhase::BackgroundAndBorders);
  168. paint_descendants(context, paint_box.layout_box(), StackingContextPaintPhase::Floats);
  169. paint_descendants(context, paint_box.layout_box(), StackingContextPaintPhase::BackgroundAndBordersForInlineLevelAndReplaced);
  170. paint_node(paint_box.layout_box(), context, PaintPhase::Foreground);
  171. paint_descendants(context, paint_box.layout_box(), StackingContextPaintPhase::Foreground);
  172. paint_node(paint_box.layout_box(), context, PaintPhase::FocusOutline);
  173. paint_node(paint_box.layout_box(), context, PaintPhase::Overlay);
  174. paint_descendants(context, paint_box.layout_box(), StackingContextPaintPhase::FocusAndOverlay);
  175. return TraversalDecision::Continue;
  176. });
  177. // Draw other positioned descendants (step 9)
  178. for (auto* child : m_children) {
  179. if (child->m_box.computed_values().z_index().has_value() && child->m_box.computed_values().z_index().value() >= 1)
  180. paint_child(child);
  181. }
  182. paint_node(m_box, context, PaintPhase::FocusOutline);
  183. paint_node(m_box, context, PaintPhase::Overlay);
  184. paint_descendants(context, m_box, StackingContextPaintPhase::FocusAndOverlay);
  185. }
  186. Gfx::FloatMatrix4x4 StackingContext::get_transformation_matrix(CSS::Transformation const& transformation) const
  187. {
  188. auto count = transformation.values.size();
  189. auto value = [this, transformation](size_t index, Optional<CSS::Length const&> reference_length = {}) -> float {
  190. return transformation.values[index].visit(
  191. [this, reference_length](CSS::LengthPercentage const& value) {
  192. if (reference_length.has_value()) {
  193. return value.resolved(m_box, reference_length.value()).to_px(m_box);
  194. }
  195. return value.length().to_px(m_box);
  196. },
  197. [](CSS::Angle const& value) {
  198. return value.to_degrees() * static_cast<float>(M_DEG2RAD);
  199. },
  200. [](float value) {
  201. return value;
  202. });
  203. };
  204. auto reference_box = paintable().absolute_rect();
  205. auto width = CSS::Length::make_px(reference_box.width());
  206. auto height = CSS::Length::make_px(reference_box.height());
  207. switch (transformation.function) {
  208. case CSS::TransformFunction::Matrix:
  209. if (count == 6)
  210. return Gfx::FloatMatrix4x4(value(0), value(2), 0, value(4),
  211. value(1), value(3), 0, value(5),
  212. 0, 0, 1, 0,
  213. 0, 0, 0, 1);
  214. break;
  215. case CSS::TransformFunction::Matrix3d:
  216. if (count == 16)
  217. return Gfx::FloatMatrix4x4(value(0), value(4), value(8), value(12),
  218. value(1), value(5), value(9), value(13),
  219. value(2), value(6), value(10), value(14),
  220. value(3), value(7), value(11), value(15));
  221. break;
  222. case CSS::TransformFunction::Translate:
  223. if (count == 1)
  224. return Gfx::FloatMatrix4x4(1, 0, 0, value(0, width),
  225. 0, 1, 0, 0,
  226. 0, 0, 1, 0,
  227. 0, 0, 0, 1);
  228. if (count == 2)
  229. return Gfx::FloatMatrix4x4(1, 0, 0, value(0, width),
  230. 0, 1, 0, value(1, height),
  231. 0, 0, 1, 0,
  232. 0, 0, 0, 1);
  233. break;
  234. case CSS::TransformFunction::Translate3d:
  235. return Gfx::FloatMatrix4x4(1, 0, 0, value(0, width),
  236. 0, 1, 0, value(1, height),
  237. 0, 0, 1, value(2),
  238. 0, 0, 0, 1);
  239. break;
  240. case CSS::TransformFunction::TranslateX:
  241. if (count == 1)
  242. return Gfx::FloatMatrix4x4(1, 0, 0, value(0, width),
  243. 0, 1, 0, 0,
  244. 0, 0, 1, 0,
  245. 0, 0, 0, 1);
  246. break;
  247. case CSS::TransformFunction::TranslateY:
  248. if (count == 1)
  249. return Gfx::FloatMatrix4x4(1, 0, 0, 0,
  250. 0, 1, 0, value(0, height),
  251. 0, 0, 1, 0,
  252. 0, 0, 0, 1);
  253. break;
  254. case CSS::TransformFunction::Scale:
  255. if (count == 1)
  256. return Gfx::FloatMatrix4x4(value(0), 0, 0, 0,
  257. 0, value(0), 0, 0,
  258. 0, 0, 1, 0,
  259. 0, 0, 0, 1);
  260. if (count == 2)
  261. return Gfx::FloatMatrix4x4(value(0), 0, 0, 0,
  262. 0, value(1), 0, 0,
  263. 0, 0, 1, 0,
  264. 0, 0, 0, 1);
  265. break;
  266. case CSS::TransformFunction::ScaleX:
  267. if (count == 1)
  268. return Gfx::FloatMatrix4x4(value(0), 0, 0, 0,
  269. 0, 1, 0, 0,
  270. 0, 0, 1, 0,
  271. 0, 0, 0, 1);
  272. break;
  273. case CSS::TransformFunction::ScaleY:
  274. if (count == 1)
  275. return Gfx::FloatMatrix4x4(1, 0, 0, 0,
  276. 0, value(0), 0, 0,
  277. 0, 0, 1, 0,
  278. 0, 0, 0, 1);
  279. break;
  280. case CSS::TransformFunction::RotateX:
  281. if (count == 1)
  282. return Gfx::rotation_matrix({ 1.0f, 0.0f, 0.0f }, value(0));
  283. break;
  284. case CSS::TransformFunction::RotateY:
  285. if (count == 1)
  286. return Gfx::rotation_matrix({ 0.0f, 1.0f, 0.0f }, value(0));
  287. break;
  288. case CSS::TransformFunction::Rotate:
  289. case CSS::TransformFunction::RotateZ:
  290. if (count == 1)
  291. return Gfx::rotation_matrix({ 0.0f, 0.0f, 1.0f }, value(0));
  292. break;
  293. default:
  294. dbgln_if(LIBWEB_CSS_DEBUG, "FIXME: Unhandled transformation function {}", CSS::TransformationStyleValue::create(transformation.function, {})->to_string());
  295. }
  296. return Gfx::FloatMatrix4x4::identity();
  297. }
  298. Gfx::FloatMatrix4x4 StackingContext::combine_transformations(Vector<CSS::Transformation> const& transformations) const
  299. {
  300. auto matrix = Gfx::FloatMatrix4x4::identity();
  301. for (auto const& transform : transformations)
  302. matrix = matrix * get_transformation_matrix(transform);
  303. return matrix;
  304. }
  305. // FIXME: This extracts the affine 2D part of the full transformation matrix.
  306. // Use the whole matrix when we get better transformation support in LibGfx or use LibGL for drawing the bitmap
  307. Gfx::AffineTransform StackingContext::affine_transform_matrix() const
  308. {
  309. auto* m = m_transform.elements();
  310. return Gfx::AffineTransform(m[0][0], m[1][0], m[0][1], m[1][1], m[0][3], m[1][3]);
  311. }
  312. void StackingContext::paint(PaintContext& context) const
  313. {
  314. Gfx::PainterStateSaver saver(context.painter());
  315. if (m_box.is_fixed_position()) {
  316. context.painter().translate(-context.painter().translation());
  317. }
  318. auto opacity = m_box.computed_values().opacity();
  319. if (opacity == 0.0f)
  320. return;
  321. auto affine_transform = affine_transform_matrix();
  322. if (opacity < 1.0f || !affine_transform.is_identity_or_translation()) {
  323. auto transform_origin = this->transform_origin();
  324. auto source_rect = paintable().absolute_paint_rect().translated(-transform_origin);
  325. auto transformed_destination_rect = affine_transform.map(source_rect).translated(transform_origin);
  326. auto destination_rect = transformed_destination_rect.to_rounded<int>();
  327. // FIXME: We should find a way to scale the paintable, rather than paint into a separate bitmap,
  328. // then scale it. This snippet now copies the background at the destination, then scales it down/up
  329. // to the size of the source (which could add some artefacts, though just scaling the bitmap already does that).
  330. // We need to copy the background at the destination because a bunch of our rendering effects now rely on
  331. // being able to sample the painter (see border radii, shadows, filters, etc).
  332. Gfx::FloatPoint destination_clipped_fixup {};
  333. auto try_get_scaled_destination_bitmap = [&]() -> ErrorOr<NonnullRefPtr<Gfx::Bitmap>> {
  334. Gfx::IntRect actual_destination_rect;
  335. auto bitmap = TRY(context.painter().get_region_bitmap(destination_rect, Gfx::BitmapFormat::BGRA8888, actual_destination_rect));
  336. // get_region_bitmap() may clip to a smaller region if the requested rect goes outside the painter, so we need to account for that.
  337. destination_clipped_fixup = Gfx::FloatPoint { destination_rect.location() - actual_destination_rect.location() };
  338. destination_rect = actual_destination_rect;
  339. if (source_rect.size() != transformed_destination_rect.size()) {
  340. auto sx = static_cast<float>(source_rect.width()) / transformed_destination_rect.width();
  341. auto sy = static_cast<float>(source_rect.height()) / transformed_destination_rect.height();
  342. bitmap = TRY(bitmap->scaled(sx, sy));
  343. destination_clipped_fixup.scale_by(sx, sy);
  344. }
  345. return bitmap;
  346. };
  347. auto bitmap_or_error = try_get_scaled_destination_bitmap();
  348. if (bitmap_or_error.is_error())
  349. return;
  350. auto bitmap = bitmap_or_error.release_value_but_fixme_should_propagate_errors();
  351. Gfx::Painter painter(bitmap);
  352. painter.translate((-paintable().absolute_paint_rect().location() + destination_clipped_fixup).to_rounded<int>());
  353. auto paint_context = context.clone(painter);
  354. paint_internal(paint_context);
  355. if (destination_rect.size() == bitmap->size())
  356. context.painter().blit(destination_rect.location(), *bitmap, bitmap->rect(), opacity);
  357. else
  358. context.painter().draw_scaled_bitmap(destination_rect, *bitmap, bitmap->rect(), opacity, Gfx::Painter::ScalingMode::BilinearBlend);
  359. } else {
  360. Gfx::PainterStateSaver saver(context.painter());
  361. context.painter().translate(affine_transform.translation().to_rounded<int>());
  362. paint_internal(context);
  363. }
  364. }
  365. Gfx::FloatPoint StackingContext::compute_transform_origin() const
  366. {
  367. auto style_value = m_box.computed_values().transform_origin();
  368. // FIXME: respect transform-box property
  369. auto reference_box = paintable().absolute_border_box_rect();
  370. auto x = reference_box.left() + style_value.x.resolved(m_box, CSS::Length::make_px(reference_box.width())).to_px(m_box);
  371. auto y = reference_box.top() + style_value.y.resolved(m_box, CSS::Length::make_px(reference_box.height())).to_px(m_box);
  372. return { x, y };
  373. }
  374. Optional<HitTestResult> StackingContext::hit_test(Gfx::FloatPoint const& position, HitTestType type) const
  375. {
  376. if (!m_box.is_visible())
  377. return {};
  378. auto transform_origin = this->transform_origin();
  379. auto transformed_position = affine_transform_matrix().inverse().value_or({}).map(position - transform_origin) + transform_origin;
  380. // FIXME: Support more overflow variations.
  381. if (paintable().computed_values().overflow_x() == CSS::Overflow::Hidden && paintable().computed_values().overflow_y() == CSS::Overflow::Hidden) {
  382. if (!paintable().absolute_border_box_rect().contains(transformed_position.x(), transformed_position.y()))
  383. return {};
  384. }
  385. // NOTE: Hit testing basically happens in reverse painting order.
  386. // https://www.w3.org/TR/CSS22/visuren.html#z-index
  387. // 7. the child stacking contexts with positive stack levels (least positive first).
  388. // NOTE: Hit testing follows reverse painting order, that's why the conditions here are reversed.
  389. for (ssize_t i = m_children.size() - 1; i >= 0; --i) {
  390. auto const& child = *m_children[i];
  391. if (child.m_box.computed_values().z_index().value_or(0) < 0)
  392. break;
  393. auto result = child.hit_test(transformed_position, type);
  394. if (result.has_value() && result->paintable->visible_for_hit_testing())
  395. return result;
  396. }
  397. Optional<HitTestResult> result;
  398. // 6. the child stacking contexts with stack level 0 and the positioned descendants with stack level 0.
  399. paintable().for_each_in_subtree_of_type<PaintableBox>([&](auto& paint_box) {
  400. // FIXME: Support more overflow variations.
  401. if (paint_box.computed_values().overflow_x() == CSS::Overflow::Hidden && paint_box.computed_values().overflow_y() == CSS::Overflow::Hidden) {
  402. if (!paint_box.absolute_border_box_rect().contains(transformed_position.x(), transformed_position.y()))
  403. return TraversalDecision::SkipChildrenAndContinue;
  404. }
  405. auto& layout_box = paint_box.layout_box();
  406. if (layout_box.is_positioned() && !paint_box.stacking_context()) {
  407. if (auto candidate = paint_box.hit_test(transformed_position, type); candidate.has_value())
  408. result = move(candidate);
  409. }
  410. return TraversalDecision::Continue;
  411. });
  412. if (result.has_value() && result->paintable->visible_for_hit_testing())
  413. return result;
  414. // 5. the in-flow, inline-level, non-positioned descendants, including inline tables and inline blocks.
  415. if (m_box.children_are_inline() && is<Layout::BlockContainer>(m_box)) {
  416. auto result = paintable().hit_test(transformed_position, type);
  417. if (result.has_value() && result->paintable->visible_for_hit_testing())
  418. return result;
  419. }
  420. // 4. the non-positioned floats.
  421. paintable().for_each_in_subtree_of_type<PaintableBox>([&](auto const& paint_box) {
  422. // FIXME: Support more overflow variations.
  423. if (paint_box.computed_values().overflow_x() == CSS::Overflow::Hidden && paint_box.computed_values().overflow_y() == CSS::Overflow::Hidden) {
  424. if (!paint_box.absolute_border_box_rect().contains(transformed_position.x(), transformed_position.y()))
  425. return TraversalDecision::SkipChildrenAndContinue;
  426. }
  427. auto& layout_box = paint_box.layout_box();
  428. if (layout_box.is_floating()) {
  429. if (auto candidate = paint_box.hit_test(transformed_position, type); candidate.has_value())
  430. result = move(candidate);
  431. }
  432. return TraversalDecision::Continue;
  433. });
  434. if (result.has_value() && result->paintable->visible_for_hit_testing())
  435. return result;
  436. // 3. the in-flow, non-inline-level, non-positioned descendants.
  437. if (!m_box.children_are_inline()) {
  438. paintable().for_each_in_subtree_of_type<PaintableBox>([&](auto const& paint_box) {
  439. // FIXME: Support more overflow variations.
  440. if (paint_box.computed_values().overflow_x() == CSS::Overflow::Hidden && paint_box.computed_values().overflow_y() == CSS::Overflow::Hidden) {
  441. if (!paint_box.absolute_border_box_rect().contains(transformed_position.x(), transformed_position.y()))
  442. return TraversalDecision::SkipChildrenAndContinue;
  443. }
  444. auto& layout_box = paint_box.layout_box();
  445. if (!layout_box.is_absolutely_positioned() && !layout_box.is_floating()) {
  446. if (auto candidate = paint_box.hit_test(transformed_position, type); candidate.has_value())
  447. result = move(candidate);
  448. }
  449. return TraversalDecision::Continue;
  450. });
  451. if (result.has_value() && result->paintable->visible_for_hit_testing())
  452. return result;
  453. }
  454. // 2. the child stacking contexts with negative stack levels (most negative first).
  455. // NOTE: Hit testing follows reverse painting order, that's why the conditions here are reversed.
  456. for (ssize_t i = m_children.size() - 1; i >= 0; --i) {
  457. auto const& child = *m_children[i];
  458. if (child.m_box.computed_values().z_index().value_or(0) >= 0)
  459. break;
  460. auto result = child.hit_test(transformed_position, type);
  461. if (result.has_value() && result->paintable->visible_for_hit_testing())
  462. return result;
  463. }
  464. // 1. the background and borders of the element forming the stacking context.
  465. if (paintable().absolute_border_box_rect().contains(transformed_position)) {
  466. return HitTestResult {
  467. .paintable = paintable(),
  468. };
  469. }
  470. return {};
  471. }
  472. void StackingContext::dump(int indent) const
  473. {
  474. StringBuilder builder;
  475. for (int i = 0; i < indent; ++i)
  476. builder.append(' ');
  477. builder.appendff("SC for {} {} [children: {}] (z-index: ", m_box.debug_description(), paintable().absolute_rect(), m_children.size());
  478. if (m_box.computed_values().z_index().has_value())
  479. builder.appendff("{}", m_box.computed_values().z_index().value());
  480. else
  481. builder.append("auto"sv);
  482. builder.append(')');
  483. auto affine_transform = affine_transform_matrix();
  484. if (!affine_transform.is_identity()) {
  485. builder.appendff(", transform: {}", affine_transform);
  486. }
  487. dbgln("{}", builder.string_view());
  488. for (auto& child : m_children)
  489. child->dump(indent + 1);
  490. }
  491. }