From 3ed3f1cce853462169032a90357dffa216eb627d Mon Sep 17 00:00:00 2001 From: lynn Date: Tue, 29 Jul 2025 23:47:03 +0800 Subject: [PATCH] fix: zero value should make sense --- src/utils.ts | 4 ++-- test/radial.test.ts | 50 +++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 52 insertions(+), 2 deletions(-) diff --git a/src/utils.ts b/src/utils.ts index a2548b2..5cc425a 100644 --- a/src/utils.ts +++ b/src/utils.ts @@ -52,7 +52,7 @@ export function resolveStops(v: string[]): ColorStop[] { return stops } -const REGEX = /^(-?\d+\.?\d*)(%|vw|vh|px|em|rem|deg|rad|grad|turn|ch|vmin|vmax)$/ +const REGEX = /^(-?\d+\.?\d*)(%|vw|vh|px|em|rem|deg|rad|grad|turn|ch|vmin|vmax)?$/ function isHint(v: string) { return REGEX.test(v) @@ -63,5 +63,5 @@ export function resolveLength(v?: string) { const [, value, unit] = v.trim().match(REGEX) || [] - return { value, unit } + return { value, unit: unit ?? 'px' } } \ No newline at end of file diff --git a/test/radial.test.ts b/test/radial.test.ts index 96ca17c..fca8b89 100644 --- a/test/radial.test.ts +++ b/test/radial.test.ts @@ -180,6 +180,56 @@ describe('radial', () => { { color: 'red', offset: undefined } ] }) + }) + + it('should parse pure number', () => { + const g = parseRadialGradient('radial-gradient(circle at 0 50%, blue, red)') + expect(g).toEqual({ + shape: 'circle', + repeating: false, + position: { + x: { type: 'length', value: { + value: "0", unit: 'px' + }}, + y: { type: 'length', value: { + value: "50", unit: '%' + } }, + }, + size: [{ type: 'keyword', value: 'farthest-corner'}], + stops: [ + { color: 'blue', offset: undefined }, + { color: 'red', offset: undefined } + ] + }) + }) + + it('should parse repeating-radial-gradient(black,black 30px,white 30px,white 60px)', () => { + const g = parseRadialGradient('repeating-radial-gradient(black,black 30px,white 30px,white 60px)') + + expect(g).toEqual({ + shape: 'ellipse', + repeating: true, + position: { + x: { type: 'keyword', value: 'center' }, + y: { type: 'keyword', value: 'center' }, + }, + size: [{ type: 'keyword', value: 'farthest-corner'}], + stops: [ + { color: 'black', offset: undefined }, + { color: 'black', offset: { + unit: 'px', + value: '30' + } }, + { color: 'white', offset: { + unit: 'px', + value: '30' + } }, + { color: 'white', offset: { + unit: 'px', + value: '60' + } } + ] + }) }) }) \ No newline at end of file