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
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
|
package org.zwobble.mammoth.tests.conversion;
import org.hamcrest.Matcher;
import org.junit.Test;
import org.zwobble.mammoth.internal.conversion.DocumentToHtml;
import org.zwobble.mammoth.internal.conversion.DocumentToHtmlOptions;
import org.zwobble.mammoth.internal.documents.*;
import org.zwobble.mammoth.internal.html.Html;
import org.zwobble.mammoth.internal.html.HtmlNode;
import org.zwobble.mammoth.internal.results.InternalResult;
import org.zwobble.mammoth.internal.styles.*;
import java.io.ByteArrayInputStream;
import java.util.List;
import java.util.Optional;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.*;
import static org.zwobble.mammoth.internal.documents.VerticalAlignment.SUBSCRIPT;
import static org.zwobble.mammoth.internal.documents.VerticalAlignment.SUPERSCRIPT;
import static org.zwobble.mammoth.internal.util.Lists.list;
import static org.zwobble.mammoth.internal.util.Maps.map;
import static org.zwobble.mammoth.internal.util.Sets.set;
import static org.zwobble.mammoth.tests.DeepReflectionMatcher.deepEquals;
import static org.zwobble.mammoth.tests.ResultMatchers.isInternalSuccess;
import static org.zwobble.mammoth.tests.documents.DocumentElementMakers.*;
public class DocumentToHtmlTests {
@Test
public void plainParagraphIsConvertedToPlainParagraph() {
assertThat(
convertToHtml(paragraph(withChildren(runWithText("Hello")))),
deepEquals(list(Html.element("p", list(Html.text("Hello"))))));
}
@Test
public void forceWriteIsInsertedIntoParagraphIfEmptyParagraphsShouldBePreserved() {
DocumentToHtmlOptions options = DocumentToHtmlOptions.DEFAULT.preserveEmptyParagraphs();
assertThat(
DocumentToHtml.convertToHtml(paragraph(), options),
isSuccess(list(Html.element("p", list(Html.FORCE_WRITE)))));
}
@Test
public void multipleParagraphsInDocumentAreConvertedToMultipleParagraphs() {
assertThat(
convertToHtml(document(
withChildren(
paragraph(withChildren(runWithText("Hello"))),
paragraph(withChildren(runWithText("there")))
)
)),
deepEquals(list(
Html.element("p", list(Html.text("Hello"))),
Html.element("p", list(Html.text("there"))))));
}
@Test
public void paragraphStyleMappingsCanBeUsedToMapParagraphs() {
assertThat(
convertToHtml(
paragraph(withStyle(new Style("TipsParagraph", Optional.empty()))),
StyleMap.builder()
.mapParagraph(
ParagraphMatcher.styleId("TipsParagraph"),
HtmlPath.element("p", map("class", "tip")))
.build()),
deepEquals(list(Html.element("p", map("class", "tip")))));
}
@Test
public void warningIfParagraphHasUnrecognisedStyle() {
assertThat(
convertToHtmlResult(
paragraph(withStyle(new Style("TipsParagraph", Optional.of("Tips Paragraph"))))),
deepEquals(new InternalResult<>(
list(Html.element("p")),
set("Unrecognised paragraph style: Tips Paragraph (Style ID: TipsParagraph)"))));
}
@Test
public void runStyleMappingsCanBeUsedToMapRuns() {
assertThat(
convertToHtml(
run(withStyle(new Style("TipsRun", Optional.empty()))),
StyleMap.builder()
.mapRun(
RunMatcher.styleId("TipsRun"),
HtmlPath.element("span", map("class", "tip")))
.build()),
deepEquals(list(Html.element("span", map("class", "tip")))));
}
@Test
public void warningIfRunHasUnrecognisedStyle() {
assertThat(
convertToHtmlResult(
run(
withStyle(new Style("TipsRun", Optional.of("Tips Run"))),
withChildren(new Text("Hello")))),
deepEquals(new InternalResult<>(
list(Html.text("Hello")),
set("Unrecognised run style: Tips Run (Style ID: TipsRun)"))));
}
@Test
public void boldRunsAreWrappedInStrongTagsByDefault() {
assertThat(
convertToHtml(run(withBold(true), withChildren(new Text("Hello")))),
deepEquals(list(Html.collapsibleElement("strong", list(Html.text("Hello"))))));
}
@Test
public void boldRunsCanBeMappedUsingStyleMapping() {
assertThat(
convertToHtml(
run(withBold(true), withChildren(new Text("Hello"))),
StyleMap.builder().bold(HtmlPath.element("em")).build()),
deepEquals(list(Html.element("em", list(Html.text("Hello"))))));
}
@Test
public void italicRunsAreWrappedInEmphasisTags() {
assertThat(
convertToHtml(run(withItalic(true), withChildren(new Text("Hello")))),
deepEquals(list(Html.collapsibleElement("em", list(Html.text("Hello"))))));
}
@Test
public void italicRunsCanBeMappedUsingStyleMapping() {
assertThat(
convertToHtml(
run(withItalic(true), withChildren(new Text("Hello"))),
StyleMap.builder().italic(HtmlPath.element("strong")).build()),
deepEquals(list(Html.element("strong", list(Html.text("Hello"))))));
}
@Test
public void underliningIsIgnoredByDefault() {
assertThat(
convertToHtml(run(withUnderline(true), withChildren(new Text("Hello")))),
deepEquals(list(Html.text("Hello"))));
}
@Test
public void underliningCanBeMappedUsingStyleMapping() {
assertThat(
convertToHtml(
run(withUnderline(true), withChildren(new Text("Hello"))),
StyleMap.builder().underline(HtmlPath.element("em")).build()),
deepEquals(list(Html.element("em", list(Html.text("Hello"))))));
}
@Test
public void struckthroughRunsAreWrappedInStrikethroughTagsByDefault() {
assertThat(
convertToHtml(run(withStrikethrough(true), withChildren(new Text("Hello")))),
deepEquals(list(Html.collapsibleElement("s", list(Html.text("Hello"))))));
}
@Test
public void struckthroughRunsCanBeMappedUsingStyleMapping() {
assertThat(
convertToHtml(
run(withStrikethrough(true), withChildren(new Text("Hello"))),
StyleMap.builder().strikethrough(HtmlPath.element("del")).build()),
deepEquals(list(Html.element("del", list(Html.text("Hello"))))));
}
@Test
public void superscriptRunsAreWrappedInSuperscriptTags() {
assertThat(
convertToHtml(run(withVerticalAlignment(SUPERSCRIPT), withChildren(new Text("Hello")))),
deepEquals(list(Html.collapsibleElement("sup", list(Html.text("Hello"))))));
}
@Test
public void subscriptRunsAreWrappedInSubscriptTags() {
assertThat(
convertToHtml(run(withVerticalAlignment(SUBSCRIPT), withChildren(new Text("Hello")))),
deepEquals(list(Html.collapsibleElement("sub", list(Html.text("Hello"))))));
}
@Test
public void tabIsConvertedToTabInHtmlText() {
assertThat(
convertToHtml(Tab.TAB),
deepEquals(list(Html.text("\t"))));
}
@Test
public void lineBreakIsConvertedToBreakElement() {
assertThat(
convertToHtml(Break.LINE_BREAK),
deepEquals(list(Html.element("br"))));
}
@Test
public void breaksThatAreNotLineBreaksAreIgnored() {
assertThat(
convertToHtml(Break.PAGE_BREAK),
deepEquals(list())
);
}
@Test
public void breaksCanBeMappedUsingStyleMappings() {
assertThat(
convertToHtml(
Break.PAGE_BREAK,
StyleMap.builder().mapBreak(BreakMatcher.PAGE_BREAK, HtmlPath.element("div")).build()
),
deepEquals(list(Html.element("div")))
);
}
@Test
public void tableIsConvertedToHtmlTable() {
assertThat(
convertToHtml(new Table(list(
new TableRow(list(
tableCell(withChildren(paragraphWithText("Top left"))),
tableCell(withChildren(paragraphWithText("Top right"))))),
new TableRow(list(
tableCell(withChildren(paragraphWithText("Bottom left"))),
tableCell(withChildren(paragraphWithText("Bottom right")))))))),
deepEquals(list(Html.element("table", list(
Html.element("tr", list(
Html.element("td", list(Html.FORCE_WRITE, Html.element("p", list(Html.text("Top left"))))),
Html.element("td", list(Html.FORCE_WRITE, Html.element("p", list(Html.text("Top right"))))))),
Html.element("tr", list(
Html.element("td", list(Html.FORCE_WRITE, Html.element("p", list(Html.text("Bottom left"))))),
Html.element("td", list(Html.FORCE_WRITE, Html.element("p", list(Html.text("Bottom right"))))))))))));
}
@Test
public void tableCellsAreWrittenWithColspanIfNotEqualToOne() {
assertThat(
convertToHtml(new Table(list(
new TableRow(list(
tableCell(
withChildren(paragraphWithText("Top left")),
withColspan(2)
),
tableCell(withChildren(paragraphWithText("Top right")))))))),
deepEquals(list(Html.element("table", list(
Html.element("tr", list(
Html.element("td", map("colspan", "2"), list(Html.FORCE_WRITE, Html.element("p", list(Html.text("Top left"))))),
Html.element("td", list(Html.FORCE_WRITE, Html.element("p", list(Html.text("Top right")))))
))
))))
);
}
@Test
public void tableCellsAreWrittenWithRowspanIfNotEqualToOne() {
assertThat(
convertToHtml(new Table(list(
new TableRow(list(
tableCell(withRowspan(2))
))
))),
deepEquals(list(Html.element("table", list(
Html.element("tr", list(
Html.element("td", map("rowspan", "2"), list(Html.FORCE_WRITE))
))
))))
);
}
@Test
public void hyperlinkWithHrefIsConvertedToAnchorTag() {
assertThat(
convertToHtml(Hyperlink.href("http://example.com", list(new Text("Hello")))),
deepEquals(list(Html.collapsibleElement("a", map("href", "http://example.com"), list(Html.text("Hello"))))));
}
@Test
public void hyperlinkWithInternalAnchorReferenceIsConvertedToAnchorTag() {
assertThat(
convertToHtml(Hyperlink.anchor("start", list(new Text("Hello")))),
deepEquals(list(Html.collapsibleElement("a", map("href", "#doc-42-start"), list(Html.text("Hello"))))));
}
@Test
public void bookmarksAreConvertedToAnchorsWithIds() {
assertThat(
convertToHtml(new Bookmark("start")),
deepEquals(list(Html.element("a", map("id", "doc-42-start"), list(Html.FORCE_WRITE)))));
}
@Test
public void noteReferencesAreConvertedToLinksToReferenceBodyAfterMainBody() {
Document document = document(withChildren(
paragraph(withChildren(
runWithText("Knock knock"),
run(withChildren(new NoteReference(NoteType.FOOTNOTE, "4")))))),
new Notes(list(new Note(NoteType.FOOTNOTE, "4", list(paragraphWithText("Who's there?"))))));
assertThat(
convertToHtml(document),
deepEquals(list(
Html.element("p", list(
Html.text("Knock knock"),
Html.element("sup", list(
Html.element("a", map("href", "#doc-42-footnote-4", "id", "doc-42-footnote-ref-4"), list(Html.text("[1]"))))))),
Html.element("ol", list(
Html.element("li", map("id", "doc-42-footnote-4"), list(
Html.element("p", list(
Html.text("Who's there?"))),
Html.collapsibleElement("p", list(
Html.text(" "),
Html.element("a", map("href", "#doc-42-footnote-ref-4"), list(Html.text("↑"))))))))))));
}
@Test
public void noteReferencesAreConvertedWithSequentialNumbers() {
Run run = run(withChildren(
new NoteReference(NoteType.FOOTNOTE, "4"),
new NoteReference(NoteType.FOOTNOTE, "7")));
assertThat(
convertToHtml(run),
deepEquals(list(
Html.element("sup", list(
Html.element("a", map("href", "#doc-42-footnote-4", "id", "doc-42-footnote-ref-4"), list(Html.text("[1]"))))),
Html.element("sup", list(
Html.element("a", map("href", "#doc-42-footnote-7", "id", "doc-42-footnote-ref-7"), list(Html.text("[2]"))))))));
}
@Test
public void commentsAreIgnoredByDefault() {
Document document = document(
withChildren(
paragraph(withChildren(
runWithText("Knock knock"),
run(withChildren(new CommentReference("4")))))),
withComments(comment("4", list(paragraphWithText("Who's there?")))));
assertThat(
convertToHtml(document),
deepEquals(list(
Html.element("p", list(
Html.text("Knock knock"))))));
}
@Test
public void commentReferencesAreLinkedToCommentAfterMainBody() {
CommentReference reference = new CommentReference("4");
Comment comment = new Comment(
"4",
list(paragraphWithText("Who's there?")),
Optional.of("The Piemaker"),
Optional.of("TP")
);
Document document = document(
withChildren(
paragraph(withChildren(
runWithText("Knock knock"),
run(withChildren(reference))))),
withComments(comment)
);
StyleMap styleMap = StyleMap.builder().commentReference(HtmlPath.element("sup")).build();
assertThat(
convertToHtml(document, styleMap),
deepEquals(list(
Html.element("p", list(
Html.text("Knock knock"),
Html.element("sup", list(
Html.element("a", map("href", "#doc-42-comment-4", "id", "doc-42-comment-ref-4"), list(Html.text("[TP1]"))))))),
Html.element("dl", list(
Html.element("dt", map("id", "doc-42-comment-4"), list(
Html.text("Comment [TP1]"))),
Html.element("dd", list(
Html.element("p", list(
Html.text("Who's there?"))),
Html.collapsibleElement("p", list(
Html.text(" "),
Html.element("a", map("href", "#doc-42-comment-ref-4"), list(Html.text("↑"))))))))))));
}
@Test
public void imagesAreConvertedToImageTagsWithDataUriByDefault() {
Image image = new Image(
Optional.empty(),
Optional.of("image/png"),
() -> new ByteArrayInputStream(new byte[]{97, 98, 99}));
assertThat(
convertToHtml(image),
deepEquals(list(Html.element("img", map("src", "data:image/png;base64,YWJj")))));
}
@Test
public void imagesHaveAltTagsIfAvailable() {
Image image = new Image(
Optional.of("It's a hat"),
Optional.of("image/png"),
() -> new ByteArrayInputStream(new byte[]{97, 98, 99}));
assertThat(
convertToHtml(image),
contains(hasProperty("attributes", hasEntry("alt", "It's a hat"))));
}
private List<HtmlNode> convertToHtml(Document document) {
return convertToHtml(document, StyleMap.EMPTY);
}
private List<HtmlNode> convertToHtml(Document document, StyleMap styleMap) {
DocumentToHtmlOptions options = DocumentToHtmlOptions.DEFAULT
.idPrefix("doc-42-")
.addStyleMap(styleMap);
InternalResult<List<HtmlNode>> result = DocumentToHtml.convertToHtml(document, options);
assertThat(result.getWarnings(), emptyIterable());
return result.getValue();
}
private List<HtmlNode> convertToHtml(DocumentElement element) {
return convertToHtml(element, StyleMap.EMPTY);
}
private List<HtmlNode> convertToHtml(DocumentElement element, StyleMap styleMap) {
InternalResult<List<HtmlNode>> result = convertToHtmlResult(element, styleMap);
assertThat(result.getWarnings(), emptyIterable());
return result.getValue();
}
private InternalResult<List<HtmlNode>> convertToHtmlResult(DocumentElement element) {
return convertToHtmlResult(element, StyleMap.EMPTY);
}
private InternalResult<List<HtmlNode>> convertToHtmlResult(DocumentElement element, StyleMap styleMap) {
DocumentToHtmlOptions options = DocumentToHtmlOptions.DEFAULT
.idPrefix("doc-42-")
.addStyleMap(styleMap);
return DocumentToHtml.convertToHtml(element, options);
}
private Matcher<? super InternalResult<List<HtmlNode>>> isSuccess(List<HtmlNode> expected) {
return isInternalSuccess(deepEquals(expected));
}
}
|