From 20eb33a32bf51bfd134bbff1beec41e2cf2c24b3 Mon Sep 17 00:00:00 2001 From: "asamuzaK (Kazz)" Date: Sat, 16 Nov 2024 10:56:02 +0900 Subject: [PATCH] Add css-color --- lib/parsers.js | 10 ++++++++-- package-lock.json | 19 +++++++++++++++++++ package.json | 1 + test/parsers.js | 37 +++++++++++++++++++++++++++++++++++++ 4 files changed, 65 insertions(+), 2 deletions(-) diff --git a/lib/parsers.js b/lib/parsers.js index 887bd51..29d6a31 100644 --- a/lib/parsers.js +++ b/lib/parsers.js @@ -4,6 +4,7 @@ ********************************************************************/ 'use strict'; +const { resolve } = require('@asamuzakjp/css-color'); const namedColors = require('./named_colors.json'); const { hslToRgb } = require('./utils/colorSpace'); @@ -323,7 +324,7 @@ exports.parseColor = function parseColor(val) { return 'rgb(' + red + ', ' + green + ', ' + blue + ')'; } - res = colorRegEx2.exec(val); + res = val.includes(',') && colorRegEx2.exec(val); if (res) { parts = res[1].split(/\s*,\s*/); if (parts.length !== 3) { @@ -346,7 +347,7 @@ exports.parseColor = function parseColor(val) { return 'rgb(' + red + ', ' + green + ', ' + blue + ')'; } - res = colorRegEx3.exec(val); + res = val.includes(',') && colorRegEx3.exec(val); if (res) { parts = res[1].split(/\s*,\s*/); if (parts.length !== 4) { @@ -402,6 +403,11 @@ exports.parseColor = function parseColor(val) { if (type === exports.TYPES.COLOR) { return val; } + + res = resolve(val); + if (res) { + return res; + } return undefined; }; diff --git a/package-lock.json b/package-lock.json index f18d9a6..b870ddb 100644 --- a/package-lock.json +++ b/package-lock.json @@ -9,6 +9,7 @@ "version": "4.1.0", "license": "MIT", "dependencies": { + "@asamuzakjp/css-color": "^1.1.1", "rrweb-cssom": "^0.7.1" }, "devDependencies": { @@ -28,6 +29,15 @@ "node": ">=18" } }, + "node_modules/@asamuzakjp/css-color": { + "version": "1.1.1", + "resolved": "https://registry.npmjs.org/@asamuzakjp/css-color/-/css-color-1.1.1.tgz", + "integrity": "sha512-wfKjiBrlQC2bexTisWDyYrZToSGKV3AYZ44MTdYb2bJiQmt29VtW43MDSax8pAghfmDKuNg08tNXBGcy96b9jQ==", + "license": "MIT", + "dependencies": { + "lru-cache": "^11.0.2" + } + }, "node_modules/@babel/code-frame": { "version": "7.24.7", "resolved": "https://registry.npmjs.org/@babel/code-frame/-/code-frame-7.24.7.tgz", @@ -2000,6 +2010,15 @@ "dev": true, "license": "MIT" }, + "node_modules/lru-cache": { + "version": "11.0.2", + "resolved": "https://registry.npmjs.org/lru-cache/-/lru-cache-11.0.2.tgz", + "integrity": "sha512-123qHRfJBmo2jXDbo/a5YOQrJoHF/GNQTLzQ5+IdK5pWpceK17yRc6ozlWd25FxvGKQbIUs91fDFkXmDHTKcyA==", + "license": "ISC", + "engines": { + "node": "20 || >=22" + } + }, "node_modules/memorystream": { "version": "0.3.1", "resolved": "https://registry.npmjs.org/memorystream/-/memorystream-0.3.1.tgz", diff --git a/package.json b/package.json index 41617d7..e559278 100644 --- a/package.json +++ b/package.json @@ -37,6 +37,7 @@ ], "main": "./lib/CSSStyleDeclaration.js", "dependencies": { + "@asamuzakjp/css-color": "^1.1.1", "rrweb-cssom": "^0.7.1" }, "devDependencies": { diff --git a/test/parsers.js b/test/parsers.js index a0b3ff9..d0f732c 100644 --- a/test/parsers.js +++ b/test/parsers.js @@ -114,15 +114,52 @@ describe('parseColor', () => { it('should convert not zero hsl with non zero hue 120 to rgb(0, 255, 0)', () => { let input = 'hsl(120, 100%, 50%)'; let output = parsers.parseColor(input); + assert.strictEqual(output, 'rgb(0, 255, 0)'); }); it('should convert not zero hsl with non zero hue 240 to rgb(0, 0, 255)', () => { let input = 'hsl(240, 100%, 50%)'; let output = parsers.parseColor(input); + assert.strictEqual(output, 'rgb(0, 0, 255)'); }); + it('should convert modern rgb to rgb values', () => { + let input = 'rgb(128 0 128 / 1)'; + let output = parsers.parseColor(input); + + assert.strictEqual(output, 'rgb(128, 0, 128)'); + }); + + it('should convert modern rgba to rgba values', () => { + let input = 'rgba(128 0 128 / 0.5)'; + let output = parsers.parseColor(input); + + assert.strictEqual(output, 'rgba(128, 0, 128, 0.5)'); + }); + + it('should convert lab to rgb values', () => { + let input = 'lab(46.2775% -47.5621 48.5837)'; // green + let output = parsers.parseColor(input); + + assert.strictEqual(output, 'rgb(0, 128, 0)'); + }); + + it('should convert color function to rgb values', () => { + let input = 'color(srgb 0 0.5 0)'; + let output = parsers.parseColor(input); + + assert.strictEqual(output, 'rgb(0, 128, 0)'); + }); + + it('should convert color-mix to rgb values', () => { + let input = 'color-mix(in srgb, red, blue)'; + let output = parsers.parseColor(input); + + assert.strictEqual(output, 'rgb(128, 0, 128)'); + }); + it.todo('Add more tests'); }); describe('parseAngle', () => {