From 5acf69b4b66785ded9079c41c1f50283f03e028e Mon Sep 17 00:00:00 2001 From: David Bosschaert Date: Mon, 10 Mar 2025 11:06:48 +0000 Subject: [PATCH 1/2] Add test to show how to match for an ending link pattern --- src/xml_util.rs | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) diff --git a/src/xml_util.rs b/src/xml_util.rs index fc705ed..2f3a04b 100644 --- a/src/xml_util.rs +++ b/src/xml_util.rs @@ -1210,6 +1210,36 @@ mod tests { Ok(()) } + #[test] + fn test_replace_hyperlink2() -> io::Result<()> { + let orgdir = "./src/test/test_tree7"; + let testdir = testdir!(); + + copy_dir_all(orgdir, &testdir)?; + + let before_doc = fs::read_to_string("./src/test/test_tree7/word/document2.xml")?; + let before = fs::read_to_string("./src/test/test_tree7/word/_rels/document2.xml.rels")?; + + assert!(before.contains("Target=\"http://www.example.com/a\""), "Precondition"); + assert!(before_doc.contains(">www.example.com/a<"), "Precondition"); + assert!(before.contains("Target=\"http://www.example.com/a/b\""), "Precondition"); + assert!(before_doc.contains(">www.example.com/a/b<"), "Precondition"); + + XMLUtil::replace_rel_attr(&testdir.to_string_lossy(), "my-source.docx", + "www.example.com/a$", "www.example.com/a/", + &Some(&testdir.join("output-2.docx").to_string_lossy())); + + let after_doc = fs::read_to_string(testdir.join("word/document2.xml"))?; + let after = fs::read_to_string(testdir.join("word/_rels/document2.xml.rels"))?; + + assert!(after.contains("Target=\"http://www.example.com/a/\"")); + assert!(after_doc.contains(">www.example.com/a<"), "Should not have changed the document text"); + assert!(after.contains("Target=\"http://www.example.com/a/b\""), "Should not have changed this link"); + assert!(after_doc.contains(">www.example.com/a/b<"), "Should not have changed the document text"); + + Ok(()) + } + #[test] fn test_replace_both() -> io::Result<()> { let orgdir = "./src/test/test_tree3"; From ac9b8b1240aa372cd094fb29ba1cfcb0edd88113 Mon Sep 17 00:00:00 2001 From: David Bosschaert Date: Tue, 11 Mar 2025 12:24:15 +0000 Subject: [PATCH 2/2] Add test resources --- src/test/test_tree7/[Content_Types].xml | 20 ++ .../test_tree7/word/_rels/document2.xml.rels | 10 + src/test/test_tree7/word/document2.xml | 305 ++++++++++++++++++ 3 files changed, 335 insertions(+) create mode 100644 src/test/test_tree7/[Content_Types].xml create mode 100644 src/test/test_tree7/word/_rels/document2.xml.rels create mode 100644 src/test/test_tree7/word/document2.xml diff --git a/src/test/test_tree7/[Content_Types].xml b/src/test/test_tree7/[Content_Types].xml new file mode 100644 index 0000000..bb8704e --- /dev/null +++ b/src/test/test_tree7/[Content_Types].xml @@ -0,0 +1,20 @@ + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/src/test/test_tree7/word/_rels/document2.xml.rels b/src/test/test_tree7/word/_rels/document2.xml.rels new file mode 100644 index 0000000..c303745 --- /dev/null +++ b/src/test/test_tree7/word/_rels/document2.xml.rels @@ -0,0 +1,10 @@ + + + + + + + + + + \ No newline at end of file diff --git a/src/test/test_tree7/word/document2.xml b/src/test/test_tree7/word/document2.xml new file mode 100644 index 0000000..ad6bc7e --- /dev/null +++ b/src/test/test_tree7/word/document2.xml @@ -0,0 +1,305 @@ + + + + + + + + + + + + + + Testing 123 + + + + + + + + + + + + + And some some more text + + + + + + and then some + + + + + + + + + + + + + + + + + + + + Something here + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + Blah + + + + + + + + + + + + + + + + + + some + + + + + + + + + + + + + + + + + + None + + + + + + + + + + + + + + + + + + + + Some + + + + + + + some + + + + + + + + + + + + + + some + + + + + + + + + + + + + + + + + + + hello + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + Here’s a hyperlink: + + + + + + + + link + + + + + + + + + + + + + + + + + + + + + And here’s just some text: + + + + + + + www.example.com/a + + + + + + www.example.com/a/b + + + + + + + + + + + \ No newline at end of file