mirror of
https://github.com/storytold/craft-libs.git
synced 2026-11-18 16:24:38 +00:00
fix(rtf): retain hidden-result and empty-field revision state
This commit is contained in:
1 parent
09938cd99e
commit
133b15453c
2 files changed
+42
-7
No files matched your search
@@ -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()) {
|
||||
|
||||
@@ -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:?}");
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user