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.
This commit is contained in:
dotCooCoo committed 2026-12-02 22:58:00 -08:00
1 parent 72e99d1c3b
commit 3d69c8d49a
5 files changed
+73 -2

No files matched your search

+13
View File
@@ -3049,6 +3049,19 @@ fn makes_tracing(d: &Document, parent: NodeId, id: NodeId, at: Option<usize>) ->
}
}
/// 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<NodeId> = removed.iter().copied().collect();
let mut parents: Vec<NodeId> = 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| {
+4
View File
@@ -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::<Vec<_>>(), "keepEmpty": true})))?;
}
+2 -1
View File
@@ -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() {
@@ -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]
+3 -1
View File
@@ -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