Compare commits

..
6 changed files with 28 additions and 24 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);
}
+8 -2
View File
@@ -1,6 +1,9 @@
use super::xml::{Attrs, Ev, events, get, length, local};
use super::{Align, Arc, Columns, Fmt, HashMap, Kind, Rgb, VAlign, model};
/// Longest style name looked at for a paragraph kind ("Heading 1", "Preformatted Text"…).
const MAX_KIND_NAME: usize = 256;
// ---------------------------------------------------------------------------------------------
// Import
@@ -346,8 +349,11 @@ impl Styles {
let mut kind_at = chain.len();
for (i, (n, s)) in chain.iter().enumerate() {
kind_at = i;
let dn = s.display.as_deref().unwrap_or(n).to_ascii_lowercase().replace("_20_", " ");
let nn = n.to_ascii_lowercase().replace("_20_", " ");
// The names that give a kind are short: a longer one isn't normalised, so a huge
// display name isn't copied again for every paragraph that uses the style.
let norm = |v: &str| if v.len() <= MAX_KIND_NAME { v.to_ascii_lowercase().replace("_20_", " ") } else { String::new() };
let dn = norm(s.display.as_deref().unwrap_or(n));
let nn = norm(n);
for cand in [dn.as_str(), nn.as_str()] {
if let Some(d) = cand.strip_prefix("heading ").and_then(|r| r.trim().parse::<u8>().ok()) {
kind = Kind::Heading(d.clamp(1, 6));
+15
View File
@@ -1135,3 +1135,18 @@ fn meta_creation_date_timezones_and_fallbacks() {
assert_eq!(m2.created, "2024-03-05T13:45:00Z");
assert_eq!(m2.author, "Solo Creator");
}
#[test]
fn style_kinds_come_from_short_names_only() {
let mut s = styles::Styles::default();
let long = "x".repeat(1 << 20);
let xml = format!(
r#"<office:document-styles xmlns:office="urn:oasis:names:tc:opendocument:xmlns:office:1.0" xmlns:style="urn:oasis:names:tc:opendocument:xmlns:style:1.0"><office:styles><style:style style:name="Heading_20_2" style:display-name="Heading 2" style:family="paragraph"/><style:style style:name="Big" style:display-name="{long}" style:family="paragraph"/></office:styles></office:document-styles>"#
);
s.read(xml.as_bytes(), false);
assert_eq!(s.para("Heading_20_2").0, Kind::Heading(2));
// A huge display name isn't normalised (again) for every paragraph that names it.
for _ in 0..10_000 {
assert_eq!(s.para("Big").0, Kind::Normal);
}
}
+1 -1
View File
File diff suppressed because one or more lines are too long