Skip to content
This repository was archived by the owner on Jan 5, 2026. It is now read-only.

Commit 662a0ce

Browse files
author
Lowell Stewart
committed
Unit test DocumentAssembler's xml:space handling
(now that UnicodeMapper.RunToString supports it well enough to tell whether it's working or not!)
1 parent e1cc7e7 commit 662a0ce

File tree

5 files changed

+27
-49
lines changed

5 files changed

+27
-49
lines changed

OpenXmlPowerTools.Tests/DocumentAssemblerTests.cs

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -171,18 +171,22 @@ public void DA259(string name, string data, bool err)
171171
Assert.Equal(4, brCount);
172172
}
173173

174-
[Theory]
175-
[InlineData("DA240-ListPunctuation.docx", "DA-DataList.xml", false)]
176-
public void DA240(string name, string data, bool err)
174+
[Fact]
175+
public void DA240()
177176
{
178-
DA101(name, data, err);
177+
string name = "DA240-Whitespace.docx";
178+
DA101(name, "DA240-Whitespace.xml", false);
179179
var assembledDocx = new FileInfo(Path.Combine(TestUtil.TempDir.FullName, name.Replace(".docx", "-processed-by-DocumentAssembler.docx")));
180180
WmlDocument afterAssembling = new WmlDocument(assembledDocx.FullName);
181181

182-
// when elements are inserted that begin or end with white space (such as the list punctuation
183-
// in this example), make sure white space is preserved
184-
string firstParaText = afterAssembling.MainDocumentPart.Element(W.body).Elements(W.p).First().Value;
185-
Assert.Equal("The children’s names are Greg, Marcia, Peter, Jan, Bobby and Cindy.", firstParaText);
182+
// when elements are inserted that begin or end with white space, make sure white space is preserved
183+
string firstParaTextIncorrect = afterAssembling.MainDocumentPart.Element(W.body).Elements(W.p).First().Value;
184+
Assert.Equal("Content may or may not have spaces: he/she; he, she; he and she.", firstParaTextIncorrect);
185+
// warning: XElement.Value returns the string resulting from direct concatenation of all W.t elements. This is fast but ignores
186+
// proper handling of xml:space="preserve" attributes, which Word honors when rendering content. Below we also check
187+
// the result of UnicodeMapper.RunToString, which has been enhanced to take xml:space="preserve" into account.
188+
string firstParaTextCorrect = InnerText(afterAssembling.MainDocumentPart.Element(W.body).Elements(W.p).First());
189+
Assert.Equal("Content may or may not have spaces: he/she; he, she; he and she.", firstParaTextCorrect);
186190
}
187191

188192
[Theory]
@@ -235,6 +239,14 @@ public void DA103_UseXmlDocument(string name, string data, bool err)
235239
Assert.Equal(err, returnedTemplateError);
236240
}
237241

242+
private static string InnerText(XContainer e)
243+
{
244+
return e.Descendants(W.r)
245+
.Where(r => r.Parent.Name != W.del)
246+
.Select(UnicodeMapper.RunToString)
247+
.StringConcatenate();
248+
}
249+
238250
private static List<string> s_ExpectedErrors = new List<string>()
239251
{
240252
"The 'http://schemas.openxmlformats.org/wordprocessingml/2006/main:evenHBand' attribute is not declared.",

TestFiles/DA-DataList.xml

Lines changed: 0 additions & 41 deletions
This file was deleted.
-15.9 KB
Binary file not shown.

TestFiles/DA240-Whitespace.docx

16.1 KB
Binary file not shown.

TestFiles/DA240-Whitespace.xml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<?xml version="1.0"?>
2+
<Test240>
3+
<MayOrMayNot>may or may not</MayOrMayNot>
4+
<Join1>/</Join1>
5+
<Join2>, </Join2>
6+
<Join3> and </Join3>
7+
</Test240>

0 commit comments

Comments
 (0)