Skip to content

Commit

Permalink
fix incorrect handling of optional comma in rgb() regex (fixes #65)
Browse files Browse the repository at this point in the history
  • Loading branch information
Qix- committed Nov 30, 2021
1 parent 0766ca7 commit 32f3e00
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 1 deletion.
2 changes: 1 addition & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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+)$/;

Expand Down
10 changes: 10 additions & 0 deletions test/basic.js
Original file line number Diff line number Diff line change
Expand Up @@ -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]);
Expand Down

0 comments on commit 32f3e00

Please sign in to comment.