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
|
var assert = require("assert");
var q = require("q");
var documents = require("../lib/documents");
var DocumentConverter = require("../lib/document-to-html").DocumentConverter;
var test = require("./testing").test;
var htmlPaths = require("../lib/html-paths");
var xmlreader = require("../lib/xmlreader");
describe('DocumentConverter', function() {
test('should empty document to empty string', function() {
var document = new documents.Document([]);
var converter = new DocumentConverter();
return converter.convertToHtml(document).then(function(result) {
assert.equal(result.html, "");
});
});
test('should convert document containing one paragraph to single p element', function() {
var document = new documents.Document([
paragraphOfText("Hello.")
]);
var converter = new DocumentConverter();
return converter.convertToHtml(document).then(function(result) {
assert.equal(result.html, "<p>Hello.</p>");
});
});
test('ignores empty paragraphs', function() {
var document = new documents.Document([
paragraphOfText("")
]);
var converter = new DocumentConverter();
return converter.convertToHtml(document).then(function(result) {
assert.equal(result.html, "");
});
});
test('can use non-default HTML element for unstyled paragraphs', function() {
var document = new documents.Document([
paragraphOfText("Hello.")
]);
var converter = new DocumentConverter({
defaultParagraphStyle: htmlPaths.topLevelElement("h1")
});
return converter.convertToHtml(document).then(function(result) {
assert.equal(result.html, "<h1>Hello.</h1>");
});
});
test('text is HTML-escaped', function() {
var document = new documents.Document([
paragraphOfText("1 < 2")
]);
var converter = new DocumentConverter();
return converter.convertToHtml(document).then(function(result) {
assert.equal(result.html, "<p>1 < 2</p>");
});
});
test('should convert document containing multiple paragraphs to multiple p elements', function() {
var document = new documents.Document([
paragraphOfText("Hello."),
paragraphOfText("Goodbye.")
]);
var converter = new DocumentConverter();
return converter.convertToHtml(document).then(function(result) {
assert.equal(result.html, "<p>Hello.</p><p>Goodbye.</p>");
});
});
test('uses style mappings to pick HTML element for docx paragraph', function() {
var document = new documents.Document([
paragraphOfText("Hello.", "Heading1"),
]);
var converter = new DocumentConverter({
paragraphStyleMap: {
"Heading1": htmlPaths.topLevelElement("h1")
}
});
return converter.convertToHtml(document).then(function(result) {
assert.equal(result.html, "<h1>Hello.</h1>");
});
});
test('can use stacked styles to generate nested HTML elements', function() {
var document = new documents.Document([
paragraphOfText("Hello.", "Heading1")
]);
var converter = new DocumentConverter({
paragraphStyleMap: {
"Heading1": htmlPaths.elements(["h1", "span"])
}
});
return converter.convertToHtml(document).then(function(result) {
assert.equal(result.html, "<h1><span>Hello.</span></h1>");
});
});
test('bold runs are wrapped in <strong> tags', function() {
var run = runOfText("Hello.", {isBold: true});
var converter = new DocumentConverter();
return converter.convertToHtml(run).then(function(result) {
assert.equal(result.html, "<strong>Hello.</strong>");
});
});
test('bold runs can exist inside other tags', function() {
var run = new documents.Paragraph([
runOfText("Hello.", {isBold: true})
]);
var converter = new DocumentConverter();
return converter.convertToHtml(run).then(function(result) {
assert.equal(result.html, "<p><strong>Hello.</strong></p>");
});
});
test('italic runs are wrapped in <em> tags', function() {
var run = runOfText("Hello.", {isItalic: true});
var converter = new DocumentConverter();
return converter.convertToHtml(run).then(function(result) {
assert.equal(result.html, "<em>Hello.</em>");
});
});
test('run can be both bold and italic', function() {
var run = runOfText("Hello.", {isBold: true, isItalic: true});
var converter = new DocumentConverter();
return converter.convertToHtml(run).then(function(result) {
assert.equal(result.html, "<strong><em>Hello.</em></strong>");
});
});
test('run styles are converted to HTML if mapping exists', function() {
var run = runOfText("Hello.", {styleName: "Emphasis"});
var converter = new DocumentConverter({
runStyleMap: {
"Emphasis": htmlPaths.elements(["strong"])
}
});
return converter.convertToHtml(run).then(function(result) {
assert.equal(result.html, "<strong>Hello.</strong>");
});
});
test('docx hyperlink is converted to <a>', function() {
var hyperlink = new documents.Hyperlink(
[runOfText("Hello.")],
{href: "http://www.example.com"}
);
var converter = new DocumentConverter();
return converter.convertToHtml(hyperlink).then(function(result) {
assert.equal(result.html, '<a href="http://www.example.com">Hello.</a>');
});
});
test('images are written with data URIs', function() {
var imageBuffer = new Buffer("Not an image at all!");
var image = new documents.Image(
function(encoding) {
return q.when(imageBuffer.toString(encoding));
}
);
var converter = new DocumentConverter();
return converter.convertToHtml(image).then(function(result) {
assert.equal(result.html, '<img src="data:image/png;base64,' + imageBuffer.toString("base64") + '" />');
});
});
test('images have alt attribute if available', function() {
var imageBuffer = new Buffer("Not an image at all!");
var image = new documents.Image(
function() {
return q.when(imageBuffer);
},
"It's a hat"
);
var converter = new DocumentConverter();
return converter.convertToHtml(image)
.then(function(result) {
return xmlreader.read(result.html);
})
.then(function(htmlImageElementDocument) {
var htmlImageElement = htmlImageElementDocument.root;
assert.equal(htmlImageElement.attributes.alt, "It's a hat");
});
});
});
function paragraphOfText(text, styleName) {
var run = runOfText(text);
return new documents.Paragraph([run], {
styleName: styleName
});
}
function runOfText(text, properties) {
var textElement = new documents.Text(text);
return new documents.Run([textElement], properties);
}
|