diff --git a/crates/engine/src/cmd/style.rs b/crates/engine/src/cmd/style.rs index e0eee77b9..0ff7c8a4f 100644 --- a/crates/engine/src/cmd/style.rs +++ b/crates/engine/src/cmd/style.rs @@ -1515,7 +1515,7 @@ fn create_object(s: &mut Session, p: &Value) -> Result { if let Some(f) = fill_param(CREATE, p, os.fill.as_ref())? { os.fill = f; } - if let Some(st) = group_param(CREATE, p, "stroke", os.stroke.as_ref().unwrap_or(&designcraft_doc::Stroke::default()))? { + if let Some(st) = stroke_param(CREATE, p, os.stroke.as_ref())? { os.stroke = st; } if let Some(c) = corners_param(CREATE, p, os.corners.unwrap_or_default())? { @@ -1535,6 +1535,18 @@ fn create_object(s: &mut Session, p: &Value) -> Result { }) } +/// `p.stroke` merged over `base` (as [`group_param`]), refused when the weight isn't a number ≥ 0 +/// or the tint isn't finite. +fn stroke_param(cmd: &str, p: &Value, base: Option<&designcraft_doc::Stroke>) -> Result>> { + let st = group_param(cmd, p, "stroke", base.unwrap_or(&designcraft_doc::Stroke::default()))?; + if let Some(Some(st)) = &st + && !(st.weight.is_finite() && st.weight >= 0.0 && st.tint.is_finite()) + { + return Err(bad(cmd, "stroke weight must be a number ≥ 0")); + } + Ok(st) +} + /// `p[key]`, an attribute group of a style, merged field by field over `base`: `None` when not /// given, `Some(None)` for null (the style no longer sets it). fn group_param(cmd: &str, p: &Value, key: &str, base: &T) -> Result>> { @@ -1616,12 +1628,7 @@ fn edit_object(s: &mut Session, p: &Value) -> Result { if let Some(f) = fill_param(CMD, p, old.fill.as_ref())? { new.fill = f; } - if let Some(st) = group_param(CMD, p, "stroke", old.stroke.as_ref().unwrap_or(&designcraft_doc::Stroke::default()))? { - if let Some(st) = &st - && !(st.weight.is_finite() && st.weight >= 0.0 && st.tint.is_finite()) - { - return Err(bad(CMD, "stroke weight must be a number ≥ 0")); - } + if let Some(st) = stroke_param(CMD, p, old.stroke.as_ref())? { new.stroke = st; } if let Some(c) = corners_param(CMD, p, old.corners.unwrap_or_default())? { diff --git a/crates/engine/src/tests.rs b/crates/engine/src/tests.rs index 47bfcb523..0441f9bd9 100644 --- a/crates/engine/src/tests.rs +++ b/crates/engine/src/tests.rs @@ -725,6 +725,13 @@ fn object_styles_carry_corners_and_their_stroke_and_corners_can_be_edited() { assert!(s.execute("style.object.edit", &json!({"name": "Framed", "rename": "[Basic Text Frame]"})).is_err()); assert!(s.execute("style.object.edit", &json!({"name": "[Basic Graphics Frame]", "rename": "Mine"})).is_err()); assert!(s.execute("style.object.edit", &json!({"name": "Framed", "corners": {"size": "big"}})).is_err()); + // A negative stroke weight is refused when creating a style as when editing one. + let styles = s.doc().unwrap().doc.styles.object.len(); + for cmd in ["style.object.create", "style.object.edit"] { + let e = s.execute(cmd, &json!({"name": "Framed", "stroke": {"weight": -3}})).unwrap_err(); + assert!(e.to_string().contains("stroke weight"), "{cmd}: {e}"); + } + assert_eq!(s.doc().unwrap().doc.styles.object.len(), styles); // null: the style stops setting corners, the items keep theirs. s.execute("style.object.edit", &json!({"name": "Framed", "corners": null})).unwrap(); assert!(s.doc().unwrap().doc.styles.object_style("Framed").unwrap().corners.is_none());