Skip to content

armgit / moli

Public Imported from GitHub
0

Best headless browser for AI agents. Lite, Fast, High-Compatibility. Built in Rust

fix(layout): preserve positioned auto paint groups

ldm0fa49b9195c6a
diff --git a/moli-layout/src/stacking.rs b/moli-layout/src/stacking.rs
index 2e4dfff74..6553e09d2 100644
--- a/moli-layout/src/stacking.rs
+++ b/moli-layout/src/stacking.rs
@@ -63,7 +63,7 @@ enum PaintGroup {
NormalFlow,
AtomicInline,
Float,
- Positioned,
+ Positioned { root_sequence: usize },
}
#[derive(Default)]
@@ -139,6 +139,9 @@ fn emit_context<N>(
.sort_by_key(|unit| unit.sequence);
collection.floats.sort_by_key(|unit| unit.sequence);
collection.inline_contents.sort_by_key(|unit| unit.sequence);
+ // Ordinary descendants share their positioned pseudo-context root's key.
+ // Keep this sort stable so its units stay in traversal order, ahead of the
+ // positioned/stacking descendants hoisted into this real context.
collection.positioned.sort_by_key(|entry| entry.sequence());
collection.outlines.sort_by_key(|unit| unit.sequence);
@@ -190,12 +193,14 @@ fn collect_subtree<N>(
return;
}
- // Positioned descendants escape the pseudo-context of an atomic inline or
- // float and participate in the nearest real stacking context. Resolve
- // that level before inheriting the atomic group; ordinary descendants
- // remain inside their atomic ancestor.
+ // Positioned descendants escape every pseudo-context, including a
+ // positioned z-index:auto ancestor, and participate in the nearest real
+ // stacking context. Each starts a separate group; ordinary descendants
+ // retain their atomic ancestor's root sequence.
let group = if layout_box.style.position() != LayoutPosition::Static {
- PaintGroup::Positioned
+ PaintGroup::Positioned {
+ root_sequence: next_sequence(sequence),
+ }
} else {
inherited_atomic_group.unwrap_or_else(|| {
if is_flex_or_grid_item {
@@ -235,7 +240,7 @@ fn collect_subtree<N>(
// override an inherited pseudo-context at the start of this function.
let descendant_group = match group {
PaintGroup::NormalFlow => None,
- PaintGroup::AtomicInline | PaintGroup::Float | PaintGroup::Positioned => Some(group),
+ PaintGroup::AtomicInline | PaintGroup::Float | PaintGroup::Positioned { .. } => Some(group),
};
for child in ordered_children(world, id) {
collect_subtree(world, child, descendant_group, sequence, collection);
@@ -272,7 +277,14 @@ fn push_unit(collection: &mut ContextCollection, group: PaintGroup, unit: PaintU
},
PaintGroup::AtomicInline => collection.inline_contents.push(unit),
PaintGroup::Float => collection.floats.push(unit),
- PaintGroup::Positioned => collection.positioned.push(AtomicPaintEntry::Unit(unit)),
+ PaintGroup::Positioned { root_sequence } => {
+ collection
+ .positioned
+ .push(AtomicPaintEntry::Unit(PaintUnit {
+ sequence: root_sequence,
+ ..unit
+ }));
+ }
}
}
diff --git a/moli-layout/tests/phase5_output_contract.rs b/moli-layout/tests/phase5_output_contract.rs
index 4de0418e7..e4c6a205e 100644
--- a/moli-layout/tests/phase5_output_contract.rs
+++ b/moli-layout/tests/phase5_output_contract.rs
@@ -3100,6 +3100,118 @@ fn scroll_is_sampled_per_pass_and_updates_geometry_clip_and_hit_testing() {
);
}
+#[test]
+fn positioned_auto_descendants_paint_after_ordinary_siblings() {
+ let source = Source(vec![
+ Node::element("root", vec![1]),
+ Node::element("positioned-row", vec![2]),
+ Node::element("view", vec![3, 4]),
+ Node::element("overlay", Vec::new()),
+ Node::element("label", Vec::new()),
+ ]);
+ let red = PaintColor::new(1.0, 0.0, 0.0, 1.0);
+ let blue = PaintColor::new(0.0, 0.0, 1.0, 1.0);
+ for row_z_index in [None, Some(0)] {
+ for opacity in [0.0, 0.5, 1.0] {
+ for label_position in [LayoutPosition::Static, LayoutPosition::Relative] {
+ let mut styles = Styles::default();
+ styles
+ .0
+ .insert(0, fixed_size(LayoutDisplay::Block, 100.0, 80.0));
+ let mut row = fixed_size(LayoutDisplay::Block, 80.0, 40.0)
+ .with_position(LayoutPosition::Relative);
+ if let Some(z_index) = row_z_index {
+ row = row.with_z_index(z_index);
+ }
+ styles.0.insert(1, row);
+ styles
+ .0
+ .insert(2, fixed_size(LayoutDisplay::Block, 80.0, 40.0));
+ styles.0.insert(
+ 3,
+ ResolvedLayoutStyle::synthetic(
+ LayoutDisplay::Block,
+ Style {
+ position: Position::Absolute,
+ inset: Rect {
+ left: length(0.0),
+ top: length(0.0),
+ right: LengthPercentageAuto::auto(),
+ bottom: LengthPercentageAuto::auto(),
+ },
+ size: Size {
+ width: length(40.0),
+ height: length(40.0),
+ },
+ ..Style::default()
+ },
+ red,
+ )
+ .with_position(LayoutPosition::Absolute)
+ .with_opacity(opacity),
+ );
+ styles.0.insert(
+ 4,
+ ResolvedLayoutStyle::synthetic(
+ LayoutDisplay::Block,
+ Style {
+ size: Size {
+ width: length(80.0),
+ height: length(40.0),
+ },
+ ..Style::default()
+ },
+ blue,
+ )
+ .with_position(label_position),
+ );
+ for paint in [false, true] {
+ let viewport = LayoutViewport::new(100, 80, 1.0);
+ let request = if paint {
+ LayoutPassRequest::with_paint(viewport, LayoutFlushReason::Test)
+ } else {
+ LayoutPassRequest::new(viewport, LayoutFlushReason::Test)
+ };
+ let output = build_with_request(&source, &mut styles, request);
+ let overlay_on_top = label_position == LayoutPosition::Static;
+ assert_eq!(
+ output
+ .hit_test(LayoutPoint::new(20.0, 20.0), false)
+ .unwrap()
+ .source,
+ if overlay_on_top { 3 } else { 4 },
+ "row z-index={row_z_index:?}, opacity={opacity}, label={label_position:?}, paint={paint}"
+ );
+ if let Some(snapshot) = output.paint_snapshot() {
+ let fills = snapshot
+ .fragments
+ .iter()
+ .filter_map(|fragment| fragment.solid_fill().map(|(_, color, _)| color))
+ .filter(|color| *color == red || *color == blue)
+ .collect::<Vec<_>>();
+ let expected = if overlay_on_top {
+ vec![blue, red]
+ } else {
+ vec![red, blue]
+ };
+ assert_eq!(fills, expected);
+ // Paint commands remain inside an opacity-zero layer;
+ // the compositor suppresses pixels, not hit geometry.
+ if opacity < 1.0 {
+ assert!(snapshot.fragments.iter().any(|fragment| matches!(
+ fragment,
+ PaintFragment::PushLayer { opacity: layer_opacity, .. }
+ if *layer_opacity == opacity
+ )));
+ }
+ assert_balanced_paint_stack(&snapshot.fragments);
+ }
+ }
+ }
+ }
+ }
+}
+
#[test]
fn transforms_and_semantic_paint_order_share_the_hit_test_projection() {
let source = Source(vec![