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): honor vertical block-start positioning

ldm0aa4d73aff584
diff --git a/moli-layout/src/style.rs b/moli-layout/src/style.rs
index d6353aebd..a93b240e8 100644
--- a/moli-layout/src/style.rs
+++ b/moli-layout/src/style.rs
@@ -128,6 +128,43 @@ pub(crate) enum InlineDirection {
Rtl,
}
+/// Physical block-flow direction retained at the Stylo/Taffy boundary.
+///
+/// Taffy currently has no writing-mode input, so its numeric algorithms run
+/// in horizontal-tb coordinates. Keeping the full authored mode here lets the
+/// browser adapter physicalize the result without confusing `vertical-lr`
+/// and `vertical-rl`.
+#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
+pub(crate) enum LayoutWritingMode {
+ #[default]
+ HorizontalTb,
+ VerticalRl,
+ VerticalLr,
+}
+
+impl LayoutWritingMode {
+ const fn from_stylo(value: StyloWritingMode) -> Self {
+ match value {
+ StyloWritingMode::HorizontalTb => Self::HorizontalTb,
+ StyloWritingMode::VerticalRl => Self::VerticalRl,
+ StyloWritingMode::VerticalLr => Self::VerticalLr,
+ }
+ }
+
+ pub(crate) const fn is_horizontal(self) -> bool {
+ matches!(self, Self::HorizontalTb)
+ }
+
+ /// Physical edge used as block-start by vertical normal flow.
+ pub(crate) const fn vertical_block_start_is_right(self) -> Option<bool> {
+ match self {
+ Self::HorizontalTb => None,
+ Self::VerticalRl => Some(true),
+ Self::VerticalLr => Some(false),
+ }
+ }
+}
+
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
pub(crate) enum LayoutOverflowMode {
#[default]
@@ -469,7 +506,7 @@ pub struct ResolvedLayoutStyle {
text_transform: InlineTextTransform,
text_align: parley::Alignment,
direction: InlineDirection,
- horizontal_writing_mode: bool,
+ writing_mode: LayoutWritingMode,
unicode_bidi: InlineUnicodeBidi,
vertical_align: InlineVerticalAlign,
text_projection_deferred: bool,
@@ -524,7 +561,7 @@ impl std::fmt::Debug for ResolvedLayoutStyle {
.field("text_transform", &self.text_transform)
.field("text_align", &self.text_align)
.field("direction", &self.direction)
- .field("horizontal_writing_mode", &self.horizontal_writing_mode)
+ .field("writing_mode", &self.writing_mode)
.field("unicode_bidi", &self.unicode_bidi)
.field("vertical_align", &self.vertical_align)
.field("text_projection_deferred", &self.text_projection_deferred)
@@ -615,8 +652,7 @@ impl ResolvedLayoutStyle {
StyloDirection::Ltr => InlineDirection::Ltr,
StyloDirection::Rtl => InlineDirection::Rtl,
};
- let horizontal_writing_mode =
- computed.clone_writing_mode() == StyloWritingMode::HorizontalTb;
+ let writing_mode = LayoutWritingMode::from_stylo(computed.clone_writing_mode());
let unicode_bidi = match computed.clone_unicode_bidi() {
StyloUnicodeBidi::Normal => InlineUnicodeBidi::Normal,
StyloUnicodeBidi::Embed => InlineUnicodeBidi::Embed,
@@ -841,7 +877,7 @@ impl ResolvedLayoutStyle {
text_transform,
text_align,
direction,
- horizontal_writing_mode,
+ writing_mode,
unicode_bidi,
vertical_align,
text_projection_deferred,
@@ -917,7 +953,7 @@ impl ResolvedLayoutStyle {
text_transform: InlineTextTransform::None,
text_align: parley::Alignment::Start,
direction: InlineDirection::Ltr,
- horizontal_writing_mode: true,
+ writing_mode: LayoutWritingMode::HorizontalTb,
unicode_bidi: InlineUnicodeBidi::Normal,
vertical_align: InlineVerticalAlign::default(),
text_projection_deferred: false,
@@ -1692,9 +1728,11 @@ impl ResolvedLayoutStyle {
&& self.scrollbar_gutter != LayoutScrollbarGutter::Auto
&& self.scrollbar_width != LayoutScrollbarWidth::None
{
- if self.horizontal_writing_mode && self.overflow_y.creates_scroll_container() {
+ if self.uses_horizontal_writing_mode() && self.overflow_y.creates_scroll_container() {
self.taffy.overflow.y = taffy::Overflow::Scroll;
- } else if !self.horizontal_writing_mode && self.overflow_x.creates_scroll_container() {
+ } else if !self.uses_horizontal_writing_mode()
+ && self.overflow_x.creates_scroll_container()
+ {
self.taffy.overflow.x = taffy::Overflow::Scroll;
}
}
@@ -1839,7 +1877,7 @@ impl ResolvedLayoutStyle {
}
if self.scrollbar_gutter != LayoutScrollbarGutter::Auto {
- if self.horizontal_writing_mode && self.overflow_y.creates_scroll_container() {
+ if self.uses_horizontal_writing_mode() && self.overflow_y.creates_scroll_container() {
if self.places_vertical_scrollbar_on_left(is_root) {
insets.left = thickness;
if self.scrollbar_gutter == LayoutScrollbarGutter::StableBothEdges {
@@ -1851,7 +1889,9 @@ impl ResolvedLayoutStyle {
insets.left = thickness;
}
}
- } else if !self.horizontal_writing_mode && self.overflow_x.creates_scroll_container() {
+ } else if !self.uses_horizontal_writing_mode()
+ && self.overflow_x.creates_scroll_container()
+ {
insets.bottom = thickness;
if self.scrollbar_gutter == LayoutScrollbarGutter::StableBothEdges {
insets.top = thickness;
@@ -1862,11 +1902,15 @@ impl ResolvedLayoutStyle {
}
pub(crate) fn places_vertical_scrollbar_on_left(&self, is_root: bool) -> bool {
- !is_root && self.horizontal_writing_mode && self.direction == InlineDirection::Rtl
+ !is_root && self.uses_horizontal_writing_mode() && self.direction == InlineDirection::Rtl
}
pub(crate) const fn uses_horizontal_writing_mode(&self) -> bool {
- self.horizontal_writing_mode
+ self.writing_mode.is_horizontal()
+ }
+
+ pub(crate) const fn vertical_block_start_is_right(&self) -> Option<bool> {
+ self.writing_mode.vertical_block_start_is_right()
}
pub(crate) fn text_leaf_from(parent: &Self) -> Self {
@@ -1889,7 +1933,7 @@ impl ResolvedLayoutStyle {
text_transform: parent.text_transform,
text_align: parent.text_align,
direction: parent.direction,
- horizontal_writing_mode: parent.horizontal_writing_mode,
+ writing_mode: parent.writing_mode,
unicode_bidi: InlineUnicodeBidi::Normal,
vertical_align: parent.vertical_align,
text_projection_deferred: parent.text_projection_deferred,
@@ -1955,7 +1999,7 @@ impl ResolvedLayoutStyle {
text_transform: parent.text_transform,
text_align: parent.text_align,
direction: parent.direction,
- horizontal_writing_mode: parent.horizontal_writing_mode,
+ writing_mode: parent.writing_mode,
unicode_bidi: InlineUnicodeBidi::Normal,
vertical_align: parent.vertical_align,
text_projection_deferred: parent.text_projection_deferred,
diff --git a/moli-layout/src/taffy_tree.rs b/moli-layout/src/taffy_tree.rs
index 81eede259..ee5a99ab3 100644
--- a/moli-layout/src/taffy_tree.rs
+++ b/moli-layout/src/taffy_tree.rs
@@ -90,6 +90,7 @@ where
height: AvailableSpace::Definite(viewport.css_height as f32),
},
);
+ physicalize_vertical_block_flow(world);
finish_block_positioned_layout(world, viewport, &positioned_static_placeholders);
finish_inline_positioned_layout(world, viewport);
finish_form_control_contents(world);
@@ -109,6 +110,78 @@ where
}
}
+/// Converts Taffy's horizontal block-start anchor into the physical edge used
+/// by a vertical writing mode.
+///
+/// Taffy deliberately has no writing-mode input yet. Its block algorithm does
+/// still produce the right border-box size, margins and physical relative
+/// inset, but it chooses the x anchor from CSS `direction`: left for LTR and
+/// right for RTL. In a vertical formatting context, direction controls the
+/// vertical inline axis instead; block-start is always left for `vertical-lr`
+/// and right for `vertical-rl`.
+///
+/// Apply this adapter immediately after numeric layout so positioned, sticky,
+/// scroll-overflow, CSSOM and paint all observe the same real box coordinates.
+/// The preserved delta carries any physical relative inset through unchanged.
+fn physicalize_vertical_block_flow<N>(world: &mut LayoutWorld<N>)
+where
+ N: Copy + Debug + Eq + Hash,
+{
+ let mut adjusted_x = Vec::new();
+
+ for parent_index in 0..world.boxes.len() {
+ let parent = LayoutBoxId::from_index(parent_index);
+ let Some(block_start_is_right) = world.boxes[parent_index]
+ .style
+ .vertical_block_start_is_right()
+ else {
+ continue;
+ };
+ if !original_parent_uses_block_layout(world, parent) {
+ continue;
+ }
+
+ let parent_layout = world.boxes[parent_index].unrounded_layout;
+ let scrollbar = world.get_scrollbar_insets(parent.to_taffy());
+ let content_left = parent_layout.border.left + parent_layout.padding.left + scrollbar.left;
+ let content_right = parent_layout.size.width
+ - parent_layout.border.right
+ - parent_layout.padding.right
+ - scrollbar.right;
+ let taffy_anchored_right =
+ world.boxes[parent_index].style.taffy.direction == taffy::Direction::Rtl;
+
+ for child in world.boxes[parent_index].layout_children.iter().copied() {
+ let child_box = &world.boxes[child.index()];
+ if child_box.style.is_absolute_positioned()
+ || child_box.style.is_fixed_positioned()
+ || box_is_effectively_floated(world, child)
+ {
+ continue;
+ }
+
+ let layout = child_box.unrounded_layout;
+ let left_anchor = content_left + layout.margin.left;
+ let right_anchor = content_right - layout.size.width - layout.margin.right;
+ let taffy_anchor = if taffy_anchored_right {
+ right_anchor
+ } else {
+ left_anchor
+ };
+ let physical_anchor = if block_start_is_right {
+ right_anchor
+ } else {
+ left_anchor
+ };
+ adjusted_x.push((child, physical_anchor + (layout.location.x - taffy_anchor)));
+ }
+ }
+
+ for (child, x) in adjusted_x {
+ world.boxes[child.index()].unrounded_layout.location.x = x;
+ }
+}
+
/// Quantize final browser geometry without teaching Taffy about CSS layout
/// units. Taffy's public rounding pass operates on an abstract integer grid;
/// this adapter presents one CSS pixel as 64 such units and converts the
diff --git a/moli-renderer-v8/src/script_vm/tests/dom_elements/dom_surface.rs b/moli-renderer-v8/src/script_vm/tests/dom_elements/dom_surface.rs
index decfe151a..c20464ad6 100644
--- a/moli-renderer-v8/src/script_vm/tests/dom_elements/dom_surface.rs
+++ b/moli-renderer-v8/src/script_vm/tests/dom_elements/dom_surface.rs
@@ -1558,6 +1558,93 @@ fn physical_scrollbar_insets_cover_box_intrinsic_ratio_and_vertical_writing_layo
);
}
+#[test]
+fn vertical_block_flow_uses_writing_mode_instead_of_inline_direction_for_x_anchor() {
+ let mut vm = new_storage_test_vm("https://vertical-block-flow-anchor.test/");
+ vm.eval(
+ r#"
+ (() => {
+ if (!document.documentElement) {
+ document.appendChild(document.createElement("html"));
+ }
+ if (!document.body) {
+ document.documentElement.appendChild(document.createElement("body"));
+ }
+ document.documentElement.style.margin = "0";
+ document.body.style.margin = "0";
+ document.body.innerHTML = `
+ <style>
+ .case {
+ width: 200px;
+ height: 200px;
+ margin: 1px;
+ padding: 0;
+ border: 0;
+ overflow: auto;
+ scrollbar-gutter: stable;
+ }
+ .case > div { width: 300px; height: 300px; }
+ .vlr { writing-mode: vertical-lr; }
+ .vrl { writing-mode: vertical-rl; }
+ .thin { scrollbar-width: thin; }
+ .none { scrollbar-width: none; }
+ .rtl { direction: rtl; }
+ .boxed {
+ box-sizing: border-box;
+ width: 240px;
+ border-style: solid;
+ border-width: 0 4px 0 3px;
+ padding: 0 7px 0 11px;
+ scrollbar-width: none;
+ }
+ .boxed > div {
+ margin-left: 13px;
+ margin-right: 17px;
+ position: relative;
+ left: 5px;
+ }
+ </style>
+ <div id="vlr-auto" class="case vlr"><div></div></div>
+ <div id="vlr-thin" class="case vlr thin"><div></div></div>
+ <div id="vlr-none" class="case vlr none"><div></div></div>
+ <div id="vrl-auto" class="case vrl"><div></div></div>
+ <div id="vrl-thin" class="case vrl thin"><div></div></div>
+ <div id="vrl-none" class="case vrl none"><div></div></div>
+ <div id="vlr-rtl" class="case vlr rtl"><div></div></div>
+ <div id="vrl-rtl" class="case vrl rtl"><div></div></div>
+ <div id="boxed-vlr" class="case boxed vlr rtl"><div></div></div>
+ <div id="boxed-vrl" class="case boxed vrl"><div></div></div>`;
+ })()
+ "#,
+ )
+ .expect("vertical block-flow fixture should initialize");
+ refresh_layout_for_test(&mut vm);
+
+ assert_eq!(
+ vm.eval(
+ r#"
+ JSON.stringify([
+ "vlr-auto", "vlr-thin", "vlr-none",
+ "vrl-auto", "vrl-thin", "vrl-none",
+ "vlr-rtl", "vrl-rtl", "boxed-vlr", "boxed-vrl"
+ ].map(id => {
+ const container = document.getElementById(id);
+ const content = container.firstElementChild;
+ return [
+ container.offsetWidth,
+ container.clientWidth,
+ container.offsetLeft,
+ content.offsetLeft,
+ content.offsetWidth,
+ ];
+ }))
+ "#,
+ )
+ .expect("vertical block-flow metrics should evaluate"),
+ "[[200,185,1,1,300],[200,190,1,1,300],[200,200,1,1,300],[200,185,1,-114,300],[200,190,1,-109,300],[200,200,1,-99,300],[200,185,1,1,300],[200,185,1,-114,300],[240,233,1,33,300],[240,233,1,-82,300]]"
+ );
+}
+
#[test]
fn nested_and_sibling_scrollbar_drags_stay_bound_to_the_pressed_scroller() {
let mut vm = new_storage_test_vm("https://nested-classic-scrollbar-input.test/");