Skip to content

Commit 6e4666f

Browse files
authored
Add test to show how to match for an ending link pattern (#26)
1 parent 17f68d8 commit 6e4666f

File tree

4 files changed

+365
-0
lines changed

4 files changed

+365
-0
lines changed
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<Types xmlns="http://schemas.openxmlformats.org/package/2006/content-types">
3+
<Default Extension="rels" ContentType="application/vnd.openxmlformats-package.relationships+xml" />
4+
<Default Extension="xml" ContentType="application/xml" />
5+
<Default Extension="png" ContentType="image/png" />
6+
<Override PartName="/word/theme/theme1.xml" ContentType="application/vnd.openxmlformats-officedocument.theme+xml" />
7+
<Override PartName="/word/settings.xml" ContentType="application/vnd.openxmlformats-officedocument.wordprocessingml.settings+xml" />
8+
<Override PartName="/word/fontTable.xml" ContentType="application/vnd.openxmlformats-officedocument.wordprocessingml.fontTable+xml" />
9+
<Override PartName="/word/webSettings.xml" ContentType="application/vnd.openxmlformats-officedocument.wordprocessingml.webSettings+xml" />
10+
<Override PartName="/docProps/app.xml" ContentType="application/vnd.openxmlformats-officedocument.extended-properties+xml" />
11+
<Override PartName="/docProps/core.xml" ContentType="application/vnd.openxmlformats-package.core-properties+xml" />
12+
<Override PartName="/word/styles.xml" ContentType="application/vnd.openxmlformats-officedocument.wordprocessingml.styles+xml" />
13+
<Override PartName="/word/document2.xml" ContentType="application/vnd.openxmlformats-officedocument.wordprocessingml.document.main+xml" />
14+
<Override PartName="/word/numbering.xml" ContentType="application/vnd.openxmlformats-officedocument.wordprocessingml.numbering+xml" />
15+
<Override PartName="/customXml/itemProps2.xml" ContentType="application/vnd.openxmlformats-officedocument.customXmlProperties+xml" />
16+
<Override PartName="/customXml/itemProps1.xml" ContentType="application/vnd.openxmlformats-officedocument.customXmlProperties+xml" />
17+
<Override PartName="/customXml/itemProps3.xml" ContentType="application/vnd.openxmlformats-officedocument.customXmlProperties+xml" />
18+
<Override PartName="/docProps/custom.xml" ContentType="application/vnd.openxmlformats-officedocument.custom-properties+xml" />
19+
<Override PartName="/word/intelligence2.xml" ContentType="application/vnd.ms-office.intelligence2+xml" />
20+
</Types>
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
2+
<Relationships xmlns="http://schemas.openxmlformats.org/package/2006/relationships">
3+
<Relationship Id="rId3" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/webSettings" Target="webSettings.xml"/>
4+
<Relationship Id="rId2" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/settings" Target="settings.xml"/>
5+
<Relationship Id="rId1" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/styles" Target="styles.xml"/>
6+
<Relationship Id="rId6" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/theme" Target="theme/theme1.xml"/>
7+
<Relationship Id="rId5" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/fontTable" Target="fontTable.xml"/>
8+
<Relationship Id="rId4" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/hyperlink" Target="http://www.example.com/a" TargetMode="External"/>
9+
<Relationship Id="rId7" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/hyperlink" Target="http://www.example.com/a/b" TargetMode="External"/>
10+
</Relationships>
Lines changed: 305 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,305 @@
1+
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
2+
<w:document xmlns:wpc="http://schemas.microsoft.com/office/word/2010/wordprocessingCanvas"
3+
xmlns:cx="http://schemas.microsoft.com/office/drawing/2014/chartex"
4+
xmlns:cx1="http://schemas.microsoft.com/office/drawing/2015/9/8/chartex"
5+
xmlns:cx2="http://schemas.microsoft.com/office/drawing/2015/10/21/chartex"
6+
xmlns:cx3="http://schemas.microsoft.com/office/drawing/2016/5/9/chartex"
7+
xmlns:cx4="http://schemas.microsoft.com/office/drawing/2016/5/10/chartex"
8+
xmlns:cx5="http://schemas.microsoft.com/office/drawing/2016/5/11/chartex"
9+
xmlns:cx6="http://schemas.microsoft.com/office/drawing/2016/5/12/chartex"
10+
xmlns:cx7="http://schemas.microsoft.com/office/drawing/2016/5/13/chartex"
11+
xmlns:cx8="http://schemas.microsoft.com/office/drawing/2016/5/14/chartex"
12+
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
13+
xmlns:aink="http://schemas.microsoft.com/office/drawing/2016/ink"
14+
xmlns:am3d="http://schemas.microsoft.com/office/drawing/2017/model3d"
15+
xmlns:o="urn:schemas-microsoft-com:office:office"
16+
xmlns:oel="http://schemas.microsoft.com/office/2019/extlst"
17+
xmlns:r="http://schemas.openxmlformats.org/officeDocument/2006/relationships"
18+
xmlns:m="http://schemas.openxmlformats.org/officeDocument/2006/math"
19+
xmlns:v="urn:schemas-microsoft-com:vml"
20+
xmlns:wp14="http://schemas.microsoft.com/office/word/2010/wordprocessingDrawing"
21+
xmlns:wp="http://schemas.openxmlformats.org/drawingml/2006/wordprocessingDrawing"
22+
xmlns:w10="urn:schemas-microsoft-com:office:word"
23+
xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main"
24+
xmlns:w14="http://schemas.microsoft.com/office/word/2010/wordml"
25+
xmlns:w15="http://schemas.microsoft.com/office/word/2012/wordml"
26+
xmlns:w16cex="http://schemas.microsoft.com/office/word/2018/wordml/cex"
27+
xmlns:w16cid="http://schemas.microsoft.com/office/word/2016/wordml/cid"
28+
xmlns:w16="http://schemas.microsoft.com/office/word/2018/wordml"
29+
xmlns:w16sdtdh="http://schemas.microsoft.com/office/word/2020/wordml/sdtdatahash"
30+
xmlns:w16se="http://schemas.microsoft.com/office/word/2015/wordml/symex"
31+
xmlns:wpg="http://schemas.microsoft.com/office/word/2010/wordprocessingGroup"
32+
xmlns:wpi="http://schemas.microsoft.com/office/word/2010/wordprocessingInk"
33+
xmlns:wne="http://schemas.microsoft.com/office/word/2006/wordml"
34+
xmlns:wps="http://schemas.microsoft.com/office/word/2010/wordprocessingShape" mc:Ignorable="w14 w15 w16se w16cid w16 w16cex w16sdtdh wp14">
35+
<w:body>
36+
<w:p w14:paraId="30AA36DC" w14:textId="52E86AE1" w:rsidR="00146782" w:rsidRDefault="008D365C">
37+
<w:pPr>
38+
<w:rPr>
39+
<w:lang w:val="en-US"/>
40+
</w:rPr>
41+
</w:pPr>
42+
<w:r>
43+
<w:rPr>
44+
<w:lang w:val="en-US"/>
45+
</w:rPr>
46+
<w:t>Testing 123</w:t>
47+
</w:r>
48+
</w:p>
49+
<w:p w14:paraId="288A8F5D" w14:textId="7608D659" w:rsidR="008D365C" w:rsidRDefault="008D365C">
50+
<w:pPr>
51+
<w:rPr>
52+
<w:lang w:val="en-US"/>
53+
</w:rPr>
54+
</w:pPr>
55+
<w:r>
56+
<w:rPr>
57+
<w:lang w:val="en-US"/>
58+
</w:rPr>
59+
<w:t>And some some more text</w:t>
60+
</w:r>
61+
<w:r w:rsidR="00B47895">
62+
<w:rPr>
63+
<w:lang w:val="en-US"/>
64+
</w:rPr>
65+
<w:t xml:space="preserve"> and then some</w:t>
66+
</w:r>
67+
</w:p>
68+
<w:p w14:paraId="39BAD9A3" w14:textId="77777777" w:rsidR="00B47895" w:rsidRDefault="00B47895">
69+
<w:pPr>
70+
<w:rPr>
71+
<w:lang w:val="en-US"/>
72+
</w:rPr>
73+
</w:pPr>
74+
</w:p>
75+
<w:p w14:paraId="2CBF7A40" w14:textId="3023CF82" w:rsidR="00B47895" w:rsidRDefault="00B47895">
76+
<w:pPr>
77+
<w:rPr>
78+
<w:lang w:val="en-US"/>
79+
</w:rPr>
80+
</w:pPr>
81+
<w:r>
82+
<w:rPr>
83+
<w:lang w:val="en-US"/>
84+
</w:rPr>
85+
<w:t>Something here</w:t>
86+
</w:r>
87+
</w:p>
88+
<w:p w14:paraId="6EBBCD16" w14:textId="77777777" w:rsidR="00B47895" w:rsidRDefault="00B47895">
89+
<w:pPr>
90+
<w:rPr>
91+
<w:lang w:val="en-US"/>
92+
</w:rPr>
93+
</w:pPr>
94+
</w:p>
95+
<w:tbl>
96+
<w:tblPr>
97+
<w:tblStyle w:val="TableGrid"/>
98+
<w:tblW w:w="0" w:type="auto"/>
99+
<w:tblLook w:val="04A0" w:firstRow="1" w:lastRow="0" w:firstColumn="1" w:lastColumn="0" w:noHBand="0" w:noVBand="1"/>
100+
</w:tblPr>
101+
<w:tblGrid>
102+
<w:gridCol w:w="3005"/>
103+
<w:gridCol w:w="3005"/>
104+
<w:gridCol w:w="3006"/>
105+
</w:tblGrid>
106+
<w:tr w:rsidR="00B47895" w14:paraId="4B71DB47" w14:textId="77777777" w:rsidTr="00B47895">
107+
<w:tc>
108+
<w:tcPr>
109+
<w:tcW w:w="3005" w:type="dxa"/>
110+
</w:tcPr>
111+
<w:p w14:paraId="5EB79979" w14:textId="46409BE4" w:rsidR="00B47895" w:rsidRDefault="00B47895">
112+
<w:pPr>
113+
<w:rPr>
114+
<w:lang w:val="en-US"/>
115+
</w:rPr>
116+
</w:pPr>
117+
<w:r>
118+
<w:rPr>
119+
<w:lang w:val="en-US"/>
120+
</w:rPr>
121+
<w:t>Blah</w:t>
122+
</w:r>
123+
</w:p>
124+
</w:tc>
125+
<w:tc>
126+
<w:tcPr>
127+
<w:tcW w:w="3005" w:type="dxa"/>
128+
</w:tcPr>
129+
<w:p w14:paraId="4487AD6B" w14:textId="5830B00E" w:rsidR="00B47895" w:rsidRDefault="00B47895">
130+
<w:pPr>
131+
<w:rPr>
132+
<w:lang w:val="en-US"/>
133+
</w:rPr>
134+
</w:pPr>
135+
<w:r>
136+
<w:rPr>
137+
<w:lang w:val="en-US"/>
138+
</w:rPr>
139+
<w:t>some</w:t>
140+
</w:r>
141+
</w:p>
142+
</w:tc>
143+
<w:tc>
144+
<w:tcPr>
145+
<w:tcW w:w="3006" w:type="dxa"/>
146+
</w:tcPr>
147+
<w:p w14:paraId="2414C261" w14:textId="57AF738E" w:rsidR="00B47895" w:rsidRDefault="00B47895">
148+
<w:pPr>
149+
<w:rPr>
150+
<w:lang w:val="en-US"/>
151+
</w:rPr>
152+
</w:pPr>
153+
<w:r>
154+
<w:rPr>
155+
<w:lang w:val="en-US"/>
156+
</w:rPr>
157+
<w:t>None</w:t>
158+
</w:r>
159+
</w:p>
160+
</w:tc>
161+
</w:tr>
162+
<w:tr w:rsidR="00B47895" w14:paraId="75A8A72B" w14:textId="77777777" w:rsidTr="00B47895">
163+
<w:tc>
164+
<w:tcPr>
165+
<w:tcW w:w="3005" w:type="dxa"/>
166+
</w:tcPr>
167+
<w:p w14:paraId="69F61D0A" w14:textId="1DE2D5C9" w:rsidR="00B47895" w:rsidRDefault="00B47895">
168+
<w:pPr>
169+
<w:rPr>
170+
<w:lang w:val="en-US"/>
171+
</w:rPr>
172+
</w:pPr>
173+
<w:r>
174+
<w:rPr>
175+
<w:lang w:val="en-US"/>
176+
</w:rPr>
177+
<w:t xml:space="preserve">Some </w:t>
178+
</w:r>
179+
<w:proofErr w:type="spellStart"/>
180+
<w:r>
181+
<w:rPr>
182+
<w:lang w:val="en-US"/>
183+
</w:rPr>
184+
<w:t>some</w:t>
185+
</w:r>
186+
<w:proofErr w:type="spellEnd"/>
187+
<w:r>
188+
<w:rPr>
189+
<w:lang w:val="en-US"/>
190+
</w:rPr>
191+
<w:t xml:space="preserve"></w:t>
192+
</w:r>
193+
<w:proofErr w:type="spellStart"/>
194+
<w:r>
195+
<w:rPr>
196+
<w:lang w:val="en-US"/>
197+
</w:rPr>
198+
<w:t>some</w:t>
199+
</w:r>
200+
<w:proofErr w:type="spellEnd"/>
201+
</w:p>
202+
</w:tc>
203+
<w:tc>
204+
<w:tcPr>
205+
<w:tcW w:w="3005" w:type="dxa"/>
206+
</w:tcPr>
207+
<w:p w14:paraId="21C4F834" w14:textId="34C11663" w:rsidR="00B47895" w:rsidRDefault="00B47895">
208+
<w:pPr>
209+
<w:rPr>
210+
<w:lang w:val="en-US"/>
211+
</w:rPr>
212+
</w:pPr>
213+
<w:r>
214+
<w:rPr>
215+
<w:lang w:val="en-US"/>
216+
</w:rPr>
217+
<w:t>hello</w:t>
218+
</w:r>
219+
</w:p>
220+
</w:tc>
221+
<w:tc>
222+
<w:tcPr>
223+
<w:tcW w:w="3006" w:type="dxa"/>
224+
</w:tcPr>
225+
<w:p w14:paraId="1C72A820" w14:textId="77777777" w:rsidR="00B47895" w:rsidRDefault="00B47895">
226+
<w:pPr>
227+
<w:rPr>
228+
<w:lang w:val="en-US"/>
229+
</w:rPr>
230+
</w:pPr>
231+
</w:p>
232+
</w:tc>
233+
</w:tr>
234+
</w:tbl>
235+
<w:p w14:paraId="661AA0A6" w14:textId="77777777" w:rsidR="00B47895" w:rsidRDefault="00B47895">
236+
<w:pPr>
237+
<w:rPr>
238+
<w:lang w:val="en-US"/>
239+
</w:rPr>
240+
</w:pPr>
241+
</w:p>
242+
<w:p w14:paraId="684CE415" w14:textId="562692B0" w:rsidR="00241272" w:rsidRDefault="00241272">
243+
<w:pPr>
244+
<w:rPr>
245+
<w:lang w:val="en-US"/>
246+
</w:rPr>
247+
</w:pPr>
248+
<w:r>
249+
<w:rPr>
250+
<w:lang w:val="en-US"/>
251+
</w:rPr>
252+
<w:t xml:space="preserve">Here’s a hyperlink: </w:t>
253+
</w:r>
254+
<w:hyperlink r:id="rId4" w:history="1">
255+
<w:r w:rsidRPr="00241272">
256+
<w:rPr>
257+
<w:rStyle w:val="Hyperlink"/>
258+
<w:lang w:val="en-US"/>
259+
</w:rPr>
260+
<w:t>link</w:t>
261+
</w:r>
262+
</w:hyperlink>
263+
</w:p>
264+
<w:p w14:paraId="38F5C028" w14:textId="77777777" w:rsidR="00241272" w:rsidRDefault="00241272">
265+
<w:pPr>
266+
<w:rPr>
267+
<w:lang w:val="en-US"/>
268+
</w:rPr>
269+
</w:pPr>
270+
</w:p>
271+
<w:p w14:paraId="2297FEA1" w14:textId="418C909A" w:rsidR="00241272" w:rsidRPr="008D365C" w:rsidRDefault="00241272">
272+
<w:pPr>
273+
<w:rPr>
274+
<w:lang w:val="en-US"/>
275+
</w:rPr>
276+
</w:pPr>
277+
<w:r>
278+
<w:rPr>
279+
<w:lang w:val="en-US"/>
280+
</w:rPr>
281+
<w:t xml:space="preserve">And here’s just some text: </w:t>
282+
</w:r>
283+
<w:proofErr w:type="gramStart"/>
284+
<w:r>
285+
<w:rPr>
286+
<w:lang w:val="en-US"/>
287+
</w:rPr>
288+
<w:t>www.example.com/a</w:t>
289+
</w:r>
290+
<w:r>
291+
<w:rPr>
292+
<w:lang w:val="en-US"/>
293+
</w:rPr>
294+
<w:t>www.example.com/a/b</w:t>
295+
</w:r>
296+
<w:proofErr w:type="gramEnd"/>
297+
</w:p>
298+
<w:sectPr w:rsidR="00241272" w:rsidRPr="008D365C">
299+
<w:pgSz w:w="11906" w:h="16838"/>
300+
<w:pgMar w:top="1440" w:right="1440" w:bottom="1440" w:left="1440" w:header="708" w:footer="708" w:gutter="0"/>
301+
<w:cols w:space="708"/>
302+
<w:docGrid w:linePitch="360"/>
303+
</w:sectPr>
304+
</w:body>
305+
</w:document>

src/xml_util.rs

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1210,6 +1210,36 @@ mod tests {
12101210
Ok(())
12111211
}
12121212

1213+
#[test]
1214+
fn test_replace_hyperlink2() -> io::Result<()> {
1215+
let orgdir = "./src/test/test_tree7";
1216+
let testdir = testdir!();
1217+
1218+
copy_dir_all(orgdir, &testdir)?;
1219+
1220+
let before_doc = fs::read_to_string("./src/test/test_tree7/word/document2.xml")?;
1221+
let before = fs::read_to_string("./src/test/test_tree7/word/_rels/document2.xml.rels")?;
1222+
1223+
assert!(before.contains("Target=\"http://www.example.com/a\""), "Precondition");
1224+
assert!(before_doc.contains(">www.example.com/a<"), "Precondition");
1225+
assert!(before.contains("Target=\"http://www.example.com/a/b\""), "Precondition");
1226+
assert!(before_doc.contains(">www.example.com/a/b<"), "Precondition");
1227+
1228+
XMLUtil::replace_rel_attr(&testdir.to_string_lossy(), "my-source.docx",
1229+
"www.example.com/a$", "www.example.com/a/",
1230+
&Some(&testdir.join("output-2.docx").to_string_lossy()));
1231+
1232+
let after_doc = fs::read_to_string(testdir.join("word/document2.xml"))?;
1233+
let after = fs::read_to_string(testdir.join("word/_rels/document2.xml.rels"))?;
1234+
1235+
assert!(after.contains("Target=\"http://www.example.com/a/\""));
1236+
assert!(after_doc.contains(">www.example.com/a<"), "Should not have changed the document text");
1237+
assert!(after.contains("Target=\"http://www.example.com/a/b\""), "Should not have changed this link");
1238+
assert!(after_doc.contains(">www.example.com/a/b<"), "Should not have changed the document text");
1239+
1240+
Ok(())
1241+
}
1242+
12131243
#[test]
12141244
fn test_replace_both() -> io::Result<()> {
12151245
let orgdir = "./src/test/test_tree3";

0 commit comments

Comments
 (0)