diff --git a/kernel/src/svg/shape.rs b/kernel/src/svg/shape.rs index a5861d63ea..e447a1f1cd 100644 --- a/kernel/src/svg/shape.rs +++ b/kernel/src/svg/shape.rs @@ -222,9 +222,18 @@ pub fn view_box_transform( if vb.width <= 0.0 || vb.height <= 0.0 { return None; } - let mut words = preserve.unwrap_or("xMidYMid meet").split_whitespace(); - let align = words.next().unwrap_or("xMidYMid"); - let slice = words.next() == Some("slice"); + // ` [meet | slice]?`; anything else is invalid and takes the + // default, as Chrome does. + const ALIGN: [&str; 10] = [ + "none", "xMinYMin", "xMidYMin", "xMaxYMin", "xMinYMid", "xMidYMid", "xMaxYMid", "xMinYMax", + "xMidYMax", "xMaxYMax", + ]; + let words: Vec<&str> = preserve.unwrap_or("").split_whitespace().collect(); + let (align, slice) = match words.as_slice() { + [a] if ALIGN.contains(a) => (*a, false), + [a, m @ ("meet" | "slice")] if ALIGN.contains(a) => (*a, *m == "slice"), + _ => ("xMidYMid", false), + }; let (mut sx, mut sy) = (width / vb.width, height / vb.height); if align != "none" { let s = if slice { sx.max(sy) } else { sx.min(sy) }; diff --git a/kernel/src/svg/tests.rs b/kernel/src/svg/tests.rs index b1047349bf..6a40d340d9 100644 --- a/kernel/src/svg/tests.rs +++ b/kernel/src/svg/tests.rs @@ -209,6 +209,38 @@ fn the_view_box_equations() { assert_eq!(view_box(&props), None, "a negative size invalidates it"); } +#[test] +fn an_invalid_preserve_aspect_ratio_is_the_default() { + // A 10 × 10 view box in 80 × 40: `xMidYMid meet` scales by 4 and centres + // (Chrome 156's `getCTM()` for each of these values). + let vb = Some(ViewBox { + x: 0.0, + y: 0.0, + width: 10.0, + height: 10.0, + }); + let default = Some([4.0, 0.0, 0.0, 4.0, 20.0, 0.0]); + for value in [ + "foo", + "xmaxymax", + "xMaxYMax slice extra", + "xMaxYMax meetx", + "xMaxYMaxx", + "", + ] { + assert_eq!( + view_box_transform(vb, Some(value), 80.0, 40.0), + default, + "{value:?}" + ); + } + assert_eq!( + view_box_transform(vb, Some(" xMaxYMax slice "), 80.0, 40.0), + Some([8.0, 0.0, 0.0, 8.0, 0.0, -40.0]), + "white space around a valid value" + ); +} + #[test] fn paint_and_dasharray_grammar() { assert_eq!(Paint::parse("none"), Some(Paint::None));