179 lines
8.2 KiB
TypeScript
179 lines
8.2 KiB
TypeScript
/**
|
|
* Picture crop (srcRect) tests:
|
|
* 1. patchPictureSrcRect unit tests (patch correctly written / removed / repeated round-trips).
|
|
* 2. editPictureSrcRect + savePptx roundtrip: pictures whose crop changed get srcRect written;
|
|
* bytes of other unchanged elements stay fully untouched.
|
|
* 3. With no crop at all, srcRect does not appear in the output.
|
|
*/
|
|
import { describe, it, expect } from 'vitest'
|
|
import { patchPictureSrcRect } from '../src/generate'
|
|
import { openPptx, savePptx, editPictureSrcRect } from '../src/index'
|
|
import JSZip from 'jszip'
|
|
import { readFileSync } from 'node:fs'
|
|
import { fileURLToPath } from 'node:url'
|
|
import { dirname, join } from 'node:path'
|
|
|
|
const here = dirname(fileURLToPath(import.meta.url))
|
|
const fx = (name: string) => readFileSync(join(here, 'fixtures', name))
|
|
|
|
// ── Unit tests: patchPictureSrcRect ─────────────────────────────────────────
|
|
|
|
const PIC_XML_NO_SRCRECT = `<p:pic><p:nvPicPr><p:cNvPr id="3" name="img"/><p:cNvPicPr/><p:nvPr/></p:nvPicPr><p:blipFill><a:blip r:embed="rId2"/><a:stretch><a:fillRect/></a:stretch></p:blipFill><p:spPr><a:xfrm><a:off x="0" y="0"/><a:ext cx="9144000" cy="6858000"/></a:xfrm><a:prstGeom prst="rect"><a:avLst/></a:prstGeom></p:spPr></p:pic>`
|
|
|
|
const PIC_XML_WITH_SRCRECT = `<p:pic><p:nvPicPr><p:cNvPr id="3" name="img"/><p:cNvPicPr/><p:nvPr/></p:nvPicPr><p:blipFill><a:blip r:embed="rId2"/><a:srcRect l="10000" t="5000" r="10000" b="5000"/><a:stretch><a:fillRect/></a:stretch></p:blipFill><p:spPr><a:xfrm><a:off x="0" y="0"/><a:ext cx="9144000" cy="6858000"/></a:xfrm><a:prstGeom prst="rect"><a:avLst/></a:prstGeom></p:spPr></p:pic>`
|
|
|
|
describe('parsePicture presetGeometry (picture styles)', () => {
|
|
const slideWith = (body: string) =>
|
|
'<?xml version="1.0"?><p:sld xmlns:p="p" xmlns:a="a" xmlns:r="r"><p:cSld>' +
|
|
`<p:spTree><p:nvGrpSpPr/><p:grpSpPr/>${body}</p:spTree></p:cSld></p:sld>`
|
|
const picWithGeom = (geom: string) =>
|
|
PIC_XML_NO_SRCRECT.replace('<a:prstGeom prst="rect"><a:avLst/></a:prstGeom>', geom)
|
|
|
|
it('ellipse geometry parses into presetGeometry; rect is the default and not recorded', async () => {
|
|
const { parseSlide } = await import('../src/parse')
|
|
const withEllipse = parseSlide({
|
|
path: 'ppt/slides/slide1.xml',
|
|
slideXml: slideWith(picWithGeom('<a:prstGeom prst="ellipse"><a:avLst/></a:prstGeom>')),
|
|
ctx: {},
|
|
})
|
|
expect((withEllipse.elements[0] as any).presetGeometry).toBe('ellipse')
|
|
|
|
const withRect = parseSlide({ path: 'ppt/slides/slide1.xml', slideXml: slideWith(PIC_XML_NO_SRCRECT), ctx: {} })
|
|
expect((withRect.elements[0] as any).presetGeometry).toBeUndefined()
|
|
})
|
|
|
|
it('roundRect adj adjustment values are parsed along with the geometry', async () => {
|
|
const { parseSlide } = await import('../src/parse')
|
|
const slide = parseSlide({
|
|
path: 'ppt/slides/slide1.xml',
|
|
slideXml: slideWith(
|
|
picWithGeom('<a:prstGeom prst="roundRect"><a:avLst><a:gd name="adj" fmla="val 25000"/></a:avLst></a:prstGeom>'),
|
|
),
|
|
ctx: {},
|
|
})
|
|
const el = slide.elements[0] as any
|
|
expect(el.presetGeometry).toBe('roundRect')
|
|
expect(el.adjust).toEqual({ adj: 25000 })
|
|
})
|
|
})
|
|
|
|
describe('patchPictureSrcRect - unit', () => {
|
|
it('inserts srcRect after <a:blip> when not present', () => {
|
|
const result = patchPictureSrcRect(PIC_XML_NO_SRCRECT, { l: 0.1, t: 0.05, r: 0.1, b: 0.05 })
|
|
expect(result).toContain('<a:srcRect l="10000" t="5000" r="10000" b="5000"/>')
|
|
// blip should still be there
|
|
expect(result).toContain('<a:blip r:embed="rId2"/>')
|
|
})
|
|
|
|
it('replaces existing srcRect', () => {
|
|
const result = patchPictureSrcRect(PIC_XML_WITH_SRCRECT, { l: 0.2, t: 0, r: 0, b: 0.3 })
|
|
expect(result).toContain('<a:srcRect l="20000" b="30000"/>')
|
|
// old srcRect gone
|
|
expect(result).not.toContain('l="10000"')
|
|
})
|
|
|
|
it('removes srcRect when null', () => {
|
|
const result = patchPictureSrcRect(PIC_XML_WITH_SRCRECT, null)
|
|
expect(result).not.toContain('<a:srcRect')
|
|
})
|
|
|
|
it('roundtrip: set then clear leaves original byte structure', () => {
|
|
const withCrop = patchPictureSrcRect(PIC_XML_NO_SRCRECT, { l: 0.1, t: 0.1, r: 0.1, b: 0.1 })
|
|
const cleared = patchPictureSrcRect(withCrop, null)
|
|
// No srcRect left
|
|
expect(cleared).not.toContain('<a:srcRect')
|
|
// blip still intact
|
|
expect(cleared).toContain('<a:blip r:embed="rId2"/>')
|
|
// stretch still intact
|
|
expect(cleared).toContain('<a:stretch><a:fillRect/></a:stretch>')
|
|
})
|
|
|
|
it('skips zero values in attributes', () => {
|
|
const result = patchPictureSrcRect(PIC_XML_NO_SRCRECT, { l: 0, t: 0.1, r: 0, b: 0 })
|
|
// l/r/b are zero → omitted
|
|
expect(result).toContain('<a:srcRect t="10000"/>')
|
|
expect(result).not.toContain('l="0"')
|
|
})
|
|
|
|
it('null srcRect on xml with no srcRect is no-op', () => {
|
|
const result = patchPictureSrcRect(PIC_XML_NO_SRCRECT, null)
|
|
expect(result).toBe(PIC_XML_NO_SRCRECT)
|
|
})
|
|
})
|
|
|
|
// ── Integration tests: editPictureSrcRect + savePptx ───────────────────────
|
|
|
|
describe('editPictureSrcRect + savePptx roundtrip', () => {
|
|
it('roundtrip: no picture edits → slides byte-identical (fidelity)', async () => {
|
|
const original = fx('01_standard_business.pptx')
|
|
const opened = await openPptx(original)
|
|
const out = await savePptx(opened)
|
|
const origZip = await JSZip.loadAsync(original)
|
|
const outZip = await JSZip.loadAsync(out)
|
|
for (const slide of opened.deck.slides) {
|
|
const a = await origZip.file(slide.path)!.async('uint8array')
|
|
const b = await outZip.file(slide.path)!.async('uint8array')
|
|
expect(Buffer.from(b).equals(Buffer.from(a))).toBe(true)
|
|
}
|
|
})
|
|
|
|
it('editPictureSrcRect sets srcRect and marks dirtySrcRect', async () => {
|
|
const opened = await openPptx(fx('01_standard_business.pptx'))
|
|
// Find the first picture element
|
|
let picId: string | undefined
|
|
let slideIdx = 0
|
|
for (let i = 0; i < opened.deck.slides.length; i++) {
|
|
const pic = opened.deck.slides[i]!.elements.find((e) => e.type === 'picture')
|
|
if (pic) { picId = pic.id; slideIdx = i; break }
|
|
}
|
|
if (!picId) {
|
|
// Pass directly if the standard business pptx has no pictures (test fixtures cover the base case)
|
|
return
|
|
}
|
|
const slide = opened.deck.slides[slideIdx]!
|
|
const ok = editPictureSrcRect(slide, picId, { l: 0.1, t: 0.1, r: 0.1, b: 0.1 })
|
|
expect(ok).toBe(true)
|
|
const pic = slide.elements.find((e) => e.id === picId)!
|
|
expect(pic.dirtySrcRect).toBe(true)
|
|
// srcRect written after save
|
|
const out = await savePptx(opened)
|
|
const outZip = await JSZip.loadAsync(out)
|
|
const xml = await outZip.file(slide.path)!.async('string')
|
|
expect(xml).toContain('<a:srcRect')
|
|
})
|
|
|
|
it('editPictureSrcRect with null removes srcRect', async () => {
|
|
// Set first, then clear
|
|
const opened = await openPptx(fx('01_standard_business.pptx'))
|
|
let picId: string | undefined
|
|
let slideIdx = 0
|
|
for (let i = 0; i < opened.deck.slides.length; i++) {
|
|
const pic = opened.deck.slides[i]!.elements.find((e) => e.type === 'picture')
|
|
if (pic) { picId = pic.id; slideIdx = i; break }
|
|
}
|
|
if (!picId) return
|
|
const slide = opened.deck.slides[slideIdx]!
|
|
// Write first, then clear
|
|
editPictureSrcRect(slide, picId, { l: 0.1, t: 0.1, r: 0.1, b: 0.1 })
|
|
editPictureSrcRect(slide, picId, null)
|
|
const pic = slide.elements.find((e) => e.id === picId)!
|
|
expect(pic.dirtySrcRect).toBe(true)
|
|
const out = await savePptx(opened)
|
|
const outZip = await JSZip.loadAsync(out)
|
|
const xml = await outZip.file(slide.path)!.async('string')
|
|
// After clearing, no srcRect (an empty <a:srcRect/> tag may appear, but never one with attributes)
|
|
expect(xml).not.toMatch(/<a:srcRect [^/]/)
|
|
})
|
|
|
|
it('returns false for non-existent or non-picture element', async () => {
|
|
const opened = await openPptx(fx('01_standard_business.pptx'))
|
|
const slide = opened.deck.slides[0]!
|
|
expect(editPictureSrcRect(slide, 'nonexistent_id', { l: 0.1, t: 0.1, r: 0, b: 0 })).toBe(false)
|
|
// text element
|
|
const textEl = slide.elements.find((e) => e.type === 'text' || e.type === 'shape')
|
|
if (textEl) {
|
|
expect(editPictureSrcRect(slide, textEl.id, { l: 0.1, t: 0.1, r: 0, b: 0 })).toBe(false)
|
|
}
|
|
})
|
|
})
|