返回 oh-my-ppt
extract-script.test.ts
根目录 / tests / unit / html-pptx / extract-script.test.ts
1 /** @vitest-environment happy-dom */
2 import { describe, expect, it } from 'vitest'
3 import fs from 'fs'
4 import path from 'path'
5 import {
6 buildHtmlToPptxExtractScript,
7 normalizeExtractedHtmlToPptxSlide
8 } from '@arcsin1/html2pptx'
9
10 describe('buildHtmlToPptxExtractScript', () => {
11 const buildScript = () =>
12 buildHtmlToPptxExtractScript({
13 pageWidthPx: 1600,
14 pageHeightPx: 900
15 })
16
17 const rect = (left: number, top: number, width: number, height: number) => ({
18 x: left,
19 y: top,
20 left,
21 top,
22 width,
23 height,
24 right: left + width,
25 bottom: top + height
26 })
27
28 const assignRect = (selector: string, left: number, top: number, width: number, height: number) => {
29 const element = document.querySelector(selector)
30 if (!element) throw new Error(`Missing test node: ${selector}`)
31 Object.defineProperty(element, 'getBoundingClientRect', {
32 configurable: true,
33 value: () => rect(left, top, width, height)
34 })
35 }
36
37 const extractInDom = async ({
38 maxTextBoxes = 360,
39 unsupportedTransformStrategy
40 }: {
41 maxTextBoxes?: number
42 unsupportedTransformStrategy?: 'native' | 'raster-fallback'
43 } = {}) =>
44 new Function(
45 `return ${
46 buildHtmlToPptxExtractScript({
47 pageWidthPx: 1600,
48 pageHeightPx: 900,
49 maxTextBoxes,
50 unsupportedTransformStrategy
51 }).trim()
52 }`
53 )() as Promise<Record<string, unknown>>
54
55 it('exports Tailwind rings with a visual hint map and computed spread fallback', () => {
56 const script = buildScript()
57
58 expect(script).toContain("['ring-1', 1]")
59 expect(script).toContain("['ring-2', 2]")
60 expect(script).toContain("['ring-4', 4]")
61 expect(script).toContain('const parseTailwindRingWidth = (utility) => {')
62 expect(script).toContain('const arbitrary = utility.match(/^ring-')
63 expect(script).toContain('Number.parseFloat(arbitrary[1])')
64 expect(script).toContain('const [offsetX, offsetY, blur, spread] = lengths;')
65 expect(script).toContain('best = { w: spread, c: color, colorSource };')
66 })
67
68 it('supports common Tailwind color scales for visual hints', () => {
69 const script = buildScript()
70
71 expect(script).toContain('const resolveTailwindColorToken = (name) => {')
72 expect(script).toContain("red: ['#FEF2F2'")
73 expect(script).toContain("'#EF4444'")
74 expect(script).toContain('const palette = tailwindColorPaletteMap.get(match[1]);')
75 })
76
77 it('generates parseable browser extraction JavaScript', () => {
78 expect(() => new Function(buildScript())).not.toThrow()
79 })
80
81 it('keeps an unexported text block in the raster fallback when the text limit is reached', async () => {
82 document.body.innerHTML = `
83 <div class="ppt-page-root" style="display:block">
84 <p id="first" style="display:block;font-size:20px">First text</p>
85 <p id="second" style="display:block;font-size:20px">Second text</p>
86 </div>
87 `
88 assignRect('.ppt-page-root', 0, 0, 1600, 900)
89 assignRect('#first', 100, 100, 320, 36)
90 assignRect('#second', 100, 160, 320, 36)
91
92 const extracted = await extractInDom({ maxTextBoxes: 1 })
93
94 expect(extracted.texts).toHaveLength(1)
95 expect(extracted.extractionReport).toMatchObject({ textLimitReached: true })
96 expect(document.querySelector('#first')?.getAttribute('data-pptx-extracted-text')).toBe('1')
97 expect(document.querySelector('#second')?.hasAttribute('data-pptx-extracted-text')).toBe(false)
98 })
99
100 it('marks extracted inline KPI text so it is removed from the raster background', async () => {
101 document.body.innerHTML = `
102 <div class="ppt-page-root" style="display:block">
103 <div id="metrics" style="display:flex">
104 <span id="market" style="font-size:48px">528</span>
105 <span id="share" style="font-size:48px">63<span id="percent" style="font-size:30px">%</span></span>
106 <span id="cost" style="font-size:48px">$2,950</span>
107 </div>
108 <p id="fallback" data-pptx-formula-block style="display:block;font-size:20px">Fallback text</p>
109 </div>
110 `
111 assignRect('.ppt-page-root', 0, 0, 1600, 900)
112 assignRect('#metrics', 100, 180, 640, 80)
113 assignRect('#market', 100, 180, 120, 60)
114 assignRect('#share', 240, 180, 120, 60)
115 assignRect('#percent', 320, 205, 32, 36)
116 assignRect('#cost', 400, 180, 180, 60)
117 assignRect('#fallback', 100, 300, 220, 36)
118
119 const originalCreateRange = document.createRange.bind(document)
120 let selectedNode: Node | null = null
121 const rangeRect = () => {
122 const id = selectedNode?.parentElement?.id
123 if (id === 'market') return rect(100, 180, 120, 60)
124 if (id === 'share') return rect(240, 180, 80, 60)
125 if (id === 'percent') return rect(320, 205, 32, 36)
126 if (id === 'cost') return rect(400, 180, 180, 60)
127 return rect(100, 300, 220, 36)
128 }
129 document.createRange = (() => ({
130 selectNode: (node: Node) => {
131 selectedNode = node
132 },
133 selectNodeContents: (node: Node) => {
134 selectedNode = node
135 },
136 setStart: () => {},
137 setEnd: () => {},
138 getBoundingClientRect: () => rangeRect(),
139 getClientRects: () => [rangeRect()],
140 detach: () => {}
141 })) as typeof document.createRange
142
143 try {
144 await extractInDom({ unsupportedTransformStrategy: 'raster-fallback' })
145
146 expect(document.querySelector('#market')?.getAttribute('data-pptx-extracted-text')).toBe('1')
147 expect(document.querySelector('#share')?.getAttribute('data-pptx-extracted-text')).toBe('1')
148 expect(document.querySelector('#percent')?.getAttribute('data-pptx-extracted-text')).toBe('1')
149 expect(document.querySelector('#cost')?.getAttribute('data-pptx-extracted-text')).toBe('1')
150 expect(document.querySelector('#fallback')?.hasAttribute('data-pptx-extracted-text')).toBe(false)
151 } finally {
152 document.createRange = originalCreateRange
153 }
154 })
155
156 it('exports simple rotation natively with a rotate field instead of a distorted rectangle', async () => {
157 document.body.innerHTML = `
158 <div class="ppt-page-root" style="display:block">
159 <div id="rotated" style="display:block;background-color:rgb(22, 96, 171);border:1px solid rgb(22, 96, 171);transform:rotate(25deg)"></div>
160 </div>
161 `
162 assignRect('.ppt-page-root', 0, 0, 1600, 900)
163 assignRect('#rotated', 120, 90, 360, 180)
164
165 const extracted = await extractInDom({ unsupportedTransformStrategy: 'raster-fallback' })
166
167 // html2pptx >= 0.2.0 通过矩阵分解重建简单旋转:导出带 rotate 的可编辑形状,
168 // 不再视为 unsupported transform 坠回栅格(修复前导出的是无旋转的畸变矩形)
169 const shapes = extracted.shapes as Array<Record<string, unknown>>
170 expect(shapes).toHaveLength(1)
171 expect(shapes[0]).toMatchObject({ shapeType: 'rect', rotate: 25, fill: '1660AB' })
172 expect(extracted.extractionReport).toMatchObject({ unsupportedTransformCount: 0 })
173 expect(document.querySelector('#rotated')?.getAttribute('data-pptx-extracted-shape')).toBe('1')
174 })
175
176 it('keeps skewed shapes in the raster fallback instead of exporting a distorted approximation', async () => {
177 document.body.innerHTML = `
178 <div class="ppt-page-root" style="display:block">
179 <div id="skewed" style="display:block;background-color:rgb(22, 96, 171);transform:skew(20deg)"></div>
180 </div>
181 `
182 assignRect('.ppt-page-root', 0, 0, 1600, 900)
183 assignRect('#skewed', 120, 90, 360, 180)
184
185 const extracted = await extractInDom({ unsupportedTransformStrategy: 'raster-fallback' })
186
187 expect(extracted.shapes).toHaveLength(0)
188 expect(extracted.extractionReport).toMatchObject({ unsupportedTransformCount: 1 })
189 expect(document.querySelector('#skewed')?.hasAttribute('data-pptx-extracted-shape')).toBe(false)
190 })
191
192 it('keeps transformed elements in the public API output unless raster fallback is requested', async () => {
193 document.body.innerHTML = `
194 <div class="ppt-page-root" style="display:block">
195 <div id="rotated" style="display:block;background-color:rgb(22, 96, 171);border:1px solid rgb(22, 96, 171);transform:rotate(25deg)"></div>
196 </div>
197 `
198 assignRect('.ppt-page-root', 0, 0, 1600, 900)
199 assignRect('#rotated', 120, 90, 360, 180)
200
201 const extracted = await extractInDom()
202
203 expect(extracted.shapes).toHaveLength(1)
204 expect(extracted.extractionReport).toMatchObject({ unsupportedTransformCount: 0 })
205 })
206
207 it('uses Tailwind font weight hints when no inline font weight overrides them', () => {
208 const script = buildHtmlToPptxExtractScript({
209 pageWidthPx: 1600,
210 pageHeightPx: 900
211 })
212
213 expect(script).toContain('const resolveTailwindFontWeight = (element) => {')
214 expect(script).toContain('const resolveInlineFontWeight = (element) => {')
215 expect(script).toContain('const inlineFontWeight = resolveInlineFontWeight(parentElement);')
216 expect(script).toContain('const tailwindFontWeight = resolveTailwindFontWeight(parentElement);')
217 expect(script).toContain(
218 'const fontWeight = inlineFontWeight || tailwindFontWeight || computedFontWeight || 400;'
219 )
220 expect(script).not.toContain('tailwindTextHints.fontWeight || computedFontWeight')
221 })
222
223 it('extracts inline text runs for styled spans inside block text', () => {
224 const script = buildScript()
225
226 expect(script).toContain('const collectInlineTextRuns = (element, baseStyle) => {')
227 expect(script).toContain('const collectInlineTextLineRuns = (element, baseStyle) => {')
228 expect(script).toContain('const appendTextRun = (runs, run) => {')
229 expect(script).toContain('const textRunFor = (text, style, element) => {')
230 expect(script).toContain('const hasStyledRun = runs.some((run) => !sameTextRunStyle(run, baseRun));')
231 expect(script).toContain('const inlineRuns = collectInlineTextRuns(element, style);')
232 expect(script).toContain('const inlineLineRuns = inlineRuns?.length ? collectInlineTextLineRuns(element, style) : [];')
233 expect(script).toContain('inlineLineRuns.every((line) =>')
234 expect(script).toContain('const rollbackTextExtraction = (startIndex, seenBefore) =>')
235 expect(script).toContain('runs: inlineRuns')
236 expect(script).toContain('...(richTextRuns?.length ? { runs: richTextRuns } : {})')
237 })
238
239 it('preserves normalized rich text runs with color and bold weight', () => {
240 const slide = normalizeExtractedHtmlToPptxSlide({
241 backgroundColor: 'FFFFFF',
242 texts: [
243 {
244 text: '本页文字梳理:原始内容',
245 x: 1,
246 y: 1,
247 w: 3,
248 h: 0.4,
249 fontSize: 18,
250 color: '3E3A39',
251 runs: [
252 {
253 text: '本页文字梳理:',
254 fontSize: 18,
255 fontFace: 'Noto Sans SC',
256 color: 'C00000',
257 bold: true
258 },
259 {
260 text: '原始内容',
261 fontSize: 18,
262 fontFace: 'Noto Sans SC',
263 color: '3E3A39'
264 }
265 ]
266 }
267 ],
268 shapes: [],
269 images: [],
270 tables: []
271 })
272
273 expect(slide.texts[0]?.runs?.[0]).toMatchObject({
274 text: '本页文字梳理:',
275 color: 'C00000',
276 bold: true
277 })
278 expect(slide.texts[0]?.runs?.[1]).toMatchObject({
279 text: '原始内容',
280 color: '3E3A39',
281 bold: false
282 })
283 })
284
285 it('uses CSS stacking order as paint-order fallback for z-indexed edits', () => {
286 const script = buildScript()
287
288 expect(script).toContain('const parseCssZIndex = (style) => {')
289 expect(script).toContain(
290 "parseCssZIndex(style) !== undefined && (style.position !== 'static' || isFlexOrGridItem(element))"
291 )
292 expect(script).toContain('const stackingKeyFor = (element) => {')
293 expect(script).toContain('const compareStackingOrder = (left, right) => {')
294 expect(script).toContain('const fallback = new Map(entries.map(([id, element]) => [id, stackingKeyFor(element)]));')
295 expect(script).toContain('if (!document.elementsFromPoint) return buildFallbackResult();')
296 expect(script).not.toContain('if (entries.length === 0 || !document.elementsFromPoint) return new Map();')
297 })
298
299 it('exports a single visible border side as a line instead of a full rectangle border', () => {
300 const script = buildScript()
301
302 expect(script).toContain('const collectBorderSides = () => {')
303 expect(script).toContain('const parseTailwindBorderSideWidth = (utility) => {')
304 expect(script).toContain('const parseTailwindBorderSideColor = (utility) => {')
305 expect(script).toContain("sideKey === 'x' ? ['left', 'right']")
306 expect(script).toContain('borderSideWidthPx: {')
307 expect(script).toContain('borderSideColorSource: {')
308 expect(script).toContain('const hintedWidth = tailwindVisualHints.borderSideWidthPx?.[sideName];')
309 expect(script).toContain('const removeSide = (sideName) => {')
310 expect(script).toContain('if (hintedWidth !== undefined && hintedWidth <= 0) {')
311 expect(script).toContain('const hasUniformFourSideBorder =')
312 expect(script).toContain('const shouldSplitBorderSides =')
313 expect(script).toContain('const mainBorderInfo = shouldSplitBorderSides ? null : borderInfo;')
314 expect(script).toContain('const buildBorderLineShape = (borderSide) => {')
315 expect(script).toContain("shapeType: 'line'")
316 expect(script).toContain("if (borderSide.side === 'bottom')")
317 expect(script).toContain('border: hasMainBorder')
318 expect(script).toContain('splitBorderLineShapes.forEach((shape) => shapes.push(shape));')
319 expect(script).toContain("dash: borderSide.dash || 'solid'")
320 })
321
322 it('keeps small grid color cells as shapes for Tailwind visual panels', () => {
323 const script = buildScript()
324
325 expect(script).toContain('const isGridPaintCell =')
326 expect(script).toContain('/grid/i.test(parentDisplay)')
327 expect(script).toContain('!normalize(element.innerText || element.textContent)')
328 expect(script).toContain('!hasBorder && !isSmallBadge && !isGridPaintCell')
329 expect(script).toContain('if (shapes.length >= maxShapes) {')
330 expect(script).toContain('extractionReport.shapeLimitReached = true;')
331 expect(script).not.toContain('if (shapes.length >= maxShapes) break;')
332 })
333
334 it('keeps thin filled strips such as Tailwind header bars', () => {
335 const script = buildScript()
336
337 expect(script).toContain('const isThinPaintStrip =')
338 expect(script).toContain('(rect.width >= 24 && rect.height >= 2 && rect.height < 12)')
339 expect(script).toContain('(rect.height >= 24 && rect.width >= 2 && rect.width < 12)')
340 expect(script).toContain("if ((rect.width < 12 || rect.height < 12) && !isThinPaintStrip) continue;")
341 })
342
343 it('exports thin border connectors and CSS chevron arrows as PPT lines', () => {
344 const script = buildScript()
345
346 expect(script).toContain('const hasVisibleBorder =')
347 expect(script).toContain("['Top', 'Right', 'Bottom', 'Left'].some")
348 expect(script).toContain('const isCssChevronBorder =')
349 expect(script).toContain("hasCornerBorderSides('top', 'right')")
350 expect(script).toContain('const buildCssChevronLineShapes = () => {')
351 expect(script).toContain('flipV: true')
352 expect(script).toContain('cssChevronLineShapes.forEach((shape) => shapes.push(shape));')
353 expect(script).toContain('const shouldExportOnlyBorderLines =')
354 })
355
356 it('uses enough text, shape and image budget for dense PPTX export', () => {
357 const rendererSource = fs.readFileSync(
358 path.join(process.cwd(), 'src/main/io/html-pptx/renderer.ts'),
359 'utf-8'
360 )
361
362 expect(rendererSource).toContain('maxTextBoxes: 360')
363 expect(rendererSource).toContain('maxShapes: 400')
364 expect(rendererSource).toContain('maxImages: 80')
365 expect(rendererSource).not.toContain('maxShapes: 80')
366 })
367 })
368
368 lines TYPESCRIPT