Scripts: document pathItems.removeAll refuses to delete a clipping path

Document.pathItems.removeAll() deleted every path of the document even when
one was the clipping path of a group or layer that keeps other objects, so
the next object became the clipping path. The scoped collections already
refuse this. The document's collection now runs the same check before
anything is deleted, and also refuses when it would empty a compound path or
group that clips other objects. edit.clear removes an emptied compound path
and keeps an emptied group, and the check follows it.

The fault came with #9041, which added Document.pathItems.
This commit is contained in:
dotCooCoo committed 2026-12-02 22:10:09 -08:00
1 parent 15cc3b048f
commit 13488c3103
4 files changed
+216 -7

No files matched your search

+49 -5
View File
@@ -495,18 +495,56 @@ fn items_index(h: &super::Host, a: &[Data]) -> Answer {
/// `pathItems.removeAll()`: deletes every path of the document in one command. A hidden or locked
/// path, one in a hidden or locked layer or group, or one on a locked artboard stops it before
/// anything is deleted, as `PageItems.removeAll()` does.
/// anything is deleted, as `PageItems.removeAll()` does. So does the clipping path of a group or
/// layer that keeps other objects, or a compound path or group that clips one and would be left
/// empty.
fn items_remove_all(h: &super::Host, a: &[Data]) -> Answer {
if super::layers::is_scoped(a, "PathItems") {
return super::layers::scoped_remove_all(h, a);
}
let doc = items_doc(h, a)?;
let (ids, refused, on_locked) = with_doc(h, doc, |_, st| {
let (ids, refused, on_locked, masks) = with_doc(h, doc, |_, st| {
let ids = path_items(h, st)?;
let refused: Vec<u64> = ids.iter().filter(|i| !editable(&st.doc, **i)).map(|i| i.0).take(20).collect();
let locks = st.doc.locked_artboards();
let d = &st.doc;
let refused: Vec<u64> = ids.iter().filter(|i| !editable(d, **i)).map(|i| i.0).take(20).collect();
let locks = d.locked_artboards();
let on_locked: Vec<u64> = ids.iter().filter(|i| locks.holds_id(**i)).map(|i| i.0).take(20).collect();
Ok::<_, HostError>((ids, refused, on_locked))
let mut next: Vec<_> = d.layers.iter().collect();
let mut nodes = Vec::new();
while let Some(n) = next.pop() {
nodes.push(n);
next.extend(n.children().into_iter().flatten());
}
let mut going: std::collections::HashSet<NodeId> = ids.iter().copied().collect();
let mut affected: std::collections::HashSet<NodeId> = ids.iter().copied().collect();
let mut geometry = std::collections::HashSet::new();
let mut masks = Vec::new();
for n in nodes.into_iter().rev() {
let kids = n.children().map_or(&[][..], Vec::as_slice);
// Clear removes compound paths emptied of direct members, but keeps empty groups.
if matches!(n.kind, NodeKind::Compound { .. })
&& !kids.is_empty()
&& kids.iter().all(|k| matches!(k.kind, NodeKind::Path { .. }) && going.contains(&k.id))
{
going.insert(n.id);
}
if kids.iter().any(|k| affected.contains(&k.id)) {
affected.insert(n.id);
}
if (matches!(n.kind, NodeKind::Path { .. }) && !going.contains(&n.id)) || kids.iter().any(|k| geometry.contains(&k.id)) {
geometry.insert(n.id);
}
if n.clips()
&& let Some(mask) = kids.first()
&& affected.contains(&mask.id)
&& !geometry.contains(&mask.id)
&& kids.iter().skip(1).any(|k| !going.contains(&k.id) && !matches!(k.kind, NodeKind::RulerGuide { .. }))
&& masks.len() < 20
{
masks.push(mask.id.0);
}
}
Ok::<_, HostError>((ids, refused, on_locked, masks))
})?;
if !refused.is_empty() {
let list = refused.iter().map(u64::to_string).collect::<Vec<_>>().join(", ");
@@ -516,6 +554,12 @@ fn items_remove_all(h: &super::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 !masks.is_empty() {
let list = masks.iter().map(u64::to_string).collect::<Vec<_>>().join(", ");
return Err(HostError::Error(format!(
"cannot delete a clipping path while its group or layer keeps other objects; release the clipping mask first: {list}"
)));
}
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<_>>()})))?;
}