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-*`, `