-
Notifications
You must be signed in to change notification settings - Fork 132
Expand file tree
/
Copy pathbetween-borders.test.ts
More file actions
1160 lines (1016 loc) Β· 44.8 KB
/
between-borders.test.ts
File metadata and controls
1160 lines (1016 loc) Β· 44.8 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
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import { afterEach, beforeEach, describe, expect, it } from 'vitest';
import {
applyParagraphBorderStyles,
getFragmentParagraphBorders,
computeBetweenBorderFlags,
createParagraphDecorationLayers,
getParagraphBorderBox,
computeBorderSpaceExpansion,
type BlockLookup,
type BetweenBorderInfo,
} from './features/paragraph-borders/index.js';
/** Helper to create BetweenBorderInfo for tests that previously passed a boolean. */
const betweenOn: BetweenBorderInfo = {
showBetweenBorder: true,
suppressTopBorder: false,
suppressBottomBorder: false,
gapBelow: 0,
};
const betweenOff: BetweenBorderInfo = {
showBetweenBorder: false,
suppressTopBorder: false,
suppressBottomBorder: false,
gapBelow: 0,
};
import { createDomPainter } from './index.js';
import type {
ParagraphBorders,
ParagraphBorder,
ParagraphBlock,
ListBlock,
Fragment,
FlowBlock,
Layout,
Measure,
ParaFragment,
ListItemFragment,
ImageFragment,
} from '@superdoc/contracts';
// ---------------------------------------------------------------------------
// Helpers
// ---------------------------------------------------------------------------
const makeParagraphBlock = (id: string, borders?: ParagraphBorders): ParagraphBlock => ({
kind: 'paragraph',
id,
runs: [],
attrs: borders ? { borders } : undefined,
});
const makeListBlock = (id: string, items: { itemId: string; borders?: ParagraphBorders }[]): ListBlock => ({
kind: 'list',
id,
listType: 'bullet',
items: items.map((item) => ({
id: item.itemId,
marker: { text: 'β’' },
paragraph: {
kind: 'paragraph',
id: `${id}-p-${item.itemId}`,
runs: [],
attrs: item.borders ? { borders: item.borders } : undefined,
},
})),
});
const stubMeasure = { kind: 'paragraph' as const, lines: [], totalHeight: 0 };
const stubListMeasure = {
kind: 'list' as const,
items: [],
totalHeight: 0,
};
const buildLookup = (entries: { block: ParagraphBlock | ListBlock; measure?: unknown }[]): BlockLookup => {
const map: BlockLookup = new Map();
for (const e of entries) {
map.set(e.block.id, {
block: e.block,
measure: (e.measure ?? (e.block.kind === 'list' ? stubListMeasure : stubMeasure)) as never,
version: '1',
});
}
return map;
};
const paraFragment = (blockId: string, overrides?: Partial<ParaFragment>): ParaFragment => ({
kind: 'para',
blockId,
fromLine: 0,
toLine: 1,
x: 0,
y: 0,
width: 100,
...overrides,
});
const listItemFragment = (
blockId: string,
itemId: string,
overrides?: Partial<ListItemFragment>,
): ListItemFragment => ({
kind: 'list-item',
blockId,
itemId,
fromLine: 0,
toLine: 1,
x: 0,
y: 0,
width: 100,
markerWidth: 20,
...overrides,
});
const imageFragment = (blockId: string): ImageFragment => ({
kind: 'image',
blockId,
x: 0,
y: 0,
width: 100,
height: 100,
});
const MATCHING_BORDERS: ParagraphBorders = {
top: { style: 'solid', width: 1, color: '#000' },
bottom: { style: 'solid', width: 1, color: '#000' },
between: { style: 'solid', width: 1, color: '#000' },
};
// ---------------------------------------------------------------------------
// applyParagraphBorderStyles
// ---------------------------------------------------------------------------
describe('applyParagraphBorderStyles β between borders', () => {
const el = () => document.createElement('div');
// --- basic activation ---
it('does not apply between border when showBetweenBorder is false', () => {
const e = el();
const borders: ParagraphBorders = {
top: { style: 'solid', width: 1, color: '#000' },
between: { style: 'solid', width: 2, color: '#FF0000' },
};
applyParagraphBorderStyles(e, borders, betweenOff);
expect(e.style.getPropertyValue('border-top-style')).toBe('solid');
expect(e.style.getPropertyValue('border-bottom-style')).toBe('');
});
it('does not apply between border when showBetweenBorder is undefined', () => {
const e = el();
applyParagraphBorderStyles(e, { between: { style: 'solid', width: 2, color: '#F00' } });
expect(e.style.getPropertyValue('border-bottom-style')).toBe('');
});
it('applies between border as bottom border when showBetweenBorder is true', () => {
const e = el();
applyParagraphBorderStyles(e, { between: { style: 'dashed', width: 3, color: '#0F0' } }, betweenOn);
expect(e.style.getPropertyValue('border-bottom-style')).toBe('dashed');
expect(e.style.getPropertyValue('border-bottom-width')).toBe('3px');
expect(e.style.getPropertyValue('border-bottom-color')).toBe('#0F0');
});
// --- overwrite semantics ---
it('overwrites normal bottom border when between is active', () => {
const e = el();
applyParagraphBorderStyles(
e,
{ bottom: { style: 'solid', width: 1, color: '#000' }, between: { style: 'double', width: 4, color: '#F00' } },
betweenOn,
);
expect(e.style.getPropertyValue('border-bottom-style')).toBe('double');
expect(e.style.getPropertyValue('border-bottom-width')).toBe('4px');
expect(e.style.getPropertyValue('border-bottom-color')).toBe('#F00');
});
it('preserves normal bottom border when showBetweenBorder is false', () => {
const e = el();
applyParagraphBorderStyles(
e,
{ bottom: { style: 'solid', width: 1, color: '#000' }, between: { style: 'double', width: 4, color: '#F00' } },
betweenOff,
);
expect(e.style.getPropertyValue('border-bottom-style')).toBe('solid');
expect(e.style.getPropertyValue('border-bottom-width')).toBe('1px');
expect(e.style.getPropertyValue('border-bottom-color')).toBe('#000');
});
it('applies all four sides plus between when active', () => {
const e = el();
const borders: ParagraphBorders = {
top: { style: 'solid', width: 1, color: '#000' },
right: { style: 'solid', width: 1, color: '#000' },
bottom: { style: 'solid', width: 1, color: '#000' },
left: { style: 'solid', width: 1, color: '#000' },
between: { style: 'dashed', width: 2, color: '#F00' },
};
applyParagraphBorderStyles(e, borders, betweenOn);
expect(e.style.getPropertyValue('border-top-style')).toBe('solid');
expect(e.style.getPropertyValue('border-right-style')).toBe('solid');
expect(e.style.getPropertyValue('border-left-style')).toBe('solid');
expect(e.style.getPropertyValue('border-bottom-style')).toBe('dashed');
expect(e.style.getPropertyValue('border-bottom-width')).toBe('2px');
});
// --- partial / degenerate border specs ---
it('handles between border with none style', () => {
const e = el();
applyParagraphBorderStyles(e, { between: { style: 'none', width: 0, color: '#000' } }, betweenOn);
expect(e.style.getPropertyValue('border-bottom-style')).toBe('none');
expect(e.style.getPropertyValue('border-bottom-width')).toBe('0px');
});
it('defaults width to 1px when between border has no width', () => {
const e = el();
applyParagraphBorderStyles(e, { between: { style: 'solid', color: '#F00' } }, betweenOn);
expect(e.style.getPropertyValue('border-bottom-width')).toBe('1px');
});
it('defaults color to #000 when between border has no color', () => {
const e = el();
applyParagraphBorderStyles(e, { between: { style: 'solid', width: 2 } }, betweenOn);
expect(e.style.getPropertyValue('border-bottom-color')).toBe('#000');
});
it('defaults style to solid when between border has no style', () => {
const e = el();
applyParagraphBorderStyles(e, { between: { width: 2, color: '#F00' } }, betweenOn);
expect(e.style.getPropertyValue('border-bottom-style')).toBe('solid');
});
it('handles between border with only width', () => {
const e = el();
applyParagraphBorderStyles(e, { between: { width: 5 } }, betweenOn);
expect(e.style.getPropertyValue('border-bottom-style')).toBe('solid');
expect(e.style.getPropertyValue('border-bottom-width')).toBe('5px');
expect(e.style.getPropertyValue('border-bottom-color')).toBe('#000');
});
it('clamps negative width to 0px', () => {
const e = el();
applyParagraphBorderStyles(e, { between: { style: 'solid', width: -3 } }, betweenOn);
expect(e.style.getPropertyValue('border-bottom-width')).toBe('0px');
});
it('handles width=0 (renders as zero-width border)', () => {
const e = el();
applyParagraphBorderStyles(e, { between: { style: 'solid', width: 0 } }, betweenOn);
expect(e.style.getPropertyValue('border-bottom-width')).toBe('0px');
});
it('no-ops when showBetweenBorder=true but borders.between is undefined', () => {
const e = el();
applyParagraphBorderStyles(e, { top: { style: 'solid', width: 1 } }, betweenOn);
// Should not crash, and no bottom border should appear
expect(e.style.getPropertyValue('border-bottom-style')).toBe('');
});
it('no-ops when borders is undefined', () => {
const e = el();
applyParagraphBorderStyles(e, undefined, betweenOn);
expect(e.style.getPropertyValue('border-bottom-style')).toBe('');
});
// --- suppressTopBorder ---
it('skips top border when suppressTopBorder is true', () => {
const e = el();
const borders: ParagraphBorders = {
top: { style: 'solid', width: 2, color: '#F00' },
left: { style: 'solid', width: 1, color: '#000' },
right: { style: 'solid', width: 1, color: '#000' },
};
const info: BetweenBorderInfo = {
showBetweenBorder: false,
suppressTopBorder: true,
suppressBottomBorder: false,
gapBelow: 0,
};
applyParagraphBorderStyles(e, borders, info);
expect(e.style.getPropertyValue('border-top-style')).toBe('');
expect(e.style.getPropertyValue('border-left-style')).toBe('solid');
expect(e.style.getPropertyValue('border-right-style')).toBe('solid');
});
it('applies top border normally when suppressTopBorder is false', () => {
const e = el();
const borders: ParagraphBorders = {
top: { style: 'dashed', width: 3, color: '#0F0' },
};
applyParagraphBorderStyles(e, borders, betweenOff);
expect(e.style.getPropertyValue('border-top-style')).toBe('dashed');
expect(e.style.getPropertyValue('border-top-width')).toBe('3px');
});
// --- suppressBottomBorder (nil/none between groups) ---
it('skips bottom border when suppressBottomBorder is true', () => {
const e = el();
const borders: ParagraphBorders = {
top: { style: 'solid', width: 1, color: '#000' },
bottom: { style: 'solid', width: 2, color: '#F00' },
left: { style: 'solid', width: 1, color: '#000' },
right: { style: 'solid', width: 1, color: '#000' },
};
const info: BetweenBorderInfo = {
showBetweenBorder: false,
suppressTopBorder: false,
suppressBottomBorder: true,
gapBelow: 10,
};
applyParagraphBorderStyles(e, borders, info);
expect(e.style.getPropertyValue('border-top-style')).toBe('solid');
expect(e.style.getPropertyValue('border-left-style')).toBe('solid');
expect(e.style.getPropertyValue('border-right-style')).toBe('solid');
expect(e.style.getPropertyValue('border-bottom-style')).toBe('');
});
it('suppresses both top and bottom for middle fragment in nil/none group', () => {
const e = el();
const borders: ParagraphBorders = {
top: { style: 'solid', width: 1, color: '#000' },
bottom: { style: 'solid', width: 1, color: '#000' },
left: { style: 'solid', width: 1, color: '#000' },
right: { style: 'solid', width: 1, color: '#000' },
};
const info: BetweenBorderInfo = {
showBetweenBorder: false,
suppressTopBorder: true,
suppressBottomBorder: true,
gapBelow: 10,
};
applyParagraphBorderStyles(e, borders, info);
expect(e.style.getPropertyValue('border-top-style')).toBe('');
expect(e.style.getPropertyValue('border-bottom-style')).toBe('');
expect(e.style.getPropertyValue('border-left-style')).toBe('solid');
expect(e.style.getPropertyValue('border-right-style')).toBe('solid');
});
});
// ---------------------------------------------------------------------------
// createParagraphDecorationLayers β gap extension
// ---------------------------------------------------------------------------
describe('createParagraphDecorationLayers β gap extension', () => {
it('sets bottom to negative gapBelow when showBetweenBorder is true', () => {
const attrs = { borders: { top: { style: 'solid' as const, width: 1 } }, shading: { fill: '#EEE' } };
const info: BetweenBorderInfo = {
showBetweenBorder: true,
suppressTopBorder: false,
suppressBottomBorder: false,
gapBelow: 8,
};
const { borderLayer, shadingLayer } = createParagraphDecorationLayers(document, 400, attrs, info);
expect(borderLayer!.style.bottom).toBe('-8px');
expect(shadingLayer!.style.bottom).toBe('-8px');
});
it('sets bottom to 0px when gapBelow is 0', () => {
const attrs = { borders: { top: { style: 'solid' as const, width: 1 } } };
const info: BetweenBorderInfo = {
showBetweenBorder: true,
suppressTopBorder: false,
suppressBottomBorder: false,
gapBelow: 0,
};
const { borderLayer } = createParagraphDecorationLayers(document, 400, attrs, info);
expect(borderLayer!.style.bottom).toBe('0px');
});
it('sets bottom to 0px when betweenInfo is undefined', () => {
const attrs = { borders: { top: { style: 'solid' as const, width: 1 } } };
const { borderLayer } = createParagraphDecorationLayers(document, 400, attrs);
expect(borderLayer!.style.bottom).toBe('0px');
});
it('sets bottom to 0px when showBetweenBorder is false and suppressBottomBorder is false', () => {
const attrs = { borders: { top: { style: 'solid' as const, width: 1 } } };
const info: BetweenBorderInfo = {
showBetweenBorder: false,
suppressTopBorder: true,
suppressBottomBorder: false,
gapBelow: 12,
};
const { borderLayer } = createParagraphDecorationLayers(document, 400, attrs, info);
expect(borderLayer!.style.bottom).toBe('0px');
});
it('sets bottom to negative gapBelow when suppressBottomBorder is true (nil/none between group)', () => {
const attrs = {
borders: { top: { style: 'solid' as const, width: 1 }, bottom: { style: 'solid' as const, width: 1 } },
};
const info: BetweenBorderInfo = {
showBetweenBorder: false,
suppressTopBorder: false,
suppressBottomBorder: true,
gapBelow: 10,
};
const { borderLayer } = createParagraphDecorationLayers(document, 400, attrs, info);
expect(borderLayer!.style.bottom).toBe('-10px');
});
});
// ---------------------------------------------------------------------------
// getFragmentParagraphBorders
// ---------------------------------------------------------------------------
describe('getFragmentParagraphBorders', () => {
it('returns borders from a paragraph block', () => {
const borders: ParagraphBorders = { top: { style: 'solid', width: 1 } };
const block = makeParagraphBlock('b1', borders);
const lookup = buildLookup([{ block }]);
expect(getFragmentParagraphBorders(paraFragment('b1'), lookup)).toEqual(borders);
});
it('returns undefined for paragraph block without borders', () => {
const block = makeParagraphBlock('b1');
const lookup = buildLookup([{ block }]);
expect(getFragmentParagraphBorders(paraFragment('b1'), lookup)).toBeUndefined();
});
it('returns borders from a list-item block', () => {
const borders: ParagraphBorders = { between: { style: 'solid', width: 1 } };
const block = makeListBlock('l1', [{ itemId: 'i1', borders }]);
const lookup = buildLookup([{ block }]);
expect(getFragmentParagraphBorders(listItemFragment('l1', 'i1'), lookup)).toEqual(borders);
});
it('returns undefined when list item is not found', () => {
const block = makeListBlock('l1', [{ itemId: 'i1' }]);
const lookup = buildLookup([{ block }]);
expect(getFragmentParagraphBorders(listItemFragment('l1', 'missing'), lookup)).toBeUndefined();
});
it('returns undefined when blockId is not in lookup', () => {
const lookup = buildLookup([]);
expect(getFragmentParagraphBorders(paraFragment('missing'), lookup)).toBeUndefined();
});
it('returns undefined for image fragment', () => {
const block = makeParagraphBlock('b1', { top: { style: 'solid', width: 1 } });
const lookup = buildLookup([{ block }]);
expect(getFragmentParagraphBorders(imageFragment('b1'), lookup)).toBeUndefined();
});
it('returns undefined for kind/block mismatch (para fragment with list block)', () => {
const block = makeListBlock('l1', [{ itemId: 'i1' }]);
const lookup = buildLookup([{ block }]);
// para fragment referencing a list block
expect(getFragmentParagraphBorders(paraFragment('l1'), lookup)).toBeUndefined();
});
});
// ---------------------------------------------------------------------------
// computeBetweenBorderFlags
// ---------------------------------------------------------------------------
describe('computeBetweenBorderFlags', () => {
// --- basic matching ---
it('flags index when two adjacent paragraphs have matching between borders', () => {
const b1 = makeParagraphBlock('b1', MATCHING_BORDERS);
const b2 = makeParagraphBlock('b2', MATCHING_BORDERS);
const lookup = buildLookup([{ block: b1 }, { block: b2 }]);
const fragments: Fragment[] = [paraFragment('b1'), paraFragment('b2')];
const flags = computeBetweenBorderFlags(fragments, lookup);
expect(flags.has(0)).toBe(true);
expect(flags.get(0)?.showBetweenBorder).toBe(true);
// Fragment 1 also gets an entry (suppressTopBorder)
expect(flags.has(1)).toBe(true);
expect(flags.get(1)?.suppressTopBorder).toBe(true);
expect(flags.size).toBe(2);
});
it('groups identical borders even when between border is not defined', () => {
// ECMA-376 Β§17.3.1.5: grouping occurs when all border properties are identical.
// When no between border is defined, the group renders as a single box (no separator).
const noBetween: ParagraphBorders = { top: { style: 'solid', width: 1 } };
const b1 = makeParagraphBlock('b1', noBetween);
const b2 = makeParagraphBlock('b2', noBetween);
const lookup = buildLookup([{ block: b1 }, { block: b2 }]);
const fragments: Fragment[] = [paraFragment('b1'), paraFragment('b2')];
const flags = computeBetweenBorderFlags(fragments, lookup);
expect(flags.size).toBe(2);
// First fragment: bottom border suppressed (no between separator, single box)
expect(flags.get(0)?.suppressBottomBorder).toBe(true);
expect(flags.get(0)?.showBetweenBorder).toBe(false);
// Second fragment: top border suppressed
expect(flags.get(1)?.suppressTopBorder).toBe(true);
});
it('does not flag when border definitions do not match', () => {
const borders1: ParagraphBorders = {
top: { style: 'solid', width: 1, color: '#000' },
between: { style: 'solid', width: 1, color: '#000' },
};
const borders2: ParagraphBorders = {
top: { style: 'dashed', width: 2, color: '#F00' },
between: { style: 'dashed', width: 2, color: '#F00' },
};
const b1 = makeParagraphBlock('b1', borders1);
const b2 = makeParagraphBlock('b2', borders2);
const lookup = buildLookup([{ block: b1 }, { block: b2 }]);
const fragments: Fragment[] = [paraFragment('b1'), paraFragment('b2')];
expect(computeBetweenBorderFlags(fragments, lookup).size).toBe(0);
});
// --- page-split handling ---
it('does not flag when fragment continuesOnNext (page split)', () => {
const b1 = makeParagraphBlock('b1', MATCHING_BORDERS);
const b2 = makeParagraphBlock('b2', MATCHING_BORDERS);
const lookup = buildLookup([{ block: b1 }, { block: b2 }]);
const fragments: Fragment[] = [paraFragment('b1', { continuesOnNext: true }), paraFragment('b2')];
expect(computeBetweenBorderFlags(fragments, lookup).size).toBe(0);
});
it('does not flag when next fragment continuesFromPrev (page split continuation)', () => {
const b1 = makeParagraphBlock('b1', MATCHING_BORDERS);
const b2 = makeParagraphBlock('b2', MATCHING_BORDERS);
const lookup = buildLookup([{ block: b1 }, { block: b2 }]);
const fragments: Fragment[] = [paraFragment('b1'), paraFragment('b2', { continuesFromPrev: true })];
expect(computeBetweenBorderFlags(fragments, lookup).size).toBe(0);
});
// --- same-block deduplication ---
it('does not flag same blockId para fragments (same paragraph split across lines)', () => {
const b1 = makeParagraphBlock('b1', MATCHING_BORDERS);
const lookup = buildLookup([{ block: b1 }]);
const fragments: Fragment[] = [
paraFragment('b1', { fromLine: 0, toLine: 3 }),
paraFragment('b1', { fromLine: 3, toLine: 6 }),
];
expect(computeBetweenBorderFlags(fragments, lookup).size).toBe(0);
});
it('does not flag same blockId + same itemId list-item fragments', () => {
const block = makeListBlock('l1', [{ itemId: 'i1', borders: MATCHING_BORDERS }]);
const lookup = buildLookup([{ block }]);
const fragments: Fragment[] = [
listItemFragment('l1', 'i1', { fromLine: 0, toLine: 2 }),
listItemFragment('l1', 'i1', { fromLine: 2, toLine: 4 }),
];
expect(computeBetweenBorderFlags(fragments, lookup).size).toBe(0);
});
it('flags different itemIds in same list block', () => {
const block = makeListBlock('l1', [
{ itemId: 'i1', borders: MATCHING_BORDERS },
{ itemId: 'i2', borders: MATCHING_BORDERS },
]);
const lookup = buildLookup([{ block }]);
const fragments: Fragment[] = [listItemFragment('l1', 'i1'), listItemFragment('l1', 'i2')];
const flags = computeBetweenBorderFlags(fragments, lookup);
expect(flags.has(0)).toBe(true);
});
// --- non-paragraph fragments ---
it('skips image fragments', () => {
const b1 = makeParagraphBlock('b1', MATCHING_BORDERS);
const b2 = makeParagraphBlock('b2', MATCHING_BORDERS);
const imgBlock: ParagraphBlock = { kind: 'paragraph', id: 'img1', runs: [] };
const lookup = buildLookup([{ block: b1 }, { block: imgBlock }, { block: b2 }]);
const fragments: Fragment[] = [paraFragment('b1'), imageFragment('img1'), paraFragment('b2')];
// Index 0 can't pair with index 1 (image), index 1 is image (skip)
const flags = computeBetweenBorderFlags(fragments, lookup);
expect(flags.has(0)).toBe(false);
// Index 1 is image, skipped β but index 1β2 is imageβpara, image is skipped
expect(flags.size).toBe(0);
});
// --- mixed para + list-item ---
it('flags para followed by list-item with matching borders', () => {
const b1 = makeParagraphBlock('b1', MATCHING_BORDERS);
const block = makeListBlock('l1', [{ itemId: 'i1', borders: MATCHING_BORDERS }]);
const lookup = buildLookup([{ block: b1 }, { block }]);
const fragments: Fragment[] = [paraFragment('b1'), listItemFragment('l1', 'i1')];
expect(computeBetweenBorderFlags(fragments, lookup).has(0)).toBe(true);
});
it('flags list-item followed by para with matching borders', () => {
const block = makeListBlock('l1', [{ itemId: 'i1', borders: MATCHING_BORDERS }]);
const b2 = makeParagraphBlock('b2', MATCHING_BORDERS);
const lookup = buildLookup([{ block }, { block: b2 }]);
const fragments: Fragment[] = [listItemFragment('l1', 'i1'), paraFragment('b2')];
expect(computeBetweenBorderFlags(fragments, lookup).has(0)).toBe(true);
});
// --- multiple consecutive ---
it('flags all boundaries in a chain of three matching paragraphs', () => {
const b1 = makeParagraphBlock('b1', MATCHING_BORDERS);
const b2 = makeParagraphBlock('b2', MATCHING_BORDERS);
const b3 = makeParagraphBlock('b3', MATCHING_BORDERS);
const lookup = buildLookup([{ block: b1 }, { block: b2 }, { block: b3 }]);
const fragments: Fragment[] = [paraFragment('b1'), paraFragment('b2'), paraFragment('b3')];
const flags = computeBetweenBorderFlags(fragments, lookup);
expect(flags.has(0)).toBe(true);
expect(flags.get(0)?.showBetweenBorder).toBe(true);
expect(flags.has(1)).toBe(true);
expect(flags.get(1)?.showBetweenBorder).toBe(true);
expect(flags.get(1)?.suppressTopBorder).toBe(true);
expect(flags.has(2)).toBe(true);
expect(flags.get(2)?.suppressTopBorder).toBe(true);
expect(flags.get(2)?.showBetweenBorder).toBe(false);
expect(flags.size).toBe(3);
});
it('breaks chain when middle paragraph has different borders', () => {
const different: ParagraphBorders = {
top: { style: 'dashed', width: 3, color: '#F00' },
between: { style: 'dashed', width: 3, color: '#F00' },
};
const b1 = makeParagraphBlock('b1', MATCHING_BORDERS);
const b2 = makeParagraphBlock('b2', different);
const b3 = makeParagraphBlock('b3', MATCHING_BORDERS);
const lookup = buildLookup([{ block: b1 }, { block: b2 }, { block: b3 }]);
const fragments: Fragment[] = [paraFragment('b1'), paraFragment('b2'), paraFragment('b3')];
const flags = computeBetweenBorderFlags(fragments, lookup);
expect(flags.size).toBe(0);
});
// --- asymmetric between definition ---
it('does not flag when only first fragment has between border', () => {
const b1 = makeParagraphBlock('b1', MATCHING_BORDERS);
const noB: ParagraphBorders = { top: { style: 'solid', width: 1, color: '#000' } };
const b2 = makeParagraphBlock('b2', noB);
const lookup = buildLookup([{ block: b1 }, { block: b2 }]);
const fragments: Fragment[] = [paraFragment('b1'), paraFragment('b2')];
expect(computeBetweenBorderFlags(fragments, lookup).size).toBe(0);
});
it('does not flag when only second fragment has between border', () => {
const noB: ParagraphBorders = { top: { style: 'solid', width: 1, color: '#000' } };
const b1 = makeParagraphBlock('b1', noB);
const b2 = makeParagraphBlock('b2', MATCHING_BORDERS);
const lookup = buildLookup([{ block: b1 }, { block: b2 }]);
const fragments: Fragment[] = [paraFragment('b1'), paraFragment('b2')];
expect(computeBetweenBorderFlags(fragments, lookup).size).toBe(0);
});
// --- edge: empty / single fragment ---
it('returns empty set for empty fragment list', () => {
const lookup = buildLookup([]);
expect(computeBetweenBorderFlags([], lookup).size).toBe(0);
});
it('returns empty set for single fragment', () => {
const b1 = makeParagraphBlock('b1', MATCHING_BORDERS);
const lookup = buildLookup([{ block: b1 }]);
expect(computeBetweenBorderFlags([paraFragment('b1')], lookup).size).toBe(0);
});
// --- edge: missing block in lookup ---
it('handles missing blockId in lookup gracefully', () => {
const b2 = makeParagraphBlock('b2', MATCHING_BORDERS);
const lookup = buildLookup([{ block: b2 }]);
// b1 is not in lookup
const fragments: Fragment[] = [paraFragment('b1'), paraFragment('b2')];
expect(computeBetweenBorderFlags(fragments, lookup).size).toBe(0);
});
// --- edge: between borders match but other sides differ ---
it('does not flag when between borders match but other sides differ (different group)', () => {
const borders1: ParagraphBorders = {
top: { style: 'solid', width: 1, color: '#000' },
between: { style: 'solid', width: 1, color: '#000' },
};
const borders2: ParagraphBorders = {
top: { style: 'solid', width: 2, color: '#F00' },
between: { style: 'solid', width: 1, color: '#000' },
};
const b1 = makeParagraphBlock('b1', borders1);
const b2 = makeParagraphBlock('b2', borders2);
const lookup = buildLookup([{ block: b1 }, { block: b2 }]);
const fragments: Fragment[] = [paraFragment('b1'), paraFragment('b2')];
// Full border hash differs (top is different), so not same border group
expect(computeBetweenBorderFlags(fragments, lookup).size).toBe(0);
});
// --- edge: last fragment on page ---
it('last fragment on page is never flagged (no next to pair with)', () => {
const b1 = makeParagraphBlock('b1', MATCHING_BORDERS);
const lookup = buildLookup([{ block: b1 }]);
const fragments: Fragment[] = [paraFragment('b1')];
const flags = computeBetweenBorderFlags(fragments, lookup);
expect(flags.has(0)).toBe(false);
});
it('does not group fragments in different columns (different x positions)', () => {
const borders: ParagraphBorders = {
top: { style: 'solid', width: 1 },
between: { style: 'solid', width: 1 },
};
const b1 = makeParagraphBlock('b1', borders);
const b2 = makeParagraphBlock('b2', borders);
const lookup = buildLookup([{ block: b1 }, { block: b2 }]);
const fragments: Fragment[] = [paraFragment('b1', { y: 100, x: 0 }), paraFragment('b2', { y: 0, x: 300 })];
const flags = computeBetweenBorderFlags(fragments, lookup);
expect(flags.size).toBe(0);
});
it('still groups fragments in the same column (same x positions)', () => {
const borders: ParagraphBorders = {
top: { style: 'solid', width: 1 },
between: { style: 'solid', width: 1 },
};
const b1 = makeParagraphBlock('b1', borders);
const b2 = makeParagraphBlock('b2', borders);
const lookup = buildLookup([{ block: b1 }, { block: b2 }]);
const fragments: Fragment[] = [paraFragment('b1', { y: 0, x: 50 }), paraFragment('b2', { y: 16, x: 50 })];
const flags = computeBetweenBorderFlags(fragments, lookup);
expect(flags.size).toBe(2);
});
// --- nil/none between grouping (continuous box without separator) ---
it('groups paragraphs with between: {style: "none"} (nil/none between)', () => {
const borders: ParagraphBorders = {
top: { style: 'solid', width: 1, color: '#000' },
right: { style: 'solid', width: 1, color: '#000' },
bottom: { style: 'solid', width: 1, color: '#000' },
left: { style: 'solid', width: 1, color: '#000' },
between: { style: 'none' },
};
const b1 = makeParagraphBlock('b1', borders);
const b2 = makeParagraphBlock('b2', borders);
const lookup = buildLookup([{ block: b1 }, { block: b2 }]);
const fragments: Fragment[] = [paraFragment('b1', { y: 0 }), paraFragment('b2', { y: 20 })];
const flags = computeBetweenBorderFlags(fragments, lookup);
expect(flags.size).toBe(2);
// First fragment: suppressBottomBorder (not showBetweenBorder)
expect(flags.get(0)?.showBetweenBorder).toBe(false);
expect(flags.get(0)?.suppressBottomBorder).toBe(true);
// Second fragment: suppressTopBorder
expect(flags.get(1)?.suppressTopBorder).toBe(true);
expect(flags.get(1)?.suppressBottomBorder).toBe(false);
});
it('groups chain of 3 paragraphs with nil/none between', () => {
const borders: ParagraphBorders = {
top: { style: 'solid', width: 1, color: '#000' },
bottom: { style: 'solid', width: 1, color: '#000' },
left: { style: 'solid', width: 1, color: '#000' },
right: { style: 'solid', width: 1, color: '#000' },
between: { style: 'none' },
};
const b1 = makeParagraphBlock('b1', borders);
const b2 = makeParagraphBlock('b2', borders);
const b3 = makeParagraphBlock('b3', borders);
const lookup = buildLookup([{ block: b1 }, { block: b2 }, { block: b3 }]);
const fragments: Fragment[] = [
paraFragment('b1', { y: 0 }),
paraFragment('b2', { y: 20 }),
paraFragment('b3', { y: 40 }),
];
const flags = computeBetweenBorderFlags(fragments, lookup);
expect(flags.size).toBe(3);
// First: suppress bottom, keep top
expect(flags.get(0)?.suppressBottomBorder).toBe(true);
expect(flags.get(0)?.suppressTopBorder).toBe(false);
// Middle: suppress both top and bottom
expect(flags.get(1)?.suppressTopBorder).toBe(true);
expect(flags.get(1)?.suppressBottomBorder).toBe(true);
// Last: suppress top, keep bottom
expect(flags.get(2)?.suppressTopBorder).toBe(true);
expect(flags.get(2)?.suppressBottomBorder).toBe(false);
});
it('does not group nil/none between with real between (different hashes)', () => {
const nilBetween: ParagraphBorders = {
top: { style: 'solid', width: 1, color: '#000' },
between: { style: 'none' },
};
const realBetween: ParagraphBorders = {
top: { style: 'solid', width: 1, color: '#000' },
between: { style: 'solid', width: 1, color: '#000' },
};
const b1 = makeParagraphBlock('b1', nilBetween);
const b2 = makeParagraphBlock('b2', realBetween);
const lookup = buildLookup([{ block: b1 }, { block: b2 }]);
const fragments: Fragment[] = [paraFragment('b1'), paraFragment('b2')];
expect(computeBetweenBorderFlags(fragments, lookup).size).toBe(0);
});
});
// ---------------------------------------------------------------------------
// getParagraphBorderBox β indent-aware sizing
// ---------------------------------------------------------------------------
describe('getParagraphBorderBox', () => {
const W = 600;
it('returns full width with no indent', () => {
const box = getParagraphBorderBox(W);
expect(box).toEqual({ leftInset: 0, width: W });
});
it('returns full width when indent is undefined', () => {
const box = getParagraphBorderBox(W, undefined);
expect(box).toEqual({ leftInset: 0, width: W });
});
it('insets by left indent', () => {
const box = getParagraphBorderBox(W, { left: 40 });
expect(box.leftInset).toBe(40);
expect(box.width).toBe(W - 40);
});
it('insets by right indent', () => {
const box = getParagraphBorderBox(W, { right: 30 });
expect(box.leftInset).toBe(0);
expect(box.width).toBe(W - 30);
});
it('insets by both left and right', () => {
const box = getParagraphBorderBox(W, { left: 40, right: 30 });
expect(box.leftInset).toBe(40);
expect(box.width).toBe(W - 40 - 30);
});
it('uses smaller of left and left+firstLine for leftInset', () => {
// firstLine=20 β leftInset = min(50, 50+20) = 50, width uses leftInset=50
const box = getParagraphBorderBox(W, { left: 50, firstLine: 20 });
expect(box.leftInset).toBe(50);
expect(box.width).toBe(W - 50);
});
it('reduces leftInset when hanging exceeds left indent', () => {
// hanging=60 β firstLineOffset = 0 - 60 = -60 β minLeft = min(50, 50-60) = -10 β clamped to 0
const box = getParagraphBorderBox(W, { left: 50, hanging: 60 });
expect(box.leftInset).toBe(0);
expect(box.width).toBe(W);
});
it('handles hanging smaller than left indent', () => {
// hanging=20 β firstLineOffset = 0 - 20 = -20 β minLeft = min(50, 50-20) = 30
const box = getParagraphBorderBox(W, { left: 50, hanging: 20 });
expect(box.leftInset).toBe(30);
expect(box.width).toBe(W - 30);
});
it('clamps negative leftInset to 0', () => {
// left=0, hanging=30 β firstLineOffset = -30 β minLeft = min(0, -30) = -30 β clamped to 0
const box = getParagraphBorderBox(W, { hanging: 30 });
expect(box.leftInset).toBe(0);
expect(box.width).toBe(W);
});
it('clamps negative rightInset to 0', () => {
const box = getParagraphBorderBox(W, { right: -10 });
expect(box.leftInset).toBe(0);
expect(box.width).toBe(W);
});
it('clamps width to 0 when indents exceed fragment width', () => {
const box = getParagraphBorderBox(100, { left: 60, right: 60 });
expect(box.width).toBe(0);
});
it('handles all indent properties together', () => {
// left=40, right=30, firstLine=10, hanging=0
// firstLineOffset = 10 - 0 = 10, minLeft = min(40, 50) = 40
const box = getParagraphBorderBox(W, { left: 40, right: 30, firstLine: 10, hanging: 0 });
expect(box.leftInset).toBe(40);
expect(box.width).toBe(W - 40 - 30);
});
it('treats NaN indent values as 0', () => {
const box = getParagraphBorderBox(W, { left: NaN, right: NaN });
expect(box).toEqual({ leftInset: 0, width: W });
});
it('treats Infinity indent values as 0', () => {
const box = getParagraphBorderBox(W, { left: Infinity });
expect(box).toEqual({ leftInset: 0, width: W });
});
});
// ---------------------------------------------------------------------------
// computeBorderSpaceExpansion β border space (padding between border and text)
// ---------------------------------------------------------------------------
describe('computeBorderSpaceExpansion', () => {
const PX_PER_PT = 96 / 72;
it('returns zero expansion when no borders', () => {
expect(computeBorderSpaceExpansion(undefined)).toEqual({ top: 0, bottom: 0, left: 0, right: 0 });
});
it('returns zero expansion when borders have no space', () => {
const borders: ParagraphBorders = {
top: { style: 'solid', width: 1 },
bottom: { style: 'solid', width: 1 },
};
expect(computeBorderSpaceExpansion(borders)).toEqual({ top: 0, bottom: 0, left: 0, right: 0 });
});
it('expands all sides by space in px', () => {
const borders: ParagraphBorders = {
top: { style: 'solid', width: 1, space: 2 },
bottom: { style: 'solid', width: 1, space: 3 },
left: { style: 'solid', width: 1, space: 1 },
right: { style: 'solid', width: 1, space: 4 },
};
const result = computeBorderSpaceExpansion(borders);
expect(result.top).toBeCloseTo(2 * PX_PER_PT);
expect(result.bottom).toBeCloseTo(3 * PX_PER_PT);
expect(result.left).toBeCloseTo(1 * PX_PER_PT);
expect(result.right).toBeCloseTo(4 * PX_PER_PT);
});
it('suppresses top expansion when suppressTopBorder is true', () => {
const borders: ParagraphBorders = {
top: { style: 'solid', width: 1, space: 2 },
left: { style: 'solid', width: 1, space: 1 },
};
const info: BetweenBorderInfo = {
showBetweenBorder: false,
suppressTopBorder: true,
suppressBottomBorder: false,
gapBelow: 0,
};
const result = computeBorderSpaceExpansion(borders, info);
expect(result.top).toBe(0);
expect(result.left).toBeCloseTo(1 * PX_PER_PT);
});
it('suppresses bottom expansion when suppressBottomBorder is true', () => {
const borders: ParagraphBorders = {
bottom: { style: 'solid', width: 1, space: 2 },
right: { style: 'solid', width: 1, space: 1 },
};
const info: BetweenBorderInfo = {
showBetweenBorder: false,
suppressTopBorder: false,
suppressBottomBorder: true,
gapBelow: 10,
};
const result = computeBorderSpaceExpansion(borders, info);
expect(result.bottom).toBe(0);
expect(result.right).toBeCloseTo(1 * PX_PER_PT);
});
it('suppresses bottom expansion when showBetweenBorder is true', () => {
const borders: ParagraphBorders = {
bottom: { style: 'solid', width: 1, space: 2 },
};
const info: BetweenBorderInfo = {
showBetweenBorder: true,
suppressTopBorder: false,
suppressBottomBorder: false,
gapBelow: 8,
};
const result = computeBorderSpaceExpansion(borders, info);
expect(result.bottom).toBe(0);
});
});
// ---------------------------------------------------------------------------
// createParagraphDecorationLayers β border layer positioning
// ---------------------------------------------------------------------------
describe('createParagraphDecorationLayers β border layer positioning', () => {
const PX_PER_PT = 96 / 72;
it('extends borders into margins using negative offsets', () => {
const attrs = {
borders: {
top: { style: 'solid' as const, width: 2, space: 5 },
bottom: { style: 'solid' as const, width: 3, space: 10 },
left: { style: 'dashed' as const, width: 1, space: 4 },
right: { style: 'dotted' as const, width: 2, space: 6 },
},
};
const { borderLayer } = createParagraphDecorationLayers(document, 260, attrs);
// top: -(space*PX_PER_PT + width)
expect(parseFloat(borderLayer!.style.top)).toBeCloseTo(-(5 * PX_PER_PT + 2));
// bottom: -(space*PX_PER_PT + width)
expect(parseFloat(borderLayer!.style.bottom)).toBeCloseTo(-(10 * PX_PER_PT + 3));
// left: -(space*PX_PER_PT + width) (no indent)
expect(parseFloat(borderLayer!.style.left)).toBeCloseTo(-(4 * PX_PER_PT + 1));
// width: fragmentWidth + left expansion + right expansion