From e1e9923471aaa32ef1a964dd39c487f5d4968cab Mon Sep 17 00:00:00 2001 From: NetterSilver <133535268+Silvertree2010@users.noreply.github.com> Date: Tue, 20 Oct 2026 18:02:07 +0000 Subject: [PATCH 1/2] Keep ':', '<', '>' and '\' in Tagged Text style names Found while testing crates/textimport/src/tagged.rs:35 (also :52) Co-Authored-By: Claude Haiku 5.5 Co-Authored-By: Claude Opus 5.5 Co-Authored-By: Claude Sonnet 5.5 --- crates/textimport/src/tagged.rs | 58 +++++++++++++++++++++++++++++---- 1 file changed, 51 insertions(+), 7 deletions(-) diff --git a/crates/textimport/src/tagged.rs b/crates/textimport/src/tagged.rs index 9bc84b76..ffb106ea 100644 --- a/crates/textimport/src/tagged.rs +++ b/crates/textimport/src/tagged.rs @@ -25,6 +25,31 @@ fn escape(out: &mut String, c: char) { } } +/// A tag value with '<', '>' and '\\' escaped by a backslash, so it keeps the tag intact. +fn escape_value(v: &str) -> String { + let mut out = String::with_capacity(v.len()); + for c in v.chars() { + if matches!(c, '<' | '>' | '\\') { + out.push('\\'); + } + out.push(c); + } + out +} + +/// A tag value with backslash escapes removed. +fn unescape_value(v: &str) -> String { + let mut out = String::with_capacity(v.len()); + let mut it = v.chars(); + while let Some(c) = it.next() { + match c { + '\\' => out.extend(it.next()), + c => out.push(c), + } + } + out +} + /// `story` as Tagged Text. pub fn tagged_text(doc: &Document, story: &Story) -> String { let mut out = String::from("\r\n\r\n"); @@ -34,7 +59,7 @@ pub fn tagged_text(doc: &Document, story: &Story) -> String { out.push_str("\r\n"); } let pf = story.paras.get(i).cloned().unwrap_or_default(); - let _ = write!(out, "", pf.style.replace(['<', '>', ':'], "")); + let _ = write!(out, "", escape_value(&pf.style)); for (rr, f) in story.runs() { let (a, b) = (rr.start.max(r.start), rr.end.min(r.end)); if a >= b { @@ -45,7 +70,7 @@ pub fn tagged_text(doc: &Document, story: &Story) -> String { } let mut open = Vec::new(); if f.style != NO_CHAR_STYLE { - let _ = write!(out, "", f.style.replace(['<', '>', ':'], "")); + let _ = write!(out, "", escape_value(&f.style)); open.push("CharStyle"); } if let Some(v) = &f.over.font_family { @@ -153,7 +178,7 @@ pub fn import(bytes: &[u8]) -> Result { } "ParaStyle" => { started = true; - let v = value.trim().to_string(); + let v = unescape_value(value.trim()); if !v.is_empty() && !para_styles.contains(&v) { para_styles.push(v.clone()); } @@ -162,10 +187,10 @@ pub fn import(bytes: &[u8]) -> Result { } } "CharStyle" => { - let v = value.trim(); - fmt.style = if v.is_empty() { NO_CHAR_STYLE.into() } else { v.to_string() }; - if !v.is_empty() && !char_styles.contains(&v.to_string()) { - char_styles.push(v.to_string()); + let v = unescape_value(value.trim()); + fmt.style = if v.is_empty() { NO_CHAR_STYLE.into() } else { v.clone() }; + if !v.is_empty() && !char_styles.contains(&v) { + char_styles.push(v); } } "cFont" => fmt.over.font_family = (!value.is_empty()).then(|| value.to_string()), @@ -268,6 +293,25 @@ mod tests { assert_eq!(back.story.paras, story.paras); } + /// Style names with ':', '<', '>' or '\' were written with those characters dropped, so + /// re-import gave the paragraphs a different style. + #[test] + fn style_names_with_tag_characters_round_trip() { + let doc = Document::new(&designcraft_doc::build::NewDocument::default()); + for name in ["Body: Indent", "Ac", "x\\y", "end\\"] { + let mut story = Story::new(StoryId(0)); + story.insert_with(0, "Text", CharFormat { style: name.into(), ..Default::default() }); + story.paras[0].style = name.into(); + let out = tagged_text(&doc, &story); + let back = import(out.as_bytes()).unwrap(); + assert_eq!(back.story.text, "Text", "{out}"); + assert_eq!(back.story.paras[0].style, name, "{out}"); + assert_eq!(back.story.char_format_at(1).style, name, "{out}"); + assert_eq!(back.para_styles.iter().map(|s| s.name.as_str()).collect::>(), [name]); + assert_eq!(back.char_styles.iter().map(|s| s.name.as_str()).collect::>(), [name]); + } + } + #[test] fn tagged_text_round_trips() { let src = "\r\n\r\nCaf<0x00E9> \\<1\\>\r\nA word and big text"; From 96a3290021f3c2faa7daf85870da515b6697d360 Mon Sep 17 00:00:00 2001 From: Brandon Thomas Date: Fri, 23 Oct 2026 17:16:26 -0400 Subject: [PATCH 2/2] Tagged Text: decode only the escapes we write; other backslashes stay literal Co-Authored-By: Claude Opus 5.5 --- crates/textimport/src/tagged.rs | 25 ++++++++++++++++++++----- 1 file changed, 20 insertions(+), 5 deletions(-) diff --git a/crates/textimport/src/tagged.rs b/crates/textimport/src/tagged.rs index ffb106ea..b5f7815b 100644 --- a/crates/textimport/src/tagged.rs +++ b/crates/textimport/src/tagged.rs @@ -37,14 +37,20 @@ fn escape_value(v: &str) -> String { out } -/// A tag value with backslash escapes removed. +/// A tag value with the escapes `escape_value` writes (`\\`, `\<`, `\>`) decoded. Any other +/// backslash, including a trailing one, stays literal, as older files and InDesign's `\:` expect. fn unescape_value(v: &str) -> String { let mut out = String::with_capacity(v.len()); - let mut it = v.chars(); + let mut it = v.chars().peekable(); while let Some(c) = it.next() { - match c { - '\\' => out.extend(it.next()), - c => out.push(c), + if c == '\\' + && let Some(&n) = it.peek() + && matches!(n, '\\' | '<' | '>') + { + out.push(n); + it.next(); + } else { + out.push(c); } } out @@ -312,6 +318,15 @@ mod tests { } } + /// A backslash that isn't one of our escapes (older files, InDesign's `\:`) stays literal. + #[test] + fn unknown_backslashes_in_style_names_stay_literal() { + assert_eq!(unescape_value(r"x\y"), r"x\y"); + assert_eq!(unescape_value(r"a\:b"), r"a\:b"); + assert_eq!(unescape_value("end\\"), "end\\"); + assert_eq!(unescape_value(r"\\\"), r"\"); + } + #[test] fn tagged_text_round_trips() { let src = "\r\n\r\nCaf<0x00E9> \\<1\\>\r\nA word and big text";