From 02490742cb5c5702fdfd278300ea2f2d5290a694 Mon Sep 17 00:00:00 2001 From: Ivan Vlasenko Date: Fri, 18 Dec 2015 15:29:06 +0300 Subject: [PATCH] Add case with rgba(rgb(), a) --- index.js | 51 +++++++++++++++++++++++++++++++++++++++++++++++++-- test/test.js | 7 +++++++ 2 files changed, 56 insertions(+), 2 deletions(-) diff --git a/index.js b/index.js index b46b928..19e35c2 100644 --- a/index.js +++ b/index.js @@ -9,6 +9,8 @@ var BW_RE_2 = /^(black|white)\((0?\.?\d+)?\)/i; var BRACKETS_RE = /\((..*)\)/; var RGBA_RE = /rgba\(#([0-9a-f]{3}|[0-9a-f]{6}),\ ?(0?\.?\d+)\)/i; var RGBA_RE_2 = /^rgba\(#([0-9a-f]{3}|[0-9a-f]{6}),\ ?(0?\.?\d+)\)/i; +var RGBA_RGB_RE = /rgba\((rgb\([0-9]{1,3},\ ?[0-9]{1,3},\ ?[0-9]{1,3}\)),\ ?(0?\.?\d+)\)/i; +var RGBA_RGB_RE_2 = /^rgba\((rgb\([0-9]{1,3},\ ?[0-9]{1,3},\ ?[0-9]{1,3}\)),\ ?(0?\.?\d+)\)/i; module.exports = postcss.plugin('postcss-color-alpha', function (opts) { return function (css, result) { @@ -17,7 +19,8 @@ module.exports = postcss.plugin('postcss-color-alpha', function (opts) { if ( !decl.value || !( decl.value.match(HEX_A_RE) || decl.value.match(BW_RE) || - decl.value.match(RGBA_RE) + decl.value.match(RGBA_RE) || + decl.value.match(RGBA_RGB_RE) ) ) { return; @@ -31,6 +34,10 @@ module.exports = postcss.plugin('postcss-color-alpha', function (opts) { return transformHexAlpha(decl.value, decl.source); }, decl.source); + decl.value = messageHelpers.try(function () { + return transformRgbRgbAlpha(decl.value, decl.source); + }, decl.source); + decl.value = messageHelpers.try(function () { return transformRgbAlpha(decl.value, decl.source); }, decl.source); @@ -38,6 +45,38 @@ module.exports = postcss.plugin('postcss-color-alpha', function (opts) { }; }); +var transformRgbRgbAlpha = function(string) { + if (!RGBA_RGB_RE.test(string)) + return string; + + var convertedCommaParts = []; + var commaParts = list.comma(string); + + for (var i = 0; i < commaParts.length; i++) { + var convertedParts = []; + var parts = list.space(commaParts[i]); + for (var j = 0; j < parts.length; j++) { + var part = parts[j]; + + var matches = RGBA_RGB_RE_2.exec(part); + if ( !matches ) { + convertedParts.push(checkInnerBrackets(part)); + continue; + } + + convertedParts.push( + part.replace( + RGBA_RGB_RE, + color(matches[1]).alpha(matches[2]).rgbaString() + ) + ); + + } + convertedCommaParts.push(convertedParts.join(' ').trim()); + } + return convertedCommaParts.join(', '); +}; + var transformRgbAlpha = function(string) { if (!RGBA_RE.test(string)) return string; @@ -148,7 +187,15 @@ function checkInnerBrackets(string) { var parts = list.comma(matches[1]); for (var i = 0; i < parts.length; i++) { var part = parts[i]; - convertedParts.push(transformRgbAlpha(transformHexAlpha(transformBlackWhiteAlpha(part)))); + convertedParts.push( + transformRgbRgbAlpha( + transformRgbAlpha( + transformHexAlpha( + transformBlackWhiteAlpha(part) + ) + ) + ) + ); } return string.substr(0, matches.index) + '(' + convertedParts.join(', ') + ')'; } diff --git a/test/test.js b/test/test.js index 890ddd3..51e319f 100644 --- a/test/test.js +++ b/test/test.js @@ -73,4 +73,11 @@ describe('postcss-color-alpha', function () { 'background-image: linear-gradient(to bottom, rgba(255, 255, 255, 0.6) 0%, rgba(255, 255, 255, 0), 100%);' ); }); + + it('converts rgba(rgb(), a)', function() { + test( + 'a { color: rgba(rgb(204, 0, 255), 0.4); }', + 'a { color: rgba(204, 0, 255, 0.4); }' + ); + }); });