Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add declaration with multiple attributes including rgba to be converted correctly #2

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 12 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ module.exports = postcss.plugin('postcss-hexrgba', function () {
* @return {array} RGB values
*/
var hexRgb = function(hex){

// If given shorthand, expand it
var shorthandCheck = /^#?([a-f\d])([a-f\d])([a-f\d])$/i;
hex = hex.replace(shorthandCheck, function(m, r, g, b) {
Expand Down Expand Up @@ -39,6 +38,8 @@ module.exports = postcss.plugin('postcss-hexrgba', function () {

var input = decl.value;
var output = '';
var prefix = input.match(/(.+) rgba\(.+\)/);
var suffix = input.match(/rgba\(.+\) (.+)/);

// Strip out surrounding css property and throw the values in an array
var rgba = input.split('(')[1].split(')')[0];
Expand Down Expand Up @@ -73,6 +74,16 @@ module.exports = postcss.plugin('postcss-hexrgba', function () {
output = converted.toString();
output = 'rgba(' + output + ')';

// Prepend the output with the beginning of the value before the conversion without the rgba part, if it exists
if (prefix) {
output = prefix[1] + ' ' + output;
}

// PAppend the output with the end of the value before the conversion without the rgba part, if it exists
if (suffix) {
output = output + ' ' + suffix[1];
}

// Create the new declaration value
decl.value = output;

Expand Down
5 changes: 5 additions & 0 deletions test/fixtures/multiple-attributes.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
.foo {
border: 1px solid rgba(#f00, 0.2);
border: rgba(#f00, 0.2) 1px solid;
border: 1px rgba(#f00, 0.2) solid;
}
5 changes: 5 additions & 0 deletions test/fixtures/multiple-attributes.expected.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
.foo {
border: 1px solid rgba(255,0,0,0.2);
border: rgba(255,0,0,0.2) 1px solid;
border: 1px rgba(255,0,0,0.2) solid;
}
4 changes: 4 additions & 0 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,4 +36,8 @@ describe('postcss-hexrgba', function() {
test('shorthand', {}, done);
});

it('handles hex in multiple attributes', function(done) {
test('multiple-attributes', {}, done);
});

});