Compare commits

..
Author SHA1 Message Date
Dylan M. Taylor 729129a8d0 style: rustfmt 2026-11-03 16:20:37 +00:00
Dylan M. Taylor 6120396fb1 fix(formats): hold ODT text:s spaces to a document-wide budget 2026-11-03 11:44:49 +00:00
6 changed files with 40 additions and 23 deletions
+2 -2
View File
File diff suppressed because one or more lines are too long
+2 -5
View File
@@ -474,17 +474,14 @@ fn parse_blocks(lines: &[String], depth: usize, level: u8, refs: &Refs) -> Vec<F
let p = Para { align: a, inlines: parse_inlines(s, &Fmt::default(), depth, refs), ..Default::default() };
Cell { blocks: vec![FBlock::Para(p)], header, ..Default::default() }
};
// A document row keeps MAX_COLS cells: only those are built, so a wide header
// doesn't pad every sparse row below it out to its width.
let cols = aligns.len().min(wordcraft_doc::table::MAX_COLS);
table.rows.push(head.iter().zip(&aligns).take(cols).map(|(s, a)| cell(s, true, *a)).collect());
table.rows.push(head.iter().zip(&aligns).map(|(s, a)| cell(s, true, *a)).collect());
i += 2;
while let Some(l) = lines.get(i) {
if is_blank(l) || !l.contains('|') || interrupts(l) {
break;
}
let mut cells = split_row(l);
cells.resize(cols, String::new());
cells.resize(aligns.len(), String::new());
table.rows.push(cells.iter().zip(&aligns).map(|(s, a)| cell(s, false, *a)).collect());
i += 1;
}
-14
View File
@@ -714,17 +714,3 @@ fn exported_link_destinations_read_back_unchanged() {
assert_eq!(md("https://example.com/a b"), "[x](<https://example.com/a b>)");
assert_eq!(md("https://e.com/x\ny"), "[x](https://e.com/xy)");
}
#[test]
fn a_wide_table_builds_only_the_cells_a_row_keeps() {
// A 5,000-column header padded every one-cell row below it to 5,000 cells.
let n = 5000;
let md = format!("{}|\n{}|\n{}", "|h".repeat(n), "|-".repeat(n), "|x|\n".repeat(200));
let flow = parse(&md);
let FBlock::Table(t) = &flow.blocks[0] else { panic!("expected table") };
assert_eq!(t.rows.len(), 201);
assert!(t.rows.iter().all(|r| r.len() == wordcraft_doc::table::MAX_COLS));
// A narrow table is unchanged.
let FBlock::Table(t) = &parse("| a | b |\n| - | - |\n| x |\n").blocks[0] else { panic!("expected table") };
assert_eq!(t.rows[1].len(), 2);
}
+14 -1
View File
@@ -157,8 +157,15 @@ pub(super) struct Body<'a> {
del: Option<DelState>,
/// Insertions open in the body: (region id, revision).
active_ins: Vec<(String, RevMark)>,
/// Spaces `text:s` may still add ([`MAX_EXPANDED_SPACES`]).
spaces_left: usize,
}
/// Most spaces the document's `text:s` elements add in all: each may ask for 1,000 in 20
/// bytes, so without a total a content.xml within its size limit could add 50 times its size.
/// Past it, a `text:s` adds one space.
pub(super) const MAX_EXPANDED_SPACES: usize = 16 << 20;
impl<'a> Body<'a> {
pub(super) fn new(styles: &'a Styles, pages: &'a Pages, zip: &'a mut dyn FnMut(&str) -> Option<Vec<u8>>) -> Self {
let page = pages.first.clone().unwrap_or_default();
@@ -195,6 +202,7 @@ impl<'a> Body<'a> {
info_field: None,
del: None,
active_ins: Vec::new(),
spaces_left: MAX_EXPANDED_SPACES,
}
}
@@ -822,7 +830,12 @@ impl Body<'_> {
self.fmt.push(fmt);
}
"s" => {
let count = get(a, "text:c").and_then(|v| v.trim().parse::<usize>().ok()).unwrap_or(1).clamp(1, 1000);
let asked = get(a, "text:c").and_then(|v| v.trim().parse::<usize>().ok()).unwrap_or(1).clamp(1, 1000);
let count = asked.min(self.spaces_left).max(1);
if count < asked && self.spaces_left > 0 {
log::warn!("odt: the document's spaces pass {MAX_EXPANDED_SPACES}; each further text:s adds one");
}
self.spaces_left = self.spaces_left.saturating_sub(count);
self.push_text(&" ".repeat(count));
}
"tab" => self.push_text("\t"),
+21
View File
@@ -1135,3 +1135,24 @@ fn meta_creation_date_timezones_and_fallbacks() {
assert_eq!(m2.created, "2024-03-05T13:45:00Z");
assert_eq!(m2.author, "Solo Creator");
}
#[test]
fn repeated_spaces_stop_growing_past_the_document_budget() {
// 20,000 `text:s` of 1,000 spaces each (20 million from 600 kB) are held to the budget;
// each one past it still adds a space.
let n = 20_000;
let spaces = r#"<text:s text:c="1000"/>"#.repeat(n);
let content = format!(
r#"<office:document-content {NS_T}><office:body><office:text><text:p>a{spaces}b</text:p></office:text></office:body></office:document-content>"#
);
let doc = import(&package(&content, None)).unwrap();
let text = doc.plain_text(wordcraft_doc::StoryRef::Body);
let got = text.chars().filter(|c| *c == ' ').count();
let full = body::MAX_EXPANDED_SPACES / 1000;
assert_eq!(got, body::MAX_EXPANDED_SPACES + (n - full - 1));
// A few spaces are kept as asked.
let content = format!(
r#"<office:document-content {NS_T}><office:body><office:text><text:p>a<text:s text:c="3"/>b</text:p></office:text></office:body></office:document-content>"#
);
assert!(import(&package(&content, None)).unwrap().plain_text(wordcraft_doc::StoryRef::Body).contains("a b"));
}
+1 -1
View File
File diff suppressed because one or more lines are too long