postcss-hexrgba
Advanced tools
Comparing version 0.1.1 to 0.1.2
60
index.js
@@ -6,5 +6,6 @@ 'use strict'; | ||
module.exports = postcss.plugin('postcss-hexrgba', function () { | ||
/** | ||
* Hex to RGB converter | ||
* @param {string} hex hexidecimal string | ||
* @param {string} hex hexidecimal string without # | ||
* @return {array} RGB values | ||
@@ -15,9 +16,9 @@ */ | ||
// If given shorthand, expand it | ||
var shorthandCheck = /^#?([a-f\d])([a-f\d])([a-f\d])$/i; | ||
var shorthandCheck = /^([a-f\d])([a-f\d])([a-f\d])$/i; | ||
hex = hex.replace(shorthandCheck, function(m, r, g, b) { | ||
return '#' + r + r + g + g + b + b; | ||
return r + r + g + g + b + b; | ||
}); | ||
// Extract full hex into an array | ||
var rgbRegex = /^#([a-f\d]{2})([a-f\d]{2})([a-f\d]{2})$/i; | ||
var rgbRegex = /^([a-f\d]{2})([a-f\d]{2})([a-f\d]{2})$/i; | ||
var rgb = hex | ||
@@ -42,41 +43,36 @@ .replace(/^\s+|\s+$/g, '') | ||
var input = decl.value; | ||
var output = ''; | ||
var input = decl.value, | ||
output = input, | ||
hexes = []; | ||
// Strip out surrounding css property and throw the values in an array | ||
var rgba = input.split('(')[1].split(')')[0]; | ||
rgba = rgba.split(','); | ||
// Get the raw hex values out of the decl value and put them in an array | ||
input.replace(/rgba\(\#(.*?)\,/g, function(a, b){ | ||
hexes.push(b); | ||
}); | ||
// Only process rgba values with 2 arguments (hex + alpha) | ||
if (rgba.length > 2){ | ||
// If there are no hexes in the value, exit | ||
if (!hexes.length) { | ||
return; | ||
} | ||
// Store the alpha value for safe keeping and strip trailing spaces | ||
var a = rgba[1]; | ||
a = a.replace(/\s/g, ''); | ||
// Convert each hex to RGB | ||
hexes.forEach(function(hex) { | ||
var rgb = hexRgb(hex); | ||
// Rip out the alpha and turn array into hex string | ||
var hex = rgba.splice(0, 1); | ||
hex = hex.toString(); | ||
// If conversion fails, warn and exit | ||
if (!rgb) { | ||
result.warn('not a valid hex', { node: decl }); | ||
return; | ||
} | ||
// Feed it to our converter | ||
var converted = hexRgb(hex); | ||
rgb = rgb.toString(); | ||
// Add the alpha back in | ||
if (!converted) { | ||
result.warn('not a valid hex', { node: decl }); | ||
return; | ||
} | ||
// Replace hex values in output string | ||
var matchHex = new RegExp('#' + hex); | ||
output = output.replace(matchHex, rgb); | ||
converted.push(a); | ||
}); | ||
decl.replaceWith({prop: decl.prop, value: output }); | ||
// Convert the whole thing to a string again and add back in css wrapper | ||
output = converted.toString(); | ||
output = 'rgba(' + output + ')'; | ||
// Create the new declaration value | ||
decl.value = output; | ||
}; | ||
@@ -83,0 +79,0 @@ |
{ | ||
"name": "postcss-hexrgba", | ||
"version": "0.1.1", | ||
"version": "0.1.2", | ||
"description": "PostCSS plugin that adds shorthand hex methods to rgba() values", | ||
@@ -5,0 +5,0 @@ "keywords": [ |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
5929
68