From ee70b5d971df8c8379ad4c464d90fe057116ca62 Mon Sep 17 00:00:00 2001 From: Yarchik Date: Wed, 8 Jul 2026 14:08:36 +0100 Subject: [PATCH] fix(parse): return undefined instead of throwing on invalid dimension A number followed by an identifier that is not an angle unit made the tokenizer emit an undefined token, which crashed consumeCoords with a TypeError. parse() now returns undefined for such input, as it does for other unparseable strings. --- src/parse.js | 4 ++-- test/parse.test.js | 14 ++++++++++++++ 2 files changed, 16 insertions(+), 2 deletions(-) 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); + }); +});