fix(editor): reject partially numeric opacity buffers
Add /^\d+$/ guard to opacityFromBuffer so strings like '12x', '5abc', or '3.5' return 100% instead of silently parsing the numeric prefix. Add unit tests for partial, decimal, scientific, negative, and whitespace-prefixed buffers.
This commit is contained in:
parent
d1284cd01b
commit
a5cffcc1ed
|
|
@ -10,6 +10,7 @@ import { createVariableBindingActions } from './variable-bindings'
|
|||
|
||||
export function opacityFromBuffer(buffer: string): number {
|
||||
if (buffer === '0') return 1
|
||||
if (!/^\d+$/.test(buffer)) return 1
|
||||
const n = Number.parseInt(buffer, 10)
|
||||
if (!Number.isFinite(n)) return 1
|
||||
const percent = buffer.length === 1 ? n * 10 : n
|
||||
|
|
|
|||
|
|
@ -150,4 +150,13 @@ describe('opacityFromBuffer', () => {
|
|||
expect(opacityFromBuffer('')).toBe(1)
|
||||
expect(opacityFromBuffer('abc')).toBe(1)
|
||||
})
|
||||
|
||||
test('rejects partially numeric buffers', () => {
|
||||
expect(opacityFromBuffer('12x')).toBe(1)
|
||||
expect(opacityFromBuffer('5abc')).toBe(1)
|
||||
expect(opacityFromBuffer('3.5')).toBe(1)
|
||||
expect(opacityFromBuffer('1e2')).toBe(1)
|
||||
expect(opacityFromBuffer('-5')).toBe(1)
|
||||
expect(opacityFromBuffer(' 5')).toBe(1)
|
||||
})
|
||||
})
|
||||
|
|
|
|||
Loading…
Reference in a new issue