-
Notifications
You must be signed in to change notification settings - Fork 145
Expand file tree
/
Copy pathparagraphNodeImporter.test.js
More file actions
298 lines (238 loc) · 9.29 KB
/
paragraphNodeImporter.test.js
File metadata and controls
298 lines (238 loc) · 9.29 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
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
import { handleParagraphNode } from '@converter/v2/importer/paragraphNodeImporter.js';
import { defaultNodeListHandler } from '@converter/v2/importer/docxImporter.js';
import { getTestDataByFileName } from '@tests/helpers/helpers.js';
import { handleListNode } from '@converter/v2/importer/listImporter.js';
import { beforeAll } from 'vitest';
describe('paragraph tests to check spacing', () => {
let lists = {};
beforeEach(() => {
lists = {};
});
it('correctly gets spacing [paragraph_spacing_missing]', async () => {
const dataName = 'paragraph_spacing_missing.docx';
const docx = await getTestDataByFileName(dataName);
const documentXml = docx['word/document.xml'];
const doc = documentXml.elements[0];
const body = doc.elements[0];
const content = body.elements;
const { nodes } = handleParagraphNode({ nodes: [content[0]], docx, nodeListHandler: defaultNodeListHandler(), lists });
const node = nodes[0];
expect(node.type).toBe('paragraph');
expect(node.content.length).toBeGreaterThan(0);
const { attrs } = node;
const { spacing } = attrs;
expect(spacing.line).toBe(1.15);
expect(spacing.lineSpaceAfter).toBe(16);
expect(spacing.lineSpaceBefore).toBe(16);
});
it('correctly gets spacing [line_space_table]', async () => {
const dataName = 'line_space_table.docx';
const docx = await getTestDataByFileName(dataName);
const documentXml = docx['word/document.xml'];
const doc = documentXml.elements[0];
const body = doc.elements[0];
const content = body.elements;
const tblNode = content[1];
const trNode = tblNode.elements[2];
const tcNode = trNode.elements[1];
// Check all nodes after the known tcPr
const { nodes } = handleParagraphNode({ nodes: tcNode.elements.slice(1), docx, nodeListHandler: defaultNodeListHandler(), lists });
const node = nodes[0];
expect(node.type).toBe('paragraph');
expect(node.content.length).toBeGreaterThan(0);
const { attrs } = node;
const { spacing } = attrs;
expect(spacing.line).toBe(1.15);
expect(spacing.lineSpaceAfter).toBe(0);
expect(spacing.lineSpaceBefore).toBe(0);
});
it('correctly gets spacing around image in p [image_p_spacing]', async () => {
const dataName = 'image_p_spacing.docx';
const docx = await getTestDataByFileName(dataName);
const documentXml = docx['word/document.xml'];
const doc = documentXml.elements[0];
const body = doc.elements[0];
const content = body.elements;
const { nodes } = handleParagraphNode({ nodes: [content[0]], docx, nodeListHandler: defaultNodeListHandler(), lists });
const node = nodes[0];
expect(node.type).toBe('paragraph');
expect(node.content.length).toBeGreaterThan(0);
const { attrs } = node;
const { spacing } = attrs;
expect(spacing.line).toBe(1.125);
expect(spacing.lineSpaceAfter).toBe(16);
expect(spacing.lineSpaceBefore).toBe(16);
// Specifically, double check we have this important line rule to prevent image clipping
// due to line height restriction
expect(spacing.lineRule).toBe('auto');
});
it('correctly gets marks for empty paragraph', async () => {
const dataName = 'doc_with_spacing.docx';
const docx = await getTestDataByFileName(dataName);
const documentXml = docx['word/document.xml'];
const doc = documentXml.elements[0];
const body = doc.elements[0];
const content = body.elements;
const { nodes } = handleParagraphNode({ nodes: [content[1]], docx, nodeListHandler: defaultNodeListHandler() });
const node = nodes[0];
expect(node.type).toBe('paragraph');
const { attrs } = node;
const { spacing, marksAttrs } = attrs;
expect(spacing.lineSpaceAfter).toBe(18);
expect(spacing.lineSpaceBefore).toBe(18);
expect(marksAttrs.length).toBe(2);
expect(marksAttrs[0].type).toBe('bold');
expect(marksAttrs[1].type).toBe('textStyle');
expect(marksAttrs[1].attrs.fontFamily).toBe('Arial');
expect(marksAttrs[1].attrs.fontSize).toBe('16pt');
});
it('correctly gets spaces from paragraph Normal styles', async () => {
const dataName = 'doc_with_spacing.docx';
const docx = await getTestDataByFileName(dataName);
const documentXml = docx['word/document.xml'];
const doc = documentXml.elements[0];
const body = doc.elements[0];
const content = body.elements;
const { nodes } = handleParagraphNode({ nodes: [content[4]], docx, nodeListHandler: defaultNodeListHandler() });
const node = nodes[0];
expect(node.type).toBe('paragraph');
const { attrs } = node;
const { spacing } = attrs;
expect(spacing.lineSpaceAfter).toBe(11);
expect(spacing.lineSpaceBefore).toBe(0);
});
it('correctly gets spacing from styles.xml by related styleId', async () => {
const dataName = 'doc_with_spaces_from_styles.docx';
const docx = await getTestDataByFileName(dataName);
const documentXml = docx['word/document.xml'];
const doc = documentXml.elements[0];
const body = doc.elements[0];
const content = body.elements;
const { nodes } = handleParagraphNode({ nodes: [content[0]], docx, nodeListHandler: defaultNodeListHandler() });
const node = nodes[0];
expect(node.type).toBe('paragraph');
const { attrs } = node;
const { spacing } = attrs;
expect(spacing.lineSpaceAfter).toBe(6);
expect(spacing.lineSpaceBefore).toBe(21);
});
it('correctly gets spacing with lists [list-def-mix]', async () => {
const dataName = 'list-def-mix.docx';
const docx = await getTestDataByFileName(dataName);
const documentXml = docx['word/document.xml'];
const doc = documentXml.elements[0];
const body = doc.elements[0];
const content = body.elements;
const firstListItem = content[0];
const { nodes } = handleListNode({ nodes: [content[0]], docx, nodeListHandler: defaultNodeListHandler(), lists });
});
it('should return empty result for empty nodes', () => {
const result = handleParagraphNode({
nodes: [],
docx: {},
nodeListHandler: defaultNodeListHandler()
});
expect(result).toEqual({ nodes: [], consumed: 0 });
});
it('should return empty result for non w:p node', () => {
const result = handleParagraphNode({
nodes: [{ name: 'w:r' }],
docx: {},
nodeListHandler: defaultNodeListHandler()
});
expect(result).toEqual({ nodes: [], consumed: 0 });
});
it('correctly handles paragraph with text alignment', () => {
const mockParagraph = {
name: 'w:p',
elements: [
{
name: 'w:pPr',
elements: [
{
name: 'w:jc',
attributes: {
'w:val': 'center'
}
}
]
}
]
};
const { nodes } = handleParagraphNode({
nodes: [mockParagraph],
docx: {},
nodeListHandler: defaultNodeListHandler()
});
const node = nodes[0];
expect(node.type).toBe('paragraph');
expect(node.attrs.textAlign).toBe('center');
});
it('correctly handles paragraph indentation in twips', () => {
const mockParagraph = {
name: 'w:p',
elements: [
{
name: 'w:pPr',
elements: [
{
name: 'w:ind',
attributes: {
'w:left': '2880',
'w:right': '1440',
'w:firstLine': '720',
'w:hanging': '270'
}
}
]
}
]
};
const { nodes } = handleParagraphNode({
nodes: [mockParagraph],
docx: {},
nodeListHandler: defaultNodeListHandler()
});
const node = nodes[0];
expect(node.type).toBe('paragraph');
// Keep raw twips values in indent object
expect(node.attrs.indent.left).toBe(192);
expect(node.attrs.indent.right).toBe(96);
expect(node.attrs.indent.firstLine).toBe(48);
expect(node.attrs.indent.hanging).toBe(18);
// textIndent should be in inches (2880twips - 270twips(hanging))
expect(node.attrs.textIndent).toBe('1.81in');
});
});
describe('paragraph tests to check indentation', () => {
it('correctly gets indents from paragraph Normal styles', async () => {
const dataName = 'paragraph_indent_normal_styles.docx';
const docx = await getTestDataByFileName(dataName);
const documentXml = docx['word/document.xml'];
const doc = documentXml.elements[0];
const body = doc.elements[0];
const content = body.elements;
const { nodes } = handleParagraphNode({ nodes: [content[0]], docx, nodeListHandler: defaultNodeListHandler() });
const node = nodes[0];
expect(node.type).toBe('paragraph');
const { attrs } = node;
const { indent } = attrs;
expect(indent.firstLine).toBe(29);
});
});
describe('paragraph with dropcaps', () => {
it('correctly gets dropcaps data', async () => {
const dataName = 'dropcaps.docx';
const docx = await getTestDataByFileName(dataName);
const documentXml = docx['word/document.xml'];
const doc = documentXml.elements[0];
const body = doc.elements[0];
const content = body.elements;
const { nodes } = handleParagraphNode({ nodes: [content[1]], docx, nodeListHandler: defaultNodeListHandler() });
const node = nodes[0];
expect(node.type).toBe('paragraph');
const { attrs } = node;
const { dropcap } = attrs;
expect(dropcap.type).toBe('drop');
});
});