Scripts: deleting a clipping path cannot make a tracing

remove(), the document's pageItems.removeAll() and textFrames.removeAll()
release the mask of a clip group whose clipping path they delete. A clip
group named Image Trace with an image next above its clipping path then
became an ordinary group named as a tracing with an image first, which
reads as a tracing. They now throw "Only Image Trace can make a tracing"
and delete nothing in that case.

docs/mcp.md names the removeAll() collections that release a mask and
the ones that refuse to delete a clipping path instead.
This commit is contained in:
dotCooCoo committed 2026-12-02 20:24:21 -08:00
1 parent 0d8a56cfde
commit 80569dc577
4 files changed
+83 -7

No files matched your search

+24 -2
View File
@@ -2337,7 +2337,7 @@ fn items_remove_all(h: &Host, a: &[Data]) -> Answer {
return layers::scoped_remove_all(h, a);
}
let doc = items_doc(h, a)?;
let (ids, refused, on_locked, released) = with_doc(h, doc, |_, st| {
let (ids, refused, on_locked, released, tracing) = with_doc(h, doc, |_, st| {
let ids = top_items(&st.doc);
let whole = |id: &NodeId| editable(&st.doc, *id) && st.doc.node(*id).is_some_and(all_editable);
let refused: Vec<u64> = ids.iter().filter(|i| !whole(i)).map(|i| i.0).take(20).collect();
@@ -2346,7 +2346,8 @@ fn items_remove_all(h: &Host, a: &[Data]) -> Answer {
// A clipping layer emptied of its art releases its mask, so art added later is not made its
// clipping path.
let released = masks_cleared_by(&st.doc, &ids);
(ids, refused, on_locked, released)
let tracing = release_makes_tracing(&st.doc, &released, &ids);
(ids, refused, on_locked, released, tracing)
});
if !refused.is_empty() {
let list = refused.iter().map(u64::to_string).collect::<Vec<_>>().join(", ");
@@ -2356,6 +2357,9 @@ fn items_remove_all(h: &Host, a: &[Data]) -> Answer {
let list = on_locked.iter().map(u64::to_string).collect::<Vec<_>>().join(", ");
return Err(HostError::Error(format!("{ON_LOCKED_ARTBOARD}: {list}")));
}
if tracing {
return Err(HostError::Error(NOT_A_TRACING.into()));
}
if !ids.is_empty() {
on_doc(h, doc.uid, true, |h| clear_releasing(h, &released, &json!({"ids": ids.iter().map(|i| i.0).collect::<Vec<_>>()})))?;
}
@@ -2851,6 +2855,10 @@ fn item_remove(h: &Host, a: &[Data]) -> Answer {
return Err(HostError::Error(NOT_A_TRACING.into()));
}
let released = with_doc(h, item.doc, |_, st| masks_cleared_by(&st.doc, &[item.id]));
// A clip group named as a tracing, released with an image left first, would become one too.
if with_doc(h, item.doc, |_, st| release_makes_tracing(&st.doc, &released, &[item.id])) {
return Err(HostError::Error(NOT_A_TRACING.into()));
}
// A compound path whose last path this is stays, empty, as it does after `move`.
let params = json!({"ids": [item.id.0], "keepEmpty": true});
paths::see_all_points(h);
@@ -2873,6 +2881,20 @@ pub(super) fn masks_cleared_by(d: &Document, ids: &[NodeId]) -> Vec<NodeId> {
.collect()
}
/// Whether releasing the masks of `released` and deleting `removed` would leave one of those groups
/// an ordinary group named as a tracing with an image first in it, which would make it a tracing.
pub(super) fn release_makes_tracing(d: &Document, released: &[NodeId], removed: &[NodeId]) -> bool {
if released.is_empty() {
return false;
}
let removed: std::collections::HashSet<NodeId> = removed.iter().copied().collect();
released.iter().filter_map(|g| d.node(*g)).any(|g| {
matches!(g.kind, NodeKind::Group { .. })
&& g.name.as_deref() == Some(crate::cmd::TRACE_NAME)
&& g.children().and_then(|c| c.iter().find(|n| !removed.contains(&n.id))).is_some_and(|n| matches!(n.kind, NodeKind::Image(_)))
})
}
/// Releases the masks of `released` ([`masks_cleared_by`]), then runs `edit.clear` with `params`.
pub(super) fn clear_releasing(h: &Host, released: &[NodeId], params: &Value) -> std::result::Result<Data, HostError> {
for g in released {
+7 -2
View File
@@ -1480,7 +1480,7 @@ fn frames_path_text(h: &Host, a: &[Data]) -> Answer {
/// locked one, or one on a locked artboard, stops it first, as `PageItems.removeAll()` does.
fn frames_remove_all(h: &Host, a: &[Data]) -> Answer {
let (doc, c) = frames_scope(h, a)?;
let (ids, refused, on_locked, released) = with_doc(h, doc, |_, st| {
let (ids, refused, on_locked, released, tracing) = with_doc(h, doc, |_, st| {
let ids = listed(h, st, c)?;
let ok = |id: &NodeId| editable(&st.doc, *id) && st.doc.node(*id).is_some_and(all_editable);
let refused: Vec<u64> = ids.iter().filter(|i| !ok(i)).map(|i| i.0).take(20).collect();
@@ -1489,7 +1489,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);
Ok::<_, HostError>((ids, refused, on_locked, released))
let tracing = super::release_makes_tracing(&st.doc, &released, &ids);
Ok::<_, HostError>((ids, refused, on_locked, released, tracing))
})?;
if !refused.is_empty() {
let list = refused.iter().map(u64::to_string).collect::<Vec<_>>().join(", ");
@@ -1499,6 +1500,10 @@ fn frames_remove_all(h: &Host, a: &[Data]) -> Answer {
let list = on_locked.iter().map(u64::to_string).collect::<Vec<_>>().join(", ");
return Err(HostError::Error(format!("{}: {list}", super::ON_LOCKED_ARTBOARD)));
}
// A clip group named as a tracing, released with an image left first, would become one.
if tracing {
return Err(HostError::Error(super::NOT_A_TRACING.into()));
}
if !ids.is_empty() {
let params = json!({"ids": ids.iter().map(|i| i.0).collect::<Vec<_>>()});
super::atomically(h, |h| on_doc(h, doc.uid, true, |h| super::clear_releasing(h, &released, &params)))?;
@@ -1197,6 +1197,51 @@ fn an_image_cannot_be_left_first_in_a_group_named_as_a_tracing() {
let _ = std::fs::remove_dir_all(base);
}
/// Deleting the clipping path of a clip group named as a tracing releases its mask, so `remove()` and
/// `textFrames.removeAll()` refuse it when an image would be left first in the released group.
#[test]
fn removing_a_clipping_path_cannot_leave_an_image_first_in_a_group_named_as_a_tracing() {
let base = folder("release-trace-name");
let (mut s, _) = session(&base, false);
log(
&mut s,
&base,
"var d = app.activeDocument; d.selection = [d.rasterItems[0], d.pathItems.rectangle(10, 10, 20, 20)];\
vc.run('object.clippingMask.make'); d.pageItems[0].name = 'Image Trace';",
);
let before = vectorcraft_format::save(&s.active().unwrap().doc, false);
let steps = undo_steps(&s);
let out = log(
&mut s,
&base,
"var d = app.activeDocument, g = d.pageItems[0], m = '';\
try { d.pathItems[0].remove(); m = 'done'; } catch (e) { m = e.message; } vc.log(m); vc.log(g.clipped);",
);
assert_eq!(out, [json!("Only Image Trace can make a tracing"), json!(true)]);
assert_eq!(vectorcraft_format::save(&s.active().unwrap().doc, false), before);
assert_eq!(undo_steps(&s), steps);
let (mut s, _) = session(&base, false);
log(
&mut s,
&base,
"var d = app.activeDocument, t = d.textFrames.add(); t.contents = 'Clip';\
d.selection = [d.rasterItems[0], t]; vc.run('object.clippingMask.make'); d.pageItems[0].name = 'Image Trace';",
);
let before = vectorcraft_format::save(&s.active().unwrap().doc, false);
let steps = undo_steps(&s);
let out = log(
&mut s,
&base,
"var d = app.activeDocument, g = d.pageItems[0], m = '';\
try { g.textFrames.removeAll(); m = 'done'; } catch (e) { m = e.message; } vc.log(m); vc.log(g.textFrames.length);",
);
assert_eq!(out, [json!("Only Image Trace can make a tracing"), json!(1)]);
assert_eq!(vectorcraft_format::save(&s.active().unwrap().doc, false), before);
assert_eq!(undo_steps(&s), steps);
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]
+7 -3
View File
@@ -3242,9 +3242,13 @@ through `$`:
is hidden or locked, is inside a hidden or locked layer or group, or is a group that holds one, and `removeAll()`
when any object is, throw `cannot delete hidden or locked objects` and delete nothing. `remove()` on an object on
a locked artboard, and `removeAll()` when any object is on one, throw `cannot delete objects on a locked artboard`
and delete nothing. `remove()`, and any collection's `removeAll()`, that deletes the clipping path of a clip group
or clipping layer also releases 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. Setting `width` or `height` scales the object along
and delete nothing. `remove()`, the document's `pageItems.removeAll()` and any `textFrames.removeAll()` that
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
`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
`Not found`; and
`app.activeDocument` with no document open throws `No active document`. `Document.selection` takes one page item,