Skip to content

Truly independent web browser

LibWeb: Bound a stacking context by the few boxes hoisted to it

Aliaksandr Kalenik4c790f4c706a
diff --git a/Libraries/LibWeb/Rust/src/painting/paint_order_plan.rs b/Libraries/LibWeb/Rust/src/painting/paint_order_plan.rs
index 1afca47b1da..d89708046db 100644
--- a/Libraries/LibWeb/Rust/src/painting/paint_order_plan.rs
+++ b/Libraries/LibWeb/Rust/src/painting/paint_order_plan.rs
@@ -56,9 +56,7 @@ impl PaintScope {
/// Whether every box the scope paints has the owner on its containing block chain, so that what the owner
/// gathers along that chain covers the whole scope. A walk below the owner only meets boxes laid out against it
- /// when the owner forms the containing block of its children: an inline box or a table row does not. A stacking
- /// context also paints the positioned descendants hoisted to it, and those are laid out against it only when
- /// it establishes both positioning containing blocks.
+ /// when the owner forms the containing block of its children: an inline box or a table row does not.
pub(crate) fn paints_only_what_its_owner_contains(self, arena: &impl PaintRead) -> bool {
let Some(row) = arena.node(self.owner) else {
return false;
@@ -71,10 +69,62 @@ impl PaintScope {
}
self.kind != PaintScopeKind::PaintedAsStackingContext
|| !arena.paintable_data(self.owner).establishes_stacking_context
+ || self.owner_contains_the_boxes_hoisted_to_its_stacking_context(arena)
+ }
+
+ /// A stacking context also paints the positioned descendants hoisted to it, and those are laid out against its
+ /// owner when each of the few it has is, or when the owner establishes both positioning containing blocks.
+ pub(crate) fn owner_contains_the_boxes_hoisted_to_its_stacking_context(self, arena: &impl PaintRead) -> bool {
+ contains_the_few_boxes_hoisted_to_its_stacking_context(arena, self.owner)
|| style_queries::establishes_positioning_containing_blocks(arena, self.owner) == (true, true)
}
}
+fn contains_the_few_boxes_hoisted_to_its_stacking_context(arena: &impl PaintRead, owner: NodeSlotId) -> bool {
+ const MOST_HOISTED_BOXES_WALKED: usize = 8;
+ let mut contexts_to_walk = SmallVec::<[NodeSlotId; MOST_HOISTED_BOXES_WALKED]>::new();
+ contexts_to_walk.push(owner);
+ let mut hoisted_boxes_walked = 0;
+ while let Some(context) = contexts_to_walk.pop() {
+ let Some(entries) = arena.stacking_context_entries(context) else {
+ continue;
+ };
+ let hoisted_boxes = entries
+ .child_contexts_with_nonzero_z_index
+ .iter()
+ .map(|entry| entry.slot)
+ .chain(entries.stack_level_zero_boxes.iter().copied());
+ for hoisted in hoisted_boxes {
+ hoisted_boxes_walked += 1;
+ if hoisted_boxes_walked > MOST_HOISTED_BOXES_WALKED
+ || !arena.paintable_row_is_populated(hoisted)
+ || !is_on_containing_block_chain_near(arena, owner, hoisted)
+ {
+ return false;
+ }
+ if arena.paintable_data(hoisted).establishes_stacking_context {
+ contexts_to_walk.push(hoisted);
+ }
+ }
+ }
+ true
+}
+
+fn is_on_containing_block_chain_near(arena: &impl PaintRead, ancestor: NodeSlotId, descendant: NodeSlotId) -> bool {
+ const LONGEST_CHAIN_WALKED: usize = 8;
+ let mut containing_block = arena.paintable_data(descendant).containing_block;
+ for _ in 0..LONGEST_CHAIN_WALKED {
+ if containing_block == ancestor {
+ return true;
+ }
+ if containing_block.is_invalid() || !arena.paintable_row_is_populated(containing_block) {
+ return false;
+ }
+ containing_block = arena.paintable_data(containing_block).containing_block;
+ }
+ false
+}
+
/// A producer of the scope's owner in paint order. A box phase is up to three producers of the
/// recorder; an SVG root paints its background, border and content in the foreground pass.
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
diff --git a/Tests/LibWeb/Text/expected/hit_testing/boxes-hoisted-to-stacking-contexts.txt b/Tests/LibWeb/Text/expected/hit_testing/boxes-hoisted-to-stacking-contexts.txt
new file mode 100644
index 00000000000..40077ca49c4
--- /dev/null
+++ b/Tests/LibWeb/Text/expected/hit_testing/boxes-hoisted-to-stacking-contexts.txt
@@ -0,0 +1,12 @@
+a box its stacking context contains: inside [inside contains-its-box body html]
+beside that stacking context: body [body html]
+a fixed box outside its stacking context: fixed-outside [fixed-outside body html]
+an absolutely positioned box outside a context that is not its containing block: absolute-outside [absolute-outside body html]
+a fixed box outside the two contexts around it: nested-fixed-outside [nested-fixed-outside body html]
+a fixed box outside a context with many positioned boxes: crowded-fixed-outside [crowded-fixed-outside body html]
+a painted child of a box with many: row-20 [row-20 many body html]
+an unpainted child of a box with many: row-21 [row-21 many body html]
+the float among many children is 30 by 30
+a float among many children: float [float row-42 many body html]
+beside the float, on a child it overlaps: row-42 [row-42 many body html]
+beside a box with many children: body [body html]
diff --git a/Tests/LibWeb/Text/input/hit_testing/boxes-hoisted-to-stacking-contexts.html b/Tests/LibWeb/Text/input/hit_testing/boxes-hoisted-to-stacking-contexts.html
new file mode 100644
index 00000000000..7d9caaddf67
--- /dev/null
+++ b/Tests/LibWeb/Text/input/hit_testing/boxes-hoisted-to-stacking-contexts.html
@@ -0,0 +1,74 @@
+<!DOCTYPE html>
+<script src="../include.js"></script>
+<style>
+ body { margin: 0; }
+ .context { position: relative; z-index: 1; width: 40px; height: 40px; }
+ .marker { width: 20px; height: 20px; }
+ #inside { position: absolute; left: 10px; top: 10px; }
+ #fixed-outside { position: fixed; left: 300px; top: 20px; }
+ #translucent { opacity: 0.9; width: 40px; height: 40px; }
+ #absolute-outside { position: absolute; left: 300px; top: 80px; }
+ #inner { opacity: 0.9; width: 20px; height: 20px; }
+ #nested-fixed-outside { position: fixed; left: 300px; top: 140px; }
+ .crowd { position: absolute; left: 0; top: 0; width: 2px; height: 2px; }
+ #crowded-fixed-outside { position: fixed; left: 300px; top: 200px; }
+ #many { position: relative; width: 200px; margin-top: 20px; }
+ #many > div { height: 4px; }
+ #many > .painted { background: green; }
+ #many > #float { float: left; width: 30px; height: 30px; }
+</style>
+<div class="context" id="contains-its-box"><div class="marker" id="inside"></div></div>
+<div class="context" id="holds-a-fixed-box"><div class="marker" id="fixed-outside"></div></div>
+<div id="translucent"><div class="marker" id="absolute-outside"></div></div>
+<div class="context" id="outer"><div id="inner"><div class="marker" id="nested-fixed-outside"></div></div></div>
+<div class="context" id="crowded"></div>
+<div id="many"></div>
+<script>
+ function describe(element) {
+ return element ? element.id || element.localName : "null";
+ }
+ function probe(label, x, y) {
+ const all = document.elementsFromPoint(x, y).map(describe).join(" ");
+ println(`${label}: ${describe(document.elementFromPoint(x, y))} [${all}]`);
+ }
+ test(() => {
+ const crowded = document.getElementById("crowded");
+ for (let index = 0; index < 12; ++index) {
+ const box = document.createElement("div");
+ box.className = "crowd";
+ crowded.appendChild(box);
+ }
+ const crowdedFixed = document.createElement("div");
+ crowdedFixed.className = "marker";
+ crowdedFixed.id = "crowded-fixed-outside";
+ crowded.appendChild(crowdedFixed);
+
+ const many = document.getElementById("many");
+ for (let index = 0; index < 60; ++index) {
+ const row = document.createElement("div");
+ row.id = `row-${index}`;
+ if (index % 2 === 0)
+ row.className = "painted";
+ many.appendChild(row);
+ }
+ const float = document.createElement("div");
+ float.id = "float";
+ many.insertBefore(float, document.getElementById("row-40"));
+
+ probe("a box its stacking context contains", 15, 15);
+ probe("beside that stacking context", 100, 15);
+ probe("a fixed box outside its stacking context", 310, 30);
+ probe("an absolutely positioned box outside a context that is not its containing block", 310, 90);
+ probe("a fixed box outside the two contexts around it", 310, 150);
+ probe("a fixed box outside a context with many positioned boxes", 310, 210);
+
+ const manyTop = many.getBoundingClientRect().top;
+ probe("a painted child of a box with many", 100, manyTop + 4 * 20 + 2);
+ probe("an unpainted child of a box with many", 100, manyTop + 4 * 21 + 2);
+ const floatRect = float.getBoundingClientRect();
+ println(`the float among many children is ${floatRect.width} by ${floatRect.height}`);
+ probe("a float among many children", floatRect.left + 10, floatRect.top + 10);
+ probe("beside the float, on a child it overlaps", floatRect.right + 10, floatRect.top + 10);
+ probe("beside a box with many children", 250, manyTop + 4 * 20 + 2);
+ });
+</script>
diff --git a/Tests/LibWeb/Text/input/visual-context/value-only-update-keeps-tree.html b/Tests/LibWeb/Text/input/visual-context/value-only-update-keeps-tree.html
index 3409a325119..8ab8312f795 100644
--- a/Tests/LibWeb/Text/input/visual-context/value-only-update-keeps-tree.html
+++ b/Tests/LibWeb/Text/input/visual-context/value-only-update-keeps-tree.html
@@ -44,13 +44,7 @@
BlockContainer<BODY>/outline COPY
BlockContainer<BODY>/overlay COPY
BlockContainer<DIV>#box COPY
- BlockContainer<DIV>#relative-box ASSEMBLE
- BlockContainer<DIV>#relative-box/preamble COPY
- BlockContainer<DIV>#relative-box/scroll-metadata COPY
- BlockContainer<DIV>#relative-box/background COPY
- BlockContainer<DIV>#relative-box/border COPY
- BlockContainer<DIV>#relative-box/foreground COPY
- BlockContainer<DIV>#relative-box/outline COPY
+ BlockContainer<DIV>#relative-box COPY
BlockContainer<DIV>#backdrop-box COPY
BlockContainer<HTML>/outline COPY
@viewport/outline COPY