#6490: reject malformed move offsets before selection or copy

This commit is contained in:
yoriswind
2026-11-10 20:45:20 -08:00
parent 8dbe7bd3cb
commit 98fcf6a497
3 changed files with 81 additions and 1 deletions
+13
View File
@@ -16,6 +16,7 @@ mod gradient;
mod limits;
mod links;
mod liquify;
mod move_offsets;
mod pen;
mod persp;
mod place;
@@ -1960,4 +1961,16 @@ fn transform_rejects_nonnumeric_steps_before_editing() {
assert!((bounds["height"].as_f64().unwrap() - 22.5).abs() < 1e-6, "{bounds}");
assert!(after["history"].as_array().unwrap().contains(&json!("Move")));
assert!(after["history"].as_array().unwrap().contains(&json!("Scale")));
let applied = call(&mut s, 16, "transform", json!({"ids": [second], "dx": 10, "rotate": 90, "origin": [0, 0]}));
assert_eq!(applied["isError"], false, "{applied}");
let applied: Value = serde_json::from_str(&text_of(&applied)).unwrap();
assert_eq!(applied["applied"], json!(["move", "rotate"]));
let after = inspect(&mut s, 17);
assert_eq!(after["selection"], json!([second]));
let bounds = &after["selectionBounds"];
for (field, expected) in [("x", 40.0), ("y", -70.0), ("width", 15.0), ("height", 20.0)] {
assert!((bounds[field].as_f64().unwrap() - expected).abs() < 1e-6, "{field}: {bounds}");
}
assert!(after["history"].as_array().unwrap().ends_with(&[json!("Move"), json!("Rotate")]));
}
+67
View File
@@ -0,0 +1,67 @@
use serde_json::{Value, json};
use super::{Server, call, id_of, server, text_of};
fn inspect(s: &mut Server, id: u64) -> Value {
serde_json::from_str(&text_of(&call(s, id, "doc_inspect", json!({})))).unwrap()
}
fn path(s: &mut Server, id: u64, d: &str) -> u64 {
id_of(&call(s, id, "draw_path", json!({"d": d})))
}
fn child(doc: &Value, id: u64) -> Value {
doc["layers"][0]["children"].as_array().unwrap().iter().find(|node| node["id"] == id).unwrap().clone()
}
#[test]
fn transform_rejects_malformed_move_offsets_before_selection_or_copy() {
let mut s = server();
let first = path(&mut s, 1, "M40 40 L60 40 L60 60 L40 60 Z");
let second = path(&mut s, 2, "M100 100 L130 100 L130 140 L100 140 Z");
let before = inspect(&mut s, 3);
assert_eq!(before["selection"], json!([second]));
let cases = [("dx", json!({"ids": [first], "dx": "bad", "copy": true})), ("dy", json!({"ids": [first], "dx": 10, "dy": "bad", "copy": true}))];
for (index, (field, args)) in cases.into_iter().enumerate() {
let request_id = 4 + index as u64 * 2;
let result = call(&mut s, request_id, "transform", args);
assert_eq!(result["isError"], true, "{field}: {result}");
assert!(text_of(&result).contains(field), "{field}: {result}");
assert_eq!(inspect(&mut s, request_id + 1), before, "{field}");
}
// The recorded request omits dx and must still move a copy down by 10 points.
let result = call(&mut s, 8, "transform", json!({"ids": [first], "dy": 10, "copy": true}));
assert_eq!(result["isError"], false, "{result}");
let reply: Value = serde_json::from_str(&text_of(&result)).unwrap();
assert_eq!(reply["applied"], json!(["move"]));
let after = inspect(&mut s, 9);
let copied = after["selection"][0].as_u64().unwrap();
assert_ne!(copied, first);
assert_ne!(copied, second);
let original_before = child(&before, first);
let original_after = child(&after, first);
let second_after = child(&after, second);
let copy_after = child(&after, copied);
assert_eq!(original_after["bounds"], original_before["bounds"]);
assert_eq!(second_after["bounds"], child(&before, second)["bounds"]);
assert_eq!(copy_after["bounds"]["x"], original_before["bounds"]["x"]);
assert_eq!(copy_after["bounds"]["y"], original_before["bounds"]["y"].as_f64().unwrap() + 10.0);
assert_eq!(copy_after["bounds"]["width"], original_before["bounds"]["width"]);
assert_eq!(copy_after["bounds"]["height"], original_before["bounds"]["height"]);
let before_history = before["history"].as_array().unwrap();
let after_history = after["history"].as_array().unwrap();
assert_eq!(after_history.len(), before_history.len() + 1);
assert_eq!(after_history.last(), Some(&json!("Move")));
// Null keeps the engine's optional-number default while a negative numeric offset is applied.
let result = call(&mut s, 10, "transform", json!({"ids": [second], "dx": null, "dy": -7}));
assert_eq!(result["isError"], false, "{result}");
let after_null = inspect(&mut s, 11);
let moved = child(&after_null, second);
let second_before = child(&before, second);
assert_eq!(moved["bounds"]["x"], second_before["bounds"]["x"]);
assert_eq!(moved["bounds"]["y"], second_before["bounds"]["y"].as_f64().unwrap() - 7.0);
assert_eq!(after_null["history"].as_array().unwrap().last(), Some(&json!("Move")));
}
+1 -1
View File
@@ -253,7 +253,7 @@ fn dispatch(b: &mut dyn Backend, name: &str, a: &Args) -> Result<ToolResult, Str
}
"transform" => {
// Validate all numeric steps before the handler changes selection or runs a step.
for field in ["rotate", "scale", "scaleX", "scaleY", "shear"] {
for field in ["dx", "dy", "rotate", "scale", "scaleX", "scaleY", "shear"] {
if a.get(field).is_some_and(|value| !value.is_null() && value.as_f64().is_none()) {
return Err(format!("`{field}` must be a number"));
}