diff --git a/crates/format/src/rest/proto_variable_tests.rs b/crates/format/src/rest/proto_variable_tests.rs index 133d45de2..3c89d8a41 100644 --- a/crates/format/src/rest/proto_variable_tests.rs +++ b/crates/format/src/rest/proto_variable_tests.rs @@ -188,6 +188,44 @@ fn bound_variables_and_explicit_modes() { assert!(has(&warnings, "pass the local variables JSON"), "{warnings:?}"); } +/// #2441: a variable bound to one or more stroke sides (`individualStrokeWeights`) binds that +/// side's weight. The live API names the sides `BORDER_TOP_WEIGHT` …, the published spec `top` …; +/// both are read, and neither is reported as skipped. +#[test] +fn variables_bound_to_stroke_sides_bind_each_sides_weight() { + let rect = |id: &str, name: &str, sides: Value| { + json!({"id": id, "name": name, "type": "RECTANGLE", "relativeTransform": at(0.0, 0.0), "size": {"x": 40, "y": 20}, + "strokes": [{"type": "SOLID", "color": rgba(0.0, 0.0, 0.0)}], "strokeWeight": 1, + "individualStrokeWeights": {"top": 1, "right": 1, "bottom": 1, "left": 1}, + "boundVariables": {"individualStrokeWeights": sides}}) + }; + let file = json!({"name": "Sides", "document": {"id": "0:0", "type": "DOCUMENT", "children": [ + {"id": "0:1", "name": "Page", "type": "CANVAS", "children": [ + rect("1:1", "Row", json!({"BORDER_BOTTOM_WEIGHT": alias(SPACE)})), + rect("1:2", "Card", json!({"BORDER_TOP_WEIGHT": alias(SPACE), "BORDER_RIGHT_WEIGHT": alias(SPACE), + "BORDER_BOTTOM_WEIGHT": alias(SPACE), "BORDER_LEFT_WEIGHT": alias(SPACE)})), + rect("1:3", "Spec", json!({"top": alias(SPACE), "left": alias(SPACE)})), + rect("1:4", "Colour", json!({"BORDER_LEFT_WEIGHT": alias(BRAND)})) + ]} + ]}}); + let (doc, warnings) = import_with(&file, Some(&variables_json()), &no_images).unwrap(); + let sides = |name: &str| -> Vec<(String, String)> { + let n = named(&doc, name); + ["strokeTopWeight", "strokeRightWeight", "strokeBottomWeight", "strokeLeftWeight"] + .iter() + .filter_map(|f| n.bound.get(*f).map(|v| (f.to_string(), v.clone()))) + .collect() + }; + let b = |f: &str| (f.to_string(), SPACE.to_string()); + assert_eq!(sides("Row"), [b("strokeBottomWeight")], "one side bound"); + assert_eq!(sides("Card"), [b("strokeTopWeight"), b("strokeRightWeight"), b("strokeBottomWeight"), b("strokeLeftWeight")]); + assert_eq!(sides("Spec"), [b("strokeTopWeight"), b("strokeLeftWeight")], "the spec's names"); + assert_eq!(sides("Colour"), [], "a colour variable can't be a weight"); + assert!(!has(&warnings, "`individualStrokeWeights`"), "{warnings:?}"); + assert!(has(&warnings, "type doesn't fit"), "{warnings:?}"); + assert!(doc.validate().is_empty(), "{:?}", doc.validate()); +} + #[test] fn embedded_variables_match_separate_ones() { let mut embedded = file_json(); diff --git a/crates/format/src/rest/variables.rs b/crates/format/src/rest/variables.rs index ba2467423..a962ded0f 100644 --- a/crates/format/src/rest/variables.rs +++ b/crates/format/src/rest/variables.rs @@ -427,6 +427,19 @@ fn node_binding<'v>(k: &str, v: &'v Value, n: &Node, out: &mut Vec<(String, &'v push(f, v.get(key).and_then(alias)); } } + // Each stroke side's weight (#2441). The live `/nodes` response names the sides + // `BORDER_TOP_WEIGHT` and so on, as `rectangleCornerRadii` names its corners; Figma's + // published OpenAPI spec names them `top` … `left`. Either is read. + "individualStrokeWeights" => { + for (keys, f) in [ + (["BORDER_TOP_WEIGHT", "top"], "strokeTopWeight"), + (["BORDER_RIGHT_WEIGHT", "right"], "strokeRightWeight"), + (["BORDER_BOTTOM_WEIGHT", "bottom"], "strokeBottomWeight"), + (["BORDER_LEFT_WEIGHT", "left"], "strokeLeftWeight"), + ] { + push(f, keys.iter().find_map(|key| v.get(*key)).and_then(alias)); + } + } "fills" | "strokes" => { let field = if k == "fills" { "fill" } else { "stroke" }; match v.as_array().and_then(|a| a.first()).and_then(alias) {