diff --git a/crates/engine/src/cmd/links.rs b/crates/engine/src/cmd/links.rs index 4d6c2c8b39..ff420c436e 100644 --- a/crates/engine/src/cmd/links.rs +++ b/crates/engine/src/cmd/links.rs @@ -165,6 +165,9 @@ struct Group { document: Option, /// An image placed from a DCS file: the plate files of their own its plates were read from. plate_files: Vec, + /// The stored relative paths of the group's objects other than `link`'s, each once: objects + /// with the same path and content can each keep a relative path of its own. + other_relatives: Vec, } /// What a placed document shows: whether the art's bounds (not the artboard or page) are shown, @@ -198,17 +201,22 @@ pub(crate) fn linked_groups(d: &Document) -> Vec<(String, Vec)> { /// from, wherever it is stored, brush art included, whether or not a file is there: each place /// [`locate`] looks at, and beside each one the plate files of an image placed from a DCS file, /// as its plate-change check finds them. A link's absolute stored and relative paths are listed -/// as they are too. +/// as they are too, for the relative path of every object in a group. pub(crate) fn link_read_places(d: &Document, base: LinkBase) -> Vec { let mut out = vec![]; for g in groups(d, None) { - let link = &g.link; - let mut mains: Vec = - std::iter::once(link.path.as_str()).chain(link.relative.as_deref()).filter(|p| Path::new(p).is_absolute()).map(str::to_string).collect(); - mains.extend(places(link, base)); - for main in mains { - out.extend(g.plate_files.iter().filter_map(|f| super::place::dcs::plate_file_path(&main, &f.name))); - out.push(main); + let others = g.other_relatives.iter().map(|r| LinkInfo { relative: Some(r.clone()), ..g.link.clone() }); + for link in std::iter::once(g.link.clone()).chain(others) { + let mut mains: Vec = std::iter::once(link.path.as_str()) + .chain(link.relative.as_deref()) + .filter(|p| Path::new(p).is_absolute()) + .map(str::to_string) + .collect(); + mains.extend(places(&link, base)); + for main in mains { + out.extend(g.plate_files.iter().filter_map(|f| super::place::dcs::plate_file_path(&main, &f.name))); + out.push(main); + } } } out @@ -231,10 +239,17 @@ fn groups(d: &Document, ids: Option<&[NodeId]>) -> Vec { None => d.images.get(key).map(ImageBlob::plate_files).unwrap_or_default(), Some(_) => vec![], }; - out.push(Group { link: link.clone(), key: key.to_string(), ids: vec![], document, plate_files }); + out.push(Group { link: link.clone(), key: key.to_string(), ids: vec![], document, plate_files, other_relatives: vec![] }); out.len() - 1 }); - if let (Some(id), Some(g)) = (id, out.get_mut(i)) { + let Some(g) = out.get_mut(i) else { return }; + if let Some(r) = &link.relative + && g.link.relative.as_ref() != Some(r) + && !g.other_relatives.contains(r) + { + g.other_relatives.push(r.clone()); + } + if let Some(id) = id { g.ids.push(id); } }; diff --git a/crates/engine/src/script/dom.rs b/crates/engine/src/script/dom.rs index d651d82867..871cd94370 100644 --- a/crates/engine/src/script/dom.rs +++ b/crates/engine/src/script/dom.rs @@ -967,7 +967,12 @@ fn new_preset(h: &Host) -> Answer { item("numArtboards", num(1.0)), item("width", num(612.0)), ]; - h.dom.preset_bytes.set(h.dom.preset_bytes.get().saturating_add(fields.iter().map(|(_, v)| data_size(v)).sum::())); + // The defaults count toward MAX_PRESET_BYTES as the setter counts a value. + let kept = h.dom.preset_bytes.get().saturating_add(fields.iter().map(|(_, v)| data_size(v)).sum::()); + if kept > MAX_PRESET_BYTES { + return Err(HostError::Range(format!("the DocumentPresets of a script can hold at most {} MB of settings", MAX_PRESET_BYTES >> 20))); + } + h.dom.preset_bytes.set(kept); presets.push(fields); Ok(host_ref("DocumentPreset", &[float(h.run), float(presets.len() as u64 - 1)])) } diff --git a/crates/engine/src/tests/script_dom.rs b/crates/engine/src/tests/script_dom.rs index c64aee6ec2..bc8f71199a 100644 --- a/crates/engine/src/tests/script_dom.rs +++ b/crates/engine/src/tests/script_dom.rs @@ -873,6 +873,23 @@ fn document_preset_settings_are_limited_in_bytes() { assert_eq!(out[3], json!(1_572_864), "replacing a setting frees what it held"); } +/// A new `DocumentPreset`'s default settings count toward the 4 MB, so one that would pass the +/// limit is refused and leaves the count as it was. +#[test] +fn a_new_document_preset_is_refused_past_the_byte_limit() { + let mut s = Session::new(); + // The six defaults hold 74 bytes; the two strings leave 10 bytes of the 4 MB free. + let out = log( + &mut s, + "var p = new DocumentPreset(); p.title = new Array(2097153).join('a'); p.units = new Array(2097065).join('b');\ + try { new DocumentPreset(); vc.log('made'); } catch (e) { vc.log([e.name, e.message]); }\ + p.colorMode = 1; vc.log(p.colorMode); p.title = 'x'; vc.log(new DocumentPreset().width);", + ); + assert_eq!(out[0], json!(["RangeError", "the DocumentPresets of a script can hold at most 4 MB of settings"])); + assert_eq!(out[1], json!(1), "a refused preset adds nothing to the count"); + assert_eq!(out[2], json!(612), "a preset below the limit is still made"); +} + #[test] fn headless_add_document_dialog_fails_without_creating() { let mut s = Session::new(); diff --git a/crates/engine/src/tests/script_raster_capture.rs b/crates/engine/src/tests/script_raster_capture.rs index 610882b419..863b5d1db6 100644 --- a/crates/engine/src/tests/script_raster_capture.rs +++ b/crates/engine/src/tests/script_raster_capture.rs @@ -653,6 +653,26 @@ fn capture_to_a_templates_relative_link_writes_nothing() { let _ = std::fs::remove_dir_all(base); } +/// Two images that share a stale path and a content but keep different relative paths are each +/// looked for at their own relative path, so a capture over either one's file is refused. +#[test] +fn capture_over_either_relative_path_of_one_linked_content_writes_nothing() { + let base = folder("two-relatives"); + let (mut s, dir) = capture_doc(&base); + s.active_mut().unwrap().path = Some(dir.join("doc.vectorcraft").to_string_lossy().into_owned()); + let stale = base.join("outside/gone/pic.png").to_string_lossy().into_owned(); + for relative in ["a.png", "b.png"] { + place_linked(&mut s, "pic", vectorcraft_doc::LinkInfo { path: stale.clone(), relative: Some(relative.into()), ..Default::default() }); + } + for name in ["a.png", "b.png"] { + let target = dir.join(name); + let e = file_failure(&mut s, &base, &format!("app.activeDocument.imageCapture(new File({}), [0, 300, 50, 250]);", quoted(&target))); + assert!(e.contains("cannot write over an open document"), "{name}: {e}"); + assert!(!target.exists(), "{name}: nothing written"); + } + let _ = std::fs::remove_dir_all(base); +} + /// A DCS image whose stored path is stale reads its plate files beside the main file its relative /// path finds, so a capture over one of those is refused. #[test]