f-office-docx 0.2.1

DOCX model and Word's layout rules.
Documentation
use super::*;

fn parse(xml: &str) -> Document {
    Document::parse(xml.as_bytes(), &f_office_xml::Limits::default()).unwrap()
}

fn styles() -> Styles {
    let doc = parse(&format!(
        r#"<w:styles xmlns:w="{W}">
        <w:style w:type="character" w:styleId="Link"><w:name w:val="Hyperlink"/><w:rPr><w:color w:val="008000"/><w:b/><w:u w:val="single"/></w:rPr></w:style>
        <w:style w:type="character" w:styleId="Visited"><w:name w:val="FollowedHyperlink"/><w:basedOn w:val="Link"/></w:style>
        <w:style w:type="character" w:styleId="Hyperlink"><w:name w:val="Custom"/><w:basedOn w:val="Link"/></w:style>
        </w:styles>"#
    ));
    Styles::read(&mut PartReader::new(&doc), doc.root().unwrap())
}

fn run(style: &str, direct: &str) -> String {
    format!(r#"<w:r><w:rPr><w:rStyle w:val="{style}"/>{direct}</w:rPr><w:t>x</w:t></w:r>"#)
}

fn complex(code: &str, result: &str) -> String {
    format!(
        r#"<w:r><w:fldChar w:fldCharType="begin"/></w:r><w:r><w:instrText>{code}</w:instrText></w:r><w:r><w:fldChar w:fldCharType="separate"/></w:r>{result}<w:r><w:fldChar w:fldCharType="end"/></w:r>"#
    )
}

fn collect(doc: &Document, node: NodeId, nodes: &mut Vec<NodeId>) {
    if doc.is(node, W, "rPr") {
        nodes.push(node);
    }
    for child in doc.child_elements(node) {
        collect(doc, child, nodes);
    }
}

fn prepared(body: &str) -> (Document, Vec<NodeId>) {
    let mut doc = parse(&format!(
        r#"<w:document xmlns:w="{W}"><w:body>{body}</w:body></w:document>"#
    ));
    prepare(
        &mut doc,
        (crate::testing::DOCUMENT, &crate::body::TEST_PARAGRAPH_IDS),
        &styles(),
    );
    let mut nodes = Vec::new();
    collect(&doc, doc.root().unwrap(), &mut nodes);
    (doc, nodes)
}

#[test]
fn selects_the_builtin_name_and_keeps_a_custom_derived_style() {
    let result = ["Link", "Visited", "Hyperlink"]
        .map(|id| run(id, ""))
        .join("");
    let (doc, rpr) = prepared(&complex(
        " TOC ",
        &format!("<w:hyperlink>{result}</w:hyperlink>"),
    ));
    let selected = rpr
        .iter()
        .map(|r| doc.child(*r, W, "rStyle").is_some())
        .collect::<Vec<_>>();
    assert_eq!(selected, [false, false, true]);
}

#[test]
fn removes_only_equal_tested_direct_values() {
    let equal = r#"<w:color w:val="008000"/><w:b/><w:u w:val="single"/><w:i/>"#;
    let different = r#"<w:color w:val="FF0000"/><w:b w:val="0"/><w:u w:val="double"/>"#;
    let result = format!(
        "<w:hyperlink>{}{}</w:hyperlink>",
        run("Visited", equal),
        run("Link", different)
    );
    let (doc, rpr) = prepared(&complex("TOC", &result));
    for name in ["rStyle", "color", "b", "u"] {
        assert!(doc.child(rpr[0], W, name).is_none(), "{name}");
    }
    assert!(doc.child(rpr[0], W, "i").is_some());
    for name in ["color", "b", "u"] {
        assert!(doc.child(rpr[1], W, name).is_some(), "{name}");
    }
}

#[test]
fn keeps_the_selected_style_and_equal_direct_values_under_the_plain_variant() {
    let equal = r#"<w:color w:val="008000"/><w:b/><w:u w:val="single"/>"#;
    let result = format!("<w:hyperlink>{}</w:hyperlink>", run("Link", equal));
    let body = complex("TOC", &result);
    let mut doc = parse(&format!(
        r#"<w:document xmlns:w="{W}"><w:body>{body}</w:body></w:document>"#
    ));
    let profile = (crate::profile::Profile::base())
        .with_variant(crate::quirks::TocLinkStyle::KeepsSelectedStyle);
    profile.reader_settings().prepare_toc_links(
        &mut doc,
        (crate::testing::DOCUMENT, &crate::body::TEST_PARAGRAPH_IDS),
        &styles(),
    );
    let mut rpr = Vec::new();
    collect(&doc, doc.root().unwrap(), &mut rpr);
    for name in ["rStyle", "color", "b", "u"] {
        assert!(doc.child(rpr[0], W, name).is_some(), "{name}");
    }
}

#[test]
fn keeps_styles_outside_toc_result_hyperlinks() {
    let run = run("Link", "");
    let result = format!("<w:hyperlink>{run}</w:hyperlink>");
    let (doc, rpr) = prepared(&format!(
        "{result}{}{}",
        complex("REF", &result),
        complex("TOC", &run)
    ));
    assert_eq!(rpr.len(), 3);
    assert!(rpr.iter().all(|r| doc.child(*r, W, "rStyle").is_some()));
}

#[test]
fn follows_simple_nested_and_multiparagraph_fields() {
    let run = run("Link", "");
    let nested = complex("REF", &run);
    let linked = format!("<w:hyperlink>{nested}</w:hyperlink>");
    let simple = format!(r#"<w:fldSimple w:instr="TOC">{linked}</w:fldSimple>"#);
    let across = complex(
        "TOC",
        &format!("</w:p><w:p><w:hyperlink>{run}</w:hyperlink>"),
    );
    let (doc, rpr) = prepared(&format!("<w:p>{simple}{across}</w:p>"));
    assert_eq!(rpr.len(), 2);
    assert!(rpr.iter().all(|r| doc.child(*r, W, "rStyle").is_none()));
}

#[test]
fn keeps_a_text_box_separate_from_its_surrounding_field() {
    let run = run("Link", "");
    let result =
        format!("<w:hyperlink><w:txbxContent><w:p>{run}</w:p></w:txbxContent>{run}</w:hyperlink>");
    let (doc, rpr) = prepared(&complex("TOC", &result));
    assert!(doc.child(rpr[0], W, "rStyle").is_some());
    assert!(doc.child(rpr[1], W, "rStyle").is_none());
}

#[test]
fn counts_a_toc_result_once_through_the_fields_inside_it() {
    let mut fields = Fields::default();
    fields.begin();
    fields.code(" toc \\o \"1-3\" \\h ");
    assert!(!fields.in_toc_result());
    fields.separate();
    assert!(fields.in_toc_result());
    // A field inside the result, and a second separator, which counts nothing again.
    fields.begin();
    fields.code("PAGEREF _Toc1 \\h");
    fields.separate();
    fields.separate();
    assert_eq!(fields.toc_results, 1);
    fields.end();
    // Text after the separator is no code.
    fields.code("TOC");
    assert!(fields.in_toc_result());
    fields.end();
    assert_eq!((fields.open.len(), fields.toc_results), (0, 0));
}

#[test]
fn counts_a_simple_toc_at_once_and_a_toc_code_without_a_result_not_at_all() {
    let mut fields = Fields::default();
    fields.begin();
    fields.code("TOC");
    assert!(!fields.in_toc_result());
    fields.simple("TOC \\o");
    assert!(fields.in_toc_result());
    fields.end();
    assert!(!fields.in_toc_result());
    fields.end();
    fields.end();
    assert_eq!((fields.open.len(), fields.toc_results), (0, 0));
}