fix(pdf): validate colors and toggles when decoding text styles (#328)

This commit is contained in:
ANIRUDDHA ADAK
2026-09-13 17:36:13 +08:00
committed by GitHub
parent eaf7bee66f
commit 77a1be5ccb
2 changed files with 23 additions and 8 deletions
+5 -3
View File
@@ -46,14 +46,16 @@ export function decodeStyle(key: string): CharStyle {
if (!key) return {}
const [color = '', font = '', size = '', bold = '', italic = ''] = key.split('|')
const out: CharStyle = {}
if (color) out.color = color
if (/^#[0-9a-f]{6}$/i.test(color)) out.color = color
if (font) out.font = font
if (size) {
const n = Number(size)
if (Number.isFinite(n) && n > 0) out.size = n
}
if (bold) out.bold = bold === '1'
if (italic) out.italic = italic === '1'
if (bold === '1') out.bold = true
else if (bold === '0') out.bold = false
if (italic === '1') out.italic = true
else if (italic === '0') out.italic = false
return out
}
+18 -5
View File
@@ -147,10 +147,23 @@ describe('style keys', () => {
})
it('ignores non-numeric, non-finite, and non-positive sizes', () => {
expect(decodeStyle('x|y|oops||')).toEqual({ color: 'x', font: 'y' })
expect(decodeStyle('x|y|Infinity||')).toEqual({ color: 'x', font: 'y' })
expect(decodeStyle('x|y|0||')).toEqual({ color: 'x', font: 'y' })
expect(decodeStyle('x|y|-3||')).toEqual({ color: 'x', font: 'y' })
expect(decodeStyle('x|y|12.5||')).toEqual({ color: 'x', font: 'y', size: 12.5 })
expect(decodeStyle('x|y|oops||')).toEqual({ font: 'y' })
expect(decodeStyle('x|y|Infinity||')).toEqual({ font: 'y' })
expect(decodeStyle('x|y|0||')).toEqual({ font: 'y' })
expect(decodeStyle('x|y|-3||')).toEqual({ font: 'y' })
expect(decodeStyle('x|y|12.5||')).toEqual({ font: 'y', size: 12.5 })
})
it('ignores non-hex colors and non-0/1 toggles', () => {
expect(decodeStyle('#d32f2f|arial|12|1|0')).toEqual({
color: '#d32f2f',
font: 'arial',
size: 12,
bold: true,
italic: false,
})
expect(decodeStyle('red|arial|12||')).toEqual({ font: 'arial', size: 12 })
expect(decodeStyle('x|a|b|2|x')).toEqual({ font: 'a' })
expect(decodeStyle('#d32f2f|a|b|||extra|more')).toEqual({ color: '#d32f2f', font: 'a' })
})
})