From 32f3e0092ffbe78f4dd7886dd1f225b114fb1a7b Mon Sep 17 00:00:00 2001 From: Josh Junon Date: Tue, 30 Nov 2021 20:11:41 +0100 Subject: [PATCH] fix incorrect handling of optional comma in rgb() regex (fixes #65) --- index.js | 2 +- test/basic.js | 10 ++++++++++ 2 files changed, 11 insertions(+), 1 deletion(-) diff --git a/index.js b/index.js index 83ce40e..a01f14f 100644 --- a/index.js +++ b/index.js @@ -50,7 +50,7 @@ cs.get.rgb = function (string) { var abbr = /^#([a-f0-9]{3,4})$/i; var hex = /^#([a-f0-9]{6})([a-f0-9]{2})?$/i; - var rgba = /^rgba?\(\s*([+-]?\d+)\s*,?\s*([+-]?\d+)\s*,?\s*([+-]?\d+)\s*(?:[,|\/]\s*([+-]?[\d\.]+)(%?)\s*)?\)$/; + var rgba = /^rgba?\(\s*([+-]?\d+)(?=[\s,])\s*(?:,\s*)?([+-]?\d+)(?=[\s,])\s*(?:,\s*)?([+-]?\d+)\s*(?:[,|\/]\s*([+-]?[\d\.]+)(%?)\s*)?\)$/; var per = /^rgba?\(\s*([+-]?[\d\.]+)\%\s*,?\s*([+-]?[\d\.]+)\%\s*,?\s*([+-]?[\d\.]+)\%\s*(?:[,|\/]\s*([+-]?[\d\.]+)(%?)\s*)?\)$/; var keyword = /^(\w+)$/; diff --git a/test/basic.js b/test/basic.js index 69ca95c..380c430 100644 --- a/test/basic.js +++ b/test/basic.js @@ -53,6 +53,16 @@ assert.strictEqual(string.get('#f'), null); assert.strictEqual(string.get('#4f'), null); assert.strictEqual(string.get('#45ab4'), null); assert.strictEqual(string.get('#45ab45e'), null); +assert.strictEqual(string.get('rgb()'), null); +assert.strictEqual(string.get('rgb(10)'), null); +assert.strictEqual(string.get('rgb(10, 2)'), null); +assert.strictEqual(string.get('rgb(10, 2, 2348723dskjfs)'), null); +assert.strictEqual(string.get('rgb(10%)'), null); +assert.strictEqual(string.get('rgb(10%, 2%)'), null); +assert.strictEqual(string.get('rgb(10%, 2%, 2348723%dskjfs)'), null); +assert.strictEqual(string.get('rgb(10%, 2%, 2348723dskjfs%)'), null); +assert.strictEqual(string.get('rgb(10$,3)'), null); +assert.strictEqual(string.get('rgba(10, 3)'), null); // with sign assert.deepEqual(string.get.rgb('rgb(-244, +233, -100)'), [0, 233, 0, 1]);