mirror of
https://github.com/storytold/vectorcraft.git
synced 2026-10-28 03:37:00 -06:00
Merge pull request #1077 from Ni-zav/fix/layer-options-target-validation-rebased-20261011
fix(layers): validate explicit Layer Options targets
This commit is contained in:
+1
-1
File diff suppressed because one or more lines are too long
@@ -54,9 +54,16 @@ fn fields_of(n: &Node) -> serde_json::Map<String, Value> {
|
||||
/// the current layer), filled in from the first.
|
||||
pub fn open(app: &mut VectorcraftApp, p: &Value) -> Result<Value, String> {
|
||||
let st = app.session.active().ok_or("no document open")?;
|
||||
let ids: Vec<NodeId> = match (p.get("ids").and_then(Value::as_array), p.get("id").and_then(Value::as_u64)) {
|
||||
(Some(a), _) => a.iter().filter_map(Value::as_u64).map(NodeId).collect(),
|
||||
(None, Some(id)) => vec![NodeId(id)],
|
||||
// Rows given are used as given: a malformed or unknown id fails rather than being skipped or
|
||||
// the dialog opening on the highlighted rows instead.
|
||||
let row = |v: &Value| -> Result<NodeId, String> {
|
||||
let id = NodeId(v.as_u64().ok_or_else(|| format!("object ids are non-negative integers, not {v}"))?);
|
||||
st.doc.node(id).map(|_| id).ok_or_else(|| "no such layer or object".to_string())
|
||||
};
|
||||
let ids: Vec<NodeId> = match (p.get("ids"), p.get("id")) {
|
||||
(Some(Value::Array(a)), _) => a.iter().map(row).collect::<Result<_, _>>()?,
|
||||
(Some(v), _) => return Err(format!("`ids` must be an array of object ids, not {v}")),
|
||||
(None, Some(v)) => vec![row(v)?],
|
||||
(None, None) => {
|
||||
let rows = st.highlighted_rows();
|
||||
if rows.is_empty() { st.current_layer().into_iter().collect() } else { rows }
|
||||
@@ -252,6 +259,21 @@ mod tests {
|
||||
app.ui.dialog.as_mut().unwrap().fields.insert(k.into(), v);
|
||||
}
|
||||
|
||||
/// Explicit object ids must not silently disappear or fall back to the highlighted row.
|
||||
#[test]
|
||||
fn invalid_layer_options_targets_do_not_open_a_partial_dialog() {
|
||||
let mut app = app();
|
||||
let id = app.run("shape.rectangle", json!({"x": 0, "y": 0, "width": 20, "height": 20})).unwrap()["id"].as_u64().unwrap();
|
||||
for params in
|
||||
[json!({"ids": [id, "invalid"]}), json!({"ids": [id, 999999]}), json!({"ids": "invalid"}), json!({"id": "invalid"}), json!({"id": null})]
|
||||
{
|
||||
assert!(app.run("ui.layerOptions", params.clone()).is_err(), "accepted {params}");
|
||||
assert!(app.ui.dialog.is_none(), "opened partial options for {params}");
|
||||
}
|
||||
app.run("ui.layerOptions", json!({"ids": [id]})).unwrap();
|
||||
assert_eq!(app.ui.dialog.as_ref().unwrap().fields["ids"], json!([id]));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn layer_options_edit_the_layer_and_ok_is_one_undo_step() {
|
||||
let mut app = app();
|
||||
|
||||
Reference in New Issue
Block a user