From a53165fa8544418932c2ee3fc91decbdf9f8846f Mon Sep 17 00:00:00 2001 From: dotCooCoo Date: Fri, 4 Dec 2026 17:03:47 -0800 Subject: [PATCH] Scripts: stale layers and artboards throw their stale error from index() layers.index(n) and artboards.index(n) returned Not found for a negative or fractional number before checking whether the collection was stale, so a script holding a stale collection got the wrong error. Both now check the collection first, as the art collections' index() does. --- crates/engine/src/script/dom/layers.rs | 16 ++++++++++++---- crates/engine/src/tests/script_dom.rs | 14 ++++++++++++++ docs/mcp.md | 3 ++- 3 files changed, 28 insertions(+), 5 deletions(-) diff --git a/crates/engine/src/script/dom/layers.rs b/crates/engine/src/script/dom/layers.rs index 95ef993284..eb8d71ad3f 100644 --- a/crates/engine/src/script/dom/layers.rs +++ b/crates/engine/src/script/dom/layers.rs @@ -634,8 +634,12 @@ fn layers_by_name(h: &Host, a: &[Data]) -> Answer { fn layers_index(h: &Host, a: &[Data]) -> Answer { match arg(a, 1) { Data::String(_) => layers_by_name(h, a), - // A number is a position here, never a name ([`layers_at`]). - Data::Number(n) if super::position(*n).is_none() => Err(HostError::Error(NOT_FOUND.into())), + // A number is a position here, never a name ([`layers_at`]). A stale collection throws its + // stale error first. + Data::Number(n) if super::position(*n).is_none() => { + layers_scope(h, arg(a, 0))?; + Err(HostError::Error(NOT_FOUND.into())) + } Data::Number(_) => match layers_at(h, a)? { Data::Undefined => Err(HostError::Error(NOT_FOUND.into())), d => Ok(d), @@ -1479,8 +1483,12 @@ fn boards_by_name(h: &Host, a: &[Data]) -> Answer { fn boards_index(h: &Host, a: &[Data]) -> Answer { match arg(a, 1) { Data::String(_) => boards_by_name(h, a), - // A number is a position here, never a name ([`boards_at`]). - Data::Number(n) if super::position(*n).is_none() => Err(HostError::Error(NOT_FOUND.into())), + // A number is a position here, never a name ([`boards_at`]). A stale collection throws its + // stale error first. + Data::Number(n) if super::position(*n).is_none() => { + boards_doc(h, a)?; + Err(HostError::Error(NOT_FOUND.into())) + } Data::Number(_) => match boards_at(h, a)? { Data::Undefined => Err(HostError::Error(NOT_FOUND.into())), d => Ok(d), diff --git a/crates/engine/src/tests/script_dom.rs b/crates/engine/src/tests/script_dom.rs index 54926e1659..66f467d576 100644 --- a/crates/engine/src/tests/script_dom.rs +++ b/crates/engine/src/tests/script_dom.rs @@ -4899,6 +4899,20 @@ fn a_stale_containers_list_throws_its_stale_error_for_any_index() { } } +/// A stale `layers` or `artboards` collection's `index(n)` throws the stale error for every +/// number, a negative or fractional one included, as the art collections do. +#[test] +fn stale_layers_and_artboards_throw_their_stale_error_for_any_index() { + let (mut s, ..) = doc(); + run(&mut s, "#targetengine keep\nvar d = app.activeDocument, L = d.layers, A = d.artboards;").unwrap(); + for list in ["L", "A"] { + for n in ["-1", "1.5", "0"] { + let e = failure(&mut s, &format!("#targetengine keep\n{list}.index({n});")); + assert!(e.contains("Stale document"), "{list}.index({n}): {e}"); + } + } +} + /// `removeAll()` on a clip group releases its mask, so an object added to the group afterwards is /// not its clipping path; the run's one undo step puts everything back. #[test] diff --git a/docs/mcp.md b/docs/mcp.md index 183f042d80..f3a02d52d4 100644 --- a/docs/mcp.md +++ b/docs/mcp.md @@ -3434,7 +3434,8 @@ through `$`: chosen. The chosen artboard stays active when another is inserted before it, even if it was already the active one. `insert`, `remove` and `setActiveArtboardIndex` throw a `RangeError` for an index outside the list. Reading an index past the last artboard gives `undefined` from `artboards[i]` and throws `Not found` from - `artboards.index(i)`. A layer that was deleted throws `Stale layer`, and an artboard that was deleted throws + `artboards.index(i)`. `layers.index(n)` and `artboards.index(n)` on a collection whose document or layer is stale + throw its stale error for any number, a negative or fractional one included, before `Not found`. A layer that was deleted throws `Stale layer`, and an artboard that was deleted throws `Stale artboard`. `Layer.blendingMode`, `artworkKnockout` and `sliced` and `GroupItems.createFromFile` throw an error that says they are not available yet. `GroupItems.createFromFile` on a collection whose document, layer or group was deleted throws `Stale page item` or `Stale document` first.