Skip to content

Commit

Permalink
Upgrade to PostCSS 5.x, fix specs
Browse files Browse the repository at this point in the history
  • Loading branch information
Jed Mao committed Sep 7, 2015
1 parent 1ec7ea8 commit c005383
Show file tree
Hide file tree
Showing 5 changed files with 60 additions and 45 deletions.
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ git:
language: node_js
node_js:
- '0.10'
- '0.11'
- '0.12'
notifications:
email: false
after_script:
Expand Down
2 changes: 1 addition & 1 deletion appveyor.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ version: '{build}'
environment:
matrix:
- nodejs_version: '0.10'
- nodejs_version: '0.11'
- nodejs_version: '0.12'

install:
- ps: Install-Product node $env:nodejs_version
Expand Down
75 changes: 40 additions & 35 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,50 +6,55 @@
'use strict';

var color = require('color');
var postcss = require('postcss');
var helpers = require('postcss-message-helpers');
var reduceFunctionCall = require('reduce-function-call');

function parseGray(value) {
return reduceFunctionCall(value, 'gray', function(argString) {
var args = argString.split(',');
var pluginName = 'postcss-color-gray';
var errorContext = {plugin: pluginName};

var rgb = args[0] + ',' + args[0] + ',' + args[0];
var alpha = args[1];
if (alpha) {
alpha = alpha.trim();
var match = alpha.match(/^[0-9](\d|\.)+?%$/);
if (match && match[0] === alpha) {
alpha = parseFloat(alpha) * 0.01;
}
function parseAlpha(alpha) {
if (alpha) {
var match = alpha.match(/^\d(\d|\.)+?%$/);
if (match && match[0] === alpha) {
return parseFloat(alpha) * 0.01;
}
}
return alpha;
}

var parsedColor;

function parseGray(decl) {
return reduceFunctionCall(decl.value, 'gray', function(body) {
if (/^,/.test(body) || /,$/.test(body)) {
throw decl.error(
'Unable to parse color from string "gray(' + body + ')"',
errorContext
);
}
var fn = 'rgb';
var args = postcss.list.comma(body);
var lightness = args[0];
var rgb = [lightness, lightness, lightness];
var alpha = parseAlpha(args[1]);
if (alpha) {
fn += 'a';
rgb.push(alpha);
}
try {
if (alpha === undefined) {
parsedColor = color('rgb' + '(' + rgb + ')');
} else {
parsedColor = color('rgba' + '(' + rgb + ',' + alpha + ')');
}
return parsedColor.rgbString();

} catch (e) {
e.message = e.message.replace(/rgba?\(.*\)/, 'gray(' + args + ')');
throw e;
return color(fn + '(' + rgb + ')').rgbString();
} catch (err) {
var message = err.message.replace(/rgba?\(.*\)/, 'gray(' + args + ')');
throw decl.error(message, errorContext);
}
});
}

function transformDecl(decl) {
if (decl.value && decl.value.indexOf('gray(') !== -1) {
decl.value = helpers.try(function transformGrayValue() {
return parseGray(decl.value);
}, decl.source);
}
}

module.exports = function pluginColorGray() {
return function(style) {
style.eachDecl(transformDecl);
module.exports = postcss.plugin(pluginName, function() {
return function(root) {
root.walkDecls(function(decl) {
if (decl.value && decl.value.indexOf('gray(') !== -1) {
decl.value = helpers.try(parseGray.bind(this, decl), decl.source);
}
});
};
};
});
6 changes: 4 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,9 @@
"url": "https://github.com/shinnn"
},
"scripts": {
"pretest": "eslint *.js && jscs *.js",
"pretest": "npm run eslint && npm run jscs",
"eslint": "eslint index.js test.js",
"jscs": "jscs index.js test.js",
"test": "node test.js | tap-spec",
"coverage": "istanbul cover test.js",
"coveralls": "${npm_package_scripts_coverage} && istanbul-coveralls"
Expand Down Expand Up @@ -39,6 +41,7 @@
],
"dependencies": {
"color": "^0.7.3",
"postcss": "^5.0.4",
"postcss-message-helpers": "^2.0.0",
"reduce-function-call": "^1.0.1"
},
Expand All @@ -47,7 +50,6 @@
"istanbul": "^0.3.5",
"istanbul-coveralls": "^1.0.1",
"jscs": "^1.8.1",
"postcss": "^4.0.2",
"tap-spec": "^2.1.0",
"tape": "^3.0.3"
}
Expand Down
20 changes: 14 additions & 6 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ function useGray() {
}

test('filterDeclarations()', function(t) {
t.plan(7);
t.plan(8);

t.equal(
useGray().process('a {color: gray(200); background: gray(00000034%)}').css,
Expand All @@ -31,31 +31,39 @@ test('filterDeclarations()', function(t) {

t.throws(
function() {
useGray().process('a {color: gray()}');
return useGray().process('a {color: gray()}').css;
},
/Unable to parse color from string "gray\(\)"/,
'should throw an error when gray() doesn\'t take any arguments.'
);

t.throws(
function() {
useGray().process('a {color: gray(,foo)}');
return useGray().process('a {color: gray(,foo)}').css;
},
/<css input>:1:4: Unable to parse color from string "gray\(,foo\)"/,
'should throw an error when gray() takes invalid argument.'
'should throw an error when gray() args start with a comma.'
);

t.throws(
function() {
useGray().process('a {color: gray(red)}', {from: 'fixture.css'});
return useGray().process('a {color: gray(foo,)}').css;
},
/<css input>:1:4: Unable to parse color from string "gray\(foo,\)"/,
'should throw an error when gray() args end with a comma.'
);

t.throws(
function() {
return useGray().process('a {color: gray(red)}', {from: 'fixture.css'}).css;
},
/fixture\.css:1:4: Unable to parse color from string "gray\(red\)"/,
'should throw a detailed error when a source file is specified.'
);

t.throws(
function() {
useGray().process('a {color: gray(,)}', {map: true});
return useGray().process('a {color: gray(,)}', {map: true}).css;
},
/<css input>:1:4: Unable to parse color from string "gray\(,\)"/,
'should throw a detailed error when source map is enabled but file isn\'t specified.'
Expand Down

0 comments on commit c005383

Please sign in to comment.