From 22b169a90547fab18aaf9914ddd5efc92e877676 Mon Sep 17 00:00:00 2001 From: dotCooCoo Date: Fri, 4 Dec 2026 17:29:32 -0800 Subject: [PATCH 1/2] Scripts: a group's pathItems.removeAll() walks only the group removeAll() on a layer's or group's pathItems walked every object in the document to check clipping paths, even when the collection was empty or held two paths. It now walks the container's own objects, or, when the container sits in the clipping path of a clipping group, the subtree of the highest such clipping path and the group it clips. An empty collection walks nothing. The document's own pathItems.removeAll() still checks every object. A scoped removeAll() now also throws when a path id in the list resolves to an object outside the container, which a damaged file with repeated ids can cause, instead of deleting that other object. The walk came in with #9288 ("Scripts: paths, path points and colors for scripts"). --- crates/engine/src/script/dom.rs | 2 ++ crates/engine/src/script/dom/paths.rs | 41 ++++++++++++++++++++++++++- crates/engine/src/tests/script_dom.rs | 22 ++++++++++++++ 3 files changed, 64 insertions(+), 1 deletion(-) diff --git a/crates/engine/src/script/dom.rs b/crates/engine/src/script/dom.rs index 3c12eb0027..0cc25dc6fe 100644 --- a/crates/engine/src/script/dom.rs +++ b/crates/engine/src/script/dom.rs @@ -122,6 +122,8 @@ pub(super) use colors::of_color as color_data; #[cfg(test)] pub(crate) use paths::POINT_LOOKS; #[cfg(test)] +pub(crate) use paths::REMOVE_ALL_VISITS; +#[cfg(test)] pub(crate) use paths::index_builds; pub use variables::{MAX_HANDLES, STALE_DATASET, STALE_VARIABLE}; diff --git a/crates/engine/src/script/dom/paths.rs b/crates/engine/src/script/dom/paths.rs index cc1cbe6a62..01dc239c1f 100644 --- a/crates/engine/src/script/dom/paths.rs +++ b/crates/engine/src/script/dom/paths.rs @@ -638,6 +638,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 = 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 @@ -658,9 +664,42 @@ fn items_remove_all(h: &super::Host, a: &[Data]) -> Answer { let refused: Vec = ids.iter().filter(|i| !editable(d, **i)).map(|i| i.0).take(20).collect(); let locks = d.locked_artboards(); let on_locked: Vec = 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> = 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> = 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()); } diff --git a/crates/engine/src/tests/script_dom.rs b/crates/engine/src/tests/script_dom.rs index 54926e1659..6b45c5105f 100644 --- a/crates/engine/src/tests/script_dom.rs +++ b/crates/engine/src/tests/script_dom.rs @@ -4058,6 +4058,28 @@ 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"); +} + /// 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] From fcc8f717dc85c3f32799d9e15b5c6d0fbbbeef88 Mon Sep 17 00:00:00 2001 From: dotCooCoo Date: Fri, 4 Dec 2026 18:15:21 -0800 Subject: [PATCH 2/2] Scripts: test that a group's pathItems.removeAll() refuses an id found outside it Adds a test that builds a document where a path outside a group has the same id as a path in the group, and checks that the group's pathItems.removeAll() throws and deletes nothing. --- crates/engine/src/tests/script_dom.rs | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/crates/engine/src/tests/script_dom.rs b/crates/engine/src/tests/script_dom.rs index 6b45c5105f..1bab9b1e4b 100644 --- a/crates/engine/src/tests/script_dom.rs +++ b/crates/engine/src/tests/script_dom.rs @@ -4080,6 +4080,33 @@ fn group_path_items_remove_all_walks_only_the_group() { 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]