forked from OpenXmlDev/Open-Xml-PowerTools
-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathUnicodeMapperTests.cs
More file actions
277 lines (250 loc) · 12.5 KB
/
UnicodeMapperTests.cs
File metadata and controls
277 lines (250 loc) · 12.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
using Codeuctivity.OpenXmlPowerTools;
using System.Linq;
using System.Xml.Linq;
using Xunit;
namespace Codeuctivity.Tests
{
public class UnicodeMapperTests
{
[Fact]
public void CanStringifyRunAndTextElements()
{
const string textValue = "Hello World!";
var textElement = new XElement(W.t, textValue);
var runElement = new XElement(W.r, textElement);
var formattedRunElement = new XElement(W.r, new XElement(W.rPr, new XElement(W.b)), textElement);
Assert.Equal(textValue, UnicodeMapper.RunToString(textElement));
Assert.Equal(textValue, UnicodeMapper.RunToString(runElement));
Assert.Equal(textValue, UnicodeMapper.RunToString(formattedRunElement));
}
[Fact]
public void CanStringifySpecialElements()
{
Assert.Equal(UnicodeMapper.CarriageReturn,
UnicodeMapper.RunToString(new XElement(W.cr)).First());
Assert.Equal(UnicodeMapper.CarriageReturn,
UnicodeMapper.RunToString(new XElement(W.br)).First());
Assert.Equal(UnicodeMapper.FormFeed,
UnicodeMapper.RunToString(new XElement(W.br, new XAttribute(W.type, "page"))).First());
Assert.Equal(UnicodeMapper.NonBreakingHyphen,
UnicodeMapper.RunToString(new XElement(W.noBreakHyphen)).First());
Assert.Equal(UnicodeMapper.SoftHyphen,
UnicodeMapper.RunToString(new XElement(W.softHyphen)).First());
Assert.Equal(UnicodeMapper.HorizontalTabulation,
UnicodeMapper.RunToString(new XElement(W.tab)).First());
}
[Fact]
public void CanCreateRunChildElementsFromSpecialCharacters()
{
Assert.Equal(W.br, UnicodeMapper.CharToRunChild(UnicodeMapper.CarriageReturn).Name);
Assert.Equal(W.noBreakHyphen, UnicodeMapper.CharToRunChild(UnicodeMapper.NonBreakingHyphen).Name);
Assert.Equal(W.softHyphen, UnicodeMapper.CharToRunChild(UnicodeMapper.SoftHyphen).Name);
Assert.Equal(W.tab, UnicodeMapper.CharToRunChild(UnicodeMapper.HorizontalTabulation).Name);
var element = UnicodeMapper.CharToRunChild(UnicodeMapper.FormFeed);
Assert.Equal(W.br, element.Name);
Assert.Equal("page", element.Attribute(W.type).Value);
Assert.Equal(W.br, UnicodeMapper.CharToRunChild('\r').Name);
}
[Fact]
public void CanCreateCoalescedRuns()
{
const string textString = "This is only text.";
const string mixedString = "First\tSecond\tThird";
var textRuns = UnicodeMapper.StringToCoalescedRunList(textString, null);
var mixedRuns = UnicodeMapper.StringToCoalescedRunList(mixedString, null);
Assert.Single(textRuns);
Assert.Equal(5, mixedRuns.Count);
Assert.Equal("First", mixedRuns.Elements(W.t).Skip(0).First().Value);
Assert.Equal("Second", mixedRuns.Elements(W.t).Skip(1).First().Value);
Assert.Equal("Third", mixedRuns.Elements(W.t).Skip(2).First().Value);
}
[Fact]
public void CanMapSymbols()
{
var sym1 = new XElement(W.sym,
new XAttribute(W.font, "Wingdings"),
new XAttribute(W._char, "F028"));
var charFromSym1 = UnicodeMapper.SymToChar(sym1);
var symFromChar1 = UnicodeMapper.CharToRunChild(charFromSym1);
var sym2 = new XElement(W.sym,
new XAttribute(W._char, "F028"),
new XAttribute(W.font, "Wingdings"));
var charFromSym2 = UnicodeMapper.SymToChar(sym2);
var sym3 = new XElement(W.sym,
new XAttribute(XNamespace.Xmlns + "w", W.w),
new XAttribute(W.font, "Wingdings"),
new XAttribute(W._char, "F028"));
var charFromSym3 = UnicodeMapper.SymToChar(sym3);
var sym4 = new XElement(W.sym,
new XAttribute(XNamespace.Xmlns + "w", W.w),
new XAttribute(W.font, "Webdings"),
new XAttribute(W._char, "F028"));
var charFromSym4 = UnicodeMapper.SymToChar(sym4);
var symFromChar4 = UnicodeMapper.CharToRunChild(charFromSym4);
Assert.Equal(charFromSym1, charFromSym2);
Assert.Equal(charFromSym1, charFromSym3);
Assert.NotEqual(charFromSym1, charFromSym4);
Assert.Equal("F028", symFromChar1.Attribute(W._char).Value);
Assert.Equal("Wingdings", symFromChar1.Attribute(W.font).Value);
Assert.Equal("F028", symFromChar4.Attribute(W._char).Value);
Assert.Equal("Webdings", symFromChar4.Attribute(W.font).Value);
}
[Fact]
public void CanStringifySymbols()
{
var charFromSym1 = UnicodeMapper.SymToChar("Wingdings", '\uF028');
var charFromSym2 = UnicodeMapper.SymToChar("Wingdings", 0xF028);
var charFromSym3 = UnicodeMapper.SymToChar("Wingdings", "F028");
var symFromChar1 = UnicodeMapper.CharToRunChild(charFromSym1);
var symFromChar2 = UnicodeMapper.CharToRunChild(charFromSym2);
var symFromChar3 = UnicodeMapper.CharToRunChild(charFromSym3);
Assert.Equal(charFromSym1, charFromSym2);
Assert.Equal(charFromSym1, charFromSym3);
Assert.Equal(symFromChar1.ToString(SaveOptions.None), symFromChar2.ToString(SaveOptions.None));
Assert.Equal(symFromChar1.ToString(SaveOptions.None), symFromChar3.ToString(SaveOptions.None));
}
private const string LastRenderedPageBreakXmlString =
@"<w:document xmlns:w=""http://schemas.openxmlformats.org/wordprocessingml/2006/main"">
<w:body>
<w:p>
<w:r>
<w:t>ThisIsAParagraphContainingNoNaturalLi</w:t>
</w:r>
<w:r>
<w:lastRenderedPageBreak/>
<w:t>neBreaksSoTheLineBreakIsForced.</w:t>
</w:r>
</w:p>
</w:body>
</w:document>";
[Fact]
public void IgnoresTemporaryLayoutMarkers()
{
XDocument partDocument = XDocument.Parse(LastRenderedPageBreakXmlString);
XElement p = partDocument.Descendants(W.p).Last();
string actual = p.Descendants(W.r)
.Select(UnicodeMapper.RunToString)
.StringConcatenate();
// p.Value is "the concatenated text content of this element", which
// (in THIS test case, which does not feature any symbols or special
// characters) should exactly match the output of UnicodeMapper:
Assert.Equal(p.Value, actual);
}
private const string PreserveSpacingXmlString =
@"<w:document xmlns:w=""http://schemas.openxmlformats.org/wordprocessingml/2006/main"">
<w:body>
<w:p>
<w:r>
<w:t xml:space=""preserve"">The following space is retained: </w:t>
</w:r>
<w:r>
<w:t>but this one is not: </w:t>
</w:r>
<w:r>
<w:t xml:space=""preserve"">. Similarly these two lines should have only a space between them: </w:t>
</w:r>
<w:r>
<w:t>
Line 1!
Line 2!
</w:t>
</w:r>
</w:p>
</w:body>
</w:document>";
[Fact]
public void HonorsXmlSpace()
{
// This somewhat rudimentary test is superceded by TreatsXmlSpaceLikeWord() below,
// but it has been left in to provide a simple/direct illustration of a couple of
// the specific test cases covered by that more extensive suite.
XDocument partDocument = XDocument.Parse(PreserveSpacingXmlString);
XElement p = partDocument.Descendants(W.p).Last();
string innerText = p.Descendants(W.r)
.Select(UnicodeMapper.RunToString)
.StringConcatenate();
Assert.Equal(@"The following space is retained: but this one is not:. Similarly these two lines should have only a space between them: Line 1! Line 2!", innerText);
}
// Verifies that UnicodeMapper.RunToString interprets whitespace in <w:t> elements
// exactly the way Microsoft Word does, including honoring xml:space="preserve".
// This is essential because RunToString is used by higher‑level features
// (OpenXmlRegex, DocumentAssembler, etc.) that rely on its output to reflect the
// text an end‑user would actually see and edit in Word.
//
// Word accepts a wide range of “valid” DOCX input, but it normalizes that input
// into a canonical form when displaying or saving the document. These tests
// compare RunToString’s output against Word’s canonicalized output to ensure
// that whitespace is treated as semantic content in the same way Word treats it.
[Fact]
public void TreatsXmlSpaceLikeWord()
{
var sourceDir = new System.IO.DirectoryInfo("../../../../TestFiles/");
// Test document: crafted to include many whitespace patterns that Word accepts as valid input
var testDoc = new System.IO.FileInfo(System.IO.Path.Combine(sourceDir.FullName, "UM-Whitespace-test.docx"));
var testWmlDoc = new WmlDocument(testDoc.FullName);
var testParagraphs = testWmlDoc.MainDocumentPart
.Element(W.body)
.Elements(W.p).ToList();
// Canonical document: the same test document after being opened and saved by Word,
// representing Word's own normalized interpretation of that whitespace
var expectedDoc = new System.IO.FileInfo(System.IO.Path.Combine(sourceDir.FullName, "UM-Whitespace-Word-saved.docx"));
var expectedWmlDoc = new WmlDocument(expectedDoc.FullName);
var expectedParagraphs = expectedWmlDoc.MainDocumentPart
.Element(W.body)
.Elements(W.p).ToList();
// Iterate through pairs of paragraphs (test name, test content, expected result)
for (int i = 0; i < testParagraphs.Count - 1; i += 2)
{
var testNameParagraph = testParagraphs[i];
var testContentParagraph = testParagraphs[i + 1];
// Get the test name from the first paragraph
var testName = testNameParagraph.Descendants(W.t)
.Select(t => (string)t)
.StringConcatenate();
// Get the actual result by calling UnicodeMapper.RunToString on the test content runs
var actualResult = testContentParagraph.Descendants(W.r)
.Select(UnicodeMapper.RunToString)
.StringConcatenate();
// Find corresponding expected result paragraph (same index in expected document)
var expectedResult = ExtractExpectedFromWord(expectedParagraphs[i + 1]);
Assert.True(
expectedResult == actualResult,
$"Test '{testName}' failed. Expected: [{expectedResult}] Actual: [{actualResult}]"
);
}
}
// Extracts the expected text from Word’s canonicalized output for the whitespace tests.
// This helper intentionally handles *only* the constructs that Word emits in the saved
// version of UM-whitespace-test.docx:
// • <w:t> → literal text
// • <w:tab/> → '\t'
// • <w:lastRenderedPageBreak/> (intentionally ignored)
// If any other run-level element appears, it means Word has emitted something this test
// was not designed to handle, and the test fails loudly. This prevents the helper
// from drifting toward reimplementing UnicodeMapper.RunToString.
private static string ExtractExpectedFromWord(XElement p)
{
var sb = new System.Text.StringBuilder();
foreach (var run in p.Elements(W.r))
{
foreach (var child in run.Elements())
{
if (child.Name == W.t)
{
sb.Append((string)child);
}
else if (child.Name == W.tab)
{
sb.Append('\t');
}
else if (child.Name != W.lastRenderedPageBreak)
{
throw new System.InvalidOperationException(
$"Unexpected element <{child.Name.LocalName}> encountered in expected Word output.");
}
}
}
return sb.ToString();
}
}
}