|
| 1 | +/** |
| 2 | + * Tests for list marker font projection (SD-3238). |
| 3 | + */ |
| 4 | + |
| 5 | +import { describe, it, expect } from 'vitest'; |
| 6 | +import { syncListMarkerFontFromParagraphRuns } from './list-marker-font.js'; |
| 7 | + |
| 8 | +const minimalContext = { |
| 9 | + translatedNumbering: { |
| 10 | + definitions: { '1': { numId: 1, abstractNumId: 1 } }, |
| 11 | + abstracts: { |
| 12 | + '1': { |
| 13 | + abstractNumId: 1, |
| 14 | + levels: { '0': { ilvl: 0, runProperties: {} } }, |
| 15 | + }, |
| 16 | + }, |
| 17 | + }, |
| 18 | + translatedLinkedStyles: { docDefaults: {}, styles: {} }, |
| 19 | + tableInfo: null, |
| 20 | +}; |
| 21 | + |
| 22 | +const symbolContext = { |
| 23 | + ...minimalContext, |
| 24 | + translatedNumbering: { |
| 25 | + definitions: { '1': { numId: 1, abstractNumId: 1 } }, |
| 26 | + abstracts: { |
| 27 | + '1': { |
| 28 | + abstractNumId: 1, |
| 29 | + levels: { |
| 30 | + '0': { |
| 31 | + ilvl: 0, |
| 32 | + runProperties: { fontFamily: { ascii: 'Symbol' } }, |
| 33 | + }, |
| 34 | + }, |
| 35 | + }, |
| 36 | + }, |
| 37 | + }, |
| 38 | +}; |
| 39 | + |
| 40 | +const paragraphWithTextStyle = (markAttrs: Record<string, unknown>) => ({ |
| 41 | + content: { |
| 42 | + forEach(fn: (child: unknown) => void) { |
| 43 | + fn({ |
| 44 | + content: { |
| 45 | + forEach(fn2: (child: unknown) => void) { |
| 46 | + fn2({ |
| 47 | + isText: true, |
| 48 | + text: 'item', |
| 49 | + marks: [{ type: { name: 'textStyle' }, attrs: markAttrs }], |
| 50 | + }); |
| 51 | + }, |
| 52 | + }, |
| 53 | + }); |
| 54 | + }, |
| 55 | + }, |
| 56 | +}); |
| 57 | + |
| 58 | +const listBlock = ({ |
| 59 | + runs, |
| 60 | + markerFamily = 'Times New Roman, serif', |
| 61 | + markerSize = 12, |
| 62 | + markerText = '1.', |
| 63 | + numberingProperties = { numId: 1, ilvl: 0 }, |
| 64 | +}: { |
| 65 | + runs: Array<{ text: string; fontFamily?: string; fontSize?: number }>; |
| 66 | + markerFamily?: string; |
| 67 | + markerSize?: number; |
| 68 | + markerText?: string; |
| 69 | + numberingProperties?: { numId: number; ilvl: number }; |
| 70 | +}) => ({ |
| 71 | + runs, |
| 72 | + attrs: { |
| 73 | + numberingProperties, |
| 74 | + wordLayout: { |
| 75 | + marker: { |
| 76 | + markerText, |
| 77 | + run: { fontFamily: markerFamily, fontSize: markerSize }, |
| 78 | + }, |
| 79 | + }, |
| 80 | + }, |
| 81 | +}); |
| 82 | + |
| 83 | +describe('syncListMarkerFontFromParagraphRuns', () => { |
| 84 | + it('syncs marker font from freshly converted text runs', () => { |
| 85 | + const block = listBlock({ |
| 86 | + runs: [{ text: 'item', fontFamily: 'Georgia, serif', fontSize: 30 }], |
| 87 | + }); |
| 88 | + |
| 89 | + syncListMarkerFontFromParagraphRuns({ block, converterContext: minimalContext as never }); |
| 90 | + |
| 91 | + expect(block.attrs.wordLayout?.marker?.run?.fontFamily).toContain('Georgia'); |
| 92 | + expect(block.attrs.wordLayout?.marker?.run?.fontSize).toBe(30); |
| 93 | + }); |
| 94 | + |
| 95 | + it('prefers converted runs over live PM textStyle by default', () => { |
| 96 | + const block = listBlock({ |
| 97 | + runs: [{ text: 'item', fontFamily: 'Georgia, serif', fontSize: 30 }], |
| 98 | + }); |
| 99 | + |
| 100 | + syncListMarkerFontFromParagraphRuns({ |
| 101 | + block, |
| 102 | + converterContext: minimalContext as never, |
| 103 | + para: paragraphWithTextStyle({ fontFamily: 'Arial', fontSize: '12pt' }) as never, |
| 104 | + }); |
| 105 | + |
| 106 | + expect(block.attrs.wordLayout?.marker?.run?.fontFamily).toContain('Georgia'); |
| 107 | + expect(block.attrs.wordLayout?.marker?.run?.fontSize).toBe(30); |
| 108 | + }); |
| 109 | + |
| 110 | + it('uses live PM textStyle over stale cached runs on cache hits', () => { |
| 111 | + const block = listBlock({ |
| 112 | + runs: [{ text: 'item', fontFamily: 'Times New Roman, serif', fontSize: 12 }], |
| 113 | + }); |
| 114 | + |
| 115 | + syncListMarkerFontFromParagraphRuns({ |
| 116 | + block, |
| 117 | + converterContext: minimalContext as never, |
| 118 | + para: paragraphWithTextStyle({ fontFamily: 'Georgia', fontSize: '30pt' }) as never, |
| 119 | + contentFontSource: 'paragraph', |
| 120 | + }); |
| 121 | + |
| 122 | + expect(block.attrs.wordLayout?.marker?.run?.fontFamily).toContain('Georgia'); |
| 123 | + expect(block.attrs.wordLayout?.marker?.run?.fontSize).toBe(40); |
| 124 | + }); |
| 125 | + |
| 126 | + it('merges partial PM textStyle with cached runs on cache hits', () => { |
| 127 | + const block = listBlock({ |
| 128 | + runs: [{ text: 'item', fontFamily: 'Georgia, serif', fontSize: 12 }], |
| 129 | + }); |
| 130 | + |
| 131 | + syncListMarkerFontFromParagraphRuns({ |
| 132 | + block, |
| 133 | + converterContext: minimalContext as never, |
| 134 | + para: paragraphWithTextStyle({ fontSize: '30pt' }) as never, |
| 135 | + contentFontSource: 'paragraph', |
| 136 | + }); |
| 137 | + |
| 138 | + expect(block.attrs.wordLayout?.marker?.run?.fontFamily).toContain('Georgia'); |
| 139 | + expect(block.attrs.wordLayout?.marker?.run?.fontSize).toBe(40); |
| 140 | + }); |
| 141 | + |
| 142 | + it('preserves numbering-defined marker font family but still syncs font size', () => { |
| 143 | + const block = listBlock({ |
| 144 | + runs: [{ text: 'item', fontFamily: 'Georgia, serif', fontSize: 30 }], |
| 145 | + markerFamily: 'Symbol', |
| 146 | + markerText: '•', |
| 147 | + }); |
| 148 | + |
| 149 | + syncListMarkerFontFromParagraphRuns({ block, converterContext: symbolContext as never }); |
| 150 | + |
| 151 | + expect(block.attrs.wordLayout?.marker?.run?.fontFamily).toContain('Symbol'); |
| 152 | + expect(block.attrs.wordLayout?.marker?.run?.fontSize).toBe(30); |
| 153 | + }); |
| 154 | + |
| 155 | + it('reads numbering from block attrs on cache hits so Symbol font is preserved', () => { |
| 156 | + const block = listBlock({ |
| 157 | + runs: [{ text: 'item', fontFamily: 'Georgia, serif', fontSize: 40 }], |
| 158 | + markerFamily: 'Symbol', |
| 159 | + markerText: '•', |
| 160 | + }); |
| 161 | + |
| 162 | + syncListMarkerFontFromParagraphRuns({ |
| 163 | + block, |
| 164 | + converterContext: symbolContext as never, |
| 165 | + para: paragraphWithTextStyle({ fontFamily: 'Georgia', fontSize: '30pt' }) as never, |
| 166 | + contentFontSource: 'paragraph', |
| 167 | + }); |
| 168 | + |
| 169 | + expect(block.attrs.wordLayout?.marker?.run?.fontFamily).toContain('Symbol'); |
| 170 | + expect(block.attrs.wordLayout?.marker?.run?.fontSize).toBe(40); |
| 171 | + }); |
| 172 | +}); |
0 commit comments