From 42e5553c867675089462504a8a15cb722a5e54b4 Mon Sep 17 00:00:00 2001 From: dotCooCoo Date: Mon, 16 Nov 2026 02:11:06 -0800 Subject: [PATCH] Color: an opacity alone refuses a span other than "object" paint.setFill and paint.setStroke given only `opacity` now check `span` as they do with a paint: "selection" asks for a gradient, and an unknown value is an error, instead of the opacity going through unchecked. --- crates/engine/src/cmd/paint.rs | 6 ++---- crates/engine/src/tests/paintopacity.rs | 9 +++++++++ 2 files changed, 11 insertions(+), 4 deletions(-) diff --git a/crates/engine/src/cmd/paint.rs b/crates/engine/src/cmd/paint.rs index ef17c5c960..2a2cab339d 100644 --- a/crates/engine/src/cmd/paint.rs +++ b/crates/engine/src/cmd/paint.rs @@ -560,10 +560,8 @@ pub(crate) fn apply_paint(s: &mut Session, p: &Value, paint: Paint, fill: bool) fn apply_paint_opacity(s: &mut Session, p: &Value, paint: Option, opacity: Option, fill: bool) -> Result { let cmd = if fill { "paint.setFill" } else { "paint.setStroke" }; let focus = switch_or(p, "focus", true, cmd)?; - let span = match &paint { - Some(paint) => spans_targets(p, paint).map_err(|e| bad(cmd, e))?, - None => false, - }; + // 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))?; // Characters selected with the Type tool take the paint, not the whole type object (#1063). if p.get("ids").is_none() && p.get("id").is_none() diff --git a/crates/engine/src/tests/paintopacity.rs b/crates/engine/src/tests/paintopacity.rs index a1e2018e83..e733bbab1c 100644 --- a/crates/engine/src/tests/paintopacity.rs +++ b/crates/engine/src/tests/paintopacity.rs @@ -156,6 +156,15 @@ fn errors_leave_the_document_and_defaults_alone() { } let e = s.execute("paint.setFill", &json!({})).unwrap_err(); assert!(e.to_string().contains("give color, none, swatch, gradient or opacity"), "{e}"); + // An opacity alone places no gradient across the selection. + let doc = s.doc().unwrap().doc.clone(); + for (span, says) in [(json!("selection"), "give `gradient`"), (json!("all"), "span must be \"object\" or \"selection\"")] { + let e = s.execute("paint.setFill", &json!({"opacity": 40, "span": span})).unwrap_err(); + assert!(e.to_string().contains(says), "{span}: {e}"); + } + assert_eq!(*s.doc().unwrap().doc, *doc); + s.execute("paint.setFill", &json!({"opacity": 40, "span": "object"})).unwrap(); + assert!((fill_opacity(&s, id) - 0.4).abs() < 1e-6); s.execute("select.none", &json!({})).unwrap(); let doc = s.doc().unwrap().doc.clone(); for p in [json!({"opacity": 40}), json!({"color": "#ff0000", "opacity": 40})] {