From fe0e4086078c53b0321a482ccf180952f665acfb Mon Sep 17 00:00:00 2001 From: dotCooCoo Date: Tue, 24 Nov 2026 16:15:50 -0800 Subject: [PATCH] Scripts: zOrder refuses art in a hidden or locked layer, and the same opacity may be assigned - zOrder() on an object in a hidden or locked layer throws Cannot edit and changes nothing, as the other setters do. - Assigning an object the opacity it already has no longer throws when its layer is hidden or locked. hidden and locked are still refused there, whatever the value. - The docs say that a top-level var selection = ... assigns the selection, that the selection order and the tab order of documents differ from the objectReferences page, and that assigning a member a collection lacks throws Not found. Docs: docs/mcp.md. --- crates/engine/src/script/dom.rs | 40 ++++++++++++++++++--------- crates/engine/src/tests/script_dom.rs | 29 +++++++++++++++---- docs/mcp.md | 23 +++++++++------ 3 files changed, 65 insertions(+), 27 deletions(-) diff --git a/crates/engine/src/script/dom.rs b/crates/engine/src/script/dom.rs index 7b9df28106..8b68a39250 100644 --- a/crates/engine/src/script/dom.rs +++ b/crates/engine/src/script/dom.rs @@ -20,17 +20,20 @@ //! describes; here //! that holds for `activeDocument`, `documents` and `selection`. //! -//! VectorCraft's own rules, which the help pages do not state: +//! VectorCraft's own rules: //! - A document's selection lists the selected objects topmost first, as `pageItems` does, whatever -//! order they were selected or made in. +//! order they were selected or made in. The objectReferences page above says instead that the +//! first object is the one last added to the page; VectorCraft does not follow that sentence. //! - A collection read with a name that nothing has throws `Not found`, and `"name" in pageItems` is //! false then. Assigning to such a name throws the same error. //! - Assigning `activeDocument` or `selection` without `app.` assigns `app.activeDocument` or the -//! active document's selection. Assigning `documents` keeps its value. -//! - Collections list documents in tab order. `pageItems` lists every object, with the objects -//! inside groups and compound paths after the group or compound path, and leaves out layers and -//! ruler guides. An index out of range gives `undefined`; `index` and `getByName` throw -//! `Not found` when nothing matches. +//! active document's selection. A top-level `var selection = ...` does the same and does +//! not make a variable of the script. Assigning `documents` keeps its value. +//! - Collections list documents in tab order. The objectReferences page says `documents[0]` is +//! always the active document; VectorCraft does not follow that yet. +//! - `pageItems` lists every object, with the objects inside groups and compound paths after the +//! group or compound path, and leaves out layers and ruler guides. An index out of range gives +//! `undefined`; `index` and `getByName` throw `Not found` when nothing matches. //! - Script coordinates are points with Y up from the bottom of the first artboard //! (`DOCUMENTCOORDINATESYSTEM`, the default) or points with Y down from the top-left corner of //! the active artboard (`ARTBOARDCOORDINATESYSTEM`), as `app.coordinateSystem` says for the run. @@ -1265,7 +1268,14 @@ fn selection_of(h: &Host, uid: u64) -> Answer { } else { let all = page_items(h, st); h.charge_call(all.len() as u64)?; - all.iter().copied().filter(|i| picked.contains(i)).collect() + let mut ids: Vec = all.iter().copied().filter(|i| picked.contains(i)).collect(); + // A selected object that `pageItems` does not list still comes last, in the order it was + // selected. + if ids.len() < picked.len() { + let mut listed: HashSet = ids.iter().copied().collect(); + ids.extend(st.selection.objects.iter().copied().filter(|i| picked.contains(i) && listed.insert(*i))); + } + ids }; h.charge_call(16 * ids.len() as u64)?; Ok(Data::Array(ids.iter().map(|i| item_ref(h, st, *i)).collect())) @@ -1509,8 +1519,8 @@ fn item_set_note(h: &Host, a: &[Data]) -> Answer { Ok(Data::Undefined) } -/// Refuses a change to the item's appearance, place or size while a layer it is in is hidden or -/// locked. Its name and note may still change. +/// Refuses a change to the item's appearance, place, stacking order or size while a layer it is in +/// is hidden or locked. Its name and note may still change. fn refuse_in_closed_layer(h: &Host, item: Item) -> std::result::Result<(), HostError> { let refused = with_doc(h, item.doc, |_, st| { let d = &st.doc; @@ -1555,10 +1565,13 @@ fn item_set_opacity(h: &Host, a: &[Data]) -> Answer { // A value the getter reports for the item's opacity sends the opacity it has, and // `object.setProps` finds the no-op itself: an opacity a graph mark or label already has still // replaces the one the graph keeps for its series or kind. + // The opacity the item already has may be assigned while its layer is hidden or locked. let item = this_item(h, a)?; - refuse_in_closed_layer(h, item)?; let now = with_doc(h, item.doc, |_, st| st.doc.node(item.id).map(|n| f64::from(n.opacity) * 100.0)); - let v = now.filter(|now| (now - v).abs() < 1e-4).unwrap_or(v); + let Some(v) = now.filter(|now| (now - v).abs() < 1e-4) else { + refuse_in_closed_layer(h, item)?; + return set_props(h, a, |_| false, json!({"opacity": v})); + }; set_props(h, a, |_| false, json!({"opacity": v})) } @@ -1923,7 +1936,7 @@ fn item_duplicate(h: &Host, a: &[Data]) -> Answer { } /// `zOrder(method)`: one of the four Object › Arrange commands for the item. A path inside a -/// compound path is refused. +/// compound path is refused, and so is an object in a hidden or locked layer. fn item_z_order(h: &Host, a: &[Data]) -> Answer { let id = match arg(a, 1) { Data::String(s) if s == "ZOrderMethod.BRINGTOFRONT" => "object.arrange.bringToFront", @@ -1936,6 +1949,7 @@ fn item_z_order(h: &Host, a: &[Data]) -> Answer { if in_compound(h, item) { return Err(HostError::Error(COMPOUND_MEMBER_ORDER.into())); } + refuse_in_closed_layer(h, item)?; let (before, after) = with_doc(h, item.doc, |_, st| { let at = st.doc.position(item.id); let count = at.and_then(|(p, ..)| st.doc.children(p).map(Vec::len)).unwrap_or(0); diff --git a/crates/engine/src/tests/script_dom.rs b/crates/engine/src/tests/script_dom.rs index 68a485c8de..e2a61ec18d 100644 --- a/crates/engine/src/tests/script_dom.rs +++ b/crates/engine/src/tests/script_dom.rs @@ -1509,6 +1509,7 @@ fn setters_refuse_art_in_a_hidden_or_locked_layer() { let (mut s, a, b) = doc(); let layer = NodeId(s.execute("layer.new", &json!({})).unwrap()["id"].as_u64().unwrap()); let c = rect(&mut s, 200.0, 200.0, 20.0, 20.0); + rect(&mut s, 300.0, 200.0, 20.0, 20.0); for (id, name) in [(a, "locked"), (b, "free"), (c, "closed")] { s.execute("object.setProps", &json!({"ids": [id.0], "name": name})).unwrap(); } @@ -1517,7 +1518,20 @@ fn setters_refuse_art_in_a_hidden_or_locked_layer() { (json!({"ids": [layer.0], "locked": true}), json!({"ids": [layer.0], "locked": false})), ] { s.execute("object.setProps", &on).unwrap(); - for set in ["opacity = 50", "hidden = true", "locked = true", "left = 5", "top = 5", "position = [5, 5]", "width = 10", "height = 10"] { + // `hidden` and `locked` are refused even when the value is the one the object has. + for set in [ + "opacity = 50", + "hidden = true", + "hidden = false", + "locked = true", + "locked = false", + "left = 5", + "top = 5", + "position = [5, 5]", + "width = 10", + "height = 10", + "zOrder(ZOrderMethod.BRINGTOFRONT)", + ] { let (before, steps) = (format!("{:?}", s.active().unwrap().doc.layers), undo_steps(&s)); // The change made before the refusal is put back with the rest of the run. let e = failure(&mut s, &format!("var p = app.activeDocument.pageItems; p.getByName('free').opacity = 20; p.getByName('closed').{set};")); @@ -1527,10 +1541,10 @@ fn setters_refuse_art_in_a_hidden_or_locked_layer() { } let out = log( &mut s, - "var c = app.activeDocument.pageItems.getByName('closed'); c.note = 'n'; c.name = 'renamed'; vc.log([c.name, c.note]);\ - c.name = 'closed'; c.note = '';", + "var c = app.activeDocument.pageItems.getByName('closed'); c.note = 'n'; c.name = 'renamed'; c.opacity = c.opacity;\ + vc.log([c.name, c.note, c.opacity]); c.name = 'closed'; c.note = '';", ); - assert_eq!(out, vec![json!(["renamed", "n"])], "{on}"); + assert_eq!(out, vec![json!(["renamed", "n", 100])], "{on}"); s.execute("object.setProps", &off).unwrap(); } s.execute("object.setProps", &json!({"ids": [a.0], "locked": true})).unwrap(); @@ -1595,8 +1609,13 @@ fn the_application_members_can_be_named_without_app() { ); assert_eq!(out, vec![json!(0), json!(2), json!([1, true]), json!([1, "object"])]); assert_eq!(s.active().unwrap().selection.objects, vec![b]); + // A top-level `var selection = []` clears the selection; a push goes to a list nobody keeps. + let out = log(&mut s, "var selection = []; selection.push(app.activeDocument.pageItems[0]); vc.log(selection.length);"); + assert_eq!(out, vec![json!(0)]); + assert!(s.active().unwrap().selection.objects.is_empty()); assert!(failure(&mut s, "activeDocument = 5;").contains("not a Document")); - failure(&mut s, "'use strict'; documents = 5;"); + let e = failure(&mut s, "'use strict'; documents = 5;"); + assert!(e.contains("TypeError") && e.contains("cannot assign to property 'documents': it has only a getter"), "{e}"); // With no document open, reading `activeDocument` throws as `app.activeDocument` does. let mut s = Session::new(); assert!(failure(&mut s, "activeDocument;").contains("No active document")); diff --git a/docs/mcp.md b/docs/mcp.md index cef9a0af60..d05579f7e3 100644 --- a/docs/mcp.md +++ b/docs/mcp.md @@ -2625,9 +2625,10 @@ app through the global object `vc`, and writes to the Script Console and runs ot `app.selection` (assigning it sets the active document's selection), `app.coordinateSystem` and `app.getIsFileOpen(path)`. A script may name the application's members without `app.`, as [Accessing and referencing objects](https://ai-scripting.docsforadobe.dev/scriptingJavascript/objectReferences/) - describes; in VectorCraft that holds for `activeDocument`, `documents` and `selection`. Assigning `activeDocument` or `selection` without `app.`, as `var selection = ...` at the top level also - does, assigns the application's member, and assigning `documents` keeps its value (VectorCraft's own rule). - `app.documents` + describes; in VectorCraft that holds for `activeDocument`, `documents` and `selection`. VectorCraft's own rules: + assigning `activeDocument` or `selection` without `app.` assigns the application's member. A top-level + `var selection = ...` does the same and does not make a variable of the script. Assigning `documents` keeps its + value. `app.documents` (https://ai-scripting.docsforadobe.dev/jsobjref/Documents/) has `length`, `[i]`, `index(key)`, `getByName(name)` and `add(colorSpace?, width?, height?, numArtBoards?, artboardLayout?, artboardSpacing?, artboardRowsOrCols?)`, which runs `file.new` (`artboardRowsOrCols` is the number of rows for `GridByCol` and `RLGridByCol`, and of @@ -2670,8 +2671,9 @@ app through the global object `vc`, and writes to the Script Console and runs ot locked layer or group, or on a locked artboard throws `Cannot move` and moves nothing. `duplicate` on such an object, with or without a relative object, throws `Cannot duplicate` and makes no copy. Assigning `hidden`, `locked`, `opacity`, `position`, `left`, `top`, `width` or `height` to an object in a hidden or locked layer - throws `Cannot edit` and changes nothing, while its `name` and `note` still change; an object that is hidden or - locked itself takes them (VectorCraft's own rule). `move` with the object itself as the relative object and + throws `Cannot edit` and changes nothing, even when the value is the one the object has, except for `opacity`, and + `zOrder()` on such an object throws `Cannot edit` too. Its `name` and `note` still change, and an object that is + hidden or locked itself takes all of them (VectorCraft's own rule). `move` with the object itself as the relative object and `PLACEBEFORE` or `PLACEAFTER` leaves it where it is and adds no undo step. When `move` takes the last path out of a compound path, the compound path stays, empty, so a script that holds it can still read it. An object with nothing in it, such as an empty group or that compound path, reads its bounds, `position`, `width` and `height` @@ -2685,7 +2687,8 @@ app through the global object `vc`, and writes to the Script Console and runs ot `pageItems` lists the art topmost first, as the example on [CMYKColor](https://ai-scripting.docsforadobe.dev/jsobjref/CMYKColor/) reads the frontmost path as `pathItems[0]`, and a document's `selection` lists the selected objects in the same order, whatever order they were selected or - made in (VectorCraft's own rule). A collection read with a name, such as `pageItems["Logo"]` or + made in. This is VectorCraft's own rule: the page cited below says the first object in a selection is the one last + added to the page, and VectorCraft does not follow that sentence. A collection read with a name, such as `pageItems["Logo"]` or `documents["Untitled-1"]`, gives the object `getByName` gives, as [Accessing and referencing objects](https://ai-scripting.docsforadobe.dev/scriptingJavascript/objectReferences/) shows. A read with a name that nothing has, a name the collection class lacks as a member included, throws @@ -2693,7 +2696,8 @@ app through the global object `vc`, and writes to the Script Console and runs ot (VectorCraft's own rule). `JSON.stringify` does not read `toJSON` from a collection this way. `name` takes a number or a Boolean as the text `String(value)` gives. `String(object)` gives `[PathItem Logo]` for a page item named `Logo` (`[PathItem ]` with no name), `[Document ]`, `[Layer ]`, `[Application]` and `[PageItems]` (VectorCraft's own rule). - VectorCraft's own rules: documents are listed in tab order, and page items with the + Documents are listed in tab order; the same page says `documents[0]` is always the active document, and + VectorCraft does not follow that yet. VectorCraft's own rules: page items are listed with the objects inside groups and compound paths after their group and without layers and ruler guides. A document's `selection`, and `app.selection` while a document is open, is an empty list when nothing is selected. Script coordinates are points with Y up from the bottom-left corner of the first artboard, or, with @@ -2721,8 +2725,9 @@ app through the global object `vc`, and writes to the Script Console and runs ot `hasOwnProperty` and `propertyIsEnumerable` are false for those members and true for every other member of an object's class, so `item.hasOwnProperty("opacity")` is true on a page item, and `for (k in item)` lists the other properties but not the methods. `Object.keys`, `Object.getOwnPropertyNames` and - `Object.getOwnPropertyDescriptor` do not report those members. Assigning a member that an object's class does not - have does not keep the value and does not throw. Other members + `Object.getOwnPropertyDescriptor` do not report those members. Assigning a member that a page item, a layer, a + document or the application does not have does not keep the value and does not throw; on a collection it throws `Not found`, + as described above. Other members those pages list, such as `app.version` and `Document.save()`, and the members of the pages of the page-item kinds themselves, such as a path's fill, are not defined yet and read as `undefined`. - `$.write(...values)` writes the values to the Script Console with no separator, each as `String(value)` gives it, and