|
| 1 | +/** |
| 2 | + * @vitest-environment jsdom |
| 3 | + */ |
| 4 | +import { describe, expect, it } from 'vitest' |
| 5 | +import { parseXml } from '@/lib/pptx-renderer/parser/xml-parser' |
| 6 | +import { renderBackground } from '@/lib/pptx-renderer/renderer/background-renderer' |
| 7 | +import type { RenderContext } from '@/lib/pptx-renderer/renderer/render-context' |
| 8 | + |
| 9 | +const EMPTY_NODE = parseXml( |
| 10 | + '<p:spTree xmlns:p="http://schemas.openxmlformats.org/presentationml/2006/main" />' |
| 11 | +) |
| 12 | + |
| 13 | +function createContext(backgroundXml: string): RenderContext { |
| 14 | + const background = parseXml(backgroundXml) |
| 15 | + const slide = { |
| 16 | + index: 0, |
| 17 | + nodes: [], |
| 18 | + background, |
| 19 | + layoutIndex: '', |
| 20 | + rels: new Map(), |
| 21 | + slidePath: 'ppt/slides/slide1.xml', |
| 22 | + showMasterSp: true, |
| 23 | + } |
| 24 | + |
| 25 | + return { |
| 26 | + presentation: { |
| 27 | + width: 960, |
| 28 | + height: 540, |
| 29 | + slides: [slide], |
| 30 | + layouts: new Map(), |
| 31 | + masters: new Map(), |
| 32 | + themes: new Map(), |
| 33 | + slideToLayout: new Map(), |
| 34 | + layoutToMaster: new Map(), |
| 35 | + masterToTheme: new Map(), |
| 36 | + media: new Map(), |
| 37 | + charts: new Map(), |
| 38 | + isWps: false, |
| 39 | + }, |
| 40 | + slide, |
| 41 | + theme: { |
| 42 | + colorScheme: new Map([['bg1', '000000']]), |
| 43 | + majorFont: { latin: 'Calibri', ea: '', cs: '' }, |
| 44 | + minorFont: { latin: 'Calibri', ea: '', cs: '' }, |
| 45 | + fillStyles: [], |
| 46 | + lineStyles: [], |
| 47 | + effectStyles: [], |
| 48 | + }, |
| 49 | + master: { |
| 50 | + colorMap: new Map(), |
| 51 | + textStyles: {}, |
| 52 | + placeholders: [], |
| 53 | + spTree: EMPTY_NODE, |
| 54 | + rels: new Map(), |
| 55 | + }, |
| 56 | + layout: { |
| 57 | + placeholders: [], |
| 58 | + spTree: EMPTY_NODE, |
| 59 | + rels: new Map(), |
| 60 | + showMasterSp: true, |
| 61 | + }, |
| 62 | + mediaUrlCache: new Map(), |
| 63 | + colorCache: new Map(), |
| 64 | + } |
| 65 | +} |
| 66 | + |
| 67 | +describe('renderBackground', () => { |
| 68 | + it('renders bgRef colors that resolve to black after modifiers', () => { |
| 69 | + const ctx = createContext(` |
| 70 | + <p:bg xmlns:p="http://schemas.openxmlformats.org/presentationml/2006/main" |
| 71 | + xmlns:a="http://schemas.openxmlformats.org/drawingml/2006/main"> |
| 72 | + <p:bgRef idx="1001"> |
| 73 | + <a:schemeClr val="bg1"> |
| 74 | + <a:shade val="50000" /> |
| 75 | + </a:schemeClr> |
| 76 | + </p:bgRef> |
| 77 | + </p:bg> |
| 78 | + `) |
| 79 | + const container = document.createElement('div') |
| 80 | + |
| 81 | + renderBackground(ctx, container) |
| 82 | + |
| 83 | + expect(container.style.backgroundColor).toBe('rgb(0, 0, 0)') |
| 84 | + }) |
| 85 | + |
| 86 | + it('keeps bgRef without a color node on the white fallback', () => { |
| 87 | + const ctx = createContext(` |
| 88 | + <p:bg xmlns:p="http://schemas.openxmlformats.org/presentationml/2006/main"> |
| 89 | + <p:bgRef idx="1001" /> |
| 90 | + </p:bg> |
| 91 | + `) |
| 92 | + const container = document.createElement('div') |
| 93 | + |
| 94 | + renderBackground(ctx, container) |
| 95 | + |
| 96 | + expect(container.style.backgroundColor).toBe('rgb(255, 255, 255)') |
| 97 | + }) |
| 98 | +}) |
0 commit comments