@open-document/core 0.14.1 → 0.14.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,484 +0,0 @@
1
- import { describe, expect, it } from 'vitest';
2
- import {
3
- buildDocxParts,
4
- type DocxModel,
5
- escapeXml,
6
- fontsFor,
7
- hexColor,
8
- wordLang,
9
- } from './ooxml.ts';
10
-
11
- describe('fontsFor', () => {
12
- it('skips system aliases for the first real family', () => {
13
- expect(
14
- fontsFor('-apple-system, BlinkMacSystemFont, "Inter", system-ui, sans-serif', 'sans'),
15
- ).toEqual({ ascii: 'Inter', eastAsia: 'Inter', kind: 'sans' });
16
- });
17
-
18
- it('keeps a CJK family in the stack as the East Asian font', () => {
19
- expect(fontsFor('"Noto Sans TC", "Helvetica Neue", sans-serif', 'sans')).toEqual({
20
- ascii: 'Helvetica Neue',
21
- eastAsia: 'Noto Sans TC',
22
- kind: 'sans',
23
- });
24
- expect(fontsFor('Georgia, "PingFang TC", serif', 'serif')).toEqual({
25
- ascii: 'Georgia',
26
- eastAsia: 'PingFang TC',
27
- kind: 'serif',
28
- });
29
- });
30
-
31
- it('skips Apple system faces no app can pick by name', () => {
32
- expect(fontsFor('ui-monospace, "SF Mono", Menlo, monospace', 'mono').ascii).toBe('Menlo');
33
- expect(fontsFor('"SF Pro Text", Helvetica, sans-serif', 'sans').ascii).toBe('Helvetica');
34
- });
35
-
36
- it('falls back by generic family when nothing is named', () => {
37
- expect(fontsFor('system-ui, sans-serif', 'sans').ascii).toBe('Arial');
38
- expect(fontsFor('serif', 'sans').ascii).toBe('Times New Roman');
39
- expect(fontsFor('ui-monospace, monospace', 'sans').ascii).toBe('Courier New');
40
- });
41
- });
42
-
43
- describe('hexColor', () => {
44
- it('reads computed colours and hex alike', () => {
45
- expect(hexColor('rgb(22, 24, 29)')).toBe('16181d');
46
- expect(hexColor('rgba(37, 99, 235, 1)')).toBe('2563eb');
47
- expect(hexColor('#2563EB')).toBe('2563eb');
48
- expect(hexColor('#abc')).toBe('aabbcc');
49
- expect(hexColor('rgba(0, 0, 0, 0)')).toBeNull();
50
- });
51
-
52
- it('reads color-mix results and flattens a translucent tint onto white', () => {
53
- expect(hexColor('color(srgb 0.0863 0.0941 0.1137)')).toBe('16181d');
54
- expect(hexColor('rgba(37, 99, 235, 0.1)')).toBe('e9effd');
55
- expect(hexColor('color(srgb 0.145 0.388 0.922 / 0.1)')).toBe('e9effd');
56
- expect(hexColor('color(srgb 0 0 0 / 0)')).toBeNull();
57
- });
58
- });
59
-
60
- describe('escapeXml', () => {
61
- it('escapes markup and drops characters XML cannot hold', () => {
62
- expect(escapeXml('a < b & "c"\u0001')).toBe('a &lt; b &amp; &quot;c&quot;');
63
- });
64
- });
65
-
66
- const MODEL: DocxModel = {
67
- title: 'Report & review',
68
- page: { width: 794 * 15, height: 1123 * 15, landscape: false, margin: 76 * 15 },
69
- fonts: {
70
- body: { ascii: 'Inter', eastAsia: 'Noto Sans TC', kind: 'sans' },
71
- heading: { ascii: 'Inter', eastAsia: 'Noto Sans TC', kind: 'sans' },
72
- mono: { ascii: 'JetBrains Mono', eastAsia: 'JetBrains Mono', kind: 'mono' },
73
- },
74
- sizes: { title: 66, h1: 42, h2: 30, h3: 24, body: 21, caption: 15 },
75
- colors: { text: '16181d', muted: '6b7280', accent: '2563eb' },
76
- line: 325,
77
- blocks: [
78
- { type: 'paragraph', style: 'Heading1', inlines: [{ type: 'text', text: '1. Method' }] },
79
- { type: 'toc', entries: [{ text: '1. Method', level: 1 }] },
80
- {
81
- type: 'paragraph',
82
- inlines: [
83
- { type: 'text', text: 'See ' },
84
- { type: 'link', href: 'https://example.com', runs: [{ type: 'text', text: 'the guide' }] },
85
- { type: 'text', text: ' and ' },
86
- { type: 'text', text: 'code', mono: true },
87
- { type: 'footnote', id: 1 },
88
- { type: 'break' },
89
- { type: 'image', image: 'figure-1' },
90
- ],
91
- },
92
- {
93
- type: 'table',
94
- columns: [3000, 2000],
95
- rows: [
96
- {
97
- header: true,
98
- cells: [
99
- { blocks: [{ type: 'paragraph', inlines: [{ type: 'text', text: 'Service' }] }] },
100
- { blocks: [] },
101
- ],
102
- },
103
- ],
104
- },
105
- { type: 'paragraph', inlines: [], pageBreakBefore: true, spacingBefore: 0, lineExact: 6000 },
106
- ],
107
- footnotes: [
108
- { id: 1, paragraphs: [{ type: 'paragraph', inlines: [{ type: 'text', text: 'A source.' }] }] },
109
- ],
110
- footer: {
111
- type: 'paragraph',
112
- rightTab: 9000,
113
- inlines: [
114
- { type: 'text', text: 'Report' },
115
- { type: 'tab' },
116
- { type: 'field', instr: 'PAGE', cached: '1' },
117
- ],
118
- },
119
- images: [{ name: 'figure-1', ext: 'png', bytes: new Uint8Array([1, 2]), width: 100, height: 50 }],
120
- lists: ['bullet', 'decimal'],
121
- };
122
-
123
- describe('buildDocxParts', () => {
124
- const parts = buildDocxParts(MODEL);
125
- const text = (name: string) => parts[name] as string;
126
-
127
- it('writes every part a package needs, and the media', () => {
128
- expect(Object.keys(parts).sort()).toEqual([
129
- '[Content_Types].xml',
130
- '_rels/.rels',
131
- 'docProps/core.xml',
132
- 'word/_rels/document.xml.rels',
133
- 'word/document.xml',
134
- 'word/fontTable.xml',
135
- 'word/footer1.xml',
136
- 'word/footnotes.xml',
137
- 'word/media/figure-1.png',
138
- 'word/numbering.xml',
139
- 'word/settings.xml',
140
- 'word/styles.xml',
141
- ]);
142
- expect(text('[Content_Types].xml')).toContain(
143
- '<Default Extension="png" ContentType="image/png"/>',
144
- );
145
- expect(text('docProps/core.xml')).toContain('<dc:title>Report &amp; review</dc:title>');
146
- });
147
-
148
- it('sets the page from the geometry and hangs the footer on the section', () => {
149
- const doc = text('word/document.xml');
150
- expect(doc).toContain('<w:pgSz w:w="11910" w:h="16845"/>');
151
- expect(doc).toContain('<w:pgMar w:top="1140"');
152
- expect(doc).toMatch(/<w:footerReference w:type="default" r:id="rId\d+"\/>/);
153
- expect(text('word/footer1.xml')).toContain('<w:fldSimple w:instr=" PAGE ">');
154
- expect(text('word/footer1.xml')).toContain('<w:tab w:val="right" w:pos="9000"/>');
155
- });
156
-
157
- it('names a stand-in of the same kind for every font', () => {
158
- const fonts = text('word/fontTable.xml');
159
- expect(fonts).toContain(
160
- '<w:font w:name="JetBrains Mono"><w:altName w:val="Courier New"/><w:family w:val="modern"/><w:pitch w:val="fixed"/></w:font>',
161
- );
162
- expect(fonts).toContain('<w:font w:name="Noto Sans TC"><w:altName w:val="Arial"/>');
163
- expect(text('word/_rels/document.xml.rels')).toContain('Target="fontTable.xml"');
164
- expect(text('[Content_Types].xml')).toContain('/word/fontTable.xml');
165
- });
166
-
167
- it('keeps the running header and footer off sections that end without them', () => {
168
- const sectioned = buildDocxParts({
169
- ...MODEL,
170
- header: {
171
- type: 'paragraph',
172
- inlines: [
173
- { type: 'text', text: 'Running head · ' },
174
- { type: 'field', instr: 'PAGE', cached: '1' },
175
- ],
176
- },
177
- blocks: [
178
- {
179
- type: 'paragraph',
180
- inlines: [{ type: 'text', text: 'Cover' }],
181
- sectionEnd: { running: false },
182
- },
183
- { type: 'paragraph', inlines: [{ type: 'text', text: 'Body' }] },
184
- ],
185
- });
186
- const doc = sectioned['word/document.xml'] as string;
187
- const rels = sectioned['word/_rels/document.xml.rels'] as string;
188
- const id = (target: string) =>
189
- rels.match(new RegExp(`Id="(rId\\d+)"[^>]*Target="${target}"`))?.[1];
190
- const refs = (header?: string, footer?: string) =>
191
- `<w:sectPr><w:headerReference w:type="default" r:id="${header}"/><w:footerReference w:type="default" r:id="${footer}"/>`;
192
- // The paragraph that ends a section holds its properties, ahead of its text.
193
- expect(doc).toContain(`${refs(id('header2.xml'), id('footer2.xml'))}`);
194
- expect(doc.indexOf(refs(id('header2.xml'), id('footer2.xml')))).toBeLessThan(
195
- doc.indexOf('Cover'),
196
- );
197
- expect(doc).toMatch(new RegExp(`Body.*${refs(id('header1.xml'), id('footer1.xml'))}`));
198
- expect(sectioned['word/header1.xml']).toContain('<w:hdr');
199
- expect(sectioned['word/header1.xml']).toContain('<w:pStyle w:val="Header"/>');
200
- expect(sectioned['word/header1.xml']).toContain('<w:fldSimple w:instr=" PAGE ">');
201
- expect(sectioned['word/header2.xml']).toContain('<w:hdr');
202
- expect(sectioned['word/footer2.xml']).toContain('<w:ftr');
203
- expect(sectioned['[Content_Types].xml']).toContain(
204
- '<Override PartName="/word/header1.xml" ContentType="application/vnd.openxmlformats-officedocument.wordprocessingml.header+xml"/>',
205
- );
206
- expect(parts['word/footer2.xml']).toBeUndefined();
207
- expect(parts['word/header1.xml']).toBeUndefined();
208
- });
209
-
210
- it('shades a cell and keeps code out of the spelling check', () => {
211
- const boxed = buildDocxParts({
212
- ...MODEL,
213
- blocks: [
214
- {
215
- type: 'table',
216
- columns: [9000],
217
- rows: [
218
- {
219
- header: false,
220
- cells: [
221
- {
222
- blocks: [
223
- { type: 'paragraph', inlines: [{ type: 'text', text: 'docs/', mono: true }] },
224
- ],
225
- fill: 'f5f5f7',
226
- padding: { top: 240, left: 300, bottom: 240, right: 300 },
227
- },
228
- ],
229
- },
230
- ],
231
- },
232
- ],
233
- });
234
- const doc = boxed['word/document.xml'] as string;
235
- expect(doc).toContain(
236
- '</w:tcBorders><w:shd w:val="clear" w:color="auto" w:fill="f5f5f7"/><w:tcMar>',
237
- );
238
- expect(doc).toMatch(/<w:noProof\/>(?:(?!<\/w:r>).)*?docs\//);
239
- });
240
-
241
- it('bookmarks a caption and points a dotted page reference at it', () => {
242
- const listed = buildDocxParts({
243
- ...MODEL,
244
- blocks: [
245
- {
246
- type: 'paragraph',
247
- rightTab: 9630,
248
- leader: true,
249
- inlines: [
250
- { type: 'text', text: 'Table 1 Usage' },
251
- { type: 'tab' },
252
- { type: 'field', instr: 'PAGEREF od_t_usage \\h', cached: '4' },
253
- ],
254
- },
255
- {
256
- type: 'paragraph',
257
- style: 'Caption',
258
- bookmark: 'od_t_usage',
259
- inlines: [{ type: 'text', text: 'Usage' }],
260
- },
261
- ],
262
- });
263
- const doc = listed['word/document.xml'] as string;
264
- expect(doc).toContain('<w:tab w:val="right" w:leader="dot" w:pos="9630"/>');
265
- expect(doc).toContain('<w:fldSimple w:instr=" PAGEREF od_t_usage \\h ">');
266
- expect(doc).toMatch(
267
- /<w:bookmarkStart w:id="0" w:name="od_t_usage"\/>.*?Usage.*?<w:bookmarkEnd w:id="0"\/>/,
268
- );
269
- });
270
-
271
- it('holds a gap open with an exact line height where space before would be dropped', () => {
272
- expect(text('word/document.xml')).toContain(
273
- '<w:pageBreakBefore/><w:spacing w:before="0" w:line="6000" w:lineRule="exact"/>',
274
- );
275
- });
276
-
277
- it('keeps the page’s line height and leads contents entries to a right tab', () => {
278
- const styles = text('word/styles.xml');
279
- expect(styles).toContain('<w:spacing w:after="160" w:line="325" w:lineRule="atLeast"/>');
280
- // Text block: the sheet less both margins.
281
- expect(styles).toContain('<w:tab w:val="right" w:leader="dot" w:pos="9630"/>');
282
- });
283
-
284
- it('writes headings as styles Word navigates by, with East Asian fonts', () => {
285
- const styles = text('word/styles.xml');
286
- expect(styles).toContain('w:styleId="Heading1"');
287
- expect(styles).toContain('<w:outlineLvl w:val="0"/>');
288
- expect(styles).toContain('w:eastAsia="Noto Sans TC"');
289
- expect(text('word/document.xml')).toContain('<w:pStyle w:val="Heading1"/>');
290
- });
291
-
292
- it('links externally, marks code in the mono font, and references the note', () => {
293
- const doc = text('word/document.xml');
294
- const rels = text('word/_rels/document.xml.rels');
295
- expect(rels).toContain('Target="https://example.com" TargetMode="External"');
296
- expect(doc).toMatch(
297
- /<w:hyperlink r:id="rId\d+"><w:r><w:rPr><w:rStyle w:val="Hyperlink"\/><\/w:rPr>/,
298
- );
299
- expect(doc).toContain('w:ascii="JetBrains Mono"');
300
- expect(doc).toContain('<w:footnoteReference w:id="1"/>');
301
- expect(text('word/footnotes.xml')).toContain('<w:footnoteRef/>');
302
- expect(text('word/footnotes.xml')).toContain('A source.');
303
- });
304
-
305
- it('embeds the image through a relationship', () => {
306
- const doc = text('word/document.xml');
307
- const rels = text('word/_rels/document.xml.rels');
308
- expect(rels).toContain('Target="media/figure-1.png"');
309
- expect(doc).toContain('<wp:extent cx="100" cy="50"/>');
310
- });
311
-
312
- it('writes a contents field Word updates, and marks table headers to repeat', () => {
313
- const doc = text('word/document.xml');
314
- expect(doc).toContain(
315
- '<w:instrText xml:space="preserve"> TOC \\o "1-3" \\h \\z \\u </w:instrText>',
316
- );
317
- const settings = text('word/settings.xml');
318
- expect(settings).toContain('<w:updateFields w:val="true"/>');
319
- // Schema order: Word rejects the part when these are swapped.
320
- expect(settings.indexOf('characterSpacingControl')).toBeLessThan(
321
- settings.indexOf('updateFields'),
322
- );
323
- expect(settings.indexOf('updateFields')).toBeLessThan(settings.indexOf('footnotePr'));
324
- expect(doc).toContain('<w:tblHeader/>');
325
- // An empty cell still ends in a paragraph, or Word calls the file corrupt.
326
- expect(doc).toContain('</w:tcPr><w:p/></w:tc>');
327
- });
328
-
329
- it('shades a paragraph and hangs its first line', () => {
330
- const doc = buildDocxParts({
331
- ...MODEL,
332
- blocks: [
333
- {
334
- type: 'paragraph',
335
- style: 'Code',
336
- shade: 'e9effd',
337
- inlines: [{ type: 'text', text: 'x' }],
338
- },
339
- {
340
- type: 'paragraph',
341
- indent: 480,
342
- hanging: 480,
343
- inlines: [
344
- { type: 'text', text: '[1]' },
345
- { type: 'tab' },
346
- { type: 'text', text: 'Knuth' },
347
- ],
348
- },
349
- ],
350
- })['word/document.xml'] as string;
351
- // Shading sits after the style and before spacing, where the schema puts it.
352
- expect(doc).toContain(
353
- '<w:pPr><w:pStyle w:val="Code"/><w:shd w:val="clear" w:color="auto" w:fill="e9effd"/></w:pPr>',
354
- );
355
- expect(doc).toContain('<w:ind w:left="480" w:hanging="480"/>');
356
- });
357
-
358
- it('sets a section in columns, ending the one before it', () => {
359
- const doc = buildDocxParts({
360
- ...MODEL,
361
- blocks: [
362
- {
363
- type: 'paragraph',
364
- inlines: [{ type: 'text', text: 'One' }],
365
- sectionEnd: { running: true },
366
- },
367
- { type: 'paragraph', inlines: [{ type: 'text', text: 'Two' }] },
368
- ],
369
- finalColumns: { count: 2, gap: 420 },
370
- })['word/document.xml'] as string;
371
- expect(doc).toMatch(/<w:pgMar[^>]*\/><\/w:sectPr>.*One/);
372
- expect(doc).toMatch(/Two.*<w:pgMar[^>]*\/><w:cols w:num="2" w:space="420"\/><\/w:sectPr>/);
373
- expect(doc.match(/<w:cols /g)).toHaveLength(1);
374
- });
375
-
376
- it('shades a pill and rules a line above a paragraph', () => {
377
- const doc = buildDocxParts({
378
- ...MODEL,
379
- blocks: [
380
- {
381
- type: 'paragraph',
382
- ruleAbove: { color: 'e2e8f0', size: 6, gap: 210 },
383
- inlines: [{ type: 'text', text: 'OPEN-DOC', shade: 'e9effd' }],
384
- },
385
- ],
386
- })['word/document.xml'] as string;
387
- expect(doc).toContain(
388
- '<w:pBdr><w:top w:val="single" w:sz="6" w:space="11" w:color="e2e8f0"/></w:pBdr>',
389
- );
390
- expect(doc).toContain(
391
- '<w:rPr><w:shd w:val="clear" w:color="auto" w:fill="e9effd"/></w:rPr><w:t xml:space="preserve">OPEN-DOC</w:t>',
392
- );
393
- });
394
-
395
- it('gives a sheet’s own foot a footer of its section, and the rest an empty one', () => {
396
- const parts = buildDocxParts({
397
- ...MODEL,
398
- footer: undefined,
399
- blocks: [
400
- {
401
- type: 'paragraph',
402
- inlines: [{ type: 'text', text: 'Cover' }],
403
- sectionEnd: {
404
- running: false,
405
- foot: { type: 'paragraph', inlines: [{ type: 'text', text: 'Byline' }] },
406
- },
407
- },
408
- { type: 'paragraph', inlines: [{ type: 'text', text: 'Body' }] },
409
- ],
410
- });
411
- const doc = parts['word/document.xml'] as string;
412
- const rels = parts['word/_rels/document.xml.rels'] as string;
413
- const id = (target: string) =>
414
- rels.match(new RegExp(`Id="(rId\\d+)"[^>]*Target="${target}"`))?.[1];
415
- expect(parts['word/footer3.xml']).toContain('Byline');
416
- // Set in the body's style, and placed a page margin above the sheet's edge.
417
- expect(parts['word/footer3.xml']).not.toContain('w:val="Footer"');
418
- expect(doc).toMatch(
419
- new RegExp(
420
- `<w:footerReference w:type="default" r:id="${id('footer3.xml')}"/>.*w:footer="1140".*Cover`,
421
- ),
422
- );
423
- // Without a footer of its own, the next section would inherit the cover's.
424
- expect(doc).toMatch(
425
- new RegExp(`Body.*<w:footerReference w:type="default" r:id="${id('footer2.xml')}"/>`),
426
- );
427
- });
428
-
429
- it('stamps the watermark behind the text from every header', () => {
430
- const parts = buildDocxParts({
431
- ...MODEL,
432
- blocks: [
433
- {
434
- type: 'paragraph',
435
- inlines: [{ type: 'text', text: 'Cover' }],
436
- sectionEnd: { running: false },
437
- },
438
- { type: 'paragraph', inlines: [{ type: 'text', text: 'Body' }] },
439
- ],
440
- images: [
441
- ...MODEL.images,
442
- {
443
- name: 'watermark',
444
- ext: 'png',
445
- bytes: new Uint8Array([3]),
446
- width: 7562850,
447
- height: 10696575,
448
- },
449
- ],
450
- watermark: 'watermark',
451
- });
452
- for (const header of ['word/header1.xml', 'word/header2.xml']) {
453
- const xml = parts[header] as string;
454
- expect(xml).toContain('behindDoc="1"');
455
- expect(xml).toContain('<wp:positionH relativeFrom="page"><wp:posOffset>0</wp:posOffset>');
456
- expect(xml).toContain('<wp:extent cx="7562850" cy="10696575"/>');
457
- expect(parts[header.replace('word/', 'word/_rels/').concat('.rels')]).toContain(
458
- 'Target="media/watermark.png"',
459
- );
460
- }
461
- });
462
-
463
- it('gives each ordered list its own count', () => {
464
- const numbering = text('word/numbering.xml');
465
- expect(numbering).toContain('<w:num w:numId="1"><w:abstractNumId w:val="0"/></w:num>');
466
- expect(numbering).toContain('<w:num w:numId="2"><w:abstractNumId w:val="1"/><w:lvlOverride');
467
- });
468
- });
469
-
470
- describe('wordLang', () => {
471
- it('files Chinese, Japanese and Korean as the East Asian language', () => {
472
- expect(wordLang('zh-Hant-TW')).toBe('<w:lang w:val="en-US" w:eastAsia="zh-TW"/>');
473
- expect(wordLang('zh-Hans')).toBe('<w:lang w:val="en-US" w:eastAsia="zh-CN"/>');
474
- expect(wordLang('zh-HK')).toBe('<w:lang w:val="en-US" w:eastAsia="zh-HK"/>');
475
- expect(wordLang('ja')).toBe('<w:lang w:val="en-US" w:eastAsia="ja-JP"/>');
476
- expect(wordLang('ko-KR')).toBe('<w:lang w:val="en-US" w:eastAsia="ko-KR"/>');
477
- });
478
-
479
- it('files anything else as the Latin language', () => {
480
- expect(wordLang()).toBe('<w:lang w:val="en-US"/>');
481
- expect(wordLang('en-GB')).toBe('<w:lang w:val="en-GB"/>');
482
- expect(wordLang('de"x')).toBe('<w:lang w:val="dex"/>');
483
- });
484
- });
@@ -1,30 +0,0 @@
1
- import { describe, expect, it } from 'vitest';
2
- import { metricsFor } from './flow-measure';
3
-
4
- /** Just what `metricsFor` reads of a block's wrapper. */
5
- function block(inner: { tagName: string; attrs?: Record<string, string> } | null): HTMLElement {
6
- return {
7
- firstElementChild: inner && {
8
- tagName: inner.tagName,
9
- getAttribute: (name: string) => inner.attrs?.[name] ?? null,
10
- },
11
- } as unknown as HTMLElement;
12
- }
13
-
14
- describe('metricsFor', () => {
15
- it('treats a block that renders nothing as plain, not as a page break', () => {
16
- expect(metricsFor(block(null), 0, 0)).toMatchObject({
17
- breakBefore: false,
18
- keepWithNext: false,
19
- keepWithPrevious: false,
20
- });
21
- });
22
-
23
- it('glues a heading to what follows, and reads declared breaks', () => {
24
- expect(metricsFor(block({ tagName: 'H2' }), 20, 0).keepWithNext).toBe(true);
25
- expect(
26
- metricsFor(block({ tagName: 'DIV', attrs: { 'data-od-break-before': '' } }), 20, 0)
27
- .breakBefore,
28
- ).toBe(true);
29
- });
30
- });
@@ -1,129 +0,0 @@
1
- import { describe, expect, it } from 'vitest';
2
- import { type BlockMetrics, paginateBlocks } from './flow.ts';
3
-
4
- const b = (height: number, extra: Partial<BlockMetrics> = {}): BlockMetrics => ({
5
- height,
6
- ...extra,
7
- });
8
-
9
- describe('paginateBlocks', () => {
10
- it('fills a page before starting the next', () => {
11
- const { pages } = paginateBlocks([b(300), b(300), b(300), b(300)], 1000);
12
- expect(pages).toEqual([[0, 1, 2], [3]]);
13
- });
14
-
15
- it('keeps everything on one page when it fits', () => {
16
- const { pages } = paginateBlocks([b(100), b(100)], 1000);
17
- expect(pages).toEqual([[0, 1]]);
18
- });
19
-
20
- it('never leaves a heading as the last block on a page', () => {
21
- // The heading fits at the bottom, but its body does not — both move.
22
- const { pages } = paginateBlocks([b(400), b(400), b(60, { keepWithNext: true }), b(300)], 1000);
23
- expect(pages).toEqual([
24
- [0, 1],
25
- [2, 3],
26
- ]);
27
- });
28
-
29
- it('moves a run of stacked headings together', () => {
30
- const { pages } = paginateBlocks(
31
- [b(800), b(50, { keepWithNext: true }), b(50, { keepWithNext: true }), b(300)],
32
- 1000,
33
- );
34
- expect(pages).toEqual([[0], [1, 2, 3]]);
35
- });
36
-
37
- it('pulls a figure along with its caption', () => {
38
- const { pages } = paginateBlocks([b(300), b(600), b(120, { keepWithPrevious: true })], 1000);
39
- expect(pages).toEqual([[0], [1, 2]]);
40
- });
41
-
42
- it('honours an explicit page break', () => {
43
- const { pages } = paginateBlocks([b(100), b(100, { breakBefore: true }), b(100)], 1000);
44
- expect(pages).toEqual([[0], [1, 2]]);
45
- });
46
-
47
- it('gives an oversized block its own page and reports it', () => {
48
- const { pages, overflowing } = paginateBlocks([b(200), b(1400), b(200)], 1000);
49
- expect(pages).toEqual([[0], [1], [2]]);
50
- expect(overflowing).toEqual([1]);
51
- });
52
-
53
- it('does not strand a page when every block wants to keep with the next', () => {
54
- const { pages } = paginateBlocks(
55
- [b(600, { keepWithNext: true }), b(600, { keepWithNext: true }), b(600)],
56
- 1000,
57
- );
58
- expect(pages).toEqual([[0], [1], [2]]);
59
- });
60
-
61
- it('returns no pages for no blocks', () => {
62
- expect(paginateBlocks([], 1000)).toEqual({ pages: [], slices: [], overflowing: [] });
63
- });
64
-
65
- it('splits a tall splittable table across pages with repeated header', () => {
66
- const tableBlock = b(680, {
67
- splittable: {
68
- headerHeight: 40,
69
- footerHeight: 40,
70
- rowHeights: [100, 100, 100, 100, 100, 100],
71
- },
72
- });
73
- const result = paginateBlocks([b(500), tableBlock], 1000);
74
- // Page 1 has 500px left: header(40) + 4 rows(400) = 440 <= 500.
75
- // Page 2 gets the remaining 2 rows + header(40) + footer(40).
76
- expect(result.pages).toEqual([[0, 1], [1]]);
77
- expect(result.slices).toEqual([
78
- [{ index: 0 }, { index: 1, rowStart: 0, rowEnd: 4 }],
79
- [{ index: 1, rowStart: 4, rowEnd: 6 }],
80
- ]);
81
- expect(result.overflowing).toEqual([]);
82
- });
83
- });
84
-
85
- describe('paginateBlocks with footnotes', () => {
86
- const block = (height: number, footnoteHeight = 0): BlockMetrics => ({ height, footnoteHeight });
87
-
88
- it('gives a page less body room when its blocks carry notes', () => {
89
- // Three 300px blocks fit 1000px on their own.
90
- expect(paginateBlocks([block(300), block(300), block(300)], 1000).pages).toEqual([[0, 1, 2]]);
91
-
92
- // Add 100px of notes to the first block plus 20px of area chrome and the
93
- // third no longer fits: notes print on the page their marker landed on.
94
- const withNotes = paginateBlocks([block(300, 100), block(300), block(300)], 1000, {
95
- footnoteOverhead: 20,
96
- });
97
- expect(withNotes.pages).toEqual([[0, 1], [2]]);
98
- });
99
-
100
- it('charges the area chrome once per page, not once per note', () => {
101
- const blocks = [block(300, 50), block(300, 50), block(300)];
102
- // 900 body + 100 notes + 20 chrome = 1020 > 1000.
103
- expect(paginateBlocks(blocks, 1000, { footnoteOverhead: 20 }).pages).toEqual([[0, 1], [2]]);
104
- // Without the chrome it is exactly 1000 and all three stay together.
105
- expect(paginateBlocks(blocks, 1000, { footnoteOverhead: 0 }).pages).toEqual([[0, 1, 2]]);
106
- });
107
-
108
- it('recomputes the notes budget for blocks that move to the next page', () => {
109
- const blocks: BlockMetrics[] = [
110
- { height: 300, footnoteHeight: 100 },
111
- { height: 100, keepWithNext: true },
112
- { height: 700 },
113
- ];
114
- const result = paginateBlocks(blocks, 1000, { footnoteOverhead: 20 });
115
- // The heading travels with the block it introduces, and its page's budget
116
- // is recomputed without the first block's notes.
117
- expect(result.pages).toEqual([[0], [1, 2]]);
118
- });
119
-
120
- it('flags a block whose own notes make it taller than a page', () => {
121
- const result = paginateBlocks([block(900, 200)], 1000, { footnoteOverhead: 20 });
122
- expect(result.overflowing).toEqual([0]);
123
- });
124
-
125
- it('leaves documents without notes packing exactly as before', () => {
126
- const blocks = [block(400), block(400), block(400)];
127
- expect(paginateBlocks(blocks, 1000, { footnoteOverhead: 20 }).pages).toEqual([[0, 1], [2]]);
128
- });
129
- });