diff --git a/crates/engine/src/script/dom.rs b/crates/engine/src/script/dom.rs index a0bd117d11..8168075b9b 100644 --- a/crates/engine/src/script/dom.rs +++ b/crates/engine/src/script/dom.rs @@ -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 = 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::>().join(", "); @@ -2356,6 +2357,9 @@ fn items_remove_all(h: &Host, a: &[Data]) -> Answer { let list = on_locked.iter().map(u64::to_string).collect::>().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::>()})))?; } @@ -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 { .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 = 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 { for g in released { diff --git a/crates/engine/src/script/dom/text.rs b/crates/engine/src/script/dom/text.rs index 0555782952..1a86a1b50e 100644 --- a/crates/engine/src/script/dom/text.rs +++ b/crates/engine/src/script/dom/text.rs @@ -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 = 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::>().join(", "); @@ -1499,6 +1500,10 @@ fn frames_remove_all(h: &Host, a: &[Data]) -> Answer { let list = on_locked.iter().map(u64::to_string).collect::>().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::>()}); super::atomically(h, |h| on_doc(h, doc.uid, true, |h| super::clear_releasing(h, &released, ¶ms)))?; diff --git a/crates/engine/src/tests/script_dom_tracing.rs b/crates/engine/src/tests/script_dom_tracing.rs index 24223785dd..0f6851d342 100644 --- a/crates/engine/src/tests/script_dom_tracing.rs +++ b/crates/engine/src/tests/script_dom_tracing.rs @@ -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] diff --git a/docs/mcp.md b/docs/mcp.md index 8e2c1b01fb..c2b52798e3 100644 --- a/docs/mcp.md +++ b/docs/mcp.md @@ -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,