From 09938cd99e9189890ace95006de4c6fc2211e468 Mon Sep 17 00:00:00 2001 From: Mauro Mazzieri Date: Tue, 17 Nov 2026 15:53:20 +0000 Subject: [PATCH 1/2] craft-rtf: page fields show each page's number Ported from storytold/wordcraft#6264 (issue storytold/wordcraft#6222), written before the RTF reader moved here. A PAGE, NUMPAGES, SECTIONPAGES, SECTION or PAGEREF field whose result is plain text in one formatting comes in as a field, with its lock, so the layout works out the number; \chpgn is a PAGE field and keeps a tracked insertion or deletion; a result whose text was hidden stays as read. --- crates/flow/src/to_doc.rs | 23 ++-- crates/rtf/src/doc.rs | 8 +- crates/rtf/src/doc/field.rs | 144 ++++++++++++++++++++ crates/rtf/src/doc/reader.rs | 6 + crates/rtf/src/doc/tests_fields.rs | 208 +++++++++++++++++++++++++++++ crates/rtf/src/doc/text.rs | 36 +++-- crates/rtf/src/doc/word.rs | 8 +- 7 files changed, 409 insertions(+), 24 deletions(-) create mode 100644 crates/rtf/src/doc/field.rs create mode 100644 crates/rtf/src/doc/tests_fields.rs diff --git a/crates/flow/src/to_doc.rs b/crates/flow/src/to_doc.rs index 0375c448c..8d9d4dca3 100644 --- a/crates/flow/src/to_doc.rs +++ b/crates/flow/src/to_doc.rs @@ -284,6 +284,18 @@ impl<'a> Builder<'a> { /// `cp` as a run may keep it: without its link and font once the runs' copies of them are /// over budget. + /// A run's properties with its tracked insertion, deletion and formatting change. + fn tracked_props(&mut self, f: &Fmt) -> CharProps { + let mut cp = self.budget(char_props(f)); + cp.ins = f.ins.as_ref().map(|m| self.revision(RevisionKind::Insert, m)); + cp.del = f.del.as_ref().map(|m| self.revision(RevisionKind::Delete, m)); + if let Some(c) = &f.fmt_change { + let old = self.budget(char_props(&c.old)); + cp.fmt_change = PropChange::boxed(self.revision(RevisionKind::Format, &c.mark), old); + } + cp + } + fn budget(&mut self, mut cp: CharProps) -> CharProps { let n = cp.link.as_ref().map_or(0, String::len).saturating_add(cp.font.as_ref().map_or(0, String::len)); match self.run_strings.checked_add(n).filter(|&t| t <= MAX_RUN_STRINGS) { @@ -553,13 +565,7 @@ impl<'a> Builder<'a> { match i { Inline::Text(t, f) => { let t = clean_text(t); - let mut cp = self.budget(char_props(f)); - cp.ins = f.ins.as_ref().map(|m| self.revision(RevisionKind::Insert, m)); - cp.del = f.del.as_ref().map(|m| self.revision(RevisionKind::Delete, m)); - if let Some(c) = &f.fmt_change { - let old = self.budget(char_props(&c.old)); - cp.fmt_change = PropChange::boxed(self.revision(RevisionKind::Format, &c.mark), old); - } + let mut cp = self.tracked_props(f); if p.kind == Kind::Code { // Code's font comes from its style, before a change as after it. if cp.font.as_deref() == Some(MONO_FONT) { @@ -633,7 +639,8 @@ impl<'a> Builder<'a> { } } Inline::Field { instr, result, fmt, locked } => { - let cp = self.budget(char_props(fmt)); + // A field inserted or deleted with tracked changes keeps them (RTF `\chpgn`). + let cp = self.tracked_props(fmt); let (instr, result) = (clean_text(instr), clean_text(result)); if instr.trim().is_empty() { append(&mut out, &result, &cp, None); diff --git a/crates/rtf/src/doc.rs b/crates/rtf/src/doc.rs index 77254151f..0426a4b7a 100644 --- a/crates/rtf/src/doc.rs +++ b/crates/rtf/src/doc.rs @@ -22,7 +22,10 @@ //! code page's bytes stream through one decoder, so a lead byte and its trail byte decode //! together and a run that looks like a UTF-8 BOM is not mistaken for one; the formatting words //! above, stylesheet-based -//! headings and `\outlinelevel`, lists through `\ls`/`\ilvl`, fields, bookmarks, pictures and +//! headings and `\outlinelevel`, lists through `\ls`/`\ilvl`, fields (in `import`, a page field +//! such as `PAGE` or `NUMPAGES` whose result is plain text in one paragraph stays a field, +//! `\fldlock` locking it, and `\chpgn` is a `PAGE` field; otherwise a field is its result's text), +//! bookmarks, pictures and //! tables, footnotes, endnotes (`\footnote` with `\ftnalt`) and comments (author, initials, date, //! text, the commented range). Group nesting, picture sizes and the number of notes and comments //! are capped. Not kept: custom note marks, comment replies (`\atnparent`), notes inside @@ -38,6 +41,7 @@ use craft_flow::{self as model, Cell, FBlock, FTable, Flow, Fmt, Inline, Kind, L use crate::date; mod export; +mod field; mod parse; mod reader; mod state; @@ -58,3 +62,5 @@ use word::hyperlink; #[cfg(test)] mod tests; +#[cfg(test)] +mod tests_fields; diff --git a/crates/rtf/src/doc/field.rs b/crates/rtf/src/doc/field.rs new file mode 100644 index 000000000..78d5f7d81 --- /dev/null +++ b/crates/rtf/src/doc/field.rs @@ -0,0 +1,144 @@ +//! Page fields kept as fields (#6222). The reader writes a field's result (`\fldrslt`) into the +//! paragraph like any text, which for a page number is the number Word last stored. When the +//! group of a field the layout works out from where it lands closes (`PAGE`, `NUMPAGES`, +//! `SECTIONPAGES`, `SECTION`, `PAGEREF`), a result that is plain text in one formatting, in the +//! paragraph the field started in, is replaced by an [`Inline::Field`] holding the instruction +//! (`\fldinst`) and that result, as for a DOCX field. Any other field, a result over several +//! paragraphs or formats, or one holding pictures or other objects stays what the reader made of +//! it: a field's result is drawn as one piece that does not wrap, so a long `MERGEFIELD` result +//! in a narrow table cell would overflow it. Only `import` keeps fields: `parse`, the flow +//! reader, reads their results as text. + +use super::reader::Reader; +use super::state::Dest; +use super::{Fmt, Inline}; + +/// The open fields read so far; deeper nesting keeps its results as text. +const MAX_OPEN: usize = 32; + +/// A field being read in the body text. +pub(super) struct OpenField { + /// The depth of the `{\field` group. + depth: usize, + /// Its instruction, in [`Reader::fields`]. + index: usize, + /// The paragraph it started in ([`Reader::para_seq`]). + para: u64, + /// The paragraph's inlines when it started, and the length of the last one when that was + /// text: a result in the same formatting runs on in that text. + start: usize, + tail_from: Option, + /// Its result group was met in the text (not inside another field's instruction). + result: bool, + locked: bool, + /// Hidden text of its result was left out: the result is not what showed. + hidden: bool, +} + +impl Reader { + /// `\field`, after its instruction slot is made: note where its result starts. + pub(super) fn open_field(&mut self) { + // Hidden text drops the result: the field stays hidden with it. + if !self.marks_on || self.st.dest != Dest::Body || self.st.hidden || self.open_fields.len() >= MAX_OPEN { + return; + } + let Some(index) = self.st.field else { return }; + let tail_from = match self.para.inlines.last() { + Some(Inline::Text(t, _)) => Some(t.len()), + _ => None, + }; + let (depth, para, start) = (self.depth(), self.para_seq, self.para.inlines.len()); + self.open_fields.push(OpenField { depth, index, para, start, tail_from, result: false, locked: false, hidden: false }); + } + + /// `\fldrslt` and `\fldlock`, read in a field's group. + pub(super) fn field_word(&mut self, w: &str) { + let (depth, field, body) = (self.depth(), self.st.field, self.st.dest == Dest::Body); + let Some(f) = self.open_fields.last_mut() else { return }; + match w { + "fldrslt" if body && field == Some(f.index) && depth == f.depth + 1 => f.result = true, + "fldlock" if depth == f.depth => f.locked = true, + _ => {} + } + } + + /// `\chpgn`, the current page number: a `PAGE` field. + pub(super) fn page_number_symbol(&mut self) { + if self.marks_on && self.st.dest == Dest::Body && !self.st.hidden { + // Tracked like text, so Accept and Reject apply to it. + let fmt = self.text_fmt(); + self.para.inlines.push(Inline::Field { instr: "PAGE".into(), result: String::new(), fmt, locked: false }); + self.para_has = true; + } + } + + /// Hidden text left out of the body: an open field's result is then not what showed, and the + /// field is left as read rather than made a field that would show. + pub(super) fn hidden_text_dropped(&mut self) { + if let Some(f) = self.open_fields.last_mut() + && f.result + { + f.hidden = true; + } + } + + /// A group closes: if it is an open field's, make the field. + pub(super) fn close_field_group(&mut self) { + if self.open_fields.last().is_none_or(|f| f.depth != self.depth()) { + return; + } + let Some(f) = self.open_fields.pop() else { return }; + if !f.result || f.hidden || f.para != self.para_seq { + return; + } + let instr = self.fields.get(f.index).map_or("", |s| s.trim()); + if !page_dependent(instr) { + return; + } + let Some((result, fmt)) = result_text(&self.para.inlines, &f, &self.st.fmt) else { return }; + let field = Inline::Field { instr: instr.to_string(), result, fmt, locked: f.locked }; + let inlines = &mut self.para.inlines; + inlines.truncate(f.start); + if let (Some(n), Some(Inline::Text(t, _))) = (f.tail_from, inlines.last_mut()) { + t.truncate(n); + } + inlines.push(field); + self.para_has = true; + } +} + +/// A field whose result the layout works out from the page it lands on. `STYLEREF` is not one +/// here: the layout looks its style up by name, and a running head whose style the RTF names +/// differently would show an error instead of Word's text. +fn page_dependent(instr: &str) -> bool { + let name = instr.split_whitespace().next().unwrap_or(""); + ["PAGE", "NUMPAGES", "SECTIONPAGES", "SECTION", "PAGEREF"].iter().any(|n| name.eq_ignore_ascii_case(n)) +} + +/// The text the field's result added to the paragraph and its formatting, when that is plain +/// text in one formatting with no tracked change; `None` otherwise. An empty result takes the +/// field group's formatting. +fn result_text(inlines: &[Inline], f: &OpenField, group: &Fmt) -> Option<(String, Fmt)> { + if inlines.len() < f.start { + return None; + } + let mut runs: Vec<(&str, &Fmt)> = Vec::new(); + if let Some(n) = f.tail_from { + let Some(Inline::Text(t, fmt)) = f.start.checked_sub(1).and_then(|i| inlines.get(i)) else { return None }; + let tail = t.get(n..)?; + if !tail.is_empty() { + runs.push((tail, fmt)); + } + } + for i in inlines.get(f.start..)? { + match i { + Inline::Text(t, fmt) => runs.push((t, fmt)), + _ => return None, + } + } + match runs.as_slice() { + [] => Some((String::new(), group.clone())), + [(t, fmt)] if fmt.ins.is_none() && fmt.del.is_none() && fmt.fmt_change.is_none() => Some(((*t).to_string(), (*fmt).clone())), + _ => None, + } +} diff --git a/crates/rtf/src/doc/reader.rs b/crates/rtf/src/doc/reader.rs index c0cc1c1ea..4c8a8b652 100644 --- a/crates/rtf/src/doc/reader.rs +++ b/crates/rtf/src/doc/reader.rs @@ -56,6 +56,10 @@ pub(super) struct Reader { pub(super) ov_list: i32, pub(super) ov_ls: i32, pub(super) fields: Vec, + /// The fields being read in the text, innermost last (`import` only). + pub(super) open_fields: Vec, + /// Counts the paragraphs ended, so a field knows whether its result stayed in one. + pub(super) para_seq: u64, // Picture being read. pub(super) pict_depth: Option, pub(super) pict_hex: Vec, @@ -344,6 +348,7 @@ impl Reader { if self.sub.as_ref().is_some_and(|s| s.depth == self.depth()) { self.end_sub(); } + self.close_field_group(); let closing = self.st.dest; // Destination-level commits. match closing { @@ -428,6 +433,7 @@ impl Reader { /// End the current paragraph (`\par`, `\cell`). pub(super) fn end_para(&mut self, in_cell: bool) { + self.para_seq += 1; let read = std::mem::take(&mut self.para); let props = self.para_props(&self.st); let mut p = Para { inlines: read.inlines, mark_ins: read.mark_ins, mark_del: read.mark_del, ..props }; diff --git a/crates/rtf/src/doc/tests_fields.rs b/crates/rtf/src/doc/tests_fields.rs new file mode 100644 index 000000000..ae1ab691f --- /dev/null +++ b/crates/rtf/src/doc/tests_fields.rs @@ -0,0 +1,208 @@ +//! Page fields read from RTF (#6222): a page-dependent field whose result is plain text in one +//! paragraph comes in as a field, so the layout works out its number; other fields stay their +//! result's text. Written by hand from the RTF specification's `\field`, `\fldinst`, `\fldrslt`, +//! `\fldlock` and `\chpgn`. + +use super::*; +use craft_wordmodel::para::InlineObject; + +/// The fields of each paragraph of a story, with the paragraph's text. +/// A paragraph's text with its fields (code, result, locked). +type ParaFields = (String, Vec<(String, String, bool)>); + +fn fields_of(blocks: &craft_wordmodel::Blocks) -> Vec { + blocks + .iter() + .filter_map(|b| if let craft_wordmodel::Block::Para(p) = &**b { Some(p) } else { None }) + .map(|p| { + let fields = p + .objects + .iter() + .filter_map(|o| match o { + InlineObject::Field { instr, result, locked, .. } => Some((instr.clone(), result.clone(), *locked)), + _ => None, + }) + .collect(); + (p.text.clone(), fields) + }) + .collect() +} + +fn footer(d: &craft_wordmodel::Document) -> Vec { + let id = d.last_section.footers.default.expect("a footer"); + fields_of(&d.parts[&id].blocks) +} + +#[test] +fn a_page_field_in_the_footer_is_a_field_not_its_stored_number() { + // As Word writes it: the text before the field has the same formatting as its result. + let rtf = br"{\rtf1\ansi\sectd +{\footerr \pard {\fs16 Pag. }{\field{\*\fldinst {\fs16 PAGE }}{\fldrslt {\fs16 2}}}\sectd \ltrsect{\par}} +\pard One\page Two\par}"; + let d = import(rtf).unwrap(); + let f = footer(&d); + assert_eq!(f.len(), 1, "{f:?}"); + assert_eq!(f[0].1, [("PAGE".to_string(), "2".to_string(), false)], "{f:?}"); + // "Pag. " stays text before the field: the result's run did not take it along. + assert!(f[0].0.starts_with("Pag. "), "{f:?}"); + assert_eq!(f[0].0.matches("Pag. ").count(), 1, "{f:?}"); +} + +#[test] +fn page_fields_in_the_body_keep_their_code_result_and_lock() { + let rtf = br#"{\rtf1\ansi +\pard Page {\field{\*\fldinst PAGE \\* roman}{\fldrslt ii}} of {\field\fldlock{\*\fldinst NUMPAGES}{\fldrslt 4}}\par +\pard {\field{\*\fldinst SECTIONPAGES}{\fldrslt }}x\par}"#; + let d = import(rtf).unwrap(); + let f = fields_of(&d.body); + assert_eq!(f[0].1, [("PAGE \\* roman".to_string(), "ii".to_string(), false), ("NUMPAGES".to_string(), "4".to_string(), true)], "{f:?}"); + assert!(f[0].0.starts_with("Page ") && f[0].0.contains(" of "), "{f:?}"); + // An empty result is still a field: the layout fills it in. + assert_eq!(f[1].1, [("SECTIONPAGES".to_string(), String::new(), false)], "{f:?}"); +} + +#[test] +fn other_fields_stay_the_text_of_their_result() { + // A field's result is drawn as one piece that does not wrap: a long merge field in a narrow + // cell keeps its text, which wraps, as before. + let rtf = br#"{\rtf1\ansi +\pard Before {\field{\*\fldinst DATE \\@ "dd/MM/yyyy"}{\fldrslt 11/11/2026}} after\par +\trowd\cellx600\pard\intbl {\field{\*\fldinst MERGEFIELD Approvato}{\fldrslt Presidente della Rete (Rossi Mario)}}\cell\row +\pard {\field{\*\fldinst AUTHOR}{\fldrslt }}x\par}"#; + let d = import(rtf).unwrap(); + let f = fields_of(&d.body); + assert_eq!((f[0].0.as_str(), f[0].1.len()), ("Before 11/11/2026 after", 0), "{f:?}"); + let cell = d.body.iter().find_map(|b| b.as_table()).and_then(|t| t.rows.first()).and_then(|r| r.cells.first()).unwrap(); + let cf = fields_of(&cell.blocks); + assert_eq!((cf[0].0.as_str(), cf[0].1.len()), ("Presidente della Rete (Rossi Mario)", 0), "{cf:?}"); + assert!(f.last().is_some_and(|(t, fl)| t == "x" && fl.is_empty()), "{f:?}"); +} + +#[test] +fn fields_that_cannot_be_one_field_stay_text() { + let rtf = br#"{\rtf1\ansi +\pard {\field{\*\fldinst{HYPERLINK "http://a.b"}}{\fldrslt{link}}}\par +\pard {\field{\*\fldinst TOC \\o "1-3"}{\fldrslt {Heading one\tab 1\par Heading two\tab 2}}}\par +\pard {\field{\*\fldinst PAGEREF a}{\fldrslt {\b 1}2}}\par +\pard {\field{\*\fldinst IF {\field{\*\fldinst PAGE}{\fldrslt 1}} = 1 "one" "many"}{\fldrslt one}}\par +\pard {\field{\*\fldinst PAGE}}\par +\pard {\field{\*\fldinst PAGE}{\fldrslt 1\par 2}}\par}"#; + let d = import(rtf).unwrap(); + let f = fields_of(&d.body); + let texts: Vec<&str> = f.iter().map(|(t, _)| t.as_str()).collect(); + // A link stays a link; a result over two paragraphs or in two formats stays its text. + assert_eq!(&texts[..4], ["link", "Heading one\t1", "Heading two\t2", "12"], "{f:?}"); + // The PAGE inside IF's instruction is part of the code, not a field of the paragraph. + assert_eq!(texts[4], "one", "{f:?}"); + // A field with no result group at all is left as it was (nothing). + assert!(f[5].0.is_empty(), "{f:?}"); + assert_eq!(&texts[6..8], ["1", "2"], "{f:?}"); + assert!(f.iter().all(|(_, fl)| fl.is_empty()), "{f:?}"); +} + +#[test] +fn a_table_of_contents_keeps_its_links_and_its_page_numbers_become_fields() { + // The shape Word writes: the TOC result spans paragraphs; each entry is a link around its + // text and a PAGEREF to the heading's bookmark. + let rtf = br#"{\rtf1\ansi +\pard {\field{\*\fldinst TOC \\o "1-3" \\h}{\fldrslt {\field{\*\fldinst HYPERLINK \\l "_Toc1"}{\fldrslt {One\tab {\field{\*\fldinst PAGEREF _Toc1 \\h}{\fldrslt 3}}}}}\par +{\field{\*\fldinst HYPERLINK \\l "_Toc2"}{\fldrslt {Two\tab {\field{\*\fldinst PAGEREF _Toc2 \\h}{\fldrslt 5}}}}}}}\par +\pard {\*\bkmkstart _Toc1}Heading{\*\bkmkend _Toc1}\par}"#; + let d = import(rtf).unwrap(); + let f = fields_of(&d.body); + assert_eq!(f[0].1, [("PAGEREF _Toc1 \\h".to_string(), "3".to_string(), false)], "{f:?}"); + assert_eq!(f[1].1, [("PAGEREF _Toc2 \\h".to_string(), "5".to_string(), false)], "{f:?}"); + assert!(f[0].0.starts_with("One\t") && f[1].0.starts_with("Two\t"), "{f:?}"); + // The entry text still links to its heading. + let p = d.body[0].as_para().unwrap(); + assert!(p.props_at(0).link.is_some(), "{:?}", p.runs); +} + +#[test] +fn hidden_styleref_tracked_and_object_results_stay_as_read() { + let rtf = br#"{\rtf1\ansi{\*\revtbl{Unknown;}{Ann;}} +\pard a{\v {\field{\*\fldinst PAGE}{\fldrslt 2}}}b\par +\pard {\field{\*\fldinst STYLEREF "heading 1"}{\fldrslt Intro}}\par +\pard {\field{\*\fldinst PAGE}{\fldrslt {\revised\revauth1 2}}}\par +\pard {\field{\*\fldinst PAGE}{\fldrslt A{\*\bkmkstart m}B{\*\bkmkend m}}}\par}"#; + let d = import(rtf).unwrap(); + let f = fields_of(&d.body); + // Hidden: neither the result nor a field. + assert_eq!((f[0].0.as_str(), f[0].1.len()), ("ab", 0), "{f:?}"); + // STYLEREF keeps Word's text. + assert_eq!((f[1].0.as_str(), f[1].1.len()), ("Intro", 0), "{f:?}"); + // A tracked result and one holding a bookmark stay text. + assert_eq!((f[2].0.as_str(), f[2].1.len()), ("2", 0), "{f:?}"); + // (The bookmark's start and end are object characters in the paragraph's text.) + assert_eq!((f[3].0.replace('\u{FFFC}', ""), f[3].1.len()), ("AB".to_string(), 0), "{f:?}"); +} + +#[test] +fn the_page_number_symbol_is_a_page_field() { + let d = import(br"{\rtf1\ansi{\footer \pard Page \chpgn\par}\pard Body\par}").unwrap(); + let f = footer(&d); + assert_eq!(f[0].1, [("PAGE".to_string(), String::new(), false)], "{f:?}"); +} + +#[test] +fn truncated_and_hostile_fields_do_not_crash() { + for rtf in [ + &br"{\rtf1{\field{\*\fldinst PAGE}{\fldrslt 2"[..], + br"{\rtf1{\field{\*\fldinst PAGE}{\fldrslt 2}", + br"{\rtf1{\field\field\field{\*\fldinst PAGE}}}}", + br"{\rtf1{\field{\fldrslt x}{\*\fldinst PAGE}}\par}", + br"{\rtf1 a{\field{\*\fldinst PAGE}{\fldrslt 1\par 2}}\par}", + ] { + let _ = import(rtf); + } + let mut deep = String::from(r"{\rtf1 "); + for _ in 0..300 { + deep.push_str(r"{\field{\*\fldinst PAGE}{\fldrslt "); + } + deep.push('x'); + let _ = import(deep.as_bytes()); +} + +#[test] +fn the_flow_reader_keeps_field_results_as_text() { + // `parse` asks for no fields, as before: the result is the paragraph's text. + let f = parse(br"{\rtf1\ansi\pard Pag. {\field{\*\fldinst PAGE}{\fldrslt 2}}\par}").unwrap(); + match &f.blocks[0] { + FBlock::Para(p) => { + assert!(p.inlines.iter().all(|i| matches!(i, Inline::Text(..))), "{:?}", p.inlines); + assert_eq!(p.text(), "Pag. 2"); + } + b => panic!("{b:?}"), + } +} + +#[test] +fn a_tracked_page_symbol_keeps_its_insertion_and_deletion() { + let rtf = br"{\rtf1\ansi{\*\revtbl{Unknown;}{Ann;}} +\pard Page {\revised\revauth1 \chpgn}\par +\pard Page {\deleted\revauthdel1 \chpgn}\par}"; + let d = import(rtf).unwrap(); + let f = fields_of(&d.body); + assert_eq!(f[0].1.len(), 1, "{f:?}"); + assert_eq!(f[1].1.len(), 1, "{f:?}"); + // Each field sits in a run carrying its tracked change, so Accept and Reject apply to it. + let marked = |i: usize, ins: bool| { + let p = d.body[i].as_para().unwrap(); + p.runs.iter().any(|r| if ins { r.props.ins.is_some() } else { r.props.del.is_some() }) + }; + assert!(marked(0, true), "{:?}", d.body[0].as_para().unwrap().runs); + assert!(marked(1, false), "{:?}", d.body[1].as_para().unwrap().runs); +} + +#[test] +fn a_result_whose_text_is_hidden_inside_it_stays_as_read() { + // `\v` inside the result: what showed was nothing, so no field that would show a number. + let rtf = br"{\rtf1\ansi +\pard x{\field{\*\fldinst PAGE}{\fldrslt \v 2}}y\par +\pard x{\field{\*\fldinst PAGE}{\fldrslt }}y\par}"; + let d = import(rtf).unwrap(); + let f = fields_of(&d.body); + assert_eq!((f[0].0.as_str(), f[0].1.len()), ("xy", 0), "{f:?}"); + // A result that is really empty is still a field. + assert_eq!(f[1].1.len(), 1, "{f:?}"); +} diff --git a/crates/rtf/src/doc/text.rs b/crates/rtf/src/doc/text.rs index dceaef6d0..ad997c680 100644 --- a/crates/rtf/src/doc/text.rs +++ b/crates/rtf/src/doc/text.rs @@ -189,31 +189,39 @@ impl Reader { } } + /// The current formatting with its tracked changes (insertion, deletion, formatting change). + pub(super) fn text_fmt(&mut self) -> Fmt { + let mut f = self.st.fmt.clone(); + let r = self.st.rev; + if r.ins { + f.ins = Some(self.rev_mark(r.auth, r.dttm)); + } + if r.del { + f.del = Some(self.rev_mark(r.authdel, r.dttmdel)); + } + let c = &self.st.chg_c; + if let Some(words) = c.old.clone() { + let mark = self.rev_mark(c.auth, c.date); + let old = self.old_fmt(&words); + f.fmt_change = Some(Box::new(model::FmtChange { mark, old })); + } + f + } + pub(super) fn text(&mut self, s: &str) { match self.st.dest { Dest::Body => { if self.st.hidden { + self.hidden_text_dropped(); return; } - let mut f = self.st.fmt.clone(); if s.contains([MARK_OPEN, MARK_CLOSE]) { + let f = self.st.fmt.clone(); self.para.push_text(&s.replace([MARK_OPEN, MARK_CLOSE], "\u{FFFD}"), &f); self.para_has = true; return; } - let r = self.st.rev; - if r.ins { - f.ins = Some(self.rev_mark(r.auth, r.dttm)); - } - if r.del { - f.del = Some(self.rev_mark(r.authdel, r.dttmdel)); - } - let c = &self.st.chg_c; - if let Some(words) = c.old.clone() { - let mark = self.rev_mark(c.auth, c.date); - let old = self.old_fmt(&words); - f.fmt_change = Some(Box::new(model::FmtChange { mark, old })); - } + let f = self.text_fmt(); self.para.push_text(s, &f); self.para_has = true; } diff --git a/crates/rtf/src/doc/word.rs b/crates/rtf/src/doc/word.rs index 2eb86692f..49367a7d1 100644 --- a/crates/rtf/src/doc/word.rs +++ b/crates/rtf/src/doc/word.rs @@ -75,6 +75,7 @@ impl Reader { "row" => self.end_row(), "line" | "softline" => self.ch('\n'), "tab" => self.ch('\t'), + "chpgn" => self.page_number_symbol(), "page" | "softpage" => self.ch('\u{000C}'), "column" => self.ch('\u{000E}'), "emdash" => self.ch('—'), @@ -283,8 +284,13 @@ impl Reader { self.defs.push(d); } } - "field" => self.start_field(), + "field" => { + self.start_field(); + self.open_field(); + } + "fldlock" => self.field_word(w), "fldrslt" => { + self.field_word(w); let link = self.st.field.and_then(|i| self.fields.get(i)).and_then(|s| hyperlink(s)); if link.is_some() { self.st.fmt.link = link.as_deref().and_then(model::kept_link); From 133b15453c0d695db0245776c46feb045e605693 Mon Sep 17 00:00:00 2001 From: "Dylan M. Taylor" Date: Wed, 18 Nov 2026 01:21:44 -0500 Subject: [PATCH 2/2] fix(rtf): retain hidden-result and empty-field revision state --- crates/rtf/src/doc/field.rs | 22 +++++++++++++++------- crates/rtf/src/doc/tests_fields.rs | 27 +++++++++++++++++++++++++++ 2 files changed, 42 insertions(+), 7 deletions(-) diff --git a/crates/rtf/src/doc/field.rs b/crates/rtf/src/doc/field.rs index 78d5f7d81..ccc12f4cf 100644 --- a/crates/rtf/src/doc/field.rs +++ b/crates/rtf/src/doc/field.rs @@ -64,7 +64,11 @@ impl Reader { /// `\chpgn`, the current page number: a `PAGE` field. pub(super) fn page_number_symbol(&mut self) { - if self.marks_on && self.st.dest == Dest::Body && !self.st.hidden { + if self.st.dest == Dest::Body && self.st.hidden { + self.hidden_text_dropped(); + return; + } + if self.marks_on && self.st.dest == Dest::Body { // Tracked like text, so Accept and Reject apply to it. let fmt = self.text_fmt(); self.para.inlines.push(Inline::Field { instr: "PAGE".into(), result: String::new(), fmt, locked: false }); @@ -75,10 +79,11 @@ impl Reader { /// Hidden text left out of the body: an open field's result is then not what showed, and the /// field is left as read rather than made a field that would show. pub(super) fn hidden_text_dropped(&mut self) { - if let Some(f) = self.open_fields.last_mut() - && f.result - { - f.hidden = true; + // An omitted nested result is also omitted from each enclosing result. + for f in &mut self.open_fields { + if f.result { + f.hidden = true; + } } } @@ -95,8 +100,11 @@ impl Reader { if !page_dependent(instr) { return; } - let Some((result, fmt)) = result_text(&self.para.inlines, &f, &self.st.fmt) else { return }; - let field = Inline::Field { instr: instr.to_string(), result, fmt, locked: f.locked }; + let instr = instr.to_string(); + // An empty result has no run to carry the group's revision marks. + let group = self.text_fmt(); + let Some((result, fmt)) = result_text(&self.para.inlines, &f, &group) else { return }; + let field = Inline::Field { instr, result, fmt, locked: f.locked }; let inlines = &mut self.para.inlines; inlines.truncate(f.start); if let (Some(n), Some(Inline::Text(t, _))) = (f.tail_from, inlines.last_mut()) { diff --git a/crates/rtf/src/doc/tests_fields.rs b/crates/rtf/src/doc/tests_fields.rs index ae1ab691f..4931356e8 100644 --- a/crates/rtf/src/doc/tests_fields.rs +++ b/crates/rtf/src/doc/tests_fields.rs @@ -206,3 +206,30 @@ fn a_result_whose_text_is_hidden_inside_it_stays_as_read() { // A result that is really empty is still a field. assert_eq!(f[1].1.len(), 1, "{f:?}"); } + +#[test] +fn hidden_page_symbols_and_nested_results_do_not_reappear_as_fields() { + for result in [r"{\v\chpgn}", r"{\field{\*\fldinst DATE}{\fldrslt {\v 2}}}"] { + let rtf = format!(r"{{\rtf1\ansi\pard x{{\field{{\*\fldinst PAGE}}{{\fldrslt {result}}}}}y\par}}"); + let d = import(rtf.as_bytes()).unwrap(); + let f = fields_of(&d.body); + assert_eq!(f[0], ("xy".into(), Vec::new()), "{result}: {f:?}"); + } + // A visible nested result remains eligible for conversion by its enclosing PAGE field. + let d = import(br"{\rtf1\ansi{\field{\*\fldinst PAGE}{\fldrslt {\field{\*\fldinst DATE}{\fldrslt 2}}}}\par}").unwrap(); + assert_eq!(fields_of(&d.body)[0].1, [("PAGE".into(), "2".into(), false)]); +} + +#[test] +fn empty_page_fields_keep_insertion_and_deletion_marks() { + for (mark, insertion) in [(r"\revised\revauth1", true), (r"\deleted\revauthdel1", false)] { + let rtf = format!(r"{{\rtf1\ansi{{\*\revtbl{{Unknown;}}{{Ann;}}}}\pard {{{mark}{{\field{{\*\fldinst PAGE}}{{\fldrslt }}}}}}\par}}"); + let d = import(rtf.as_bytes()).unwrap(); + let fields = fields_of(&d.body); + assert_eq!(fields[0].1, [("PAGE".into(), String::new(), false)]); + let p = d.body[0].as_para().unwrap(); + assert_eq!(p.text, "\u{FFFC}", "the sole character is the field marker"); + let props = p.props_at(0); + assert!(if insertion { props.ins.is_some() } else { props.del.is_some() }, "{mark}: {props:?}"); + } +}