Merge pull request #10329 from dotCooCoo/fix/script-app-members-checks

Scripts: getIsFileOpen keeps backslashes in Unix file names; DocumentPreset checks its receiver
This commit is contained in:
.CooCoo authored and GitHub committed 2026-11-30 16:42:58 -08:00
commit 251d06b8b3
4 files changed
+81 -11

No files matched your search

+29 -8
View File
@@ -1013,13 +1013,13 @@ fn app_set_coords(h: &Host, a: &[Data]) -> Answer {
Ok(Data::Undefined)
}
/// A path as `getIsFileOpen` compares it: `\` and `/` alike, and on Windows paths (a drive letter
/// or a `\`) letters in one case.
/// A path as `getIsFileOpen` compares it. A Windows path (any path on Windows, or one that starts
/// with a drive letter) takes `\` and `/` alike and letters in one case. On other systems `\` is a
/// character of a file name, so `/work/a\b.ai` and `/work/a/b.ai` are different files.
fn path_key(p: &str) -> String {
let windows = p.contains('\\') || p.as_bytes().get(1) == Some(&b':');
let p = p.replace('\\', "/");
let p = p.trim_end_matches('/').to_string();
if windows { p.to_lowercase() } else { p }
let b = p.as_bytes();
let windows = cfg!(windows) || (b.len() >= 2 && b[0].is_ascii_alphabetic() && b[1] == b':');
if windows { p.replace('\\', "/").trim_end_matches('/').to_lowercase() } else { p.trim_end_matches('/').to_string() }
}
/// `app.redraw()`: returns without changing anything. The app draws a frame between the run's
@@ -1503,6 +1503,13 @@ fn docs_add_document(h: &Host, a: &[Data]) -> Answer {
})
}
/// `documents.addDocumentNoUI(startupPreset)`: a new document from the New Document profile
/// `startupPreset` names, as `addDocument(startupPreset)` makes it, without the New Document
/// dialog. Arguments after `startupPreset` are ignored.
fn docs_add_document_no_ui(h: &Host, a: &[Data]) -> Answer {
docs_add_document(h, &a[..a.len().min(2)])
}
/// `new DocumentPreset()`: a record of new-document settings with the defaults the help page
/// <https://ai-scripting.docsforadobe.dev/jsobjref/DocumentPreset/> gives; the other settings are
/// left out. The settings stay with the run, which hands the script an object of the
@@ -1581,6 +1588,19 @@ fn preset_of(h: &Host, d: &Data) -> Option<usize> {
(i < h.dom.presets.borrow().len()).then_some(i)
}
/// `DocumentPreset.typename`: a `TypeError` for a preset from an earlier run, as its settings give.
fn preset_typename(h: &Host, a: &[Data]) -> Answer {
preset_of(h, arg(a, 0)).ok_or_else(|| HostError::Type("this is not a DocumentPreset".into()))?;
Ok(text("DocumentPreset"))
}
/// `DocumentPreset.toString()`: `[DocumentPreset]`, or a `TypeError` for a preset from an earlier
/// run.
fn preset_to_string(h: &Host, a: &[Data]) -> Answer {
preset_of(h, arg(a, 0)).ok_or_else(|| HostError::Type("this is not a DocumentPreset".into()))?;
Ok(text("[DocumentPreset]"))
}
/// The enumeration whose values the `DocumentPreset` setting `key` takes, when it takes one.
fn preset_enum(key: &str) -> Option<&'static str> {
Some(match key {
@@ -2949,8 +2969,8 @@ fn classes(link: &Link) -> Vec<HostClass> {
new_preset(&h)
});
let mut members = vec![
("typename".to_string(), typename_of("DocumentPreset"), typed("string")),
("toString".to_string(), class_text("DocumentPreset"), returns("string", &[])),
("typename".to_string(), get(link, preset_typename), typed("string")),
("toString".to_string(), method(link, preset_to_string), returns("string", &[])),
];
members.extend(PRESET_FIELDS.iter().map(|k| (k.to_string(), preset_field(link, k), typed(preset_type(k)))));
HostClass { construct: Some(f), ..class("DocumentPreset", None, members, None) }
@@ -2988,6 +3008,7 @@ fn classes(link: &Link) -> Vec<HostClass> {
method(l, docs_add_document),
returns("Document", &[("startupPreset", "string"), ("presetSettings", "DocumentPreset"), ("showOptionsDialog", "boolean")]),
),
("addDocumentNoUI", method(l, docs_add_document_no_ui), returns("Document", &[("startupPreset", "string")])),
("toString", class_text("Documents"), returns("string", &[])),
]),
Some(function(l, docs_at)),
+44
View File
@@ -1382,6 +1382,18 @@ fn a_document_preset_is_an_instance_of_its_class() {
assert_eq!(out, vec![json!(true), json!("DocumentPreset"), json!(612)]);
}
/// A preset kept from an earlier run throws from `typename` and `toString()` as it does from its
/// settings.
#[test]
fn a_document_preset_from_an_earlier_run_throws_from_typename_and_to_string() {
let mut s = Session::new();
run(&mut s, "#targetengine keep\nvar p = new DocumentPreset();").unwrap();
for member in ["p.width", "p.typename", "p.toString()", "String(p)"] {
let e = failure(&mut s, &format!("#targetengine keep\n{member};"));
assert!(e.contains("TypeError") && e.contains("not a DocumentPreset"), "{member}: {e}");
}
}
/// The values the presets hold count against the run's heap, a value assigned again in place of
/// the one before it.
#[test]
@@ -1559,6 +1571,23 @@ fn headless_add_document_dialog_fails_without_creating() {
assert_eq!(s.documents().len(), 1);
}
/// `addDocumentNoUI(startupPreset)` makes the document `addDocument(startupPreset)` makes, and
/// ignores the arguments after the preset's name.
#[test]
fn add_document_no_ui_makes_the_startup_presets_document() {
let mut s = Session::new();
let out = log(
&mut s,
"var a = app.documents.addDocument('Web'); var p = new DocumentPreset(); p.width = 333;\
var b = app.documents.addDocumentNoUI('Web', p, true);\
vc.log([b.typename, b.width === a.width, b.height === a.height, b.width === 333, app.documents.length]);",
);
assert_eq!(out, vec![json!(["Document", true, true, false, 2])]);
let e = failure(&mut s, "app.documents.addDocumentNoUI('No Such Preset');");
assert!(e.contains("no startup preset called No Such Preset"), "{e}");
assert_eq!(s.documents().len(), 2);
}
#[test]
fn setting_selected_keeps_how_the_other_objects_are_selected() {
let (mut s, a, b) = doc();
@@ -2046,6 +2075,21 @@ fn get_is_file_open_cannot_probe_outside_script_scope() {
assert!(failure(&mut s, "app.getIsFileOpen(new Array(5000).join('a'));").contains("too long"));
}
/// Outside Windows a `\` is part of a file name: `<dir>/a\b.svg` is not the open `<dir>/a/b.svg`.
#[test]
fn get_is_file_open_keeps_a_backslash_in_a_unix_file_name() {
let (mut s, _, _) = doc();
let fixture = vectorcraft_testkit::TempDir::new("script-file-open-backslash");
let dir = fixture.join("a");
std::fs::create_dir_all(&dir).unwrap();
let path = dir.join("b.svg");
s.execute("document.save", &json!({"path": path.to_string_lossy()})).unwrap();
let same = serde_json::to_string(&path.to_string_lossy()).unwrap();
let backslash = serde_json::to_string(&format!("{}\\b.svg", dir.to_string_lossy())).unwrap();
let out = log(&mut s, &format!("vc.log(app.getIsFileOpen({same})); vc.log(app.getIsFileOpen({backslash}));"));
assert_eq!(out, vec![json!(true), json!(cfg!(windows))]);
}
#[test]
fn a_menu_command_runs_its_command_on_the_selection() {
let (mut s, a, b) = doc();
@@ -108,7 +108,7 @@ const INSIDE: &[(&str, &str)] = &[
toString transparencyGrid transparencyGrid= typename units units= width width=",
),
("Documents.", "prototype"),
("Documents.prototype.", "add addDocument constructor getByName index length parent toString typename"),
("Documents.prototype.", "add addDocument addDocumentNoUI constructor getByName index length parent toString typename"),
("Document.", "prototype"),
(
"Document.prototype.",
+7 -2
View File
@@ -2992,7 +2992,10 @@ through `$`:
profile and the reason, and makes no document. Each warning from reading the profile's file (a color profile in it
that cannot be used, for example) goes to the Script Console as a line `addDocument: <warning>`. A `previewMode` of
`DocumentPreviewMode.PixelPreview` throws an error that says it is not available yet, and no document is made.
`showOptionsDialog` true throws `Dialog unavailable` with no document made. A `Document` (https://ai-scripting.docsforadobe.dev/jsobjref/Document/) has `name`,
`showOptionsDialog` true throws `Dialog unavailable` with no document made. `addDocumentNoUI(startupPreset)`
makes the document `addDocument(startupPreset)` makes and ignores any further arguments. A `DocumentPreset` kept
from an earlier run (with `#targetengine`) throws a `TypeError` when its settings are read or assigned and when its `typename`
or `toString()` is read. A `Document` (https://ai-scripting.docsforadobe.dev/jsobjref/Document/) has `name`,
`width`, `height`, `rulerUnits`, `documentColorSpace`, `rulerOrigin`, `convertCoordinate(point, source,
destination)`, `pageItems`, `selection` and `views`. `views` (https://ai-scripting.docsforadobe.dev/jsobjref/Views/)
lists each window, tab, or tiled pane the desktop app has drawn for the document, in view-number order. Each view
@@ -3096,7 +3099,9 @@ through `$`:
nothing changed. While View › Guides › Lock Guides is on, a path made into a guide cannot be selected this way or
with `selected`. Assigning a page item's `selected` leaves the direct-selected anchors and segments of the other
selected objects selected. `getIsFileOpen` compares the path with the open documents' files as text (in a Windows path,
letter case is ignored and `\` matches `/`) and does not resolve the path it is given. A document counts only when
letter case is ignored and `\` matches `/`) and does not resolve the path it is given. On Windows every path is a
Windows path; on macOS and Linux only a path that starts with a drive letter is, and in the others `\` is a
character of a file name. A document counts only when
the automation read roots allow its file, and that check can look the document's file up on disk. A path with
`..`, a network path, a NUL character or more than 4,096 bytes throws an error. `app.redraw()` returns `undefined`
and does not change the documents or add an undo step. In the app, the documents are drawn as they were before the