Compare commits

..
6 changed files with 24 additions and 24 deletions
+2 -2
View File
File diff suppressed because one or more lines are too long
+5 -2
View File
@@ -474,14 +474,17 @@ 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() }
};
table.rows.push(head.iter().zip(&aligns).map(|(s, a)| cell(s, true, *a)).collect());
// 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());
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(aligns.len(), String::new());
cells.resize(cols, String::new());
table.rows.push(cells.iter().zip(&aligns).map(|(s, a)| cell(s, false, *a)).collect());
i += 1;
}
+14
View File
@@ -714,3 +714,17 @@ 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);
}
+2 -3
View File
@@ -598,11 +598,10 @@ fn find_link(s: &Session, url: &str) -> Option<(Pos, Pos)> {
}
/// Where the body references note `id`.
pub(crate) fn note_ref_pos(s: &Session, id: u32) -> Option<Pos> {
fn note_ref_pos(s: &Session, id: u32) -> Option<Pos> {
for path in s.doc.para_paths(StoryRef::Body) {
let Some(p) = s.doc.para(StoryRef::Body, &path) else { continue };
// One pass over the objects: `object_at` per offset counted the objects before each one.
let hit = p.objects_with_offsets().find(|(_, o)| matches!(o, InlineObject::NoteRef { id: n, .. } if *n == id)).map(|(off, _)| off);
let hit = p.object_offsets().into_iter().find(|o| matches!(p.object_at(*o), Some(InlineObject::NoteRef { id: n, .. }) if *n == id));
if let Some(off) = hit {
return Some(Pos::new(StoryRef::Body, path, off));
}
-16
View File
@@ -588,19 +588,3 @@ fn citation_at_document_end_stays_closed() {
let back = wordcraft_engine::io::open_bytes("probe.docx", &bytes).unwrap();
assert_eq!(back.field_ranges(StoryRef::Body).len(), 1);
}
#[test]
fn a_note_reference_is_found_in_one_pass_over_its_paragraph() {
// 20,000 other notes' marks before the one wanted: each was counted again from the start.
let n = 20_000u32;
let mut d = Document::from_text("");
if let Some(wordcraft_doc::Block::Para(p)) = d.body.first_mut().map(std::sync::Arc::make_mut) {
p.text = OBJ.to_string().repeat(n as usize);
p.runs = vec![wordcraft_doc::para::Run { len: p.text.len(), props: Default::default() }];
p.objects = (1..=n).map(|id| InlineObject::NoteRef { kind: wordcraft_doc::para::NoteKind::Footnote, id, custom: String::new() }).collect();
}
let s = Session::new(d);
let at = crate::bridge::note_ref_pos(&s, n).expect("found");
assert_eq!(at.off, (n as usize - 1) * OBJ.len_utf8());
assert!(crate::bridge::note_ref_pos(&s, n + 1).is_none());
}
+1 -1
View File
File diff suppressed because one or more lines are too long