diff --git a/crates/engine/src/script/dom/paths.rs b/crates/engine/src/script/dom/paths.rs index 4673e6277e..74d08e2972 100644 --- a/crates/engine/src/script/dom/paths.rs +++ b/crates/engine/src/script/dom/paths.rs @@ -760,6 +760,10 @@ fn path_closed(h: &super::Host, a: &[Data]) -> Answer { fn path_set_closed(h: &super::Host, a: &[Data]) -> Answer { let closed = boolean(arg(a, 1), "closed")?; let item = this_path(h, a)?; + // A path with no points has no subpath to keep the value in. + if closed && read_points(h, item, |subs| subs.is_empty())? { + return Err(HostError::Error("Cannot close a path that has no points".into())); + } edit_points_keeping(h, item, true, |subs| subs.iter_mut().for_each(|sp| sp.closed = closed))?; Ok(Data::Undefined) } diff --git a/crates/engine/src/tests/script_dom_paths.rs b/crates/engine/src/tests/script_dom_paths.rs index 7df8022c5c..3be3901e7a 100644 --- a/crates/engine/src/tests/script_dom_paths.rs +++ b/crates/engine/src/tests/script_dom_paths.rs @@ -193,6 +193,15 @@ fn opening_a_path_deselects_its_closing_segment() { assert!(sel.objects.contains(&a)); } +#[test] +fn closing_a_path_without_points_throws() { + let (mut s, _, _) = doc(); + let e = failure(&mut s, "var p = app.activeDocument.pathItems.add(); p.closed = true;"); + assert!(e.contains("Cannot close a path that has no points"), "{e}"); + // Opening it stores nothing and is allowed. + log(&mut s, "var p = app.activeDocument.pathItems.add(); p.closed = false;"); +} + #[test] fn set_anchors_with_other_anchor_counts_deselects_the_paths_anchors() { let (mut s, a, b) = doc(); diff --git a/docs/mcp.md b/docs/mcp.md index 8280d0f07a..8e3dd85484 100644 --- a/docs/mcp.md +++ b/docs/mcp.md @@ -3001,6 +3001,7 @@ other files through `$`: `setEntirePath`, `pathPoints` and each `PathPoint`) are written together with one `path.setAnchors` before the script uses any other member or command, or the points of another path, and when the script ends; a path aligned to the pixel grid, or in an object that is, is written at each change, so its points read as the grid snaps them. + Setting `closed` to true on a path with no points throws `Cannot close a path that has no points`. Writing changes that added, removed or replaced points deselects the path's direct-selected anchors, even when the path ends with as many points as before; changes that only move points or handles, change a point's type or set `closed` keep them selected.