use f_office_docx::testing::{self, WordParts as _};
use super::{Address, Document, Options, TextBlock, TextBlockKind};
use crate::test_fonts::{CARLITO_RUN, carlito};
fn paragraph(ppr: &str, text: &str) -> String {
format!("<w:p><w:pPr>{ppr}</w:pPr><w:r>{CARLITO_RUN}<w:t>{text}</w:t></w:r></w:p>")
}
fn table(texts: &[&str]) -> String {
let mut rows = String::new();
for text in texts {
rows.push_str("<w:tr><w:tc>");
rows.push_str(¶graph("", text));
rows.push_str("</w:tc></w:tr>");
}
format!(r#"<w:tbl><w:tblGrid><w:gridCol w:w="2000"/></w:tblGrid>{rows}</w:tbl>"#)
}
fn text_blocks(body: &str) -> Vec<TextBlock> {
let docx = testing::docx(body).plain_styles().zip().unwrap();
let document = Document::open(&docx, &carlito(), &Options::base())
.unwrap()
.value;
document.text_blocks().to_vec()
}
fn page_indices(blocks: &[TextBlock]) -> Vec<(String, Option<(usize, usize)>)> {
blocks
.iter()
.map(|block| {
let indices = block
.page_indices
.as_ref()
.map(|span| (*span.start(), *span.end()));
(block.address.to_string(), indices)
})
.collect()
}
fn indices_of(blocks: &[TextBlock], address: &str) -> Option<(usize, usize)> {
page_indices(blocks)
.into_iter()
.find_map(|(at, indices)| (at == address).then_some(indices))
.flatten()
}
#[test]
fn gives_each_block_the_pages_that_its_lines_lie_on() {
let next_page = "<w:pageBreakBefore/>";
let body = format!(
"{}{}{}{}",
paragraph(r#"<w:pStyle w:val="Title"/>"#, "first"),
paragraph(next_page, "second"),
table(&["in a cell"]),
paragraph(next_page, "third"),
);
let blocks = text_blocks(&body);
let expected = [
("p1", Some((0, 0))),
("p2", Some((1, 1))),
("t1", Some((1, 1))),
("t1/r1/c1/p1", Some((1, 1))),
("p3", Some((2, 2))),
]
.map(|(address, indices)| (address.to_owned(), indices));
assert_eq!(page_indices(&blocks), expected);
assert_eq!(
blocks.first().map(|block| &block.kind),
Some(&TextBlockKind::Paragraph {
style: Some("Title".to_owned()),
text: "first".to_owned(),
})
);
}
#[test]
fn gives_a_paragraph_that_runs_onto_the_next_page_both_pages() {
let body = format!(
r#"<w:p><w:r>{CARLITO_RUN}<w:t>before</w:t><w:br w:type="page"/><w:t>after</w:t></w:r></w:p>{}"#,
paragraph("", "next"),
);
let blocks = text_blocks(&body);
let expected = [("p1", Some((0, 1))), ("p2", Some((1, 1)))]
.map(|(address, indices)| (address.to_owned(), indices));
assert_eq!(page_indices(&blocks), expected);
}
#[test]
fn gives_a_table_that_runs_onto_the_next_page_both_pages() {
let texts: Vec<String> = (1..=100).map(|row| format!("row {row}")).collect();
let texts: Vec<&str> = texts.iter().map(String::as_str).collect();
let blocks = text_blocks(&table(&texts));
assert_eq!(indices_of(&blocks, "t1"), Some((0, 1)));
assert_eq!(indices_of(&blocks, "t1/r1/c1/p1"), Some((0, 0)));
assert_eq!(indices_of(&blocks, "t1/r100/c1/p1"), Some((1, 1)));
}
#[test]
fn reads_back_the_address_of_a_paragraph_in_a_nested_cell() {
let inner = format!(
r#"<w:tbl><w:tblGrid><w:gridCol w:w="1000"/></w:tblGrid><w:tr><w:tc>{}{}</w:tc></w:tr></w:tbl>"#,
paragraph("", "x"),
paragraph("", "inner"),
);
let body = format!(
r#"{}<w:tbl><w:tblGrid><w:gridCol w:w="2000"/><w:gridCol w:w="2000"/></w:tblGrid><w:tr><w:tc>{}</w:tc><w:tc>{}{inner}</w:tc></w:tr></w:tbl>"#,
paragraph("", "before"),
paragraph("", "a"),
paragraph("", "b"),
);
let blocks = text_blocks(&body);
let block = blocks
.iter()
.find(
|block| matches!(&block.kind, TextBlockKind::Paragraph { text, .. } if text == "inner"),
)
.unwrap();
let written = block.address.to_string();
assert_eq!(written, "t1/r1/c2/t1/r1/c1/p2");
let read: Address = written.parse().unwrap();
assert_eq!(read.steps(), block.address.steps());
assert_eq!(read, block.address);
}
#[test]
fn gives_a_hidden_paragraph_no_page() {
let hidden = concat!(
"<w:p><w:pPr><w:rPr><w:vanish/></w:rPr></w:pPr>",
"<w:r><w:rPr><w:vanish/></w:rPr><w:t>hidden</w:t></w:r></w:p>",
);
let body = format!("{}{hidden}{}", paragraph("", "one"), paragraph("", "two"));
let blocks = text_blocks(&body);
let expected = [("p1", Some((0, 0))), ("p2", None), ("p3", Some((0, 0)))]
.map(|(address, indices)| (address.to_owned(), indices));
assert_eq!(page_indices(&blocks), expected);
}