StackingContext.cpp 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588
  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));
  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));
  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* paintable = parent ? parent->paintable() : nullptr;
  128. if (paintable)
  129. paintable->before_children_paint(context, PaintPhase::Foreground);
  130. child->paint(context);
  131. if (paintable)
  132. paintable->after_children_paint(context, PaintPhase::Foreground);
  133. };
  134. // Draw positioned descendants with negative z-indices (step 3)
  135. for (auto* child : m_children) {
  136. if (child->m_box.computed_values().z_index().has_value() && child->m_box.computed_values().z_index().value() < 0)
  137. paint_child(child);
  138. }
  139. // Draw the background and borders for block-level children (step 4)
  140. paint_descendants(context, m_box, StackingContextPaintPhase::BackgroundAndBorders);
  141. // Draw the non-positioned floats (step 5)
  142. paint_descendants(context, m_box, StackingContextPaintPhase::Floats);
  143. // Draw inline content, replaced content, etc. (steps 6, 7)
  144. paint_descendants(context, m_box, StackingContextPaintPhase::BackgroundAndBordersForInlineLevelAndReplaced);
  145. paint_node(m_box, context, PaintPhase::Foreground);
  146. paint_descendants(context, m_box, StackingContextPaintPhase::Foreground);
  147. // Draw positioned descendants with z-index `0` or `auto` in tree order. (step 8)
  148. // NOTE: Non-positioned descendants that establish stacking contexts with z-index `0` or `auto` are also painted here.
  149. // FIXME: There's more to this step that we have yet to understand and implement.
  150. m_box.paint_box()->for_each_in_subtree_of_type<PaintableBox>([&](PaintableBox const& paint_box) {
  151. auto const& z_index = paint_box.computed_values().z_index();
  152. if (auto* child = paint_box.stacking_context()) {
  153. if (!z_index.has_value() || z_index.value() == 0)
  154. paint_child(child);
  155. return TraversalDecision::SkipChildrenAndContinue;
  156. }
  157. if (z_index.has_value() && z_index.value() != 0)
  158. return TraversalDecision::Continue;
  159. if (!paint_box.layout_box().is_positioned())
  160. return TraversalDecision::Continue;
  161. // At this point, `paint_box` is a positioned descendant with z-index: auto
  162. // but no stacking context of its own.
  163. // FIXME: This is basically duplicating logic found elsewhere in this same function. Find a way to make this more elegant.
  164. paint_node(paint_box.layout_box(), context, PaintPhase::Background);
  165. paint_node(paint_box.layout_box(), context, PaintPhase::Border);
  166. paint_descendants(context, paint_box.layout_box(), StackingContextPaintPhase::BackgroundAndBorders);
  167. paint_descendants(context, paint_box.layout_box(), StackingContextPaintPhase::Floats);
  168. paint_descendants(context, paint_box.layout_box(), StackingContextPaintPhase::BackgroundAndBordersForInlineLevelAndReplaced);
  169. paint_node(paint_box.layout_box(), context, PaintPhase::Foreground);
  170. paint_descendants(context, paint_box.layout_box(), StackingContextPaintPhase::Foreground);
  171. paint_node(paint_box.layout_box(), context, PaintPhase::FocusOutline);
  172. paint_node(paint_box.layout_box(), context, PaintPhase::Overlay);
  173. paint_descendants(context, paint_box.layout_box(), StackingContextPaintPhase::FocusAndOverlay);
  174. return TraversalDecision::Continue;
  175. });
  176. // Draw other positioned descendants (step 9)
  177. for (auto* child : m_children) {
  178. if (child->m_box.computed_values().z_index().has_value() && child->m_box.computed_values().z_index().value() >= 1)
  179. paint_child(child);
  180. }
  181. paint_node(m_box, context, PaintPhase::FocusOutline);
  182. paint_node(m_box, context, PaintPhase::Overlay);
  183. paint_descendants(context, m_box, StackingContextPaintPhase::FocusAndOverlay);
  184. }
  185. Gfx::FloatMatrix4x4 StackingContext::get_transformation_matrix(CSS::Transformation const& transformation) const
  186. {
  187. auto count = transformation.values.size();
  188. auto value = [this, transformation](size_t index, Optional<CSS::Length const&> reference_length = {}) -> float {
  189. return transformation.values[index].visit(
  190. [this, reference_length](CSS::LengthPercentage const& value) {
  191. if (reference_length.has_value()) {
  192. return value.resolved(m_box, reference_length.value()).to_px(m_box);
  193. }
  194. return value.length().to_px(m_box);
  195. },
  196. [](CSS::Angle const& value) {
  197. return value.to_degrees() * static_cast<float>(M_DEG2RAD);
  198. },
  199. [](float value) {
  200. return value;
  201. });
  202. };
  203. auto reference_box = paintable().absolute_rect();
  204. auto width = CSS::Length::make_px(reference_box.width());
  205. auto height = CSS::Length::make_px(reference_box.height());
  206. switch (transformation.function) {
  207. case CSS::TransformFunction::Matrix:
  208. if (count == 6)
  209. return Gfx::FloatMatrix4x4(value(0), value(2), 0, value(4),
  210. value(1), value(3), 0, value(5),
  211. 0, 0, 1, 0,
  212. 0, 0, 0, 1);
  213. break;
  214. case CSS::TransformFunction::Matrix3d:
  215. if (count == 16)
  216. return Gfx::FloatMatrix4x4(value(0), value(4), value(8), value(12),
  217. value(1), value(5), value(9), value(13),
  218. value(2), value(6), value(10), value(14),
  219. value(3), value(7), value(11), value(15));
  220. break;
  221. case CSS::TransformFunction::Translate:
  222. if (count == 1)
  223. return Gfx::FloatMatrix4x4(1, 0, 0, value(0, width),
  224. 0, 1, 0, 0,
  225. 0, 0, 1, 0,
  226. 0, 0, 0, 1);
  227. if (count == 2)
  228. return Gfx::FloatMatrix4x4(1, 0, 0, value(0, width),
  229. 0, 1, 0, value(1, height),
  230. 0, 0, 1, 0,
  231. 0, 0, 0, 1);
  232. break;
  233. case CSS::TransformFunction::Translate3d:
  234. return Gfx::FloatMatrix4x4(1, 0, 0, value(0, width),
  235. 0, 1, 0, value(1, height),
  236. 0, 0, 1, value(2),
  237. 0, 0, 0, 1);
  238. break;
  239. case CSS::TransformFunction::TranslateX:
  240. if (count == 1)
  241. return Gfx::FloatMatrix4x4(1, 0, 0, value(0, width),
  242. 0, 1, 0, 0,
  243. 0, 0, 1, 0,
  244. 0, 0, 0, 1);
  245. break;
  246. case CSS::TransformFunction::TranslateY:
  247. if (count == 1)
  248. return Gfx::FloatMatrix4x4(1, 0, 0, 0,
  249. 0, 1, 0, value(0, height),
  250. 0, 0, 1, 0,
  251. 0, 0, 0, 1);
  252. break;
  253. case CSS::TransformFunction::Scale:
  254. if (count == 1)
  255. return Gfx::FloatMatrix4x4(value(0), 0, 0, 0,
  256. 0, value(0), 0, 0,
  257. 0, 0, 1, 0,
  258. 0, 0, 0, 1);
  259. if (count == 2)
  260. return Gfx::FloatMatrix4x4(value(0), 0, 0, 0,
  261. 0, value(1), 0, 0,
  262. 0, 0, 1, 0,
  263. 0, 0, 0, 1);
  264. break;
  265. case CSS::TransformFunction::ScaleX:
  266. if (count == 1)
  267. return Gfx::FloatMatrix4x4(value(0), 0, 0, 0,
  268. 0, 1, 0, 0,
  269. 0, 0, 1, 0,
  270. 0, 0, 0, 1);
  271. break;
  272. case CSS::TransformFunction::ScaleY:
  273. if (count == 1)
  274. return Gfx::FloatMatrix4x4(1, 0, 0, 0,
  275. 0, value(0), 0, 0,
  276. 0, 0, 1, 0,
  277. 0, 0, 0, 1);
  278. break;
  279. case CSS::TransformFunction::RotateX:
  280. if (count == 1)
  281. return Gfx::rotation_matrix({ 1.0f, 0.0f, 0.0f }, value(0));
  282. break;
  283. case CSS::TransformFunction::RotateY:
  284. if (count == 1)
  285. return Gfx::rotation_matrix({ 0.0f, 1.0f, 0.0f }, value(0));
  286. break;
  287. case CSS::TransformFunction::Rotate:
  288. case CSS::TransformFunction::RotateZ:
  289. if (count == 1)
  290. return Gfx::rotation_matrix({ 0.0f, 0.0f, 1.0f }, value(0));
  291. break;
  292. default:
  293. dbgln_if(LIBWEB_CSS_DEBUG, "FIXME: Unhandled transformation function {}", CSS::TransformationStyleValue::create(transformation.function, {})->to_string());
  294. }
  295. return Gfx::FloatMatrix4x4::identity();
  296. }
  297. Gfx::FloatMatrix4x4 StackingContext::combine_transformations(Vector<CSS::Transformation> const& transformations) const
  298. {
  299. auto matrix = Gfx::FloatMatrix4x4::identity();
  300. for (auto const& transform : transformations)
  301. matrix = matrix * get_transformation_matrix(transform);
  302. return matrix;
  303. }
  304. // FIXME: This extracts the affine 2D part of the full transformation matrix.
  305. // Use the whole matrix when we get better transformation support in LibGfx or use LibGL for drawing the bitmap
  306. Gfx::AffineTransform StackingContext::affine_transform_matrix() const
  307. {
  308. auto* m = m_transform.elements();
  309. return Gfx::AffineTransform(m[0][0], m[1][0], m[0][1], m[1][1], m[0][3], m[1][3]);
  310. }
  311. void StackingContext::paint(PaintContext& context) const
  312. {
  313. Gfx::PainterStateSaver saver(context.painter());
  314. if (m_box.is_fixed_position()) {
  315. context.painter().translate(-context.painter().translation());
  316. }
  317. auto opacity = m_box.computed_values().opacity();
  318. if (opacity == 0.0f)
  319. return;
  320. auto affine_transform = affine_transform_matrix();
  321. if (opacity < 1.0f || !affine_transform.is_identity_or_translation()) {
  322. auto transform_origin = this->transform_origin();
  323. auto source_rect = paintable().absolute_paint_rect().translated(-transform_origin);
  324. auto transformed_destination_rect = affine_transform.map(source_rect).translated(transform_origin);
  325. auto destination_rect = transformed_destination_rect.to_rounded<int>();
  326. // FIXME: We should find a way to scale the paintable, rather than paint into a separate bitmap,
  327. // then scale it. This snippet now copies the background at the destination, then scales it down/up
  328. // to the size of the source (which could add some artefacts, though just scaling the bitmap already does that).
  329. // We need to copy the background at the destination because a bunch of our rendering effects now rely on
  330. // being able to sample the painter (see border radii, shadows, filters, etc).
  331. Gfx::FloatPoint destination_clipped_fixup {};
  332. auto try_get_scaled_destination_bitmap = [&]() -> ErrorOr<NonnullRefPtr<Gfx::Bitmap>> {
  333. Gfx::IntRect actual_destination_rect;
  334. auto bitmap = TRY(context.painter().get_region_bitmap(destination_rect, Gfx::BitmapFormat::BGRA8888, actual_destination_rect));
  335. // get_region_bitmap() may clip to a smaller region if the requested rect goes outside the painter, so we need to account for that.
  336. destination_clipped_fixup = Gfx::FloatPoint { destination_rect.location() - actual_destination_rect.location() };
  337. destination_rect = actual_destination_rect;
  338. if (source_rect.size() != transformed_destination_rect.size()) {
  339. auto sx = static_cast<float>(source_rect.width()) / transformed_destination_rect.width();
  340. auto sy = static_cast<float>(source_rect.height()) / transformed_destination_rect.height();
  341. bitmap = TRY(bitmap->scaled(sx, sy));
  342. destination_clipped_fixup.scale_by(sx, sy);
  343. }
  344. return bitmap;
  345. };
  346. auto bitmap_or_error = try_get_scaled_destination_bitmap();
  347. if (bitmap_or_error.is_error())
  348. return;
  349. auto bitmap = bitmap_or_error.release_value_but_fixme_should_propagate_errors();
  350. Gfx::Painter painter(bitmap);
  351. painter.translate((-paintable().absolute_paint_rect().location() + destination_clipped_fixup).to_rounded<int>());
  352. auto paint_context = context.clone(painter);
  353. paint_internal(paint_context);
  354. if (destination_rect.size() == bitmap->size())
  355. context.painter().blit(destination_rect.location(), *bitmap, bitmap->rect(), opacity);
  356. else
  357. context.painter().draw_scaled_bitmap(destination_rect, *bitmap, bitmap->rect(), opacity, Gfx::Painter::ScalingMode::BilinearBlend);
  358. } else {
  359. Gfx::PainterStateSaver saver(context.painter());
  360. context.painter().translate(affine_transform.translation().to_rounded<int>());
  361. paint_internal(context);
  362. }
  363. }
  364. Gfx::FloatPoint StackingContext::compute_transform_origin() const
  365. {
  366. auto style_value = m_box.computed_values().transform_origin();
  367. // FIXME: respect transform-box property
  368. auto reference_box = paintable().absolute_border_box_rect();
  369. auto x = reference_box.left() + style_value.x.resolved(m_box, CSS::Length::make_px(reference_box.width())).to_px(m_box);
  370. auto y = reference_box.top() + style_value.y.resolved(m_box, CSS::Length::make_px(reference_box.height())).to_px(m_box);
  371. return { x, y };
  372. }
  373. template<typename U, typename Callback>
  374. static TraversalDecision for_each_in_inclusive_subtree_of_type_within_same_stacking_context_in_reverse(Paintable const& paintable, Callback callback)
  375. {
  376. if (is<PaintableBox>(paintable) && static_cast<PaintableBox const&>(paintable).stacking_context())
  377. return TraversalDecision::SkipChildrenAndContinue;
  378. for (auto* child = paintable.last_child(); child; child = child->previous_sibling()) {
  379. if (for_each_in_inclusive_subtree_of_type_within_same_stacking_context_in_reverse<U>(*child, callback) == TraversalDecision::Break)
  380. return TraversalDecision::Break;
  381. }
  382. if (is<U>(paintable)) {
  383. if (auto decision = callback(static_cast<U const&>(paintable)); decision != TraversalDecision::Continue)
  384. return decision;
  385. }
  386. return TraversalDecision::Continue;
  387. }
  388. template<typename U, typename Callback>
  389. static TraversalDecision for_each_in_subtree_of_type_within_same_stacking_context_in_reverse(Paintable const& paintable, Callback callback)
  390. {
  391. for (auto* child = paintable.last_child(); child; child = child->previous_sibling()) {
  392. if (for_each_in_inclusive_subtree_of_type_within_same_stacking_context_in_reverse<U>(*child, callback) == TraversalDecision::Break)
  393. return TraversalDecision::Break;
  394. }
  395. return TraversalDecision::Continue;
  396. }
  397. Optional<HitTestResult> StackingContext::hit_test(Gfx::FloatPoint const& position, HitTestType type) const
  398. {
  399. if (!m_box.is_visible())
  400. return {};
  401. auto transform_origin = this->transform_origin();
  402. auto transformed_position = affine_transform_matrix().inverse().value_or({}).map(position - transform_origin) + transform_origin;
  403. // FIXME: Support more overflow variations.
  404. if (paintable().computed_values().overflow_x() == CSS::Overflow::Hidden && paintable().computed_values().overflow_y() == CSS::Overflow::Hidden) {
  405. if (!paintable().absolute_border_box_rect().contains(transformed_position.x(), transformed_position.y()))
  406. return {};
  407. }
  408. // NOTE: Hit testing basically happens in reverse painting order.
  409. // https://www.w3.org/TR/CSS22/visuren.html#z-index
  410. // 7. the child stacking contexts with positive stack levels (least positive first).
  411. // NOTE: Hit testing follows reverse painting order, that's why the conditions here are reversed.
  412. for (ssize_t i = m_children.size() - 1; i >= 0; --i) {
  413. auto const& child = *m_children[i];
  414. if (child.m_box.computed_values().z_index().value_or(0) <= 0)
  415. break;
  416. auto result = child.hit_test(transformed_position, type);
  417. if (result.has_value() && result->paintable->visible_for_hit_testing())
  418. return result;
  419. }
  420. Optional<HitTestResult> result;
  421. // 6. the child stacking contexts with stack level 0 and the positioned descendants with stack level 0.
  422. for_each_in_subtree_of_type_within_same_stacking_context_in_reverse<PaintableBox>(paintable(), [&](PaintableBox const& paint_box) {
  423. // FIXME: Support more overflow variations.
  424. if (paint_box.computed_values().overflow_x() == CSS::Overflow::Hidden && paint_box.computed_values().overflow_y() == CSS::Overflow::Hidden) {
  425. if (!paint_box.absolute_border_box_rect().contains(transformed_position.x(), transformed_position.y()))
  426. return TraversalDecision::SkipChildrenAndContinue;
  427. }
  428. if (paint_box.stacking_context()) {
  429. auto const& z_index = paint_box.computed_values().z_index();
  430. if (!z_index.has_value() || z_index.value() == 0) {
  431. auto candidate = paint_box.stacking_context()->hit_test(transformed_position, type);
  432. if (candidate.has_value() && candidate->paintable->visible_for_hit_testing()) {
  433. result = move(candidate);
  434. return TraversalDecision::Break;
  435. }
  436. }
  437. }
  438. auto& layout_box = paint_box.layout_box();
  439. if (layout_box.is_positioned() && !paint_box.stacking_context()) {
  440. if (auto candidate = paint_box.hit_test(transformed_position, type); candidate.has_value()) {
  441. result = move(candidate);
  442. return TraversalDecision::Break;
  443. }
  444. }
  445. return TraversalDecision::Continue;
  446. });
  447. if (result.has_value() && result->paintable->visible_for_hit_testing())
  448. return result;
  449. // 5. the in-flow, inline-level, non-positioned descendants, including inline tables and inline blocks.
  450. if (m_box.children_are_inline() && is<Layout::BlockContainer>(m_box)) {
  451. auto result = paintable().hit_test(transformed_position, type);
  452. if (result.has_value() && result->paintable->visible_for_hit_testing())
  453. return result;
  454. }
  455. // 4. the non-positioned floats.
  456. for_each_in_subtree_of_type_within_same_stacking_context_in_reverse<PaintableBox>(paintable(), [&](auto const& paint_box) {
  457. // FIXME: Support more overflow variations.
  458. if (paint_box.computed_values().overflow_x() == CSS::Overflow::Hidden && paint_box.computed_values().overflow_y() == CSS::Overflow::Hidden) {
  459. if (!paint_box.absolute_border_box_rect().contains(transformed_position.x(), transformed_position.y()))
  460. return TraversalDecision::SkipChildrenAndContinue;
  461. }
  462. auto& layout_box = paint_box.layout_box();
  463. if (layout_box.is_floating()) {
  464. if (auto candidate = paint_box.hit_test(transformed_position, type); candidate.has_value()) {
  465. result = move(candidate);
  466. return TraversalDecision::Break;
  467. }
  468. }
  469. return TraversalDecision::Continue;
  470. });
  471. if (result.has_value() && result->paintable->visible_for_hit_testing())
  472. return result;
  473. // 3. the in-flow, non-inline-level, non-positioned descendants.
  474. if (!m_box.children_are_inline()) {
  475. for_each_in_subtree_of_type_within_same_stacking_context_in_reverse<PaintableBox>(paintable(), [&](auto const& paint_box) {
  476. // FIXME: Support more overflow variations.
  477. if (paint_box.computed_values().overflow_x() == CSS::Overflow::Hidden && paint_box.computed_values().overflow_y() == CSS::Overflow::Hidden) {
  478. if (!paint_box.absolute_border_box_rect().contains(transformed_position.x(), transformed_position.y()))
  479. return TraversalDecision::SkipChildrenAndContinue;
  480. }
  481. auto& layout_box = paint_box.layout_box();
  482. if (!layout_box.is_absolutely_positioned() && !layout_box.is_floating()) {
  483. if (auto candidate = paint_box.hit_test(transformed_position, type); candidate.has_value()) {
  484. result = move(candidate);
  485. return TraversalDecision::Break;
  486. }
  487. }
  488. return TraversalDecision::Continue;
  489. });
  490. if (result.has_value() && result->paintable->visible_for_hit_testing())
  491. return result;
  492. }
  493. // 2. the child stacking contexts with negative stack levels (most negative first).
  494. // NOTE: Hit testing follows reverse painting order, that's why the conditions here are reversed.
  495. for (ssize_t i = m_children.size() - 1; i >= 0; --i) {
  496. auto const& child = *m_children[i];
  497. if (child.m_box.computed_values().z_index().value_or(0) >= 0)
  498. break;
  499. auto result = child.hit_test(transformed_position, type);
  500. if (result.has_value() && result->paintable->visible_for_hit_testing())
  501. return result;
  502. }
  503. // 1. the background and borders of the element forming the stacking context.
  504. if (paintable().absolute_border_box_rect().contains(transformed_position)) {
  505. return HitTestResult {
  506. .paintable = paintable(),
  507. };
  508. }
  509. return {};
  510. }
  511. void StackingContext::dump(int indent) const
  512. {
  513. StringBuilder builder;
  514. for (int i = 0; i < indent; ++i)
  515. builder.append(' ');
  516. builder.appendff("SC for {} {} [children: {}] (z-index: ", m_box.debug_description(), paintable().absolute_rect(), m_children.size());
  517. if (m_box.computed_values().z_index().has_value())
  518. builder.appendff("{}", m_box.computed_values().z_index().value());
  519. else
  520. builder.append("auto"sv);
  521. builder.append(')');
  522. auto affine_transform = affine_transform_matrix();
  523. if (!affine_transform.is_identity()) {
  524. builder.appendff(", transform: {}", affine_transform);
  525. }
  526. dbgln("{}", builder.string_view());
  527. for (auto& child : m_children)
  528. child->dump(indent + 1);
  529. }
  530. }