Skip to content

Commit 6252f3b

Browse files
authored
Fix isColor() (#76)
1 parent 944503f commit 6252f3b

File tree

3 files changed

+30
-1
lines changed

3 files changed

+30
-1
lines changed

src/js/util.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@ const DEG_HALF = 180;
3131

3232
/* regexp */
3333
const REG_COLOR = new RegExp(`^(?:${SYN_COLOR_TYPE})$`);
34+
const REG_FN_COLOR =
35+
/^(?:(?:ok)?l(?:ab|ch)|color(?:-mix)?|hsla?|hwb|rgba?|var)\(/;
3436
const REG_MIX = new RegExp(SYN_MIX);
3537

3638
/**
@@ -175,7 +177,7 @@ export const isColor = (value: unknown, opt: Options = {}): boolean => {
175177
}
176178
} else if (REG_COLOR.test(value) || REG_MIX.test(value)) {
177179
return true;
178-
} else {
180+
} else if (REG_FN_COLOR.test(value)) {
179181
opt.nullable = true;
180182
if (!opt.format) {
181183
opt.format = VAL_SPEC;

test/css-gradient.test.ts

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -411,4 +411,21 @@ describe('is CSS gradient', () => {
411411
const res = func('linear-gradient(red, blue)');
412412
assert.strictEqual(res, true, 'result');
413413
});
414+
415+
it('should get true', () => {
416+
const res = func('radial-gradient(transparent, var(--custom-color))');
417+
assert.strictEqual(res, true, 'result');
418+
});
419+
420+
it('should get true', () => {
421+
const res = func('radial-gradient(transparent, var(--custom-color))');
422+
assert.strictEqual(res, true, 'result');
423+
});
424+
425+
it('should get true', () => {
426+
const res = func(
427+
'radial-gradient(transparent, /* comment */ var(--custom-color))'
428+
);
429+
assert.strictEqual(res, true, 'result');
430+
});
414431
});

test/util.test.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -190,6 +190,16 @@ describe('is color', () => {
190190
);
191191
assert.strictEqual(res, true, 'result');
192192
});
193+
194+
it('should get false', () => {
195+
const res = func('url(var(--foo))');
196+
assert.strictEqual(res, false, 'result');
197+
});
198+
199+
it('should get false', () => {
200+
const res = func('radial-gradient(transparent, var(--custom-color))');
201+
assert.strictEqual(res, false, 'result');
202+
});
193203
});
194204

195205
describe('value to JSON string', () => {

0 commit comments

Comments
 (0)