Check the rewritten SVG viewport output under the same limits

This commit is contained in:
FredApps committed 2026-11-15 15:52:47 +01:00
1 parent 4f6e49b1ee
commit cec6e24381
2 files changed
+24 -1

No files matched your search

+5 -1
View File
@@ -212,7 +212,8 @@ pub struct Viewport {
}
/// Wraps a validated SVG root in a new viewport while preserving the original root as content.
/// The original root must have finite, positive pixel or point dimensions.
/// The original root must have finite, positive pixel or point dimensions, and the wrapped
/// output must still pass [`preflight`] under `limits`.
pub fn rewrite_root_viewport(svg: &str, target: Viewport, limits: &Limits, output_limit: usize) -> Result<String, Error> {
if !target.width.is_finite() || !target.height.is_finite() || target.width <= 0.0 || target.height <= 0.0 {
return Err(Error::Viewport("target dimensions must be finite and positive".into()));
@@ -256,6 +257,9 @@ pub fn rewrite_root_viewport(svg: &str, target: Viewport, limits: &Limits, outpu
output.push_str(&prefix);
output.push_str(inner);
output.push_str("</svg>");
// The wrapper adds a level, so a source at the depth limit can come out over it: check the
// output under the limits `parse_text` applies rather than return text it would refuse.
preflight(&output, limits)?;
Ok(output)
}
+19
View File
@@ -666,3 +666,22 @@ fn rewrite_root_viewport_refusals() {
assert_eq!(rewrite_root_viewport(sized, one, &limits, fits.len()).unwrap(), fits);
assert!(matches!(rewrite_root_viewport(sized, one, &limits, fits.len() - 1), Err(Error::OutputTooLarge(n)) if n == fits.len() - 1));
}
#[test]
fn rewrite_root_viewport_output_stays_within_the_limits() {
let limits = shallow();
let one = Viewport { width: 1.0, height: 1.0 };
let mut refused_at_the_limit = false;
for n in 0..limits.depth {
let source = svg(&groups(n));
if preflight(&source, &limits).is_err() {
break;
}
match rewrite_root_viewport(&source, one, &limits, 1 << 20) {
Ok(out) => assert!(parse_text(&out, &limits, &options()).is_ok(), "{n} groups"),
Err(Error::TooDeep(_)) => refused_at_the_limit = true,
Err(e) => panic!("{n} groups: {e:?}"),
}
}
assert!(refused_at_the_limit, "a source at the depth limit is refused, not wrapped past it");
}