use f_office_docx::testing::{self, MAIN, WordParts as _};
use f_office_opc::{Limits, Package};
use f_office_xml::Error as XmlError;
use super::{
Edit, EditError, EditFault, EditLimits, NamedEdit, NamedFault, Path, PathError, Place, XmlEdit,
element_xml,
};
use crate::test_fonts::carlito;
use crate::{Document, Options, TextBlock, TextBlockKind, Warned, Warning};
fn apply(docx: &[u8], edits: &[Edit], limits: &EditLimits) -> Result<Warned<Vec<u8>>, EditError> {
super::apply(docx, &[], edits, limits).map(|edited| {
assert_eq!(edited.value.expected, None);
edited.map(|value| value.docx)
})
}
const BODY: &str = r#"<w:p><w:r><w:t>one</w:t></w:r></w:p><w:tbl><w:tr><w:tc><w:p><w:r><w:t>cell</w:t></w:r></w:p></w:tc></w:tr></w:tbl><w:p w:rsidR="00AB"><w:r><w:t xml:space="preserve">two </w:t></w:r></w:p>"#;
fn docx() -> Vec<u8> {
testing::docx(BODY).plain_styles().zip().unwrap()
}
fn raw(part: &str, at: &str, expect: &str, xml: &str) -> Edit {
Edit::Xml(XmlEdit {
part: part.to_owned(),
at: at.parse().unwrap(),
expect: expect.to_owned(),
xml: xml.to_owned(),
})
}
fn body(at: &str, expect: &str, xml: &str) -> Edit {
raw("word/document.xml", at, expect, xml)
}
fn fault(docx: &[u8], edits: &[Edit]) -> (Option<usize>, EditFault) {
let EditError { edit, fault } = apply(docx, edits, &EditLimits::default()).unwrap_err();
(edit, fault)
}
fn part(docx: &[u8], name: &str) -> Vec<u8> {
Package::open(docx, &Limits::default())
.unwrap()
.part(name)
.unwrap()
.to_vec()
}
const SECOND: &str = r#"<w:p w:rsidR="00AB"><w:r><w:t xml:space="preserve">two </w:t></w:r></w:p>"#;
#[test]
fn replaces_one_element_and_keeps_every_other_byte() {
let original = docx();
let edited = apply(
&original,
&[body(
"w:body/w:p[2]",
SECOND,
"<w:p><w:r><w:t>new</w:t></w:r></w:p>",
)],
&EditLimits::default(),
)
.unwrap();
assert!(edited.warnings.is_empty());
let before = String::from_utf8(part(&original, "word/document.xml")).unwrap();
let after = String::from_utf8(part(&edited.value, "word/document.xml")).unwrap();
assert_eq!(
after,
before.replace(SECOND, "<w:p><w:r><w:t>new</w:t></w:r></w:p>")
);
assert_eq!(
part(&edited.value, "word/styles.xml"),
part(&original, "word/styles.xml")
);
assert_eq!(
apply(&original, &[], &EditLimits::default()).unwrap().value,
original
);
}
#[test]
fn counts_a_position_among_the_siblings_of_the_steps_name_only() {
let original = docx();
assert_eq!(
element_xml(
&original,
"/WORD/document.xml",
&"w:body/w:p[2]".parse().unwrap(),
&EditLimits::default()
)
.unwrap(),
SECOND
);
let cell = "w:body/w:tbl/w:tr/w:tc/w:p";
let xml = element_xml(
&original,
"word/document.xml",
&cell.parse().unwrap(),
&EditLimits::default(),
)
.unwrap();
assert_eq!(xml, "<w:p><w:r><w:t>cell</w:t></w:r></w:p>");
}
#[test]
fn finds_an_element_by_an_attribute_and_by_the_uri_of_another_prefix() {
let styles = format!(
r#"<s:styles xmlns:s="{MAIN}"><s:style s:styleId="Normal"/><s:style s:styleId="Heading1"><s:name s:val="heading 1"/></s:style></s:styles>"#
);
let original = testing::docx(BODY)
.relationship(testing::DOCUMENT, "styles", "styles", "styles.xml")
.part("word/styles.xml", styles)
.zip()
.unwrap();
let lookup = r#"w:style[@w:styleId="Heading1"]/w:name"#;
let found = element_xml(
&original,
"word/styles.xml",
&lookup.parse().unwrap(),
&EditLimits::default(),
);
assert_eq!(
found,
Err(EditError {
edit: None,
fault: EditFault::UndeclaredPathPrefix("w".to_owned())
})
);
let lookup = r#"s:style[@s:styleId="Heading1"]/s:name"#;
let edited = apply(
&original,
&[raw(
"word/styles.xml",
lookup,
&format!(r#"<w:name xmlns:w="{MAIN}" w:val='heading 1' />"#),
r#"<s:name s:val="Title"/>"#,
)],
&EditLimits::default(),
)
.unwrap();
let after = String::from_utf8(part(&edited.value, "word/styles.xml")).unwrap();
assert!(after.contains(r#"<s:name s:val="Title"/>"#), "{after}");
}
#[test]
fn says_which_step_matches_no_element_or_too_many() {
let original = docx();
assert_eq!(
fault(&original, &[body("w:body/w:p", SECOND, "")]),
(Some(0), EditFault::Ambiguous { step: 2, count: 2 })
);
assert_eq!(
fault(&original, &[body("w:body/w:p[3]", SECOND, "")]),
(Some(0), EditFault::NoElement { step: 2 })
);
assert_eq!(
fault(&original, &[body("w:body/w:sectPr", SECOND, "")]),
(Some(0), EditFault::NoElement { step: 2 })
);
assert_eq!(
fault(&original, &[raw("word/missing.xml", "w:a", "<w:a/>", "")]),
(
Some(0),
EditFault::NoSuchPart("word/missing.xml".to_owned())
)
);
}
#[test]
fn compares_expect_by_namespace_and_text_exactly() {
let original = docx();
let respelled = format!(
r#"<v:p xmlns:v="{MAIN}" v:rsidR='00AB'><v:r><v:t xml:space="preserve">two </v:t></v:r></v:p>"#
);
assert!(
apply(
&original,
&[body("w:body/w:p[2]", &format!("\n {respelled}\n"), "")],
&EditLimits::default()
)
.is_ok()
);
let trimmed = r#"<w:p w:rsidR="00AB"><w:r><w:t xml:space="preserve">two</w:t></w:r></w:p>"#;
assert_eq!(
fault(&original, &[body("w:body/w:p[2]", trimmed, "")]),
(Some(0), EditFault::ExpectDiffers)
);
assert_eq!(
fault(&original, &[body("w:body/w:p[2]", "text", "")]),
(Some(0), EditFault::ExpectNotOneElement)
);
}
#[test]
fn refuses_new_xml_with_a_prefix_that_its_place_does_not_declare() {
let original = docx();
assert_eq!(
fault(&original, &[body("w:body/w:p[2]", SECOND, "<w14:p/>")]),
(
Some(0),
EditFault::XmlNotValid(XmlError::UndeclaredPrefix("w14".to_owned()))
)
);
let declared = r#"<w14:p xmlns:w14="urn:w14"/>"#;
assert!(
apply(
&original,
&[body("w:body/w:p[2]", SECOND, declared)],
&EditLimits::default()
)
.is_ok()
);
}
#[test]
fn refuses_two_edits_of_one_element_or_of_an_element_and_its_child() {
let original = docx();
let run = r#"<w:r><w:t xml:space="preserve">two </w:t></w:r>"#;
assert_eq!(
fault(
&original,
&[
body("w:body/w:p[2]", SECOND, ""),
body("w:body/w:p[2]/w:r", run, "")
]
),
(Some(1), EditFault::Overlap { other: 0 })
);
assert_eq!(
fault(
&original,
&[
body("w:body/w:p[2]", SECOND, ""),
body("w:body/w:p[2]", SECOND, "<w:p/>")
]
),
(Some(1), EditFault::Overlap { other: 0 })
);
}
#[test]
fn keeps_to_the_limits_of_a_list() {
let original = docx();
let small = EditLimits {
max_edits: 1,
max_xml_bytes: 8,
..EditLimits::default()
};
let edit = body("w:body/w:p[2]", SECOND, "");
assert_eq!(
apply(&original, &[edit.clone(), edit.clone()], &small),
Err(EditError {
edit: None,
fault: EditFault::TooManyEdits { limit: 1 }
})
);
assert_eq!(
apply(&original, &[edit], &small),
Err(EditError {
edit: Some(0),
fault: EditFault::XmlTooLarge { limit: 8 }
})
);
}
const FIRST: &str = "<w:p><w:r><w:t>one</w:t></w:r></w:p>";
#[test]
fn makes_each_edit_of_a_list_on_the_file_as_it_was_before_the_list() {
let original = docx();
let edits = [
body("w:body/w:p[1]", FIRST, ""),
body("w:body/w:p[2]", SECOND, "<w:p/><w:p/>"),
];
let edited = apply(&original, &edits, &EditLimits::default()).unwrap();
let before = String::from_utf8(part(&original, "word/document.xml")).unwrap();
let after = String::from_utf8(part(&edited.value, "word/document.xml")).unwrap();
assert_eq!(
after,
before.replace(FIRST, "").replace(SECOND, "<w:p/><w:p/>")
);
}
#[test]
fn refuses_new_xml_that_parses_alone_but_not_in_its_part() {
let original = docx();
let mut limits = EditLimits::default();
limits.package.xml.max_depth = 10;
let deep = format!("{}{}", "<w:p>".repeat(9), "</w:p>".repeat(9));
let edits = [body("w:body/w:p[2]", SECOND, &deep)];
assert_eq!(
apply(&original, &edits, &limits),
Err(EditError {
edit: Some(0),
fault: EditFault::PartBroken(XmlError::TooDeep)
})
);
}
#[test]
fn warns_that_an_edit_of_a_signed_package_breaks_its_signature() {
let original = testing::docx(BODY)
.part("_xmlsignatures/sig1.xml", "<Signature/>")
.zip()
.unwrap();
let edited = apply(
&original,
&[body("w:body/w:p[2]", SECOND, "")],
&EditLimits::default(),
)
.unwrap();
let warnings: Vec<&Warning> = edited.warnings.iter().map(|(warning, _)| warning).collect();
assert_eq!(warnings, [&Warning::SignatureBroken]);
}
#[test]
fn reads_a_path_with_a_slash_and_a_bracket_in_a_quoted_value() {
let path: Path = "w:style[@w:styleId='a/b]c']/w:name".parse().unwrap();
assert_eq!(path.len(), 2);
assert_eq!(path.to_string(), "w:style[@w:styleId='a/b]c']/w:name");
}
#[test]
fn refuses_text_that_is_no_path() {
assert_eq!("".parse::<Path>(), Err(PathError::Empty));
assert_eq!("/w:body".parse::<Path>(), Err(PathError::LeadingSlash));
assert_eq!(
"w:body//w:p".parse::<Path>(),
Err(PathError::Name { step: 2 })
);
assert_eq!(
"w:body/w:p[0]".parse::<Path>(),
Err(PathError::Zero { step: 2 })
);
assert_eq!("w:p[x]".parse::<Path>(), Err(PathError::Test { step: 1 }));
assert_eq!(
"w:p[@w:a='b\"]".parse::<Path>(),
Err(PathError::Test { step: 1 })
);
}
#[cfg(feature = "json")]
mod json {
use f_office_paint::ImageFormat;
use f_office_units::Length;
use super::super::json::EntryFault;
use super::super::{EditLimits, ImageError, ListContext, ListError, parse};
use super::*;
#[test]
fn reads_a_raw_edit_from_json_and_refuses_what_is_no_edit() {
let list = r#"[{"op": "xml", "part": "word/document.xml", "at": "w:body/w:p[2]", "expect": "<w:p/>", "xml": ""}]"#;
let limits = EditLimits::default();
assert_eq!(
parse(list.as_bytes(), &limits, &ListContext::default()).unwrap(),
[body("w:body/w:p[2]", "<w:p/>", "")]
);
let entry = |json: &str, index: usize| match parse(
json.as_bytes(),
&limits,
&ListContext::default(),
) {
Err(ListError::Entry {
index: found,
fault,
}) if found == index => fault,
other => panic!("{other:?}"),
};
assert_eq!(entry("[1]", 0), EntryFault::NotAnObject);
assert_eq!(
entry(r#"[{"op": "move"}]"#, 0),
EntryFault::UnknownOperation("move".to_owned())
);
assert_eq!(
entry(
r#"[{"op": "xml", "part": "a", "at": "b", "expect": "c"}]"#,
0
),
EntryFault::Field("xml")
);
}
#[test]
fn reads_the_named_edits_from_json() {
let list = r#"[
{"op": "replace", "at": "p2", "find": "30 days", "with": "45 days"},
{"op": "insert-after", "at": "t1/r1/c1/p1", "text": "new"},
{"op": "insert-before", "at": "t1", "text": "", "style": "Heading1"},
{"op": "delete", "at": "t1/r2"},
{"op": "style", "at": "p1", "style": "heading 1"}
]"#;
let at = |text: &str| text.parse().unwrap();
assert_eq!(
parse(
list.as_bytes(),
&EditLimits::default(),
&ListContext::default()
)
.unwrap(),
[
named(NamedEdit::Replace {
at: at("p2"),
find: "30 days".to_owned(),
with: "45 days".to_owned(),
}),
insert("t1/r1/c1/p1", Place::After, "new"),
named(NamedEdit::Insert {
at: at("t1"),
place: Place::Before,
text: String::new(),
style: Some("Heading1".to_owned()),
}),
named(NamedEdit::Delete { at: at("t1/r2") }),
named(NamedEdit::Style {
at: at("p1"),
style: "heading 1".to_owned(),
}),
]
);
let limits = EditLimits::default();
let entry = |json: &str| match parse(json.as_bytes(), &limits, &ListContext::default()) {
Err(ListError::Entry { index: 0, fault }) => fault,
other => panic!("{other:?}"),
};
assert!(matches!(
entry(r#"[{"op": "delete", "at": "w:body/w:p"}]"#),
EntryFault::Address(_)
));
assert_eq!(
entry(r#"[{"op": "insert-after", "at": "p1", "text": "a", "style": 1}]"#),
EntryFault::Field("style")
);
assert_eq!(
entry(r#"[{"op": "delete", "at": "p1", "find": "a"}]"#),
EntryFault::UnknownField("find".to_owned())
);
}
#[test]
fn refuses_unknown_fields_paths_and_lists_beyond_the_limit() {
let limits = EditLimits::default();
let entry = |json: &str| match parse(json.as_bytes(), &limits, &ListContext::default()) {
Err(ListError::Entry { index: 0, fault }) => fault,
other => panic!("{other:?}"),
};
assert_eq!(
entry(
r#"[{"op": "xml", "part": "a", "at": "b", "expect": "c", "xml": "", "expected": ""}]"#
),
EntryFault::UnknownField("expected".to_owned())
);
assert_eq!(
entry(r#"[{"op": "xml", "part": "a", "at": "/b", "expect": "c", "xml": ""}]"#),
EntryFault::Path(PathError::LeadingSlash)
);
assert_eq!(
parse(b"{}", &limits, &ListContext::default()),
Err(ListError::NotAList)
);
assert!(matches!(
parse(b"[", &limits, &ListContext::default()),
Err(ListError::Json(_))
));
let small = EditLimits {
max_list_bytes: 4,
..EditLimits::default()
};
assert_eq!(
parse(b"[{}, {}]", &small, &ListContext::default()),
Err(ListError::TooLarge { limit: 4 })
);
}
#[test]
fn reads_the_edits_of_cells_comments_and_images_from_json() {
let list = r#"[
{"op": "set-text", "at": "t1/r2/c1", "text": "45"},
{"op": "insert-row-after", "at": "t1/r2", "cells": ["Total", "45"]},
{"op": "insert-row-before", "at": "t1/r1"},
{"op": "comment", "at": "p2", "text": "Check.", "find": "30 days"},
{"op": "comment", "at": "p3", "text": "Fine.", "author": "Reviewer"},
{"op": "insert-image-after", "at": "p1", "image": "chart.png", "alt": "A chart", "width": "2in"}
]"#;
let png = png_file(192, 96);
let read = |path: &str| {
assert_eq!(path, "chart.png");
Ok(png.clone())
};
let context = ListContext {
author: Some("Editor"),
read_image: &read,
};
let at = |text: &str| text.parse().unwrap();
let edits = parse(list.as_bytes(), &EditLimits::default(), &context).unwrap();
assert_eq!(
edits[..5],
[
named(NamedEdit::SetText {
at: at("t1/r2/c1"),
text: "45".to_owned(),
}),
named(NamedEdit::InsertRow {
at: at("t1/r2"),
place: Place::After,
cells: vec!["Total".to_owned(), "45".to_owned()],
}),
named(NamedEdit::InsertRow {
at: at("t1/r1"),
place: Place::Before,
cells: Vec::new(),
}),
named(NamedEdit::Comment {
at: at("p2"),
text: "Check.".to_owned(),
find: Some("30 days".to_owned()),
author: "Editor".to_owned(),
}),
named(NamedEdit::Comment {
at: at("p3"),
text: "Fine.".to_owned(),
find: None,
author: "Reviewer".to_owned(),
}),
]
);
let Edit::Named(NamedEdit::InsertImage {
image, alt, size, ..
}) = &edits[5]
else {
panic!("{:?}", edits[5]);
};
assert_eq!((alt.as_str(), image.data()), ("A chart", png.as_slice()));
assert_eq!(size.0, Length::from_emus(2 * 914_400));
assert_eq!(size.1, None);
}
#[test]
fn refuses_comments_without_an_author_and_images_that_do_not_read() {
let limits = EditLimits::default();
let entry = |json: &str| match parse(json.as_bytes(), &limits, &ListContext::default()) {
Err(ListError::Entry { index: 0, fault }) => fault,
other => panic!("{other:?}"),
};
assert_eq!(
entry(r#"[{"op": "comment", "at": "p1", "text": "a"}]"#),
EntryFault::NoAuthor
);
assert_eq!(
entry(r#"[{"op": "insert-row-after", "at": "t1/r1", "cells": "a"}]"#),
EntryFault::Cells
);
assert_eq!(
entry(r#"[{"op": "insert-row-after", "at": "t1/r1", "cells": [1]}]"#),
EntryFault::Cells
);
assert_eq!(
entry(
r#"[{"op": "insert-image-after", "at": "p1", "image": "a.png", "alt": "a", "width": "12"}]"#
),
EntryFault::Length("width")
);
assert!(matches!(
entry(r#"[{"op": "insert-image-after", "at": "p1", "image": "a.png", "alt": "a"}]"#),
EntryFault::ImageFile(_)
));
assert_eq!(
entry(r#"[{"op": "insert-image-after", "at": "p1", "image": "a.png"}]"#),
EntryFault::Field("alt")
);
}
#[test]
fn refuses_an_image_file_by_its_first_bytes() {
let limits = EditLimits::default();
let gif = |_: &str| Ok(b"GIF89a....".to_vec());
let context = ListContext {
author: None,
read_image: &gif,
};
assert_eq!(
parse(
br#"[{"op": "insert-image-after", "at": "p1", "image": "a.gif", "alt": "a"}]"#,
&limits,
&context
),
Err(ListError::Entry {
index: 0,
fault: EntryFault::Image(ImageError::Format(ImageFormat::Gif)),
})
);
}
}
fn blocks_of(docx: &[u8]) -> Vec<TextBlock> {
Document::open(docx, &carlito(), &Options::base())
.unwrap()
.value
.text_blocks()
.to_vec()
}
fn texts(blocks: &[TextBlock]) -> Vec<String> {
blocks
.iter()
.filter_map(|block| match &block.kind {
TextBlockKind::Paragraph { text, .. } => Some(text.clone()),
_ => None,
})
.collect()
}
fn named(edit: NamedEdit) -> Edit {
Edit::Named(edit)
}
fn png_file(width: u32, height: u32) -> Vec<u8> {
let mut png = Vec::new();
image::RgbImage::new(width, height)
.write_to(&mut std::io::Cursor::new(&mut png), image::ImageFormat::Png)
.unwrap();
png
}
fn insert(at: &str, place: Place, text: &str) -> Edit {
named(NamedEdit::Insert {
at: at.parse().unwrap(),
place,
text: text.to_owned(),
style: None,
})
}
#[test]
fn makes_named_edits_with_raw_edits_of_another_part_and_gives_the_blocks_to_check() {
let original = docx();
let blocks = blocks_of(&original);
let edits = [
raw(
"word/styles.xml",
"w:docDefaults/w:pPrDefault",
"<w:pPrDefault/>",
"<w:pPrDefault/>",
),
named(NamedEdit::Replace {
at: "p2".parse().unwrap(),
find: "two".to_owned(),
with: "2".to_owned(),
}),
insert("t1", Place::Before, "above"),
named(NamedEdit::Delete {
at: "p1".parse().unwrap(),
}),
];
let edited = super::apply(&original, &blocks, &edits, &EditLimits::default()).unwrap();
let after = blocks_of(&edited.value.docx);
assert_eq!(texts(&after), ["above", "cell", "2 "]);
let expected = edited.value.expected.unwrap();
assert_eq!(expected.compare(&after), Ok(()));
assert!(expected.compare(&blocks).is_err());
}
#[test]
fn a_raw_edit_of_the_main_part_leaves_the_blocks_unchecked_and_keeps_the_order_of_a_new_paragraph()
{
let original = docx();
let blocks = blocks_of(&original);
let edits = [
body(
"w:body/w:p[2]",
SECOND,
"<w:p><w:r><w:t>new</w:t></w:r></w:p>",
),
insert("p2", Place::Before, "before"),
];
let edited = super::apply(&original, &blocks, &edits, &EditLimits::default()).unwrap();
assert_eq!(edited.value.expected, None);
assert_eq!(
texts(&blocks_of(&edited.value.docx)),
["one", "cell", "before", "new"]
);
}
#[test]
fn refuses_a_named_edit_inside_an_element_that_a_raw_edit_replaces() {
let original = docx();
let blocks = blocks_of(&original);
let edits = [
body("w:body/w:p[2]", SECOND, "<w:p/>"),
named(NamedEdit::Replace {
at: "p2".parse().unwrap(),
find: "two".to_owned(),
with: "2".to_owned(),
}),
];
let EditError { edit, fault } =
super::apply(&original, &blocks, &edits, &EditLimits::default()).unwrap_err();
assert_eq!((edit, fault), (Some(1), EditFault::Overlap { other: 0 }));
let EditError { edit, fault } =
super::apply(&original, &[], &edits[1..], &EditLimits::default()).unwrap_err();
assert_eq!(edit, Some(0));
assert!(
matches!(fault, EditFault::Named(NamedFault::NoBlock(_))),
"{fault:?}"
);
}
#[test]
fn refuses_a_named_edit_of_a_header_a_note_or_a_text_box() {
let original = docx();
let blocks = blocks_of(&original);
for at in ["h1/p1", "n2/p1", "p1/b1/p1"] {
let edits = [named(NamedEdit::Delete {
at: at.parse().unwrap(),
})];
let EditError { fault, .. } =
super::apply(&original, &blocks, &edits, &EditLimits::default()).unwrap_err();
let EditFault::Named(NamedFault::OutsideBody(refused)) = &fault else {
panic!("{at}: {fault:?}");
};
assert_eq!(refused.to_string(), at);
assert!(fault.to_string().contains("use a raw edit"), "{fault}");
}
}
#[test]
fn makes_a_comment_an_image_and_a_row_with_the_parts_that_they_change_and_add() {
let types = r#"<Types xmlns="http://schemas.openxmlformats.org/package/2006/content-types"><Default Extension="rels" ContentType="application/vnd.openxmlformats-package.relationships+xml"/><Default Extension="xml" ContentType="application/xml"/></Types>"#;
let original = testing::docx(BODY)
.plain_styles()
.part("[Content_Types].xml", types)
.zip()
.unwrap();
let blocks = blocks_of(&original);
let image = super::image_file(png_file(192, 96), &EditLimits::default()).unwrap();
let edits = [
named(NamedEdit::Comment {
at: "p2".parse().unwrap(),
text: "Why?".to_owned(),
find: Some("two".to_owned()),
author: "Ann".to_owned(),
}),
named(NamedEdit::InsertImage {
at: "p1".parse().unwrap(),
place: Place::After,
image,
alt: "A chart".to_owned(),
size: (None, None),
}),
named(NamedEdit::InsertRow {
at: "t1/r1".parse().unwrap(),
place: Place::After,
cells: vec!["row".to_owned()],
}),
];
let edited = super::apply(&original, &blocks, &edits, &EditLimits::default()).unwrap();
let after = blocks_of(&edited.value.docx);
assert_eq!(texts(&after), ["one", "{PICTURE}", "cell", "row", "two "]);
assert_eq!(edited.value.expected.unwrap().compare(&after), Ok(()));
let package = Package::open(&edited.value.docx, &Limits::default()).unwrap();
let mut names: Vec<&str> = package.part_names().collect();
names.sort_unstable();
assert_eq!(
names,
[
"[Content_Types].xml",
"_rels/.rels",
"word/_rels/document.xml.rels",
"word/comments.xml",
"word/document.xml",
"word/media/image1.png",
"word/styles.xml"
]
);
assert_eq!(
package.part("word/media/image1.png"),
Some(png_file(192, 96).as_slice())
);
assert_eq!(
package.part("word/styles.xml"),
Some(part(&original, "word/styles.xml").as_slice())
);
let comments = String::from_utf8(part(&edited.value.docx, "word/comments.xml")).unwrap();
assert!(comments.contains(r#"w:author="Ann""#), "{comments}");
}
#[test]
fn says_that_the_note_of_a_deleted_reference_stays() {
let body = r#"<w:p><w:r><w:footnoteReference w:id="1"/></w:r></w:p><w:p><w:r><w:t>end</w:t></w:r></w:p>"#;
let original = testing::docx(body).plain_styles().zip().unwrap();
let blocks = blocks_of(&original);
let edits = [named(NamedEdit::Delete {
at: "p1".parse().unwrap(),
})];
let edited = super::apply(&original, &blocks, &edits, &EditLimits::default()).unwrap();
let warnings: Vec<&Warning> = edited.warnings.iter().map(|(warning, _)| warning).collect();
assert_eq!(warnings, [&Warning::NoteKept]);
assert_eq!(Warning::NoteKept.kind(), "note-kept");
}