diff --git a/crates/engine/src/cmd/paint.rs b/crates/engine/src/cmd/paint.rs index 4b20947a86..f5c4983b62 100644 --- a/crates/engine/src/cmd/paint.rs +++ b/crates/engine/src/cmd/paint.rs @@ -636,6 +636,10 @@ fn apply_paint_opacity(s: &mut Session, p: &Value, paint: Option, opacity let cmd = if fill { "paint.setFill" } else { "paint.setStroke" }; let bucket = bucket_color_params(s, p, cmd)?; let p = bucket.as_ref().unwrap_or(p); + // A bad `target` or `aligned` is refused for every kind of target: `item_target` reads no + // `target` once `id` or `ids` names an object, and selected characters read neither. + super::appearance::aligned(p, cmd)?; + super::appearance::targets_contents(p)?; let focus = switch_or(p, "focus", true, cmd)?; // An opacity alone places no gradient, so `span` may only be "object" with it. let span = spans_targets(p, paint.as_ref().unwrap_or(&Paint::None)).map_err(|e| bad(cmd, e))?; diff --git a/crates/engine/src/tests/paintopacity.rs b/crates/engine/src/tests/paintopacity.rs index 53d72b0e5d..5b980f9803 100644 --- a/crates/engine/src/tests/paintopacity.rs +++ b/crates/engine/src/tests/paintopacity.rs @@ -427,6 +427,39 @@ fn an_object_in_a_symbol_instance_refuses_a_bad_target() { assert_ne!(*s.doc().unwrap().doc, *doc); } +/// A bad `target` is refused when `id` or `ids` names an object, and a bad `target` or `aligned` +/// while the Type tool has characters selected; each leaves the document alone. A good `target` +/// with an `id` still paints the object. +#[test] +fn a_bad_target_is_refused_with_an_id_or_selected_characters() { + let mut s = session(); + let r = rect(&mut s, 10.0); + select(&mut s, &[r]); + let doc = s.doc().unwrap().doc.clone(); + for cmd in ["paint.setFill", "paint.setStroke"] { + for p in [ + json!({"color": "#ff0000", "target": "content", "id": r.0}), + json!({"color": "#ff0000", "target": "content", "ids": [r.0]}), + json!({"opacity": 40, "target": "content", "id": r.0}), + json!({"opacity": 40, "target": "content", "ids": [r.0]}), + ] { + let e = s.execute(cmd, &p).unwrap_err(); + assert!(e.to_string().contains("`target` must be"), "{cmd} {p}: {e}"); + assert_eq!(*s.doc().unwrap().doc, *doc, "{cmd} {p}"); + } + } + assert_eq!(s.execute("paint.setFill", &json!({"color": "#ff0000", "target": "object", "id": r.0})).unwrap()["objects"], 1); + assert_ne!(*s.doc().unwrap().doc, *doc); + let t = text(&mut s); + select(&mut s, &[t]); + select_characters(&mut s, t); + let doc = s.doc().unwrap().doc.clone(); + for p in [json!({"color": "#ff0000", "target": "content"}), json!({"color": "#ff0000", "aligned": "yes"})] { + assert!(s.execute("paint.setFill", &p).is_err(), "{p}"); + assert_eq!(*s.doc().unwrap().doc, *doc, "{p}"); + } +} + /// A gradient mesh point picked with the Mesh tool takes a fill opacity itself, and the proxy shows /// it; the object's fill item and the other points stay as they were. #[test]