From 9c9641c848381516eba54cd17c24d9af5f7bfca8 Mon Sep 17 00:00:00 2001 From: makesomethingshit Date: Wed, 18 Nov 2026 20:59:05 +0900 Subject: [PATCH] #2694: count data merge markers after IDML reopening --- crates/engine/src/cmd/datamerge.rs | 22 ++++++++++- crates/engine/src/cmd/datamerge/fill.rs | 22 ++++++++++- crates/engine/src/cmd/datamerge/tests.rs | 49 ++++++++++++++++++++++++ 3 files changed, 90 insertions(+), 3 deletions(-) diff --git a/crates/engine/src/cmd/datamerge.rs b/crates/engine/src/cmd/datamerge.rs index 424a7adaf..1adbbcbdc 100644 --- a/crates/engine/src/cmd/datamerge.rs +++ b/crates/engine/src/cmd/datamerge.rs @@ -147,8 +147,26 @@ fn fields_cmd(s: &mut Session, p: &Value) -> Result { let Some(src) = st.doc.data_merge.sources.first() else { return Ok(json!([])); }; - let placeholders = st.doc.data_merge.placeholders.clone(); - Ok(fields_json(&src.fields, |name| placeholders.iter().filter(|ph| ph.field == name).count())) + let placeholders = &st.doc.data_merge.placeholders; + let mut marker_counts = std::collections::HashMap::::new(); + for (story_id, story) in &st.doc.stories { + // The generated endnote frame repeats note text already counted in its owning story. + if Some(*story_id) == st.doc.endnote_story { + continue; + } + for (start, end, name) in fill::find_markers(&story.text) { + // A bound placeholder already accounts for its marker in the usage count. + let covered = placeholders.iter().any(|ph| { + matches!((&ph.role, &ph.anchor), (PlaceholderRole::Text | PlaceholderRole::Image, PlaceholderAnchor::Text { story, start: a, end: b }) + if story == story_id && start < *b && end > *a) + }); + if !covered { + *marker_counts.entry(name).or_default() += 1; + } + } + fill::count_nested_markers(story, &mut marker_counts, 0); + } + Ok(fields_json(&src.fields, |name| placeholders.iter().filter(|ph| ph.field == name).count() + marker_counts.get(name).copied().unwrap_or(0))) } fn placeholder_add(s: &mut Session, p: &Value) -> Result { diff --git a/crates/engine/src/cmd/datamerge/fill.rs b/crates/engine/src/cmd/datamerge/fill.rs index 24d097757..b7d465320 100644 --- a/crates/engine/src/cmd/datamerge/fill.rs +++ b/crates/engine/src/cmd/datamerge/fill.rs @@ -417,6 +417,26 @@ fn fill_inner(st: &mut Story, fields: &[DataField], row: &[String], depth: usize } } +/// Count markers in nested text to match the merge's depth limit. +pub(super) fn count_nested_markers(st: &Story, counts: &mut HashMap, depth: usize) { + if depth >= MAX_NESTED_DEPTH { + return; + } + let mut count = |inner: &Story| { + for (_, _, name) in find_markers(&inner.text) { + *counts.entry(name).or_default() += 1; + } + count_nested_markers(inner, counts, depth + 1); + }; + for table in st.tables.values() { + for cell in &table.cells { + count(&cell.text); + } + } + for note in st.notes.iter().chain(st.endnotes.iter()) { + count(¬e.text); + } +} /// Replace the markers of known fields in `st`'s own text with the record's values. fn fill_own(st: &mut Story, fields: &[DataField], row: &[String]) { let found: Vec<(usize, usize, String)> = @@ -426,7 +446,7 @@ fn fill_own(st: &mut Story, fields: &[DataField], row: &[String]) { } } -fn find_markers(text: &str) -> Vec<(usize, usize, String)> { +pub(super) fn find_markers(text: &str) -> Vec<(usize, usize, String)> { let mut out = Vec::new(); let mut i = 0; // The next `>>` at or after the `<<` being looked at: found once, kept until passed, so the diff --git a/crates/engine/src/cmd/datamerge/tests.rs b/crates/engine/src/cmd/datamerge/tests.rs index e631b69ea..cda4f2072 100644 --- a/crates/engine/src/cmd/datamerge/tests.rs +++ b/crates/engine/src/cmd/datamerge/tests.rs @@ -1654,6 +1654,7 @@ fn a_field_converted_into_a_table_is_filled_in_its_cell() { exec(&mut s, "text.select", json!({"story": made["story"], "anchor": 0, "focus": 13})); exec(&mut s, "table.convertFromText", json!({"columnSeparator": "tab"})); assert_eq!(cell_texts(&s), ["Name", "<>"]); + assert_eq!(exec(&mut s, "data.fields", json!({}))[0]["uses"], 1); exec(&mut s, "data.preview", json!({"record": 1})); assert_eq!(cell_texts(&s), ["Name", "Ada"], "the field is filled in its cell"); } @@ -1745,6 +1746,7 @@ fn a_field_in_an_endnote_is_filled() { s.documents()[s.active_index().unwrap()].doc.stories.values().flat_map(|st| st.endnotes.iter().map(|e| e.text.text.clone())).collect() }; assert_eq!(endnotes(&s), ["From <>"]); + assert_eq!(exec(&mut s, "data.fields", json!({}))[1]["uses"], 1); exec(&mut s, "data.preview", json!({"record": 1})); assert_eq!(endnotes(&s), ["From London"]); } @@ -1799,3 +1801,50 @@ fn select_records_stops_expanding_once_the_limit_is_met_but_still_checks_every_r assert!(parse::select_records(10, "range", 1, "1-10,5-11", Some(2)).is_err()); assert!(parse::select_records(10, "range", 1, "1-2,0", Some(2)).is_err()); } + +#[test] +fn idml_reopen_relink_counts_existing_text_field() { + let dir = scratch("idml-field-count"); + let csv = dir.join("people.csv"); + let idml = dir.join("template.idml"); + write(&csv, "Name\nAda\n"); + let mut s = Session::new(); + new_doc(&mut s, 1); + text_frame(&mut s, [36.0, 36.0, 240.0, 80.0], "", None); + exec(&mut s, "data.source.select", json!({"path": csv})); + exec(&mut s, "data.placeholder.add", json!({"field": "Name"})); + assert_eq!(exec(&mut s, "data.fields", json!({}))[0]["uses"], 1); + exec(&mut s, "file.exportIdml", json!({"path": idml})); + let mut reopened = Session::new(); + exec(&mut reopened, "file.openIdml", json!({"path": idml})); + exec(&mut reopened, "data.source.select", json!({"path": csv})); + let uses = exec(&mut reopened, "data.fields", json!({}))[0]["uses"].clone(); + exec(&mut reopened, "data.merge", json!({})); + assert!(texts(&reopened).contains(&"Ada".to_string())); + assert_eq!(uses, 1); +} + +#[test] +fn fields_count_unbound_markers_without_double_counting_bound_placeholders() { + let mut s = Session::new(); + new_doc(&mut s, 1); + select_csv(&mut s, "Name\nAda\n"); + text_frame(&mut s, [36.0, 36.0, 240.0, 80.0], "<> <>", None); + text_frame(&mut s, [36.0, 100.0, 240.0, 150.0], "", None); + exec(&mut s, "data.placeholder.add", json!({"field": "Name"})); + assert_eq!(exec(&mut s, "data.fields", json!({}))[0]["uses"], 3); + assert_eq!(exec(&mut s, "data.fields", json!({"csv": "Name\nAda\n"}))[0]["uses"], 0); +} + +#[test] +fn hyperlink_over_marker_does_not_hide_its_field_usage() { + let mut s = Session::new(); + new_doc(&mut s, 1); + select_csv(&mut s, "Name,Link\nAda,https://example.com\n"); + let made = text_frame(&mut s, [36.0, 36.0, 240.0, 80.0], "<>", None); + exec(&mut s, "text.select", json!({"story": made["story"], "anchor": 0, "focus": 8})); + exec(&mut s, "data.placeholder.add", json!({"field": "Link", "role": "hyperlink"})); + let fields = exec(&mut s, "data.fields", json!({})); + assert_eq!(fields[0]["uses"], 1); + assert_eq!(fields[1]["uses"], 1); +}