From e823655f095e7bb5888d388c426160e0922ffe0b Mon Sep 17 00:00:00 2001 From: bchanot Date: Thu, 29 Oct 2026 16:24:07 +0100 Subject: [PATCH] fix(anatomy): V1-2.2 owner arbitrations on SVG references and bytes Refuse local references to ids a drawing does not define, scope canvas ids per view, upload every drawing in one stored form checked by the verdict and the preview, keep strict numeric rgb() colours, and commit real Inkscape 1.4.3 exports with their generator. --- apps/app/tests/helpers/svgTree.ts | 25 ++ apps/app/tests/unit/canvas.trees.test.tsx | 6 +- crates/core/src/anatomy/compose.rs | 36 +++ crates/core/src/anatomy/svg/clean.rs | 69 +++-- crates/core/src/anatomy/svg/mod.rs | 7 +- crates/core/src/anatomy/svg/refs.rs | 70 ++++- crates/core/src/anatomy/svg/sanitize.rs | 174 ++++++++++- crates/core/src/anatomy/svg/serialize.rs | 13 + crates/core/src/anatomy/svg/tree.rs | 5 + crates/core/src/curation/upload.rs | 7 +- crates/core/src/domain/sheet/preview.rs | 10 +- crates/core/src/domain/sheet/verdict.rs | 7 + crates/core/tests/anatomy_canvas_op.rs | 16 +- crates/core/tests/anatomy_compose.rs | 20 +- crates/core/tests/common/svg_tree.rs | 24 ++ crates/core/tests/curation_upload.rs | 9 +- .../core/tests/fixtures/svg/exports/README.md | 20 +- .../fixtures/svg/exports/generate-inkscape.sh | 21 ++ .../exports/inkscape-1.4.3-inkscape-svg.svg | 83 ++++++ .../svg/exports/inkscape-1.4.3-paths.svg | 62 ++++ .../svg/exports/inkscape-1.4.3-plain-svg.svg | 71 +++++ .../fixtures/svg/exports/inkscape-source.svg | 15 + crates/core/tests/sheet_preview.rs | 30 ++ crates/core/tests/sheet_verdict.rs | 44 ++- crates/core/tests/svg_clean.rs | 268 ++++++++++++++--- crates/core/tests/svg_sanitiser.rs | 36 +++ crates/core/tests/svg_scope.rs | 276 ++++++++++++++++++ docs/ANNOTATION-RENDERING.md | 12 + packages/core-wasm/tests/preview.test.ts | 28 +- packages/core-wasm/types/DrawingResult.ts | 2 +- packages/core-wasm/types/SvgCleaning.ts | 6 +- 31 files changed, 1362 insertions(+), 110 deletions(-) create mode 100644 crates/core/tests/fixtures/svg/exports/generate-inkscape.sh create mode 100644 crates/core/tests/fixtures/svg/exports/inkscape-1.4.3-inkscape-svg.svg create mode 100644 crates/core/tests/fixtures/svg/exports/inkscape-1.4.3-paths.svg create mode 100644 crates/core/tests/fixtures/svg/exports/inkscape-1.4.3-plain-svg.svg create mode 100644 crates/core/tests/fixtures/svg/exports/inkscape-source.svg create mode 100644 crates/core/tests/svg_scope.rs diff --git a/apps/app/tests/helpers/svgTree.ts b/apps/app/tests/helpers/svgTree.ts index 02e532f6..990e7e0f 100644 --- a/apps/app/tests/helpers/svgTree.ts +++ b/apps/app/tests/helpers/svgTree.ts @@ -101,3 +101,28 @@ export function withTriggerNamesPerLanguage(tree: unknown, views: readonly ViewN visit(copy) return copy } + +/** + * Owner decision 2026-10-29 (V1-2.2): the canvas, inlined in the page, scopes the drawing's + * ids and the references to them to its view (`fv..`). Applied to a frozen tree (a + * deep copy), written apart from the core's rewrite: every `id`, every `#id` href and every + * plain `url(#` (the only spelling of the product drawing). Mirrors + * `crates/core/tests/common/svg_tree.rs`. + */ +export function withIdsScopedPerView(tree: unknown, viewId: string): unknown { + const copy = structuredClone(tree) + const scope = `fv.${viewId}.` + const visit = (node: unknown): void => { + if (typeof node !== 'object' || node === null) return + const n = node as { attrs?: Record; children?: unknown[] } + for (const [name, value] of Object.entries(n.attrs ?? {})) { + if (name === 'id') n.attrs![name] = `${scope}${value}` + else if ((name === 'href' || name === 'xlink:href') && value.startsWith('#')) { + n.attrs![name] = `#${scope}${value.slice(1)}` + } else n.attrs![name] = value.replaceAll('url(#', `url(#${scope}`) + } + n.children?.forEach(visit) + } + visit(copy) + return copy +} diff --git a/apps/app/tests/unit/canvas.trees.test.tsx b/apps/app/tests/unit/canvas.trees.test.tsx index fcf88199..93426a8d 100644 --- a/apps/app/tests/unit/canvas.trees.test.tsx +++ b/apps/app/tests/unit/canvas.trees.test.tsx @@ -21,6 +21,7 @@ import { import { checkR3Golden, treeOf, + withIdsScopedPerView, withTriggerNamesPerLanguage, type TreeNode, } from '../helpers/svgTree' @@ -81,7 +82,10 @@ describe.each(VIEWS)('the %s canvas', (viewId) => { it('builds the frozen tree with no state', () => { const build = buildViewSvg(sheet, viewId) if (!build.ok) throw new Error(`canvas ${viewId}: ${build.failure}`) - checkR3Golden(`canvas.${viewId}.json`, treeOf(build.svg), declared) + // and the owner's decision 2026-10-29: the drawing's ids scoped to the view + checkR3Golden(`canvas.${viewId}.json`, treeOf(build.svg), (frozen) => + withIdsScopedPerView(declared(frozen), viewId), + ) }) it('shows the frozen layer for every state set of the reference reports', async () => { diff --git a/crates/core/src/anatomy/compose.rs b/crates/core/src/anatomy/compose.rs index 6c7e3389..593e1808 100644 --- a/crates/core/src/anatomy/compose.rs +++ b/crates/core/src/anatomy/compose.rs @@ -4,10 +4,18 @@ //! is known, the interactive layer) and the PDF highlight (`packages/anatomy/src/render/ //! compose.ts`: the asset's root framed in place, the selected markers only, recoloured, the //! halo filter on request). Both take a sanitised tree, never text. +//! +//! The canvas is inlined into the page, where `#id` resolves against the whole document: its +//! ids and the references to them are scoped to the view (`fv..`, owner decision +//! 2026-10-29), so a view never resolves into another view's drawing or the page, and the page +//! never into a drawing. The reader already refused references to ids a drawing does not +//! define. Stored bytes are never rewritten; the PDF view is rasterised as a document of its +//! own and keeps the drawing's ids. use std::collections::HashSet; use super::layer::{LayerError, find_view, render_layer, view_box_of}; +use super::svg::sanitize::{local_reference_text, scope_url_targets}; use super::svg::serialize::is_xml_text; use super::svg::{Element, Node}; use crate::domain::sheet::AnatomicalSheetDocument; @@ -73,11 +81,39 @@ pub fn compose_canvas( .or_else(|| asset.attr("viewBox").map(str::to_owned)) .unwrap_or_else(|| CANVAS_FALLBACK_VIEW_BOX.to_owned()); root.set_attr("viewBox", view_box); + scope_ids(&mut root, &id_scope(view_id)); let layer = render_layer(document, view_id, &HashSet::new())?; root.children.push(Node::Element(checked(layer)?)); Ok(root) } +/// The prefix of a view's ids in its canvas. Stable ids hold no `.`, so the scope of one view +/// is never the start of another's, and no page id starts with `fv.`. +pub fn id_scope(view_id: &str) -> String { + format!("fv.{view_id}.") +} + +/// Every `id`, every local `href` / `xlink:href` and every `url(#id)` of the drawing, with +/// `scope` written in front of the id. +fn scope_ids(element: &mut Element, scope: &str) { + for attr in &mut element.attrs { + attr.value = match attr.qname.as_str() { + "id" => format!("{scope}{}", attr.value), + "href" | "xlink:href" => match local_reference_text(&attr.value).strip_prefix('#') { + Some(id) => format!("#{scope}{id}"), + None => continue, + }, + _ if attr.value.contains('(') => scope_url_targets(&attr.value, scope), + _ => continue, + }; + } + for child in &mut element.children { + if let Node::Element(child) = child { + scope_ids(child, scope); + } + } +} + /// The PDF view of `view_id`: `asset` (sanitised, consumed) framed on the view, the markers /// of `annotation_ids` only, recoloured, with the halo filter when `halo`. pub fn compose_highlight( diff --git a/crates/core/src/anatomy/svg/clean.rs b/crates/core/src/anatomy/svg/clean.rs index f116f623..d18fd35e 100644 --- a/crates/core/src/anatomy/svg/clean.rs +++ b/crates/core/src/anatomy/svg/clean.rs @@ -3,24 +3,28 @@ //! //! - rejected: a hard rejection of the reader (with its reason), a text that is not UTF-8 //! (`malformed`), or any removal of active or external content, or a processing -//! instruction (`svg_active_content`, the names in `active`, a PI as `?`); no -//! cleaning is offered; +//! 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`); no cleaning is offered; //! - cleanable: only other removals (editor metadata and namespaces, `data-*`, `