-
Notifications
You must be signed in to change notification settings - Fork 150
Expand file tree
/
Copy pathtable-styles.test.ts
More file actions
371 lines (334 loc) · 11.1 KB
/
table-styles.test.ts
File metadata and controls
371 lines (334 loc) · 11.1 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
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
import { describe, expect, it, beforeEach, vi } from 'vitest';
import { hydrateTableStyleAttrs } from './table-styles.js';
import type { PMNode } from '../types.js';
import * as tblTranslator from '@superdoc/super-editor/converter/internal/v3/handlers/w/tbl/tbl-translator.js';
// Mock the external super-converter module that's imported by table-styles.ts
// This module is part of super-editor package and not available in pm-adapter tests
vi.mock('@superdoc/super-editor/converter/internal/v3/handlers/w/tbl/tbl-translator.js');
describe('hydrateTableStyleAttrs', () => {
beforeEach(() => {
vi.clearAllMocks();
});
it('hydrates from tableProperties even without converter context', () => {
const table = {
attrs: {
tableProperties: {
cellMargins: {
marginLeft: { value: 108, type: 'dxa' },
top: { value: 12, type: 'px' },
},
tableWidth: { value: 1440, type: 'dxa' },
},
},
} as unknown as PMNode;
const result = hydrateTableStyleAttrs(table, undefined);
expect(result?.cellPadding?.left).toBeCloseTo((108 / 1440) * 96);
expect(result?.cellPadding?.top).toBe(12);
expect(result?.tableWidth).toEqual({ width: 96, type: 'px' });
});
it('merges referenced styles when context available', () => {
vi.mocked(tblTranslator._getReferencedTableStyles).mockReturnValue({
borders: { top: { val: 'single', size: 8 } },
cellMargins: { left: { value: 72, type: 'dxa' } },
justification: 'center',
tableCellSpacing: { value: 24, type: 'dxa' },
});
const table = {
attrs: {
tableStyleId: 'TableGrid',
tableProperties: {
tableWidth: { value: 500, type: 'px' },
},
},
} as unknown as PMNode;
const result = hydrateTableStyleAttrs(table, { docx: {} });
expect(result?.borders).toEqual({ top: { val: 'single', size: 8 } });
expect(result?.justification).toBe('center');
expect(result?.cellPadding?.left).toBeCloseTo((72 / 1440) * 96);
expect(result?.tableCellSpacing).toEqual({ value: 24, type: 'dxa' });
expect(result?.tableWidth).toEqual({ width: 500, type: 'px' });
});
describe('extractTableStyleParagraphProps (via hydrateTableStyleAttrs)', () => {
it('extracts spacing with all attributes (before, after, line, lineRule)', () => {
const mockDocx = {
'word/styles.xml': {
elements: [
{
elements: [
{
name: 'w:style',
attributes: { 'w:styleId': 'TableNormal' },
elements: [
{
name: 'w:pPr',
elements: [
{
name: 'w:spacing',
attributes: {
'w:before': '120',
'w:after': '240',
'w:line': '276',
'w:lineRule': 'auto',
},
},
],
},
],
},
],
},
],
},
};
const table = {
attrs: {
tableStyleId: 'TableNormal',
},
} as unknown as PMNode;
const result = hydrateTableStyleAttrs(table, { docx: mockDocx });
expect(result?.paragraphProps).toBeDefined();
expect(result?.paragraphProps?.spacing?.before).toBeCloseTo((120 / 1440) * 96);
expect(result?.paragraphProps?.spacing?.after).toBeCloseTo((240 / 1440) * 96);
// For 'auto' lineRule, line is in 240ths: 276/240 = 1.15
expect(result?.paragraphProps?.spacing?.line).toBeCloseTo(1.3225);
expect(result?.paragraphProps?.spacing?.lineRule).toBe('auto');
});
it('extracts spacing with partial attributes (only before/after)', () => {
const mockDocx = {
'word/styles.xml': {
elements: [
{
elements: [
{
name: 'w:style',
attributes: { 'w:styleId': 'TableGrid' },
elements: [
{
name: 'w:pPr',
elements: [
{
name: 'w:spacing',
attributes: {
'w:before': '100',
'w:after': '100',
},
},
],
},
],
},
],
},
],
},
};
const table = {
attrs: {
tableStyleId: 'TableGrid',
},
} as unknown as PMNode;
const result = hydrateTableStyleAttrs(table, { docx: mockDocx });
expect(result?.paragraphProps).toBeDefined();
expect(result?.paragraphProps?.spacing?.before).toBeCloseTo((100 / 1440) * 96);
expect(result?.paragraphProps?.spacing?.after).toBeCloseTo((100 / 1440) * 96);
expect(result?.paragraphProps?.spacing?.line).toBeUndefined();
expect(result?.paragraphProps?.spacing?.lineRule).toBeUndefined();
});
it('handles different lineRule values (exact and atLeast use twipsToPx)', () => {
const mockDocxExact = {
'word/styles.xml': {
elements: [
{
elements: [
{
name: 'w:style',
attributes: { 'w:styleId': 'TableExact' },
elements: [
{
name: 'w:pPr',
elements: [
{
name: 'w:spacing',
attributes: {
'w:line': '240',
'w:lineRule': 'exact',
},
},
],
},
],
},
],
},
],
},
};
const tableExact = {
attrs: {
tableStyleId: 'TableExact',
},
} as unknown as PMNode;
const resultExact = hydrateTableStyleAttrs(tableExact, { docx: mockDocxExact });
// For 'exact' lineRule, use twipsToPx: (240/1440)*96 = 16
expect(resultExact?.paragraphProps?.spacing?.line).toBeCloseTo(16);
expect(resultExact?.paragraphProps?.spacing?.lineUnit).toBe('px');
expect(resultExact?.paragraphProps?.spacing?.lineRule).toBe('exact');
const mockDocxAtLeast = {
'word/styles.xml': {
elements: [
{
elements: [
{
name: 'w:style',
attributes: { 'w:styleId': 'TableAtLeast' },
elements: [
{
name: 'w:pPr',
elements: [
{
name: 'w:spacing',
attributes: {
'w:line': '360',
'w:lineRule': 'atLeast',
},
},
],
},
],
},
],
},
],
},
};
const tableAtLeast = {
attrs: {
tableStyleId: 'TableAtLeast',
},
} as unknown as PMNode;
const resultAtLeast = hydrateTableStyleAttrs(tableAtLeast, { docx: mockDocxAtLeast });
// For 'atLeast' lineRule, use twipsToPx: (360/1440)*96 = 24
expect(resultAtLeast?.paragraphProps?.spacing?.line).toBeCloseTo(24);
expect(resultAtLeast?.paragraphProps?.spacing?.lineRule).toBe('atLeast');
});
it('returns undefined when style not found', () => {
const mockDocx = {
'word/styles.xml': {
elements: [
{
elements: [
{
name: 'w:style',
attributes: { 'w:styleId': 'OtherStyle' },
elements: [],
},
],
},
],
},
};
const table = {
attrs: {
tableStyleId: 'NonExistentStyle',
},
} as unknown as PMNode;
const result = hydrateTableStyleAttrs(table, { docx: mockDocx });
expect(result?.paragraphProps).toBeUndefined();
});
it('returns undefined when style has no w:pPr', () => {
const mockDocx = {
'word/styles.xml': {
elements: [
{
elements: [
{
name: 'w:style',
attributes: { 'w:styleId': 'TableNoPPr' },
elements: [
{
name: 'w:tblPr',
elements: [],
},
],
},
],
},
],
},
};
const table = {
attrs: {
tableStyleId: 'TableNoPPr',
},
} as unknown as PMNode;
const result = hydrateTableStyleAttrs(table, { docx: mockDocx });
expect(result?.paragraphProps).toBeUndefined();
});
it('returns undefined when w:pPr has no w:spacing', () => {
const mockDocx = {
'word/styles.xml': {
elements: [
{
elements: [
{
name: 'w:style',
attributes: { 'w:styleId': 'TableNoSpacing' },
elements: [
{
name: 'w:pPr',
elements: [
{
name: 'w:ind',
attributes: { 'w:left': '120' },
},
],
},
],
},
],
},
],
},
};
const table = {
attrs: {
tableStyleId: 'TableNoSpacing',
},
} as unknown as PMNode;
const result = hydrateTableStyleAttrs(table, { docx: mockDocx });
expect(result?.paragraphProps).toBeUndefined();
});
it('gracefully handles malformed/missing XML structure', () => {
const mockDocxMalformed1 = {
'word/styles.xml': {
elements: [],
},
};
const table1 = {
attrs: {
tableStyleId: 'TableGrid',
},
} as unknown as PMNode;
const result1 = hydrateTableStyleAttrs(table1, { docx: mockDocxMalformed1 });
expect(result1?.paragraphProps).toBeUndefined();
const mockDocxMalformed2 = {
'word/styles.xml': undefined,
};
const table2 = {
attrs: {
tableStyleId: 'TableGrid',
},
} as unknown as PMNode;
const result2 = hydrateTableStyleAttrs(table2, { docx: mockDocxMalformed2 });
expect(result2?.paragraphProps).toBeUndefined();
const mockDocxMalformed3 = {};
const table3 = {
attrs: {
tableStyleId: 'TableGrid',
},
} as unknown as PMNode;
const result3 = hydrateTableStyleAttrs(table3, { docx: mockDocxMalformed3 });
expect(result3?.paragraphProps).toBeUndefined();
});
});
});