fix(engine): TXT outline import decodes BOM-marked UTF-16 and strips the UTF-8 BOM (#2395)

This commit is contained in:
Josias Eli Rodriguez
2026-11-27 07:02:47 -06:00
parent 74591abae8
commit e1ce3b95eb
2 changed files with 89 additions and 4 deletions
+31 -4
View File
@@ -181,7 +181,7 @@ fn open_other(name: &str, bytes: &[u8]) -> Result<(Presentation, Vec<String>, Ve
let lower = name.to_ascii_lowercase();
let rtf = deckcraft_format::rtf::sniff_rtf(bytes);
if rtf || lower.ends_with(".txt") || lower.ends_with(".md") {
let text = String::from_utf8_lossy(bytes);
let text = if rtf { String::from_utf8_lossy(bytes).into_owned() } else { decode_outline_bytes(bytes) };
let mut p = defaults::blank_presentation(defaults::WIDE, Default::default(), false);
if rtf {
deckcraft_format::rtf::rtf_to_slides(&mut p, &text);
@@ -234,10 +234,37 @@ fn probe_clips(p: &mut Presentation) {
}
}
/// An outline file's text: RTF is read into the tab-indented outline, anything else as UTF-8.
/// Decode outline text bytes by BOM: `EF BB BF` strips the UTF-8 BOM, `FF FE` decodes UTF-16LE,
/// `FE FF` decodes UTF-16BE, and anything else keeps lossy UTF-8 decoding.
fn decode_outline_bytes(bytes: &[u8]) -> String {
if let Some(rest) = bytes.strip_prefix(&[0xEF, 0xBB, 0xBF]) {
String::from_utf8_lossy(rest).into_owned()
} else if let Some(rest) = bytes.strip_prefix(&[0xFF, 0xFE]) {
decode_utf16(rest, u16::from_le_bytes)
} else if let Some(rest) = bytes.strip_prefix(&[0xFE, 0xFF]) {
decode_utf16(rest, u16::from_be_bytes)
} else {
String::from_utf8_lossy(bytes).into_owned()
}
}
fn decode_utf16(bytes: &[u8], decode: fn([u8; 2]) -> u16) -> String {
let (chunks, tail) = bytes.as_chunks::<2>();
let mut out: String = char::decode_utf16(chunks.iter().copied().map(decode)).map(|r| r.unwrap_or(char::REPLACEMENT_CHARACTER)).collect();
if !tail.is_empty() {
out.push(char::REPLACEMENT_CHARACTER);
}
out
}
/// An outline file's text: RTF is read into the tab-indented outline; plain text and Markdown
/// honour UTF-8, UTF-16LE and UTF-16BE BOMs and otherwise decode as UTF-8.
pub fn outline_text(bytes: &[u8]) -> String {
let text = String::from_utf8_lossy(bytes);
if deckcraft_format::rtf::sniff_rtf(bytes) { deckcraft_format::rtf::rtf_to_outline(&text) } else { text.into_owned() }
if deckcraft_format::rtf::sniff_rtf(bytes) {
deckcraft_format::rtf::rtf_to_outline(&String::from_utf8_lossy(bytes))
} else {
decode_outline_bytes(bytes)
}
}
pub fn add_opened(s: &mut Session, name: &str, path: Option<String>, p: Presentation) -> usize {
+58
View File
@@ -2765,6 +2765,64 @@ fn plain_outlines_are_unchanged() {
assert_eq!(p.slides.first().map(|s| s.title()).as_deref(), Some("# Intro"));
}
#[test]
fn txt_outline_decodes_utf16_boms() {
let source = "Intro — café\r\n\tPoint one\r\n\t\tSub point 🚀\r\nSecond\r\n\tMore\r\n";
let want = vec![
("Intro — café".to_string(), vec![(0, "Point one".to_string()), (1, "Sub point 🚀".to_string())]),
("Second".to_string(), vec![(0, "More".to_string())]),
];
let mut utf16_le = vec![0xFF, 0xFE];
utf16_le.extend(source.encode_utf16().flat_map(u16::to_le_bytes));
let mut utf16_be = vec![0xFE, 0xFF];
utf16_be.extend(source.encode_utf16().flat_map(u16::to_be_bytes));
for (label, bytes) in [("utf-16le bom", utf16_le.as_slice()), ("utf-16be bom", utf16_be.as_slice())] {
let p = crate::cmd::file::open_presentation("outline.txt", bytes).unwrap();
assert_eq!(outline_of(&p), want, "open_presentation ({label})");
assert_eq!(crate::cmd::file::outline_text(bytes), source, "outline_text ({label})");
}
// Truncated trailing byte and unpaired surrogate in UTF-16LE replace rather than panic.
let truncated_le = [0xFF, 0xFE, b'O', 0x00, b'k', 0x00, 0x00, 0xD8, 0x42];
let p = crate::cmd::file::open_presentation("truncated.txt", &truncated_le).unwrap();
assert_eq!(p.slides.len(), 1);
assert_eq!(p.slides[0].title(), "Ok\u{FFFD}\u{FFFD}");
assert_eq!(crate::cmd::file::outline_text(&truncated_le), "Ok\u{FFFD}\u{FFFD}");
// BOM-only UTF-16 files open as one blank slide.
for bom in [&[0xFF, 0xFE][..], &[0xFE, 0xFF][..]] {
let p = crate::cmd::file::open_presentation("empty.txt", bom).unwrap();
assert_eq!(p.slides.len(), 1);
assert_eq!(p.slides[0].title(), "");
}
}
#[test]
fn txt_outline_strips_utf8_bom_and_keeps_plain_utf8() {
let source = "Intro — café\r\n\tPoint one\r\n\t\tSub point 🚀\r\nSecond\r\n\tMore\r\n";
let want = vec![
("Intro — café".to_string(), vec![(0, "Point one".to_string()), (1, "Sub point 🚀".to_string())]),
("Second".to_string(), vec![(0, "More".to_string())]),
];
let mut utf8_bom = vec![0xEF, 0xBB, 0xBF];
utf8_bom.extend_from_slice(source.as_bytes());
for (label, bytes) in [("utf-8 bom", utf8_bom.as_slice()), ("utf-8 no bom", source.as_bytes())] {
let p = crate::cmd::file::open_presentation("outline.txt", bytes).unwrap();
assert_eq!(outline_of(&p), want, "open_presentation ({label})");
assert!(!p.slides[0].title().starts_with('\u{feff}'), "leading BOM kept ({label})");
assert_eq!(crate::cmd::file::outline_text(bytes), source, "outline_text ({label})");
}
let p = crate::cmd::file::open_presentation("empty.txt", &[0xEF, 0xBB, 0xBF]).unwrap();
assert_eq!(p.slides.len(), 1);
assert_eq!(p.slides[0].title(), "");
}
#[test]
fn rtf_without_heading_paragraphs_opens_as_one_blank_slide() {
let p = crate::cmd::file::open_presentation("empty.rtf", br"{\rtf1\ansi}").unwrap();