From 2c12b5a03a0cf5d121dd5257dc703beb1eb147cf Mon Sep 17 00:00:00 2001 From: SGavrl <277843151+SGavrl@users.noreply.github.com> Date: Sun, 26 Jul 2026 21:03:28 +0000 Subject: [PATCH] render: reject unitless nonzero lengths --- crates/obscura-render/src/style.rs | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) diff --git a/crates/obscura-render/src/style.rs b/crates/obscura-render/src/style.rs index 43ec688..16b4efe 100644 --- a/crates/obscura-render/src/style.rs +++ b/crates/obscura-render/src/style.rs @@ -2639,7 +2639,13 @@ fn dimension_value(tok: &str) -> crate::Dimension { if let Some(v) = lower.strip_suffix("vh").and_then(parse) { return Dimension::Vh(v); } if let Some(v) = lower.strip_suffix("px").and_then(parse) { return Dimension::Px(v); } if let Some(v) = lower.strip_suffix("pt").and_then(parse) { return Dimension::Px(v * 1.333); } - if let Some(v) = parse(&lower) { return Dimension::Px(v); } + // CSS lengths accept a unitless number only when it is zero. Treating + // arbitrary numbers as pixels changes invalid declarations into tiny + // geometry (for example `font-size:.813` must be ignored and inherited, + // not rendered at 0.813px). + if let Some(v) = parse(&lower).filter(|v| *v == 0.0) { + return Dimension::Px(v); + } Dimension::Auto } @@ -3583,6 +3589,15 @@ mod tests { assert_eq!(computed_font_weight(Some("lighter"), 900), 700); } + #[test] + fn nonzero_unitless_font_size_is_invalid() { + let inherited = compute_style("div", Some("font-size:14px;font-size:.813")); + assert_eq!(inherited.font_size, Some(14.0)); + + let zero = compute_style("div", Some("font-size:0")); + assert_eq!(zero.font_size, Some(0.0)); + } + #[test] fn containing_block_property_triggers_are_independent() { let s = compute_style("div", Some("transform:rotate(0deg);filter:none"));