Skip to content

DesignCraft: An open-source page and layout design editor written by scratch in pure Rust

fix(textimport): preserve leading empty Tagged Text paragraphs

Jason Brashear91263c912f1c
diff --git a/crates/textimport/src/tagged.rs b/crates/textimport/src/tagged.rs
index cfd08eea7..b84577eae 100644
--- a/crates/textimport/src/tagged.rs
+++ b/crates/textimport/src/tagged.rs
@@ -99,6 +99,7 @@ pub fn import(bytes: &[u8]) -> Result<Imported, ImportError> {
let chars: Vec<char> = text.chars().collect();
// Paragraphs: (style, runs of (text, format)).
let mut paras: Vec<(String, Vec<(String, CharFormat)>)> = vec![(String::new(), Vec::new())];
+ let mut started = false;
let mut fmt = CharFormat::default();
let mut para_styles: Vec<String> = Vec::new();
let mut char_styles: Vec<String> = Vec::new();
@@ -151,6 +152,7 @@ pub fn import(bytes: &[u8]) -> Result<Imported, ImportError> {
}
}
"ParaStyle" => {
+ started = true;
let v = value.trim().to_string();
if !v.is_empty() && !para_styles.contains(&v) {
para_styles.push(v.clone());
@@ -187,7 +189,7 @@ pub fn import(bytes: &[u8]) -> Result<Imported, ImportError> {
}
let style = paras.last().map(|p| p.0.clone()).unwrap_or_default();
// Header lines before any text don't start paragraphs.
- if paras.len() == 1 && paras[0].1.is_empty() {
+ if !started && paras.len() == 1 && paras.first().is_some_and(|p| p.1.is_empty()) {
i += 1;
continue;
}
@@ -243,6 +245,18 @@ mod tests {
assert_eq!(import("<ASCII-WIN>\r\n<ParaStyle:Body>A<0xD83D>B<0xDE00>C".as_bytes()).unwrap().story.text, "ABC");
}
+ #[test]
+ fn tagged_text_keeps_a_leading_empty_paragraph() {
+ let doc = Document::new(&designcraft_doc::build::NewDocument::default());
+ let mut story = Story::new(StoryId(0));
+ story.insert(0, "\nText");
+ story.paras[0].style = "Head".into();
+ story.paras[1].style = "Body".into();
+ let back = import(tagged_text(&doc, &story).as_bytes()).unwrap();
+ assert_eq!(back.story.text, story.text);
+ assert_eq!(back.story.paras, story.paras);
+ }
+
#[test]
fn tagged_text_round_trips() {
let src = "<ASCII-WIN>\r\n<Version:7>\r\n<ParaStyle:Head>Caf<0x00E9> \\<1\\>\r\n<ParaStyle:Body>A <CharStyle:Em>word<CharStyle:> and <cSize:18.000000>big<cSize:> text";