diff --git a/src/parse.js b/src/parse.js index 140c7a07..8c5cfa55 100644 --- a/src/parse.js +++ b/src/parse.js @@ -216,7 +216,7 @@ export function tokenize(str = '') { let alpha; if (is_num(chars)) { alpha = num(chars); - if (alpha.type !== Tok.Hue) { + if (alpha !== undefined && alpha.type !== Tok.Hue) { tokens.push({ type: Tok.Alpha, value: alpha }); continue; } @@ -251,7 +251,7 @@ export function tokenize(str = '') { return undefined; } - return tokens; + return tokens.includes(undefined) ? undefined : tokens; } export function parseColorSyntax(tokens) { diff --git a/test/parse.test.js b/test/parse.test.js index 6f1fd072..bd65c085 100644 --- a/test/parse.test.js +++ b/test/parse.test.js @@ -408,3 +408,17 @@ test('undefined', t => { test('Issue #204', t => { assert.equal(parse('oklch(70% 0..1 156)'), undefined); }); + +test('invalid dimension does not throw', t => { + const tests = [ + 'rgb(1px 2 3)', + 'hsl(90DEG 100% 50%)', + 'oklab(1x 2 3)', + 'lch(50% 40 90DEG)', + 'color(srgb 1 2px 3)', + 'rgb(1 2 3 / 1px)' + ]; + tests.forEach(t => { + assert.equal(parse(t), undefined, t); + }); +});