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:
sadkodev 2026-07-17 15:30:43 -04:00
parent d1284cd01b
commit a5cffcc1ed
No known key found for this signature in database
GPG key ID: C0AAF1AFD0AE9C4E
2 changed files with 10 additions and 0 deletions

View file

@ -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

View file

@ -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)
})
})