fix(pptx): replace solidFill when recoloring preset and system runs (#326)

This commit is contained in:
ANIRUDDHA ADAK
2026-09-13 17:35:37 +08:00
committed by GitHub
parent 32e16d6a2c
commit ad0ff48252
2 changed files with 34 additions and 0 deletions
+9
View File
@@ -586,6 +586,15 @@ function patchRunColor(runXml: string, color: string): string {
`<a:solidFill><a:srgbClr val="${hex}"/></a:solidFill>`,
)
}
// Any other solidFill flavor (prstClr/sysClr/hslClr): replace the whole
// block so a second solidFill is never injected (duplicate solidFill
// children fail strict validation and trigger PowerPoint repair).
if (/<a:solidFill>[\s\S]*?<\/a:solidFill>/.test(runXml)) {
return runXml.replace(
/<a:solidFill>[\s\S]*?<\/a:solidFill>/,
`<a:solidFill><a:srgbClr val="${hex}"/></a:solidFill>`,
)
}
// No solidFill: expand a self-closing rPr to a pair first, then inject at the start
const fill = `<a:solidFill><a:srgbClr val="${hex}"/></a:solidFill>`
if (/<a:rPr\b[^>]*\/>/.test(runXml)) {
@@ -410,3 +410,28 @@ describe('text highlight <a:highlight>', () => {
expect(out).toContain('<a:highlight><a:srgbClr val="FF0000"/></a:highlight>')
})
})
describe('recolor replaces non-srgb fills instead of duplicating solidFill', () => {
const recolor = (runXml: string) => {
const { slide, el } = parseEl(runXml)
expect(setElementFont(slide, el.id, { color: '#112233' })).toBe(true)
return patchTextElementXml(el, el.anchor.originalXml)
}
it('preset-color run keeps a single solidFill', () => {
const out = recolor(
'<a:r><a:rPr><a:solidFill><a:prstClr val="red"/></a:solidFill></a:rPr><a:t>x</a:t></a:r>',
)
expect(out.match(/<a:solidFill>/g)).toHaveLength(1)
expect(out).toContain('<a:srgbClr val="112233"/>')
expect(out).not.toContain('prstClr')
})
it('system-color run keeps a single solidFill', () => {
const out = recolor(
'<a:r><a:rPr><a:solidFill><a:sysClr val="windowText"/></a:solidFill></a:rPr><a:t>x</a:t></a:r>',
)
expect(out.match(/<a:solidFill>/g)).toHaveLength(1)
expect(out).toContain('<a:srgbClr val="112233"/>')
})
})