-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathparagraphParser.test.ts
More file actions
584 lines (523 loc) · 19.6 KB
/
Copy pathparagraphParser.test.ts
File metadata and controls
584 lines (523 loc) · 19.6 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
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
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
import { describe, expect, test } from "bun:test";
import { getParagraphText, parseParagraph } from "./paragraphParser";
import type { XmlElement } from "./xmlParser";
import { parseXmlDocument } from "./xmlParser";
function parseParagraphXml(xml: string) {
const root = parseXmlDocument(xml) as XmlElement | null;
if (!root) {
throw new Error("Failed to parse paragraph XML fixture");
}
return parseParagraph(root, null, null, null, null, null);
}
describe("parseParagraph tracked-change hardening", () => {
test("parses deletion text from w:delText runs", () => {
const paragraph = parseParagraphXml(`
<w:p xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main">
<w:del w:id="7" w:author="Reviewer" w:date="2026-02-22T10:00:00Z">
<w:r>
<w:delText xml:space="preserve"> removed </w:delText>
</w:r>
</w:del>
</w:p>
`);
const deletion = paragraph.content[0];
expect(deletion?.type).toBe("deletion");
if (!deletion || deletion.type !== "deletion") {
return;
}
expect(deletion.info.id).toBe(7);
expect(deletion.info.author).toBe("Reviewer");
expect(deletion.info.date).toBe("2026-02-22T10:00:00Z");
expect(deletion.content).toHaveLength(1);
const run = deletion.content[0];
expect(run.type).toBe("run");
if (run.type !== "run") {
return;
}
expect(run.content).toHaveLength(1);
expect(run.content[0].type).toBe("text");
if (run.content[0].type !== "text") {
return;
}
expect(run.content[0].text).toBe(" removed ");
expect(run.content[0].preserveSpace).toBe(true);
});
test("parses deletion instruction text from w:delInstrText", () => {
const paragraph = parseParagraphXml(`
<w:p xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main">
<w:del w:id="8" w:author="Reviewer">
<w:r>
<w:delInstrText> MERGEFIELD name </w:delInstrText>
</w:r>
</w:del>
</w:p>
`);
const deletion = paragraph.content[0];
expect(deletion?.type).toBe("deletion");
if (!deletion || deletion.type !== "deletion") {
return;
}
const run = deletion.content[0];
expect(run.type).toBe("run");
if (run.type !== "run") {
return;
}
expect(run.content).toHaveLength(1);
expect(run.content[0].type).toBe("instrText");
if (run.content[0].type !== "instrText") {
return;
}
expect(run.content[0].text).toBe(" MERGEFIELD name ");
});
test("normalizes tracked-change metadata when attributes are invalid or blank", () => {
const paragraph = parseParagraphXml(`
<w:p xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main">
<w:ins w:id="invalid" w:author=" " w:date=" ">
<w:r><w:t>Added</w:t></w:r>
</w:ins>
</w:p>
`);
const insertion = paragraph.content[0];
expect(insertion?.type).toBe("insertion");
if (!insertion || insertion.type !== "insertion") {
return;
}
expect(insertion.info.id).toBe(0);
expect(insertion.info.author).toBe("Unknown");
expect(insertion.info.date).toBeUndefined();
});
test("preserves tracked-change metadata for marker-only wrappers", () => {
const paragraph = parseParagraphXml(`
<w:p xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main">
<w:ins w:id="12" w:author="Reviewer">
<w:bookmarkStart w:id="5" w:name="insertedMarker"/>
</w:ins>
</w:p>
`);
expect(paragraph.content.map((content) => content.type)).toEqual([
"insertion",
"bookmarkStart",
]);
const insertion = paragraph.content.at(0);
expect(insertion?.type).toBe("insertion");
if (!insertion || insertion.type !== "insertion") {
return;
}
expect(insertion.info).toMatchObject({ id: 12, author: "Reviewer" });
expect(insertion.content).toHaveLength(0);
});
test("preserves inline SDT metadata for marker-only controls", () => {
const paragraph = parseParagraphXml(`
<w:p xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main">
<w:sdt>
<w:sdtPr>
<w:alias w:val="Clause marker"/>
<w:tag w:val="clause-marker"/>
</w:sdtPr>
<w:sdtContent>
<w:bookmarkStart w:id="9" w:name="controlledMarker"/>
</w:sdtContent>
</w:sdt>
</w:p>
`);
expect(paragraph.content.map((content) => content.type)).toEqual([
"inlineSdt",
"bookmarkStart",
]);
const sdt = paragraph.content.at(0);
expect(sdt?.type).toBe("inlineSdt");
if (!sdt || sdt.type !== "inlineSdt") {
return;
}
expect(sdt.properties).toMatchObject({
alias: "Clause marker",
tag: "clause-marker",
});
expect(sdt.content).toHaveLength(0);
});
test("reads inline date-SDT format from <w:dateFormat>, not <w:date w:fullDate>", () => {
// Regression: parseSdtProperties used to read w:date@w:fullDate as the
// format, but w:fullDate is the bound *value*; the display format lives
// in the child <w:dateFormat w:val="..."/>. Picked up from upstream
// eigenpal/docx-editor#661.
const paragraph = parseParagraphXml(`
<w:p xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main">
<w:sdt>
<w:sdtPr>
<w:tag w:val="effective-date"/>
<w:date w:fullDate="2026-06-02T00:00:00Z">
<w:dateFormat w:val="d MMMM yyyy"/>
<w:lid w:val="en-GB"/>
</w:date>
</w:sdtPr>
<w:sdtContent><w:r><w:t>2 June 2026</w:t></w:r></w:sdtContent>
</w:sdt>
</w:p>
`);
const sdt = paragraph.content.at(0);
if (!sdt || sdt.type !== "inlineSdt") {
throw new Error("expected inline SDT");
}
expect(sdt.properties).toMatchObject({
sdtType: "date",
tag: "effective-date",
dateFormat: "d MMMM yyyy",
});
});
test("preserves point comment references from runs", () => {
const paragraph = parseParagraphXml(`
<w:p xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main">
<w:r><w:t>Commented text</w:t></w:r>
<w:r>
<w:rPr><w:rStyle w:val="CommentReference"/></w:rPr>
<w:commentReference w:id="42"/>
</w:r>
</w:p>
`);
expect(paragraph.content.at(0)?.type).toBe("run");
expect(paragraph.content.at(1)).toEqual({
type: "commentReference",
id: 42,
});
});
test("lifts bookmark markers out of tracked-change wrappers", () => {
const paragraph = parseParagraphXml(`
<w:p xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main">
<w:bookmarkStart w:id="5" w:name="deletedRange"/>
<w:del w:id="7" w:author="Reviewer">
<w:r><w:delText>removed</w:delText></w:r>
<w:bookmarkEnd w:id="5"/>
</w:del>
</w:p>
`);
expect(paragraph.content.map((content) => content.type)).toEqual([
"bookmarkStart",
"deletion",
"bookmarkEnd",
]);
expect(paragraph.content.at(2)).toMatchObject({
type: "bookmarkEnd",
id: 5,
});
});
test("folds an out-of-range id on an inline w:ins into the int32 range (eigenpal #1093)", () => {
const OUT_OF_RANGE = "2147483654"; // MAX_REVISION_ID + 7
const paragraph = parseParagraphXml(`
<w:p xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main">
<w:ins w:id="${OUT_OF_RANGE}" w:author="A">
<w:r><w:t>added</w:t></w:r>
</w:ins>
</w:p>
`);
const insertion = paragraph.content.find((c) => c.type === "insertion");
expect(insertion?.type).toBe("insertion");
if (!insertion || insertion.type !== "insertion") {
return;
}
expect(insertion.info.id).toBeLessThanOrEqual(2_147_483_647);
expect(insertion.info.id).toBeGreaterThanOrEqual(0);
});
test("folds an out-of-range id on a paragraph-mark w:ins (pPrMark)", () => {
const OUT_OF_RANGE = "2147483654";
const paragraph = parseParagraphXml(`
<w:p xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main">
<w:pPr><w:rPr><w:ins w:id="${OUT_OF_RANGE}" w:author="A"/></w:rPr></w:pPr>
<w:r><w:t>text</w:t></w:r>
</w:p>
`);
expect(paragraph.pPrMark).toBeDefined();
expect(paragraph.pPrMark?.info.id).toBeLessThanOrEqual(2_147_483_647);
expect(paragraph.pPrMark?.info.id).toBeGreaterThanOrEqual(0);
});
});
describe("parseParagraph rendered page break markers", () => {
test("marks a paragraph when Word rendered-page-break appears before visible text", () => {
const paragraph = parseParagraphXml(`
<w:p xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main">
<w:proofErr w:type="spellStart"/>
<w:r>
<w:lastRenderedPageBreak/>
<w:t>Moved to next page</w:t>
</w:r>
</w:p>
`);
expect(paragraph.renderedPageBreakBefore).toBe(true);
});
test("does not treat a hard page break as a cached rendered-page hint", () => {
const paragraph = parseParagraphXml(`
<w:p xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main">
<w:r>
<w:br w:type="page"/>
<w:t>After hard break</w:t>
</w:r>
</w:p>
`);
expect(paragraph.renderedPageBreakBefore).toBeUndefined();
});
test("keeps a real rendered-page hint after a hard-break paragraph", () => {
const hardBreak = parseParagraphXml(`
<w:p xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main">
<w:r><w:br w:type="page"/></w:r>
</w:p>
`);
const cachedNextPage = parseParagraphXml(`
<w:p xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main">
<w:r><w:lastRenderedPageBreak/><w:t>Next page</w:t></w:r>
</w:p>
`);
expect(hardBreak.renderedPageBreakBefore).toBeUndefined();
expect(cachedNextPage.renderedPageBreakBefore).toBe(true);
});
test("keeps a real rendered-page hint that precedes a hard break", () => {
const paragraph = parseParagraphXml(`
<w:p xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main">
<w:r><w:lastRenderedPageBreak/><w:br w:type="page"/></w:r>
</w:p>
`);
expect(paragraph.renderedPageBreakBefore).toBe(true);
});
test("does not mark a paragraph when rendered page break follows visible text", () => {
const paragraph = parseParagraphXml(`
<w:p xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main">
<w:r>
<w:t>Previous page text</w:t>
<w:lastRenderedPageBreak/>
</w:r>
</w:p>
`);
expect(paragraph.renderedPageBreakBefore).toBeUndefined();
expect(paragraph.content.at(0)).toMatchObject({
type: "run",
content: [{ type: "text", text: "Previous page text" }, { type: "renderedPageBreak" }],
});
});
test("lastRenderedPageBreak inside a hyperlink wrapper is honored", () => {
const paragraph = parseParagraphXml(`
<w:p xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main">
<w:hyperlink>
<w:r><w:lastRenderedPageBreak/><w:t>Hyper</w:t></w:r>
</w:hyperlink>
</w:p>
`);
expect(paragraph.renderedPageBreakBefore).toBe(true);
});
});
describe("parseParagraph spacing explicit flags", () => {
test("inline w:before is flagged", () => {
const paragraph = parseParagraphXml(`
<w:p xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main">
<w:pPr><w:spacing w:before="200"/></w:pPr>
</w:p>
`);
expect(paragraph.formatting?.spacingExplicit).toEqual({ before: true });
});
test("inline w:after only is flagged", () => {
const paragraph = parseParagraphXml(`
<w:p xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main">
<w:pPr><w:spacing w:after="100"/></w:pPr>
</w:p>
`);
expect(paragraph.formatting?.spacingExplicit).toEqual({ after: true });
});
test("paragraph without inline w:spacing has no explicit flags", () => {
const paragraph = parseParagraphXml(`
<w:p xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main">
<w:pPr><w:pStyle w:val="Normal"/></w:pPr>
</w:p>
`);
expect(paragraph.formatting?.spacingExplicit).toBeUndefined();
});
});
// Mirror of upstream eigenpal/docx-editor PR #482 parser tests
// (see commit 29f95751d). OOXML allows simple/complex fields, nested
// SDTs, and math equations directly inside `<w:sdtContent>`. The folio
// fork previously split these out as siblings of the SDT wrapper, so
// docProps-bound title fields (and similar template content) lost their
// wrapper on parse.
describe("parseParagraph SDT content preservation", () => {
test("keeps tracked run changes inside an inline SDT", () => {
const paragraph = parseParagraphXml(`
<w:p xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main">
<w:sdt>
<w:sdtPr><w:alias w:val="reviewed-control"/></w:sdtPr>
<w:sdtContent>
<w:ins w:id="1" w:author="Reviewer"><w:r><w:t>added</w:t></w:r></w:ins>
<w:del w:id="2" w:author="Reviewer"><w:r><w:delText>removed</w:delText></w:r></w:del>
</w:sdtContent>
</w:sdt>
</w:p>
`);
expect(paragraph.content).toHaveLength(1);
const sdt = paragraph.content.at(0);
expect(sdt?.type).toBe("inlineSdt");
if (sdt?.type !== "inlineSdt") {
return;
}
expect(sdt.content.map((content) => content.type)).toEqual(["insertion", "deletion"]);
});
test("keeps a simple field that lives inside an inline SDT", () => {
const paragraph = parseParagraphXml(`
<w:p xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main">
<w:sdt>
<w:sdtPr><w:alias w:val="title-control"/></w:sdtPr>
<w:sdtContent>
<w:fldSimple w:instr="TITLE">
<w:r><w:t>Cached title</w:t></w:r>
</w:fldSimple>
</w:sdtContent>
</w:sdt>
</w:p>
`);
expect(paragraph.content).toHaveLength(1);
const sdt = paragraph.content[0];
expect(sdt.type).toBe("inlineSdt");
if (sdt.type !== "inlineSdt") {
return;
}
expect(sdt.content).toHaveLength(1);
expect(sdt.content[0].type).toBe("simpleField");
});
test("keeps a complex field that lives inside an inline SDT", () => {
const paragraph = parseParagraphXml(`
<w:p xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main">
<w:sdt>
<w:sdtPr><w:alias w:val="page-ref"/></w:sdtPr>
<w:sdtContent>
<w:r><w:fldChar w:fldCharType="begin"/></w:r>
<w:r><w:instrText> PAGE </w:instrText></w:r>
<w:r><w:fldChar w:fldCharType="separate"/></w:r>
<w:r><w:t>3</w:t></w:r>
<w:r><w:fldChar w:fldCharType="end"/></w:r>
</w:sdtContent>
</w:sdt>
</w:p>
`);
expect(paragraph.content).toHaveLength(1);
const sdt = paragraph.content[0];
expect(sdt.type).toBe("inlineSdt");
if (sdt.type !== "inlineSdt") {
return;
}
expect(sdt.content).toHaveLength(1);
expect(sdt.content[0].type).toBe("complexField");
});
test("keeps a nested inline SDT inside an inline SDT", () => {
const paragraph = parseParagraphXml(`
<w:p xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main">
<w:sdt>
<w:sdtPr><w:alias w:val="outer"/></w:sdtPr>
<w:sdtContent>
<w:sdt>
<w:sdtPr><w:alias w:val="inner"/></w:sdtPr>
<w:sdtContent>
<w:r><w:t>Nested text</w:t></w:r>
</w:sdtContent>
</w:sdt>
</w:sdtContent>
</w:sdt>
</w:p>
`);
const outer = paragraph.content[0];
expect(outer.type).toBe("inlineSdt");
if (outer.type !== "inlineSdt") {
return;
}
expect(outer.content).toHaveLength(1);
expect(outer.content[0].type).toBe("inlineSdt");
});
});
describe("parseParagraph complex field formatting (#909)", () => {
test("captures the field run's w:rPr when there is no separate result run", () => {
// Footer pattern: begin / instrText / separate / end all in a single run
// carrying the w:rPr, with no separate result run. The formatting must
// survive on the ComplexField so the rendered PAGE number keeps the
// footer's size and color instead of falling back to the default.
const paragraph = parseParagraphXml(`
<w:p xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main">
<w:r>
<w:rPr><w:color w:val="595959"/><w:sz w:val="14"/><w:szCs w:val="14"/></w:rPr>
<w:fldChar w:fldCharType="begin"/>
<w:instrText xml:space="preserve"> PAGE </w:instrText>
<w:fldChar w:fldCharType="separate"/>
<w:fldChar w:fldCharType="end"/>
</w:r>
</w:p>
`);
const field = paragraph.content.find((c) => c.type === "complexField");
expect(field?.type).toBe("complexField");
if (!field) {
return;
}
expect(field.fieldResult).toHaveLength(0);
expect(field.formatting?.color).toEqual({ rgb: "595959" });
// raw w:sz is in half-points (14 half-points = 7pt).
expect(field.formatting?.fontSize).toBe(14);
});
test("captures a later run's formatting when the begin run's w:rPr is empty", () => {
// The begin run carries an empty <w:rPr/>; the size/colour lives on the
// result run. The empty object must not block capturing the later run.
const paragraph = parseParagraphXml(`
<w:p xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main">
<w:r>
<w:rPr/>
<w:fldChar w:fldCharType="begin"/>
<w:instrText xml:space="preserve"> PAGE </w:instrText>
<w:fldChar w:fldCharType="separate"/>
</w:r>
<w:r>
<w:rPr><w:color w:val="FF0000"/><w:sz w:val="28"/></w:rPr>
<w:t>1</w:t>
</w:r>
<w:r><w:fldChar w:fldCharType="end"/></w:r>
</w:p>
`);
const field = paragraph.content.find((c) => c.type === "complexField");
expect(field?.type).toBe("complexField");
if (!field) {
return;
}
expect(field.formatting?.color).toEqual({ rgb: "FF0000" });
expect(field.formatting?.fontSize).toBe(28);
});
});
describe("parseParagraph smartTag wrapper", () => {
test("recurses into w:smartTag instead of dropping wrapped runs", () => {
const paragraph = parseParagraphXml(`
<w:p xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main">
<w:smartTag w:uri="urn:schemas-microsoft-com:office:smarttags" w:element="City">
<w:r><w:t>SMARTTAG-CITY</w:t></w:r>
</w:smartTag>
<w:r><w:t xml:space="preserve">, STATE</w:t></w:r>
</w:p>
`);
const text = getParagraphText(paragraph);
expect(text).toContain("SMARTTAG-CITY");
expect(text).toContain(", STATE");
});
});
describe("parseParagraph native frame geometry", () => {
test("captures frame size, offsets, anchors, and wrap spacing", () => {
const paragraph = parseParagraphXml(`
<w:p xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main">
<w:pPr>
<w:framePr w:dropCap="none" w:lines="2" w:w="3600" w:h="1440" w:hSpace="144" w:vSpace="72"
w:hAnchor="page" w:vAnchor="text" w:x="720" w:y="144" w:wrap="around"/>
</w:pPr>
<w:r><w:t>Framed text</w:t></w:r>
</w:p>
`);
expect(paragraph.formatting?.frame).toEqual({
dropCap: "none",
lines: 2,
width: 3600,
height: 1440,
hSpace: 144,
vSpace: 72,
hAnchor: "page",
vAnchor: "text",
x: 720,
y: 144,
wrap: "around",
});
});
});