diff --git a/crates/core/src/anatomy/compose.rs b/crates/core/src/anatomy/compose.rs index 453274fe..358ad160 100644 --- a/crates/core/src/anatomy/compose.rs +++ b/crates/core/src/anatomy/compose.rs @@ -13,12 +13,15 @@ //! own and keeps the drawing's ids, so it never holds one the composition writes //! (`svg::refs::RESERVED_IDS`, the halo filter): the reader refuses such a drawing and both //! composers refuse such a tree. +//! Both forms append the marker layer as the root's last child, so the root never hides it: a +//! root `visibility` other than `visible` is refused by the reader and by both composers. use std::collections::HashSet; use super::layer::{LayerError, find_view, render_layer, view_box_of}; pub use super::svg::refs::HALO_FILTER_ID; use super::svg::refs::holds_reserved_id; +use super::svg::sanitize::check_root_visibility; use super::svg::sanitize::{local_reference_text, scope_url_targets, url_targets, urls_are_local}; use super::svg::serialize::is_xml_text; use super::svg::{Element, Node}; @@ -44,6 +47,9 @@ pub enum ComposeError { /// composes it. Fail-closed: the reader refuses it (`reserved_id`), so only a tree that did /// not come out of `read_svg` reaches it, a composed view given back included. ReservedId, + /// The drawing's root carries a `visibility` other than `visible`, which the marker layer + /// would inherit. Fail-closed: the reader refuses it (`root_visibility`). + RootVisibility, } impl From for ComposeError { @@ -82,6 +88,9 @@ pub fn compose_canvas( if holds_reserved_id(asset) { return Err(ComposeError::ReservedId); } + if check_root_visibility(asset).is_err() { + return Err(ComposeError::RootVisibility); + } let mut root = Element { qname: "svg".to_owned(), namespace: asset.namespace.clone(), @@ -171,6 +180,9 @@ pub fn compose_highlight( if holds_reserved_id(&asset) { return Err(ComposeError::ReservedId); } + if check_root_visibility(&asset).is_err() { + return Err(ComposeError::RootVisibility); + } if let Some(view_box) = view_box_of(view) { asset.set_attr("viewBox", view_box); } diff --git a/crates/core/src/anatomy/svg/clean.rs b/crates/core/src/anatomy/svg/clean.rs index 4d08ceb9..cea8eccd 100644 --- a/crates/core/src/anatomy/svg/clean.rs +++ b/crates/core/src/anatomy/svg/clean.rs @@ -6,7 +6,8 @@ //! instruction (`svg_active_content`, the names in `active`, a PI as `?`), or, once //! no active content is found, a reference to an id the drawing does not define, in the //! file or after a removal (`dangling_reference`), then an id the composition writes -//! (`reserved_id`, `refs::RESERVED_IDS`); no cleaning is offered; +//! (`reserved_id`, `refs::RESERVED_IDS`), then a root `visibility` other than `visible` +//! (`root_visibility`); no cleaning is offered; //! - cleanable: only other removals (editor metadata and namespaces, `data-*`, `{BODY}"), + ), + "style", + ), + ( + rooted( + "", + &format!("{BODY}"), + ), + "style", + ), + ]; + for (text, removal) in &cleanable { + let read = read_svg(text).unwrap(); + assert!( + read.removed.iter().any(|r| r == removal), + "{text}: {:?}", + read.removed + ); + let cleaning = clean_svg(text.as_bytes()); + assert_eq!(cleaning.outcome, CleaningOutcome::Cleanable, "{text}"); + let stored = cleaning.stored.unwrap(); + let again = clean_svg(stored.as_bytes()); + assert_eq!( + (again.outcome, again.stored.as_deref()), + (CleaningOutcome::Clean, Some(stored.as_str())), + "the stored form is a fixpoint: {text}" + ); + let (canvas, highlight) = compose_both(&read_svg(&stored).unwrap().root); + assert!( + layer_shows(&canvas.unwrap()) && layer_shows(&highlight.unwrap()), + "{text}" + ); + } + let descendant = rooted( + "", + "", + ); + let read = read_svg(&descendant).unwrap(); + assert!(read.removed.is_empty()); + let (canvas, highlight) = compose_both(&read.root); + assert!(layer_shows(&canvas.unwrap()) && layer_shows(&highlight.unwrap())); +} + +/// The other root properties that reach the layer. Removed by the reader before V1-2.2: the +/// paint servers and effects (`filter`, `mask`, `clip-path`), `transform`, `class`, `style`. +/// Kept: `opacity` and `display` act on the whole root, drawing and markers alike (the +/// browser proofs of RUST.md §12.10 show both); `fill` and `fill-opacity` are set on every PDF +/// marker. The stroke properties no marker sets (`stroke-opacity`, `stroke-dasharray`, +/// `stroke-width` on the hitbox) are an open owner decision, not covered here. +#[test] +fn other_root_properties_are_removed_or_act_on_the_whole_view() { + for name in ["filter", "mask", "clip-path"] { + let text = rooted( + &format!("{name}=\"url(#m)\""), + &format!("{BODY}"), + ); + let read = read_svg(&text).unwrap(); + assert!(read.removed.contains(&format!("svg@{name}")), "{name}"); + assert!(read.root.attr(name).is_none(), "{name}"); + } + let read = read_svg(&rooted("transform=\"scale(0)\"", BODY)).unwrap(); + assert_eq!(read.removed, ["svg@transform"]); + + for root in [ + "opacity=\"0\"", + "display=\"none\"", + "fill=\"none\"", + "fill-opacity=\"0\"", + ] { + let read = read_svg(&rooted(root, BODY)).unwrap_or_else(|e| panic!("{root}: {e:?}")); + assert!(read.removed.is_empty(), "{root}"); + let ids: HashSet<&str> = ["hock"].into_iter().collect(); + let composed = compose_highlight(&sheet(), "main", read.root, &ids, true).unwrap(); + for marker in composed + .element_children() + .last() + .unwrap() + .element_children() + { + assert!(marker.attr("fill").is_some() && marker.attr("fill-opacity").is_some()); + } + } +} + +#[test] +fn the_equine_drawing_has_no_root_visibility() { + let horse = common::repo_file("supabase/seed/anatomical-sheets/equine/draft/assets/horse.svg"); + let read = read_svg(&horse).unwrap(); + assert!(read.root.attr("visibility").is_none() && read.removed.is_empty()); + let (canvas, highlight) = compose_both(&read.root); + assert!(canvas.is_ok() && highlight.is_ok()); +} diff --git a/packages/core-wasm/tests/preview.test.ts b/packages/core-wasm/tests/preview.test.ts index 501b6450..c6a92122 100644 --- a/packages/core-wasm/tests/preview.test.ts +++ b/packages/core-wasm/tests/preview.test.ts @@ -171,6 +171,12 @@ describe('clean_svg through the shipped module', () => { const reserved = new TextEncoder().encode(``) expect(value(glue.clean_svg(reserved)).reason, id).toBe('reserved_id') } + for (const visibility of ['hidden', 'COLLAPSE', 'inherit']) { + const hiding = new TextEncoder().encode( + ``, + ) + expect(value(glue.clean_svg(hiding)).reason, visibility).toBe('root_visibility') + } const cairo = value( glue.clean_svg(new Uint8Array(readFileSync(path(`${EXPORTS}cairo-1.18.4-pycairo-1.27.0-shapes.svg`)))), )