Scripts: list the object model in the host name check, refuse zOrder inside a compound path

The host name test now has rows for app, the document model classes and the enumerations the script host adds. zOrder on a path inside a compound path throws an error and leaves the document unchanged, as duplicate does.
This commit is contained in:
dotCooCoo
2026-11-21 10:34:09 -08:00
parent 6cce980e2b
commit 626d843520
4 changed files with 111 additions and 8 deletions
+14 -5
View File
@@ -1563,16 +1563,21 @@ fn item_move(h: &Host, a: &[Data]) -> Answer {
/// The error of `duplicate` on a path inside a compound path.
pub const COMPOUND_MEMBER: &str = "Duplicating a path inside a compound path is not available yet";
/// The error of `zOrder` on a path inside a compound path.
pub const COMPOUND_MEMBER_ORDER: &str = "Reordering a path inside a compound path is not available yet";
/// Whether `item` is a path inside a compound path.
fn in_compound(h: &Host, item: Item) -> bool {
with_doc(h, item.doc, |_, st| st.doc.parent_of(item.id).and_then(|p| st.doc.node(p)).is_some_and(|p| matches!(p.kind, NodeKind::Compound { .. })))
}
/// `duplicate(relativeObject?, insertionLocation?)`: a copy of the item, directly above it as
/// Edit › Duplicate makes one, or into or beside `relativeObject` as `move` puts it. → the copy.
/// When `node.move` refuses the place, the copy and every change it made are put back, and the
/// error is thrown.
fn item_duplicate(h: &Host, a: &[Data]) -> Answer {
let item = this_item(h, a)?;
let in_compound = with_doc(h, item.doc, |_, st| {
st.doc.parent_of(item.id).and_then(|p| st.doc.node(p)).is_some_and(|p| matches!(p.kind, NodeKind::Compound { .. }))
});
if in_compound {
if in_compound(h, item) {
return Err(HostError::Error(COMPOUND_MEMBER.into()));
}
let to = match arg(a, 1) {
@@ -1597,7 +1602,8 @@ fn item_duplicate(h: &Host, a: &[Data]) -> Answer {
Ok(item_ref(h, st, NodeId(id)))
}
/// `zOrder(method)`: one of the four Object › Arrange commands for the item.
/// `zOrder(method)`: one of the four Object › Arrange commands for the item. A path inside a
/// compound path is refused.
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",
@@ -1607,6 +1613,9 @@ fn item_z_order(h: &Host, a: &[Data]) -> Answer {
_ => return Err(HostError::Type("zOrder needs a ZOrderMethod value".into())),
};
let item = this_item(h, a)?;
if in_compound(h, item) {
return Err(HostError::Error(COMPOUND_MEMBER_ORDER.into()));
}
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);
+36
View File
@@ -358,6 +358,42 @@ fn a_path_inside_a_compound_path_is_not_duplicated() {
assert_eq!(out, vec![json!(["CompoundPathItem", 6])]);
}
#[test]
fn a_path_inside_a_compound_path_is_not_reordered() {
let (mut s, a, b) = doc();
s.execute("select.set", &json!({"ids": [a.0, b.0]})).unwrap();
s.execute("object.compoundPath.make", &json!({})).unwrap();
rect(&mut s, 200.0, 200.0, 20.0, 20.0);
let before = format!("{:?}", s.active().unwrap().doc.layers);
let steps = undo_steps(&s);
let out = log(
&mut s,
"var p = app.activeDocument.pageItems, m = [], out = [];\
for (var i = 0; i < p.length; i++) if (p[i].parent.typename == 'CompoundPathItem') m.push(p[i]);\
var ways = [ZOrderMethod.BRINGTOFRONT, ZOrderMethod.BRINGFORWARD, ZOrderMethod.SENDBACKWARD, ZOrderMethod.SENDTOBACK];\
for (var j = 0; j < m.length; j++) for (var k = 0; k < ways.length; k++) {\
try { m[j].zOrder(ways[k]); out.push('ran'); } catch (e) { out.push(e.message); }\
}\
vc.log(m.length); vc.log(out);",
);
assert_eq!(out[0], json!(2));
let messages = out[1].as_array().cloned().unwrap_or_default();
assert_eq!(messages.len(), 8, "{out:?}");
for m in &messages {
assert!(m.as_str().is_some_and(|m| m.contains("Reordering a path inside a compound path is not available yet")), "{out:?}");
}
assert_eq!(format!("{:?}", s.active().unwrap().doc.layers), before);
assert_eq!(undo_steps(&s), steps);
// The compound path itself still moves.
let out = log(
&mut s,
"var p = app.activeDocument.pageItems, c;\
for (var i = 0; i < p.length; i++) if (p[i].typename == 'CompoundPathItem') c = p[i];\
var at = c.zOrderPosition; c.zOrder(ZOrderMethod.BRINGTOFRONT); vc.log([at, c.zOrderPosition]);",
);
assert_eq!(out, vec![json!([1, 2])]);
}
#[test]
fn a_copy_of_an_object_with_a_large_id_is_the_copy() {
let mut s = Session::new();
@@ -41,7 +41,8 @@ const OUTSIDE: &[(&str, &str, &str)] = &[
/// The names the host adds that stay inside the document and the run, by the object that has them.
/// Reading a file or a folder, a file dialog and a message to the person do not change anything
/// outside it.
/// outside it. The document model (`app`, its classes and the enumerations) acts on the open
/// documents; `Document.saveAs` and `close` throw "not available yet".
const INSIDE: &[(&str, &str)] = &[
("", "$ File Folder alert confirm localize prompt toSource"),
("Object.prototype.", "toSource"),
@@ -70,8 +71,63 @@ const INSIDE: &[(&str, &str)] = &[
"absoluteURI alias changePath constructor created displayName error error= exists fsName fullName getFiles \
getRelativeURI modified name parent path relativeURI resolve selectDlg toString",
),
(
"",
"app Application CoordinateSystem Document DocumentArtboardLayout DocumentColorSpace DocumentPresetType \
DocumentPreviewMode DocumentRasterResolution DocumentTransparencyGrid Documents ElementPlacement Layer PageItem \
PageItems RulerUnits ZOrderMethod",
),
("Application.", "prototype"),
(
"Application.prototype.",
"activeDocument activeDocument= beep beep= constructor coordinateSystem coordinateSystem= documents getIsFileOpen \
selection typename",
),
("Documents.", "prototype"),
("Documents.prototype.", "add addDocument constructor getByName index length parent typename"),
("Document.", "prototype"),
(
"Document.prototype.",
"activeLayer activeLayer= artboards artboards= close close= constructor convertCoordinate documentColorSpace \
groupItems groupItems= height layers layers= name pageItems parent pathItems pathItems= rulerOrigin rulerOrigin= \
rulerUnits saveAs saveAs= selection selection= swatches swatches= symbols symbols= textFrames textFrames= typename \
views views= width",
),
("Layer.", "prototype"),
("Layer.prototype.", "constructor name typename"),
("PageItems.", "prototype"),
("PageItems.prototype.", "constructor getByName index length parent removeAll typename"),
("PageItem.", "prototype"),
(
"PageItem.prototype.",
"artworkKnockout artworkKnockout= blendingMode blendingMode= bringInPerspective bringInPerspective= constructor \
controlBounds duplicate editable geometricBounds height height= hidden hidden= isIsolated isIsolated= layer layer= \
left left= locked locked= move name name= note note= opacity opacity= parent pixelAligned pixelAligned= position \
position= remove resize resize= rotate rotate= selected selected= sliced sliced= tags tags= top top= transform \
transform= translate translate= typename uRL uRL= uuid uuid= visibilityVariable visibilityVariable= visibleBounds \
width width= wrapInside wrapInside= wrapOffset wrapOffset= wrapped wrapped= zOrder zOrderPosition",
),
("CoordinateSystem.", "ARTBOARDCOORDINATESYSTEM DOCUMENTCOORDINATESYSTEM"),
("DocumentArtboardLayout.", "Column GridByCol GridByRow RLGridByCol RLGridByRow RLRow Row"),
("DocumentColorSpace.", "CMYK RGB"),
("DocumentPresetType.", "BasicCMYK BasicRGB Mobile Print Video Web"),
("DocumentPreviewMode.", "DefaultPreview OverprintPreview PixelPreview"),
("DocumentRasterResolution.", "HighResolution MediumResolution ScreenResolution"),
(
"DocumentTransparencyGrid.",
"TransparencyGridBlue TransparencyGridDark TransparencyGridGreen TransparencyGridLight TransparencyGridMedium \
TransparencyGridNone TransparencyGridOrange TransparencyGridPurple TransparencyGridRed",
),
("ElementPlacement.", "INSIDE PLACEAFTER PLACEATBEGINNING PLACEATEND PLACEBEFORE"),
("RulerUnits.", "Centimeters Inches Millimeters Picas Pixels Points Qs Unknown"),
("ZOrderMethod.", "BRINGFORWARD BRINGTOFRONT SENDBACKWARD SENDTOBACK"),
];
/// The classes the host adds with nothing but a prototype and its constructor: `DocumentPreset`
/// and the page item types.
const BARE_CLASSES: &str = "BlendItem CompoundPathItem DocumentPreset EnvelopeItem GraphItem GroupItem IntertwineItem MeshItem PathItem \
PlacedDocumentItem PlacedItem RasterItem RepeatItem SymbolItem TextFrame";
/// The name the checker does not read, which also stands for its members: `vc` is VectorCraft's
/// own API, and what `vc.run` does depends on the command it runs.
const NOT_CHECKED: &str = "vc";
@@ -118,7 +174,8 @@ fn language_names() -> BTreeSet<String> {
fn table_names() -> BTreeSet<String> {
let inside = INSIDE.iter().flat_map(|(prefix, names)| names.split_whitespace().map(move |n| format!("{prefix}{n}")));
OUTSIDE.iter().map(|(n, ..)| n.to_string()).chain(inside).chain([NOT_CHECKED.to_string()]).collect()
let bare = BARE_CLASSES.split_whitespace().flat_map(|c| [c.to_string(), format!("{c}.prototype"), format!("{c}.prototype.constructor")]);
OUTSIDE.iter().map(|(n, ..)| n.to_string()).chain(inside).chain(bare).chain([NOT_CHECKED.to_string()]).collect()
}
#[test]
+2 -1
View File
@@ -2651,7 +2651,8 @@ app through the global object `vc`, and writes to the Script Console and runs ot
directly below it. An object of another document throws `Cross-document placement is not available`, and when
`node.move` refuses the place of a copy, `duplicate` throws that error and leaves the documents, the selection and
the undo history as they were before the call. `duplicate` on a path inside a compound path throws
`Duplicating a path inside a compound path is not available yet`. A script that holds a placed image keeps the
`Duplicating a path inside a compound path is not available yet`, and `zOrder` on one throws
`Reordering a path inside a compound path is not available yet` and leaves the document unchanged. A script that holds a placed image keeps the
same object when the image is embedded: the next read gives that object, now a `RasterItem`. The
enumerations `ElementPlacement`, `ZOrderMethod`, `RulerUnits`, `DocumentColorSpace`, `CoordinateSystem`,
`DocumentArtboardLayout`, `DocumentPresetType`, `DocumentPreviewMode`, `DocumentRasterResolution` and