Skip to content

The headless browser for AI agents and web scraping

Normalize functional replaced max sizing

SGavrld065c84bed8f
diff --git a/crates/obscura-render/src/dom.rs b/crates/obscura-render/src/dom.rs
index d273675a..ce24028f 100644
--- a/crates/obscura-render/src/dom.rs
+++ b/crates/obscura-render/src/dom.rs
@@ -4467,6 +4467,19 @@ fn layout_dom_once(
let Some(expression) = style.size_expressions[index].as_deref() else {
continue;
};
+ // A proper replaced box's cyclic percentage maximum has a
+ // distinct intrinsic-sizing rule. Preserve functional
+ // spellings such as calc(100%) as the same typed percent
+ // used by the bare-token path instead of prematurely
+ // collapsing them against an ancestor width.
+ if index == 4 && style.has_replaced_sizing {
+ if let Some(percent) =
+ crate::style::functional_percentage_factor(expression)
+ {
+ style.max_width = crate::Dimension::Percent(percent);
+ continue;
+ }
+ }
let percent_base = if matches!(index, 1 | 3 | 5) {
viewport.1
} else {
@@ -10768,6 +10781,7 @@ fn defer_cyclic_flex_inline_sizes(
let functional = style.size_expressions[slot]
.as_ref()
.filter(|expression| expression.contains('%'))
+ .filter(|_| slot != 4 || !style.has_replaced_sizing)
.cloned()
.map(DeferredCyclicInlineSource::Expression);
let dimension = match slot {
diff --git a/crates/obscura-render/src/style.rs b/crates/obscura-render/src/style.rs
index 3acb7407..7284bc71 100644
--- a/crates/obscura-render/src/style.rs
+++ b/crates/obscura-render/src/style.rs
@@ -6310,6 +6310,31 @@ pub(crate) fn resolve_contextual_length(
resolve_contextual(value, &context)
}
+/// Return the percentage factor of a functional length whose value is purely
+/// proportional to its percentage basis.
+///
+/// CSSOM preserves spellings such as `calc(100%)`, but sizing algorithms must
+/// treat them like the typed `100%` value. Sampling at zero and two positive
+/// bases rejects offsets and non-linear min/max/clamp expressions while still
+/// accepting algebraically equivalent percentage-only forms.
+pub(crate) fn functional_percentage_factor(value: &str) -> Option<f32> {
+ if !value.contains('%') || !value.contains('(') {
+ return None;
+ }
+ let at_zero = resolve_contextual_length(value, 16.0, 16.0, 1.0, 1.0, 0.0)?;
+ let at_hundred = resolve_contextual_length(value, 16.0, 16.0, 1.0, 1.0, 100.0)?;
+ let at_two_hundred =
+ resolve_contextual_length(value, 16.0, 16.0, 1.0, 1.0, 200.0)?;
+ let tolerance = 0.0001;
+ if at_zero.abs() > tolerance
+ || (at_two_hundred - at_hundred * 2.0).abs() > tolerance
+ || !at_hundred.is_finite()
+ {
+ return None;
+ }
+ Some(at_hundred / 100.0)
+}
+
#[derive(Clone, Copy)]
struct LengthContext {
em_px: f32,
diff --git a/crates/obscura-render/tests/layout_test.rs b/crates/obscura-render/tests/layout_test.rs
index d5cbbc25..084653ce 100644
--- a/crates/obscura-render/tests/layout_test.rs
+++ b/crates/obscura-render/tests/layout_test.rs
@@ -3366,6 +3366,51 @@ fn percentage_max_replaced_items_shrink_in_a_flex_row() {
);
}
+/// Functional percentage-only maximums have the same computed sizing type as
+/// a bare percentage. Framework and utility stylesheets emit both spellings,
+/// so the replaced-element intrinsic contribution cannot depend on syntax.
+#[test]
+fn functional_percentage_max_replaced_items_shrink_in_a_flex_row() {
+ let tree = parse_html(
+ r#"
+ <style>
+ html, body { margin:0 }
+ #row { display:flex; gap:8px; width:1184px; overflow:hidden }
+ .col { display:flex; flex-direction:column; gap:8px }
+ img { display:block; max-width:calc(100%); height:auto }
+ </style>
+ <div id="row">
+ <div id="c1" class="col"><img id="i1" width="303" height="255"></div>
+ <div id="c2" class="col"><img id="i2" width="200" height="400"></div>
+ <div id="c3" class="col"><img id="i3" width="500" height="250"></div>
+ <div id="c4" class="col"><img id="i4" width="200" height="400"></div>
+ <div id="c5" class="col"><img id="i5" width="293" height="354"></div>
+ </div>
+ "#,
+ );
+ let ids = ["i1", "i2", "i3", "i4", "i5"]
+ .map(|id| tree.get_element_by_id(id).unwrap());
+ let intrinsic = HashMap::from([
+ (ids[0], (303.0, 255.0)),
+ (ids[1], (200.0, 400.0)),
+ (ids[2], (500.0, 250.0)),
+ (ids[3], (200.0, 400.0)),
+ (ids[4], (293.0, 354.0)),
+ ]);
+ let layout = layout_dom_with_images(&tree, (1280.0, 1000.0), &intrinsic);
+ let columns = ["c1", "c2", "c3", "c4", "c5"]
+ .map(|id| layout.rects[&tree.get_element_by_id(id).unwrap()]);
+ let images = ids.map(|id| layout.rects[&id]);
+ let expected_widths: [f32; 5] =
+ [233.328125, 154.0, 385.03125, 154.015625, 225.625];
+
+ for (index, expected) in expected_widths.into_iter().enumerate() {
+ let expected = expected.round();
+ assert_eq!(columns[index].width, expected, "column {index}: {:?}", columns[index]);
+ assert_eq!(images[index].width, expected, "image {index}: {:?}", images[index]);
+ }
+}
+
/// The compressible-percentage exception is specific to proper replaced
/// sizing. A non-replaced descendant with `max-width:100%` keeps its ordinary
/// min-content contribution while its flex item is intrinsically sized; its