Merge pull request #10890 from dotCooCoo/fix/scoped-remove-all-walk

Scripts: a group's pathItems.removeAll() walks only the group
This commit is contained in:
.CooCoo authored and GitHub committed 2026-12-04 22:26:30 -08:00
commit db8d2c11e9
3 files changed
+90 -2

No files matched your search

+1 -1
View File
@@ -123,7 +123,7 @@ pub(super) use colors::of_color as color_data;
#[cfg(test)]
pub(crate) use paths::index_builds;
#[cfg(test)]
pub(crate) use paths::{PATH_LIST_BUILDS, POINT_LOOKS};
pub(crate) use paths::{PATH_LIST_BUILDS, POINT_LOOKS, REMOVE_ALL_VISITS};
pub(super) use resources::{Kind as ResourceKind, gone as resources_gone, held as holds_resources};
pub use variables::{MAX_HANDLES, STALE_DATASET, STALE_VARIABLE};
+40 -1
View File
@@ -707,6 +707,12 @@ fn items_index(h: &super::Host, a: &[Data]) -> Answer {
}
}
#[cfg(test)]
thread_local! {
/// How many objects [`items_remove_all`] walked on this thread, for the tests.
pub(crate) static REMOVE_ALL_VISITS: std::cell::Cell<u64> = const { std::cell::Cell::new(0) };
}
/// `pathItems.removeAll()`: deletes every path the collection lists 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. A compound path's
@@ -727,9 +733,42 @@ fn items_remove_all(h: &super::Host, a: &[Data]) -> Answer {
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();
let mut next: Vec<_> = d.layers.iter().collect();
// The document's list is checked against every object. A layer's or group's list is checked
// against the container's subtree, or, when the container lies in the clipping path of a
// clipping group above it, against the subtree of the highest such clipping path and the
// group it clips; no other outline holds its paths. An empty list is not walked.
let mut next: Vec<&Arc<crate::doc::Node>> = Vec::new();
let mut nodes = Vec::new();
if !ids.is_empty() {
match c.and_then(|c| d.index_path(c)) {
None => next.extend(d.layers.iter()),
Some(at) => {
// A damaged file can give two objects one id; a path that the id finds outside
// the container would be deleted in place of the container's own.
if let Some(id) = ids.iter().find(|i| d.index_path(**i).is_none_or(|p| p.len() != at.len() + 1 || !p.starts_with(&at))) {
return Err(HostError::Error(format!("cannot delete PathItem {}: its id also names another object", id.0)));
}
let mut line: Vec<&Arc<crate::doc::Node>> = Vec::with_capacity(at.len());
let mut kids = d.layers.as_slice();
for &i in &at {
let Some(n) = kids.get(i) else { break };
line.push(n);
kids = n.children().map_or(&[][..], Vec::as_slice);
}
// A clipping path is its group's first child, found by place, not by id.
let clip = (1..line.len()).find(|&k| line[k - 1].clips() && at[k] == 0);
if let Some(k) = clip {
#[cfg(test)]
REMOVE_ALL_VISITS.with(|v| v.set(v.get() + 1));
nodes.push(line[k - 1]);
}
next.extend(line.get(clip.unwrap_or(line.len().saturating_sub(1))).copied());
}
}
}
while let Some(n) = next.pop() {
#[cfg(test)]
REMOVE_ALL_VISITS.with(|v| v.set(v.get() + 1));
nodes.push(n);
next.extend(n.children().into_iter().flatten());
}
+49
View File
@@ -4095,6 +4095,55 @@ const CAUGHT_PATHS_REMOVE_ALL: &str = "var p = app.activeDocument.pathItems, m =
try { p.removeAll(); } catch (e) { m = e.message; }\
vc.log(m); vc.log(p.length);";
/// A group's `pathItems.removeAll()` walks the group, not the document: emptying a group of two
/// paths beside 500 other paths looks at a handful of objects, and an empty group's looks at none.
#[test]
fn group_path_items_remove_all_walks_only_the_group() {
let (mut s, a, b) = doc();
s.execute("select.set", &json!({"ids": [a.0, b.0]})).unwrap();
s.execute("object.group", &json!({})).unwrap();
for i in 0..500 {
rect(&mut s, f64::from(i % 25) * 10.0, f64::from(i / 25) * 10.0, 5.0, 5.0);
}
s.execute("select.none", &json!({})).unwrap();
let start = crate::script::dom::REMOVE_ALL_VISITS.with(|n| n.get());
let out = log(
&mut s,
"var g = app.activeDocument.groupItems[0]; g.pathItems.removeAll(); g.pathItems.removeAll();\
vc.log(g.pathItems.length); vc.log(app.activeDocument.pathItems.length);",
);
let visits = crate::script::dom::REMOVE_ALL_VISITS.with(|n| n.get()) - start;
assert_eq!(out, vec![json!(0), json!(500)]);
assert!(visits <= 3, "{visits} objects walked to empty a group of two paths");
}
/// In a damaged document where a path outside a group has the same id as a path in it, the
/// group's `pathItems.removeAll()` throws and deletes nothing, instead of deleting the outside path.
#[test]
fn group_path_items_remove_all_refuses_an_id_found_outside_the_group() {
let (mut s, a, b) = doc();
s.execute("select.set", &json!({"ids": [a.0, b.0]})).unwrap();
s.execute("object.group", &json!({})).unwrap();
let c = rect(&mut s, 200.0, 200.0, 20.0, 20.0);
s.execute("select.set", &json!({"ids": [c.0]})).unwrap();
s.execute("object.arrange.sendToBack", &json!({})).unwrap();
s.execute("select.none", &json!({})).unwrap();
let d = std::sync::Arc::make_mut(&mut s.active_mut().unwrap().doc);
d.node_mut(c).unwrap().id = a;
d.drop_lookup_cache();
// The first object in tree order with the id is the one outside the group.
assert_eq!(d.index_path(a).map(|p| p.len()), Some(2));
let n = d.layers[0].children().map_or(0, Vec::len);
let out = log(
&mut s,
"var d = app.activeDocument, m = ''; try { d.groupItems[0].pathItems.removeAll(); } catch (e) { m = e.message; }\
vc.log(m); vc.log(d.groupItems[0].pathItems.length);",
);
assert_eq!(out, vec![json!(format!("cannot delete PathItem {}: its id also names another object", a.0)), json!(2)]);
let d = &s.active().unwrap().doc;
assert_eq!(d.layers[0].children().map_or(0, Vec::len), n, "the path outside the group is kept");
}
/// The document's `pathItems.removeAll()` refuses to delete the path that clips a group while the
/// group keeps other art, and deletes nothing, as the scoped collections do.
#[test]