From 1ae221897ac95ffa8b03167d9ddf2d979904849f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jorge=20Gregorio=20Rinc=C3=B3n?= Date: Tue, 1 Dec 2026 10:39:47 -0500 Subject: [PATCH 1/3] fix(project): check a saved layout import step against the layout its command reads (#3482) --- crates/engine/src/project_capabilities.rs | 88 ++++++++++++----------- crates/engine/src/tests/layout_files.rs | 38 ++++++++++ 2 files changed, 85 insertions(+), 41 deletions(-) diff --git a/crates/engine/src/project_capabilities.rs b/crates/engine/src/project_capabilities.rs index ba63ebfd65..0cee3851b7 100644 --- a/crates/engine/src/project_capabilities.rs +++ b/crates/engine/src/project_capabilities.rs @@ -498,7 +498,7 @@ fn command_uses_text_statistics(id: &str, params: &Value) -> bool { // kind of the page it changes, so a title with a token counts. "presentation.mapPage" | "presentation.updatePage" => params.get("title").is_some_and(holds_statistic), // A layout file, read as `layout.import` reads it, which `report.existingTemplate` also does. - "layout.import" | "report.existingTemplate" => imported_layout_uses(params, &STATISTIC_KEYS, text_statistics), + "layout.import" | "report.existingTemplate" => imported_layout_uses(id, params, &STATISTIC_KEYS, text_statistics), "presentation.import" => crate::cmd::items::inline_text(params) .ok() .flatten() @@ -594,7 +594,7 @@ fn command_uses_text_size(id: &str, params: &Value) -> bool { params.get("textSize").is_some_and(|v| !v.is_null()) || params.get("grid").is_some_and(|g| sized(g, "labelSize", GRID_LABEL_PT)) } "layout.import" | "report.existingTemplate" => { - imported_layout_uses(params, &["\"text_size\"", "\"label_size\""], |e| e.kind.has_custom_text_size()) + imported_layout_uses(id, params, &["\"text_size\"", "\"label_size\""], |e| e.kind.has_custom_text_size()) } "script.run" => params.get("script").and_then(Value::as_str).is_some_and(|s| script_uses(s, command_uses_text_size)), _ => false, @@ -631,7 +631,7 @@ fn command_uses_legend_fit(id: &str, params: &Value) -> bool { } // A layout file, read as `layout.import` reads it, which `report.existingTemplate` also does. "layout.import" | "report.existingTemplate" => { - imported_layout_uses(params, &["\"fit\"", "\"show_layer_names\"", "\"columns\""], legends_fitting) + imported_layout_uses(id, params, &["\"fit\"", "\"show_layer_names\"", "\"columns\""], legends_fitting) } "script.run" => params.get("script").and_then(Value::as_str).is_some_and(|s| script_uses(s, command_uses_legend_fit)), _ => false, @@ -649,7 +649,7 @@ fn command_uses_legend_class_labels(id: &str, params: &Value) -> bool { .filter_map(Value::as_object) .any(|classes| classes.values().any(|t| t.as_str().is_some_and(|t| !t.trim().is_empty()))) }), - "layout.import" | "report.existingTemplate" => imported_layout_uses(params, &["\"class_labels\""], legend_labels_classes), + "layout.import" | "report.existingTemplate" => imported_layout_uses(id, params, &["\"class_labels\""], legend_labels_classes), "script.run" => params.get("script").and_then(Value::as_str).is_some_and(|s| script_uses(s, command_uses_legend_class_labels)), _ => false, } @@ -666,7 +666,7 @@ fn legend_labels_classes(e: &rustgis_map::layout::Element) -> bool { fn command_uses_legend_order(id: &str, params: &Value) -> bool { match id { "layout.addElement" | "layout.update" => params.get("layerOrder").and_then(Value::as_array).is_some_and(|names| !names.is_empty()), - "layout.import" | "report.existingTemplate" => imported_layout_uses(params, &["\"layer_order\""], legend_orders_layers), + "layout.import" | "report.existingTemplate" => imported_layout_uses(id, params, &["\"layer_order\""], legend_orders_layers), "script.run" => params.get("script").and_then(Value::as_str).is_some_and(|s| script_uses(s, command_uses_legend_order)), _ => false, } @@ -685,7 +685,7 @@ fn command_uses_legend_column_breaks(id: &str, params: &Value) -> bool { "layout.addElement" | "layout.update" => { params.get("columnBreaks").and_then(Value::as_array).is_some_and(|names| !names.is_empty()) } - "layout.import" | "report.existingTemplate" => imported_layout_uses(params, &["\"column_breaks\""], legend_breaks_columns), + "layout.import" | "report.existingTemplate" => imported_layout_uses(id, params, &["\"column_breaks\""], legend_breaks_columns), "script.run" => params.get("script").and_then(Value::as_str).is_some_and(|s| script_uses(s, command_uses_legend_column_breaks)), _ => false, } @@ -701,7 +701,7 @@ fn legend_breaks_columns(e: &rustgis_map::layout::Element) -> bool { fn command_uses_legend_one_row(id: &str, params: &Value) -> bool { match id { "layout.addElement" | "layout.update" => matches!(crate::cmd::layout::legend_one_row_param(params), Ok(Some(true))), - "layout.import" | "report.existingTemplate" => imported_layout_uses(params, &["\"one_row\""], legend_in_one_row), + "layout.import" | "report.existingTemplate" => imported_layout_uses(id, params, &["\"one_row\""], legend_in_one_row), // It opens its `text` only, whatever else the step holds. "project.openText" => params.get("text").and_then(Value::as_str).is_some_and(text_lays_a_legend_in_one_row), "script.run" => params.get("script").and_then(Value::as_str).is_some_and(|s| script_uses(s, command_uses_legend_one_row)), @@ -735,7 +735,7 @@ fn legend_in_one_row(e: &rustgis_map::layout::Element) -> bool { fn command_uses_legend_counts(id: &str, params: &Value) -> bool { match id { "layout.addElement" | "layout.update" => crate::cmd::layout::legend_counts_param(params) == Ok(Some(true)), - "layout.import" | "report.existingTemplate" => imported_layout_uses(params, &["\"show_counts\""], legend_counts_features), + "layout.import" | "report.existingTemplate" => imported_layout_uses(id, params, &["\"show_counts\""], legend_counts_features), "script.run" => params.get("script").and_then(Value::as_str).is_some_and(|s| script_uses(s, command_uses_legend_counts)), _ => false, } @@ -753,7 +753,7 @@ fn command_uses_legend_subtitle(id: &str, params: &Value) -> bool { "layout.addElement" | "layout.update" => { crate::cmd::layout::legend_subtitle_param(params).is_ok_and(|t| t.is_some_and(|t| !t.is_empty())) } - "layout.import" | "report.existingTemplate" => imported_layout_uses(params, &["\"subtitle\""], legend_subtitle_features), + "layout.import" | "report.existingTemplate" => imported_layout_uses(id, params, &["\"subtitle\""], legend_subtitle_features), "script.run" => params.get("script").and_then(Value::as_str).is_some_and(|s| script_uses(s, command_uses_legend_subtitle)), _ => false, } @@ -769,7 +769,7 @@ fn command_uses_extent_indicators(id: &str, params: &Value) -> bool { "layout.update" => params .get("extentIndicator") .is_some_and(|v| v.get("of").is_some_and(|of| !of.is_null()) || v.get("remove") == Some(&Value::Bool(true))), - "layout.import" | "report.existingTemplate" => imported_layout_uses(params, &["\"more_extents\""], extent_indicators_features), + "layout.import" | "report.existingTemplate" => imported_layout_uses(id, params, &["\"more_extents\""], extent_indicators_features), "script.run" => params.get("script").and_then(Value::as_str).is_some_and(|s| script_uses(s, command_uses_extent_indicators)), _ => false, } @@ -786,7 +786,7 @@ fn extent_indicators_features(e: &rustgis_map::layout::Element) -> bool { fn command_numbers_extents(id: &str, params: &Value) -> bool { match id { "layout.update" => params.get("extentNumbers") == Some(&Value::Bool(true)), - "layout.import" | "report.existingTemplate" => imported_layout_uses(params, &["\"extent_numbers\""], extent_numbers_features), + "layout.import" | "report.existingTemplate" => imported_layout_uses(id, params, &["\"extent_numbers\""], extent_numbers_features), // It opens its `text` only, whatever else the step holds. "project.openText" => params.get("text").and_then(Value::as_str).is_some_and(text_numbers_extents), "script.run" => params.get("script").and_then(Value::as_str).is_some_and(|s| script_uses(s, command_numbers_extents)), @@ -814,7 +814,7 @@ fn command_uses_legend_nested_sizes(id: &str, params: &Value) -> bool { "layout.addElement" | "layout.update" => { crate::cmd::layout::legend_sizes_param(params) == Ok(Some(rustgis_map::layout::LegendSizes::Nested)) } - "layout.import" | "report.existingTemplate" => imported_layout_uses(params, &["\"sizes\""], legend_nested_sizes_features), + "layout.import" | "report.existingTemplate" => imported_layout_uses(id, params, &["\"sizes\""], legend_nested_sizes_features), "script.run" => params.get("script").and_then(Value::as_str).is_some_and(|s| script_uses(s, command_uses_legend_nested_sizes)), _ => false, } @@ -849,7 +849,7 @@ fn command_uses_picture_source(id: &str, params: &Value) -> bool { match id { "layout.update" => crate::cmd::layout::gives_picture(params), // A layout file, read as `layout.import` reads it, which `report.existingTemplate` also does. - "layout.import" | "report.existingTemplate" => imported_layout_uses(params, &["\"source\""], pictures_with_files), + "layout.import" | "report.existingTemplate" => imported_layout_uses(id, params, &["\"source\""], pictures_with_files), "script.run" => params.get("script").and_then(Value::as_str).is_some_and(|s| script_uses(s, command_uses_picture_source)), _ => false, } @@ -861,7 +861,7 @@ fn command_uses_picture_source(id: &str, params: &Value) -> bool { fn command_uses_frame_border(id: &str, params: &Value) -> bool { match id { "layout.update" => params.get("border").is_some_and(|b| !b.is_null()), - "layout.import" | "report.existingTemplate" => imported_layout_uses(params, &["\"border_"], |e| e.kind.has_custom_border()), + "layout.import" | "report.existingTemplate" => imported_layout_uses(id, params, &["\"border_"], |e| e.kind.has_custom_border()), "script.run" => params.get("script").and_then(Value::as_str).is_some_and(|s| script_uses(s, command_uses_frame_border)), _ => false, } @@ -874,7 +874,7 @@ fn command_uses_section(id: &str, params: &Value) -> bool { "layout.addElement" => params.get("kind").and_then(Value::as_str).is_some_and(|k| k.trim().eq_ignore_ascii_case("section")), "layout.update" => params.get("section").is_some_and(|v| !v.is_null()), // A layout file, read as `layout.import` reads it, which `report.existingTemplate` also does. - "layout.import" | "report.existingTemplate" => imported_layout_uses(params, &["\"sections\""], |e| e.kind.has_sections()), + "layout.import" | "report.existingTemplate" => imported_layout_uses(id, params, &["\"sections\""], |e| e.kind.has_sections()), "script.run" => params.get("script").and_then(Value::as_str).is_some_and(|s| script_uses(s, command_uses_section)), _ => false, } @@ -895,7 +895,7 @@ fn command_uses_profile(id: &str, params: &Value) -> bool { || params.get("section").is_some_and(Value::is_string) } // A layout file, read as `layout.import` reads it, which `report.existingTemplate` also does. - "layout.import" | "report.existingTemplate" => imported_layout_uses(params, &["\"Profile\""], |e| e.kind.is_profile()), + "layout.import" | "report.existingTemplate" => imported_layout_uses(id, params, &["\"Profile\""], |e| e.kind.is_profile()), "script.run" => params.get("script").and_then(Value::as_str).is_some_and(|s| script_uses(s, command_uses_profile)), _ => false, } @@ -908,7 +908,7 @@ fn command_colours_the_page(id: &str, params: &Value) -> bool { match id { "layout.page" => crate::cmd::opaque_color(params, "color") .is_ok_and(|c| c.flatten().is_some_and(|c| c != rustgis_map::Rgba::rgb(0xFF, 0xFF, 0xFF))), - "layout.import" | "report.existingTemplate" => imported_layout(params, &["\"paper\""]).is_some_and(|mut l| { + "layout.import" | "report.existingTemplate" => imported_layout(id, params, &["\"paper\""]).is_some_and(|mut l| { l.clamp(); l.paper.is_some() }), @@ -936,7 +936,7 @@ fn command_uses_blank_paper(id: &str, params: &Value) -> bool { fn command_uses_element_background(id: &str, params: &Value) -> bool { match id { "layout.addElement" | "layout.update" => params.get("background").is_some_and(|b| !b.is_null()), - "layout.import" | "report.existingTemplate" => imported_layout_uses(params, &["\"background\""], |e| e.background.is_some()), + "layout.import" | "report.existingTemplate" => imported_layout_uses(id, params, &["\"background\""], |e| e.background.is_some()), "script.run" => params.get("script").and_then(Value::as_str).is_some_and(|s| script_uses(s, command_uses_element_background)), _ => false, } @@ -947,7 +947,7 @@ fn command_uses_element_background(id: &str, params: &Value) -> bool { /// and `layout.update`, so such a command fails there instead of losing the ink. fn command_uses_element_ink(id: &str, params: &Value) -> bool { match id { - "layout.import" | "report.existingTemplate" => imported_layout_uses(params, &["\"color\""], |e| e.kind.ink().is_some()), + "layout.import" | "report.existingTemplate" => imported_layout_uses(id, params, &["\"color\""], |e| e.kind.ink().is_some()), "script.run" => params.get("script").and_then(Value::as_str).is_some_and(|s| script_uses(s, command_uses_element_ink)), _ => false, } @@ -964,7 +964,7 @@ fn wraps(e: &rustgis_map::layout::Element) -> bool { fn command_uses_element_wrap(id: &str, params: &Value) -> bool { match id { "layout.addElement" | "layout.update" => params.get("wrap").and_then(crate::cmd::bool_value) == Some(true), - "layout.import" | "report.existingTemplate" => imported_layout_uses(params, &["\"wrap\""], wraps), + "layout.import" | "report.existingTemplate" => imported_layout_uses(id, params, &["\"wrap\""], wraps), "script.run" => params.get("script").and_then(Value::as_str).is_some_and(|s| script_uses(s, command_uses_element_wrap)), _ => false, } @@ -999,7 +999,7 @@ fn command_uses_text_block(id: &str, params: &Value) -> bool { }) } "layout.import" | "report.existingTemplate" => { - imported_layout_uses(params, &["\"valign\"", "\"line_spacing\"", "\"case\""], text_blocks) + imported_layout_uses(id, params, &["\"valign\"", "\"line_spacing\"", "\"case\""], text_blocks) } "script.run" => params.get("script").and_then(Value::as_str).is_some_and(|s| script_uses(s, command_uses_text_block)), _ => false, @@ -1031,7 +1031,7 @@ fn command_uses_chart_data(id: &str, params: &Value) -> bool { || params.get("other").and_then(crate::cmd::bool_value) == Some(true) } "layout.import" | "report.existingTemplate" => { - imported_layout_uses(params, &["\"group\"", "\"statistic\"", "\"order\"", "\"bars\"", "\"other\""], charts_data) + imported_layout_uses(id, params, &["\"group\"", "\"statistic\"", "\"order\"", "\"bars\"", "\"other\""], charts_data) } "script.run" => params.get("script").and_then(Value::as_str).is_some_and(|s| script_uses(s, command_uses_chart_data)), _ => false, @@ -1064,6 +1064,7 @@ fn command_uses_chart_look(id: &str, params: &Value) -> bool { } "layout.update" => only_chart(), "layout.import" | "report.existingTemplate" => imported_layout_uses( + id, params, &["\"orientation\"", "\"values\"", "\"decimals\"", "\"title\"", "\"bar_color\"", "\"label_align\""], charts_look, @@ -1088,7 +1089,7 @@ fn command_uses_chart_classes(id: &str, params: &Value) -> bool { params.get("kind").and_then(Value::as_str).is_some_and(|k| k.trim().eq_ignore_ascii_case("chart")) && classes() } "layout.update" => classes(), - "layout.import" | "report.existingTemplate" => imported_layout_uses(params, &["\"classes\""], charts_classes), + "layout.import" | "report.existingTemplate" => imported_layout_uses(id, params, &["\"classes\""], charts_classes), "script.run" => params.get("script").and_then(Value::as_str).is_some_and(|s| script_uses(s, command_uses_chart_classes)), _ => false, } @@ -1108,7 +1109,7 @@ fn command_uses_chart_stack(id: &str, params: &Value) -> bool { params.get("kind").and_then(Value::as_str).is_some_and(|k| k.trim().eq_ignore_ascii_case("chart")) && stack() } "layout.update" => stack(), - "layout.import" | "report.existingTemplate" => imported_layout_uses(params, &["\"stack\""], charts_stack), + "layout.import" | "report.existingTemplate" => imported_layout_uses(id, params, &["\"stack\""], charts_stack), "script.run" => params.get("script").and_then(Value::as_str).is_some_and(|s| script_uses(s, command_uses_chart_stack)), _ => false, } @@ -1147,6 +1148,7 @@ fn command_uses_scale_bar_details(id: &str, params: &Value) -> bool { || name("fit").and_then(ScaleBarFit::from_name).is_some_and(|v| v != ScaleBarFit::default()) } "layout.import" | "report.existingTemplate" => imported_layout_uses( + id, params, &[ "\"subdivisions\"", @@ -1185,7 +1187,7 @@ fn command_uses_chart_facing(id: &str, params: &Value) -> bool { } "layout.update" => facing(), "layout.import" | "report.existingTemplate" => { - imported_layout_uses(params, &["\"mirror\"", "\"max\"", "\"hide_labels\""], charts_facing) + imported_layout_uses(id, params, &["\"mirror\"", "\"max\"", "\"hide_labels\""], charts_facing) } "script.run" => params.get("script").and_then(Value::as_str).is_some_and(|s| script_uses(s, command_uses_chart_facing)), _ => false, @@ -1212,7 +1214,7 @@ fn command_uses_chart_outline(id: &str, params: &Value) -> bool { params.get("kind").and_then(Value::as_str).is_some_and(|k| k.trim().eq_ignore_ascii_case("chart")) && outlined() } "layout.update" => outlined(), - "layout.import" | "report.existingTemplate" => imported_layout_uses(params, &["\"bar_outline\""], charts_outlined), + "layout.import" | "report.existingTemplate" => imported_layout_uses(id, params, &["\"bar_outline\""], charts_outlined), "script.run" => params.get("script").and_then(Value::as_str).is_some_and(|s| script_uses(s, command_uses_chart_outline)), _ => false, } @@ -1238,7 +1240,7 @@ fn command_uses_table_look(id: &str, params: &Value) -> bool { } "layout.update" => given("fields") || given("header"), "layout.import" | "report.existingTemplate" => { - imported_layout_uses(params, &["\"fields\"", "\"text_size\"", "\"header\""], tables_with_a_look) + imported_layout_uses(id, params, &["\"fields\"", "\"text_size\"", "\"header\""], tables_with_a_look) } "script.run" => params.get("script").and_then(Value::as_str).is_some_and(|s| script_uses(s, command_uses_table_look)), _ => false, @@ -1262,7 +1264,7 @@ fn command_uses_table_rows(id: &str, params: &Value) -> bool { } "layout.update" => given("sort") || given("descending") || given("selectedOnly"), "layout.import" | "report.existingTemplate" => { - imported_layout_uses(params, &["\"sort\"", "\"descending\"", "\"selected_only\""], tables_with_rows) + imported_layout_uses(id, params, &["\"sort\"", "\"descending\"", "\"selected_only\""], tables_with_rows) } "script.run" => params.get("script").and_then(Value::as_str).is_some_and(|s| script_uses(s, command_uses_table_rows)), _ => false, @@ -1287,7 +1289,7 @@ fn command_uses_colorbar_labels(id: &str, params: &Value) -> bool { .is_some_and(|v| !v.is_null() && !v.as_str().is_some_and(|t| t.trim().eq_ignore_ascii_case("auto")))) } "layout.import" | "report.existingTemplate" => { - imported_layout_uses(params, &["\"text_size\"", "\"decimals\""], colorbars_with_labels) + imported_layout_uses(id, params, &["\"text_size\"", "\"decimals\""], colorbars_with_labels) } "script.run" => params.get("script").and_then(Value::as_str).is_some_and(|s| script_uses(s, command_uses_colorbar_labels)), _ => false, @@ -1304,7 +1306,7 @@ fn north_arrows_with_font(e: &rustgis_map::layout::Element) -> bool { fn command_uses_north_arrow_font(id: &str, params: &Value) -> bool { match id { "layout.addElement" => adds_with_font(params, "northArrow"), - "layout.import" | "report.existingTemplate" => imported_layout_uses(params, &["\"font"], north_arrows_with_font), + "layout.import" | "report.existingTemplate" => imported_layout_uses(id, params, &["\"font"], north_arrows_with_font), // It opens its `text` only, whatever else the step holds. "project.openText" => params.get("text").and_then(Value::as_str).is_some_and(|t| text_gives_font(t, "layout-north-arrow-font")), "script.run" => params.get("script").and_then(Value::as_str).is_some_and(|s| script_uses(s, command_uses_north_arrow_font)), @@ -1328,7 +1330,7 @@ fn scale_bars_with_font(e: &rustgis_map::layout::Element) -> bool { fn command_uses_scale_bar_font(id: &str, params: &Value) -> bool { match id { "layout.addElement" => adds_with_font(params, "scaleBar"), - "layout.import" | "report.existingTemplate" => imported_layout_uses(params, &["\"font"], scale_bars_with_font), + "layout.import" | "report.existingTemplate" => imported_layout_uses(id, params, &["\"font"], scale_bars_with_font), "project.openText" => params.get("text").and_then(Value::as_str).is_some_and(|t| text_gives_font(t, "layout-scale-bar-font")), "script.run" => params.get("script").and_then(Value::as_str).is_some_and(|s| script_uses(s, command_uses_scale_bar_font)), _ => false, @@ -1350,19 +1352,23 @@ fn adds_with_font(params: &Value, kind: &str) -> bool { /// save asks, so a text with none of the feature's `keys` (a saved layout writes them only when /// set) isn't read through, unless it has a backslash: JSON may spell a key with escapes /// (`"\u0063olor"`). It is read once, whatever its keys. -fn imported_layout_uses(params: &Value, keys: &[&str], uses: fn(&rustgis_map::layout::Element) -> bool) -> bool { - imported_layout(params, keys).is_some_and(|l| l.elements.iter().any(uses)) +fn imported_layout_uses(id: &str, params: &Value, keys: &[&str], uses: fn(&rustgis_map::layout::Element) -> bool) -> bool { + imported_layout(id, params, keys).is_some_and(|l| l.elements.iter().any(uses)) } /// The layout a command carries inline, read only when its text names one of `keys` (or escapes, -/// which could hide one). `data` (base64) goes before `text`, as `report.existingTemplate` reads -/// them; one it refuses is no layout. `data` is decoded for each check, rather than keeping it: -/// only a step that carries it pays, and only on a save. -fn imported_layout(params: &Value, keys: &[&str]) -> Option { - crate::cmd::items::inline_text(params) - .ok() - .flatten() - .filter(|t| keys.iter().any(|key| t.contains(key)) || may_hide_a_key(t)) +/// which could hide one). The text is the one the command `id` reads (#3482): `layout.import` takes +/// `text` only, never `data`; `report.existingTemplate` takes a layout of the project named by +/// `layout` before any text, and otherwise `data` (base64) before `text`. One it refuses is no +/// layout. `data` is decoded for each check, rather than keeping it: only a step that carries it +/// pays, and only on a save. +fn imported_layout(id: &str, params: &Value, keys: &[&str]) -> Option { + let text = match id { + "layout.import" => crate::cmd::ps(params, "text").map(std::borrow::Cow::Borrowed), + _ if crate::cmd::ps(params, "layout").is_some() => None, + _ => crate::cmd::items::inline_text(params).ok().flatten(), + }; + text.filter(|t| keys.iter().any(|key| t.contains(key)) || may_hide_a_key(t)) .and_then(|t| serde_json::from_str::(&t).ok()) } diff --git a/crates/engine/src/tests/layout_files.rs b/crates/engine/src/tests/layout_files.rs index 6ef803cd0f..80d36f0a77 100644 --- a/crates/engine/src/tests/layout_files.rs +++ b/crates/engine/src/tests/layout_files.rs @@ -269,3 +269,41 @@ fn a_layout_file_with_a_text_block_says_so_in_its_header() { s.run("layout.import", &json!({"text": text})).unwrap(); assert!(s.project.layouts[2].elements.iter().any(|e| e.kind.text_uses_block())); } + +/// Whether a project with one saved step requires `layout-element-wrap`. +fn step_requires_wrap(command: &str, params: Value) -> bool { + let mut p = Project::default(); + p.tasks.push(serde_json::from_value(json!({"name": "t", "steps": [{"title": "s", "command": command, "params": params}]})).unwrap()); + serde_json::to_value(&p).unwrap()["requires"].as_array().is_some_and(|r| r.contains(&json!("layout-element-wrap"))) +} + +/// A saved step that imports a layout is checked against the layout its command reads (#3482): +/// `layout.import` reads `text` and never `data`; `report.existingTemplate` takes a layout's name +/// first, then `data`, then `text`. +#[test] +fn a_saved_import_step_is_checked_against_the_layout_its_command_reads() { + use base64::Engine; + let (mut s, plain) = with_layout(); + s.run("layout.addElement", &json!({"kind": "text", "wrap": true})).unwrap(); + let wrapped = serde_json::to_string(&s.project.layouts[0]).unwrap(); + let plain = plain.to_string(); + let data = |t: &str| base64::engine::general_purpose::STANDARD.encode(t); + + let mixed = json!({"text": wrapped, "data": data(&plain)}); + assert!(step_requires_wrap("layout.import", mixed.clone())); + assert!(!step_requires_wrap("layout.import", json!({"text": plain, "data": data(&wrapped)}))); + assert!(!step_requires_wrap("layout.import", json!({"data": data(&wrapped)}))); + assert!(step_requires_wrap("layout.import", json!({"text": wrapped}))); + assert!(step_requires_wrap("report.existingTemplate", json!({"text": plain, "data": data(&wrapped)}))); + assert!(!step_requires_wrap("report.existingTemplate", json!({"layout": "L", "text": wrapped}))); + + // The same step in a script line and as an inline model step. + assert!(step_requires_wrap("script.run", json!({"script": format!("layout.import {mixed}")}))); + assert!(step_requires_wrap("model.new", json!({"name": "m", "steps": [{"tool": "layout.import", "params": mixed.clone()}]}))); + + // What is checked is what the step imports. + s.run("task.new", &json!({"name": "t", "steps": [{"title": "s", "command": "layout.import", "params": mixed}]})).unwrap(); + s.run("task.run", &json!({"task": "t"})).unwrap(); + let imported = s.project.layouts.last().unwrap(); + assert!(imported.elements.iter().any(|e| matches!(e.kind, rustgis_map::layout::ElementKind::Text { wrap: true, .. }))); +} From 5280470c76ea8f1f10f866be0803b1064af1f216 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jorge=20Gregorio=20Rinc=C3=B3n?= Date: Tue, 1 Dec 2026 11:08:56 -0500 Subject: [PATCH 2/3] refactor(project): name both layout import commands in the capability check --- crates/engine/src/project_capabilities.rs | 4 ++-- crates/engine/src/tests/layout_files.rs | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/crates/engine/src/project_capabilities.rs b/crates/engine/src/project_capabilities.rs index 0cee3851b7..167d1b1b8f 100644 --- a/crates/engine/src/project_capabilities.rs +++ b/crates/engine/src/project_capabilities.rs @@ -1365,8 +1365,8 @@ fn imported_layout_uses(id: &str, params: &Value, keys: &[&str], uses: fn(&rustg fn imported_layout(id: &str, params: &Value, keys: &[&str]) -> Option { let text = match id { "layout.import" => crate::cmd::ps(params, "text").map(std::borrow::Cow::Borrowed), - _ if crate::cmd::ps(params, "layout").is_some() => None, - _ => crate::cmd::items::inline_text(params).ok().flatten(), + "report.existingTemplate" if crate::cmd::ps(params, "layout").is_none() => crate::cmd::items::inline_text(params).ok().flatten(), + _ => None, }; text.filter(|t| keys.iter().any(|key| t.contains(key)) || may_hide_a_key(t)) .and_then(|t| serde_json::from_str::(&t).ok()) diff --git a/crates/engine/src/tests/layout_files.rs b/crates/engine/src/tests/layout_files.rs index 80d36f0a77..4adef530b8 100644 --- a/crates/engine/src/tests/layout_files.rs +++ b/crates/engine/src/tests/layout_files.rs @@ -295,7 +295,7 @@ fn a_saved_import_step_is_checked_against_the_layout_its_command_reads() { assert!(!step_requires_wrap("layout.import", json!({"data": data(&wrapped)}))); assert!(step_requires_wrap("layout.import", json!({"text": wrapped}))); assert!(step_requires_wrap("report.existingTemplate", json!({"text": plain, "data": data(&wrapped)}))); - assert!(!step_requires_wrap("report.existingTemplate", json!({"layout": "L", "text": wrapped}))); + assert!(!step_requires_wrap("report.existingTemplate", json!({"layout": s.project.layouts[0].name, "text": wrapped}))); // The same step in a script line and as an inline model step. assert!(step_requires_wrap("script.run", json!({"script": format!("layout.import {mixed}")}))); From f98f8cecae22e3fbce50dc1c55dae223f824e9fc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jorge=20Gregorio=20Rinc=C3=B3n?= Date: Tue, 1 Dec 2026 11:18:02 -0500 Subject: [PATCH 3/3] docs(project): say how each layout import command's text is checked --- crates/engine/src/project_capabilities.rs | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/crates/engine/src/project_capabilities.rs b/crates/engine/src/project_capabilities.rs index 167d1b1b8f..1437a5259d 100644 --- a/crates/engine/src/project_capabilities.rs +++ b/crates/engine/src/project_capabilities.rs @@ -497,7 +497,7 @@ fn command_uses_text_statistics(id: &str, params: &Value) -> bool { // A map page's title, which the page draws over its map frame; `updatePage` doesn't say the // kind of the page it changes, so a title with a token counts. "presentation.mapPage" | "presentation.updatePage" => params.get("title").is_some_and(holds_statistic), - // A layout file, read as `layout.import` reads it, which `report.existingTemplate` also does. + // A layout given inline, taken as each of the two commands takes it ([`imported_layout`]). "layout.import" | "report.existingTemplate" => imported_layout_uses(id, params, &STATISTIC_KEYS, text_statistics), "presentation.import" => crate::cmd::items::inline_text(params) .ok() @@ -629,7 +629,7 @@ fn command_uses_legend_fit(id: &str, params: &Value) -> bool { || legend_names_param(params) == Ok(Some(false)) || legend_columns_param(params).is_ok_and(|n| n.is_some_and(|n| n > LEGEND_COLUMNS_BEFORE_FIT)) } - // A layout file, read as `layout.import` reads it, which `report.existingTemplate` also does. + // A layout given inline, taken as each of the two commands takes it ([`imported_layout`]). "layout.import" | "report.existingTemplate" => { imported_layout_uses(id, params, &["\"fit\"", "\"show_layer_names\"", "\"columns\""], legends_fitting) } @@ -848,7 +848,7 @@ fn pictures_with_files(e: &rustgis_map::layout::Element) -> bool { fn command_uses_picture_source(id: &str, params: &Value) -> bool { match id { "layout.update" => crate::cmd::layout::gives_picture(params), - // A layout file, read as `layout.import` reads it, which `report.existingTemplate` also does. + // A layout given inline, taken as each of the two commands takes it ([`imported_layout`]). "layout.import" | "report.existingTemplate" => imported_layout_uses(id, params, &["\"source\""], pictures_with_files), "script.run" => params.get("script").and_then(Value::as_str).is_some_and(|s| script_uses(s, command_uses_picture_source)), _ => false, @@ -873,7 +873,7 @@ fn command_uses_section(id: &str, params: &Value) -> bool { match id { "layout.addElement" => params.get("kind").and_then(Value::as_str).is_some_and(|k| k.trim().eq_ignore_ascii_case("section")), "layout.update" => params.get("section").is_some_and(|v| !v.is_null()), - // A layout file, read as `layout.import` reads it, which `report.existingTemplate` also does. + // A layout given inline, taken as each of the two commands takes it ([`imported_layout`]). "layout.import" | "report.existingTemplate" => imported_layout_uses(id, params, &["\"sections\""], |e| e.kind.has_sections()), "script.run" => params.get("script").and_then(Value::as_str).is_some_and(|s| script_uses(s, command_uses_section)), _ => false, @@ -894,7 +894,7 @@ fn command_uses_profile(id: &str, params: &Value) -> bool { PROFILE_ONLY_KEYS.iter().any(|k| params.get(*k).is_some_and(|v| !v.is_null())) || params.get("section").is_some_and(Value::is_string) } - // A layout file, read as `layout.import` reads it, which `report.existingTemplate` also does. + // A layout given inline, taken as each of the two commands takes it ([`imported_layout`]). "layout.import" | "report.existingTemplate" => imported_layout_uses(id, params, &["\"Profile\""], |e| e.kind.is_profile()), "script.run" => params.get("script").and_then(Value::as_str).is_some_and(|s| script_uses(s, command_uses_profile)), _ => false, @@ -1361,7 +1361,7 @@ fn imported_layout_uses(id: &str, params: &Value, keys: &[&str], uses: fn(&rustg /// `text` only, never `data`; `report.existingTemplate` takes a layout of the project named by /// `layout` before any text, and otherwise `data` (base64) before `text`. One it refuses is no /// layout. `data` is decoded for each check, rather than keeping it: only a step that carries it -/// pays, and only on a save. +/// pays, and only on a save. Any other `id` reads none. fn imported_layout(id: &str, params: &Value, keys: &[&str]) -> Option { let text = match id { "layout.import" => crate::cmd::ps(params, "text").map(std::borrow::Cow::Borrowed),