From 3d69c8d49ac66a54652ff08053a12049d465d348 Mon Sep 17 00:00:00 2001 From: dotCooCoo Date: Wed, 2 Dec 2026 22:54:26 -0800 Subject: [PATCH] Scripts: removeAll cannot leave an image first in a group named Image Trace textFrames.removeAll() and the removeAll() of a group's pageItems, groupItems and compoundPathItems checked only clip groups whose mask they release. Deleting the type or art below an image in an ordinary group named Image Trace left the image first, so the group was read as a tracing and the image dropped out of pageItems. Both now throw "Only Image Trace can make a tracing" and delete nothing, checking the whole set being deleted at once. --- crates/engine/src/script/dom.rs | 13 +++++ crates/engine/src/script/dom/layers.rs | 4 ++ crates/engine/src/script/dom/text.rs | 3 +- crates/engine/src/tests/script_dom_tracing.rs | 51 +++++++++++++++++++ docs/mcp.md | 4 +- 5 files changed, 73 insertions(+), 2 deletions(-) diff --git a/crates/engine/src/script/dom.rs b/crates/engine/src/script/dom.rs index 4d87c3b8a8..2ad7469dbb 100644 --- a/crates/engine/src/script/dom.rs +++ b/crates/engine/src/script/dom.rs @@ -3049,6 +3049,19 @@ fn makes_tracing(d: &Document, parent: NodeId, id: NodeId, at: Option) -> } } +/// Whether deleting every object in `removed` together would leave one of their parents, an +/// ordinary group named as a tracing, with an image first in it, which would make it a tracing. +pub(super) fn removal_makes_tracing(d: &Document, removed: &[NodeId]) -> bool { + let going: std::collections::HashSet = removed.iter().copied().collect(); + let mut parents: Vec = removed.iter().filter_map(|i| d.parent_of(*i)).collect(); + parents.sort_unstable_by_key(|p| p.0); + parents.dedup(); + parents + .into_iter() + .filter(|p| named_as_tracing(d, *p)) + .any(|p| d.children(Some(p)).and_then(|c| c.iter().find(|n| !going.contains(&n.id))).is_some_and(|n| matches!(n.kind, NodeKind::Image(_)))) +} + /// Moves the object `id` to `to` with `node.move`, its index worked out once it has left its place. fn place(h: &Host, doc: Doc, id: NodeId, to: &Destination) -> std::result::Result<(), HostError> { let index = with_doc(h, doc, |_, st| { diff --git a/crates/engine/src/script/dom/layers.rs b/crates/engine/src/script/dom/layers.rs index 0b1b1f0e57..3f40ad4c11 100644 --- a/crates/engine/src/script/dom/layers.rs +++ b/crates/engine/src/script/dom/layers.rs @@ -855,6 +855,10 @@ fn art_remove_all(h: &Host, a: &[Data], kind: Kind) -> Answer { "cannot delete a clipping path while its group or layer keeps other objects; release the clipping mask first: {list}" ))); } + // Deleting the objects above an image in an ordinary group named as a tracing would make it one. + if with_doc(h, doc, |_, st| super::removal_makes_tracing(&st.doc, &ids)) { + return Err(HostError::Error(super::NOT_A_TRACING.into())); + } if !ids.is_empty() { on_doc(h, doc.uid, true, |h| command(h, "edit.clear", &json!({"ids": ids.iter().map(|i| i.0).collect::>(), "keepEmpty": true})))?; } diff --git a/crates/engine/src/script/dom/text.rs b/crates/engine/src/script/dom/text.rs index 6237af9179..b14b065c84 100644 --- a/crates/engine/src/script/dom/text.rs +++ b/crates/engine/src/script/dom/text.rs @@ -1542,7 +1542,8 @@ fn frames_remove_all(h: &Host, a: &[Data]) -> Answer { // Type that clips a group or layer releases its mask, so the object above it is not made // the clipping path. let released = super::masks_cleared_by(&st.doc, &ids); - let tracing = super::release_makes_tracing(&st.doc, &released, &ids); + // So would deleting the type above an image in an ordinary group named as a tracing. + let tracing = super::release_makes_tracing(&st.doc, &released, &ids) || super::removal_makes_tracing(&st.doc, &ids); Ok::<_, HostError>((ids, refused, on_locked, released, tracing)) })?; if !refused.is_empty() { diff --git a/crates/engine/src/tests/script_dom_tracing.rs b/crates/engine/src/tests/script_dom_tracing.rs index 0f6851d342..403510d73c 100644 --- a/crates/engine/src/tests/script_dom_tracing.rs +++ b/crates/engine/src/tests/script_dom_tracing.rs @@ -1242,6 +1242,57 @@ fn removing_a_clipping_path_cannot_leave_an_image_first_in_a_group_named_as_a_tr let _ = std::fs::remove_dir_all(base); } +/// `textFrames.removeAll()` and `groupItems.removeAll()` on an ordinary group named as a tracing +/// refuse to leave an image first in it, before the document changes, and delete when another +/// object stays first. +#[test] +fn remove_all_cannot_leave_an_image_first_in_a_group_named_as_a_tracing() { + let base = folder("remove-all-trace-name"); + let refused = json!("Only Image Trace can make a tracing"); + for (under, call) in [ + ("var u = d.textFrames.add(); u.contents = 'Under';", "g.textFrames.removeAll();"), + ("d.selection = [d.pathItems.rectangle(50, 50, 5, 5)]; vc.run('object.group'); var u = d.pageItems[0];", "g.groupItems.removeAll();"), + ] { + let (mut s, _) = session(&base, false); + // The group holds the type or group at the bottom and the image above it. + log( + &mut s, + &base, + &format!( + "var d = app.activeDocument; var p = d.pathItems.rectangle(10, 10, 20, 20); d.selection = [p]; vc.run('object.group');\ + var g = d.pageItems[0]; g.name = 'Image Trace'; {under} u.move(g, ElementPlacement.PLACEATEND);\ + d.rasterItems[0].move(g, ElementPlacement.PLACEATBEGINNING); p.remove();" + ), + ); + let before = vectorcraft_format::save(&s.active().unwrap().doc, false); + let steps = undo_steps(&s); + let out = log( + &mut s, + &base, + &format!( + "var d = app.activeDocument, g = d.pageItems[0], m = ''; try {{ {call} m = 'done'; }} catch (e) {{ m = e.message; }} vc.log(m); vc.log(g.typename);" + ), + ); + assert_eq!(out, [refused.clone(), json!("GroupItem")], "{call}"); + assert_eq!(vectorcraft_format::save(&s.active().unwrap().doc, false), before, "{call}"); + assert_eq!(undo_steps(&s), steps, "{call}"); + assert!(traces(&s).is_empty()); + } + // With a rectangle left first, the type goes and the group stays an ordinary group. + let (mut s, _) = session(&base, false); + let out = log( + &mut s, + &base, + "var d = app.activeDocument; d.selection = [d.pathItems.rectangle(10, 10, 20, 20)]; vc.run('object.group');\ + var g = d.pageItems[0]; g.name = 'Image Trace'; var t = d.textFrames.add(); t.contents = 'Over';\ + t.move(g, ElementPlacement.PLACEATBEGINNING); d.rasterItems[0].move(g, ElementPlacement.PLACEATBEGINNING);\ + g.textFrames.removeAll(); vc.log(g.textFrames.length); vc.log(g.pageItems.length); vc.log(g.typename);", + ); + assert_eq!(out, [json!(0), json!(2), json!("GroupItem")]); + assert!(traces(&s).is_empty()); + let _ = std::fs::remove_dir_all(base); +} + /// `releaseTracing()` keeps only the source image, so it refuses a tracing that holds an object /// its trace did not make, before the document changes. #[test] diff --git a/docs/mcp.md b/docs/mcp.md index 1abf72fbba..30ac0bfb82 100644 --- a/docs/mcp.md +++ b/docs/mcp.md @@ -3246,7 +3246,9 @@ through `$`: delete the clipping path of a clip group or clipping layer also release that clipping mask in the same undo step, so the object above the clipping path does not become the new clipping path; a deletion that leaves the clipping path keeps the mask. When the released group is named `Image Trace` and an image would be left at its bottom, they - throw `Only Image Trace can make a tracing` and delete nothing. The `removeAll()` of a layer's or group's + throw `Only Image Trace can make a tracing` and delete nothing. `textFrames.removeAll()` and the `removeAll()` of a + group's `pageItems`, `groupItems` or `compoundPathItems` throw the same error and delete nothing when they would + leave an image at the bottom of an ordinary group named `Image Trace`. The `removeAll()` of a layer's or group's `pageItems`, and of any `groupItems` or `compoundPathItems`, does not release a mask: it throws as described above. Setting `width` or `height` scales the object along the page's axes, so a rotated object then reads back the size that was set. `index` and `getByName` throw