From eb5e4c5eb7d0a4b5bbdea3e369373c0a23785192 Mon Sep 17 00:00:00 2001 From: dotCooCoo Date: Wed, 2 Dec 2026 08:32:22 -0800 Subject: [PATCH] Scripts: retrace and release refuse a tracing with locked traced artwork Retracing and releasing a tracing discard the objects its trace made, so both now throw "Cannot edit page item " when one of them is locked, with nothing changed. Added artwork is still reported first. The scan stops at the first added object and charges the objects it looked at to the run on both the success and refusal paths. --- crates/engine/src/script/dom/tracing.rs | 42 ++++++++++++------- crates/engine/src/tests/script_dom_tracing.rs | 29 +++++++++++++ docs/mcp.md | 3 +- 3 files changed, 58 insertions(+), 16 deletions(-) diff --git a/crates/engine/src/script/dom/tracing.rs b/crates/engine/src/script/dom/tracing.rs index a5b4ee561c..a37a062849 100644 --- a/crates/engine/src/script/dom/tracing.rs +++ b/crates/engine/src/script/dom/tracing.rs @@ -321,9 +321,11 @@ fn replace(h: &Host, item: Item, id: &str, params: &Value, key: &str) -> Result< }) } -/// Throws `refusal` when the Image Trace object of `item` holds an object its trace did not make. +/// Throws `refusal` when the Image Trace object of `item` holds an object its trace did not make, +/// and "Cannot edit page item" when it holds a locked object other than its source image, which +/// the replacement would discard. The objects looked at are charged to the run either way. fn refuse_added(h: &Host, item: Item, refusal: &str) -> Result<(), HostError> { - read_group(h, item, |_, group| { + let (visited, result) = read_group(h, item, |_, group| { remember_source(h, item.doc.uid, group); // The objects the trace made, as the tracing records them; a tracing saved without that // record is checked against the objects it held when this run first saw it. @@ -333,20 +335,30 @@ fn refuse_added(h: &Host, item: Item, refusal: &str) -> Result<(), HostError> { Some(_) => None, None => Some(descendants.get(&(item.doc.uid, item.id.0)).ok_or_else(stale)?), }; - let mut added = false; - group.walk(&mut |n| { - added |= n.id != group.id - && match (&made, ids) { - (Some(m), _) => !m.contains(n.id), - (None, Some(ids)) => !ids.contains(&n.id), - (None, None) => false, - }; - }); - if added { - return Err(HostError::Error(refusal.into())); + let source = group.children().and_then(|c| c.first()).map(|n| n.id); + let mut visited = 0u64; + let mut locked = None; + let mut todo: Vec<&Arc> = group.children().map(|c| c.iter().collect()).unwrap_or_default(); + while let Some(n) = todo.pop() { + visited += 1; + let added = match (&made, ids) { + (Some(m), _) => !m.contains(n.id), + (None, Some(ids)) => !ids.contains(&n.id), + (None, None) => false, + }; + // Added artwork is reported before a locked object, whichever comes first. + if added { + return Ok((visited, Err(HostError::Error(refusal.into())))); + } + if n.locked && Some(n.id) != source && locked.is_none() { + locked = Some(n.id); + } + todo.extend(n.children().into_iter().flatten()); } - Ok(()) - }) + Ok((visited, locked.map_or(Ok(()), |id| Err(HostError::Error(format!("Cannot edit page item {}", id.0)))))) + })?; + h.charge_call(visited)?; + result } /// Traces the Image Trace object of `item` again with `params` (`id` is added) and makes its diff --git a/crates/engine/src/tests/script_dom_tracing.rs b/crates/engine/src/tests/script_dom_tracing.rs index ee3bb6a1b8..24223785dd 100644 --- a/crates/engine/src/tests/script_dom_tracing.rs +++ b/crates/engine/src/tests/script_dom_tracing.rs @@ -1224,6 +1224,35 @@ fn release_refuses_added_artwork_before_changing_the_document() { let _ = std::fs::remove_dir_all(base); } +/// Retracing and releasing discard the objects the trace made, so both refuse a tracing that holds +/// a locked one, before the document changes. +#[test] +fn retrace_and_release_refuse_locked_traced_artwork() { + let base = folder("locked-traced-artwork"); + for action in ["p.tracing.tracingOptions.threshold = 90;", "p.tracing.releaseTracing();"] { + let (mut s, image) = session(&base, false); + let r = s.execute("imageTrace.make", &json!({"id": image.0})).unwrap(); + let group = NodeId(r["id"].as_u64().unwrap()); + let made = s.active().unwrap().doc.node(group).unwrap().children().unwrap()[1].id; + s.edit("Lock traced artwork", |d, _| { + d.node_mut(made).unwrap().locked = true; + Ok(()) + }) + .unwrap(); + let before = vectorcraft_format::save(&s.active().unwrap().doc, false); + let steps = undo_steps(&s); + let out = log( + &mut s, + &base, + &format!("var p = app.activeDocument.pageItems[0], m = ''; try {{ {action} }} catch (e) {{ m = e.message; }} vc.log(m);"), + ); + assert_eq!(out, [json!(format!("Cannot edit page item {}", made.0))], "{action}"); + assert_eq!(vectorcraft_format::save(&s.active().unwrap().doc, false), before, "{action}"); + assert_eq!(undo_steps(&s), steps, "{action}"); + } + let _ = std::fs::remove_dir_all(base); +} + /// The `TracingOptions` and `TracingObject` members that are not implemented are left out of /// `hasOwnProperty` and `reflect`, and still throw when read. #[test] diff --git a/docs/mcp.md b/docs/mcp.md index 22b1c65c7c..378206435f 100644 --- a/docs/mcp.md +++ b/docs/mcp.md @@ -3270,7 +3270,8 @@ through `$`: and `TracingOptions` follow the new object, and a run traces again at most 10,000 times. Tracing again throws `Cannot retrace a tracing containing added artwork`, with nothing changed, when the tracing holds an object its trace did not make, whether this run, an earlier run or a command added it, and `releaseTracing()` throws - `Cannot release a tracing containing added artwork` in the same case. `imageTrace.make` records the objects it made in + `Cannot release a tracing containing added artwork` in the same case. When no object was added, both throw + `Cannot edit page item `, with nothing changed, when an object the trace made is locked. `imageTrace.make` records the objects it made in the tracing's `trace` settings as `made`, ranges of ids `[[first, last], ...]`, and a copy of the tracing records their copies. A tracing saved without `made` is checked only for objects added in the same run. `threshold` takes a whole number from 0 to 255, `maxColors` from 2 to 256 and `minArea` (the Noise setting, in pixels) from 0 to the largest