Skip to content

Commit e6e08f9

Browse files
committed
Enhanced exceptions
1 parent 24670fb commit e6e08f9

File tree

4 files changed

+16
-38
lines changed

4 files changed

+16
-38
lines changed

CHANGELOG.md

+4
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
# 1.1.0 - 2014-11-25
2+
3+
- Enhanced exceptions
4+
15
# 1.0.0 - 2014-10-04
26

37
Initial release from [postcss-color](https://github.com/postcss/postcss-color)

index.js

+7-36
Original file line numberDiff line numberDiff line change
@@ -3,43 +3,25 @@
33
*/
44
var balanced = require("balanced-match")
55
var colorFn = require("css-color-function")
6+
var helpers = require("postcss-message-helpers")
67

78
/**
89
* PostCSS plugin to transform color()
910
*/
1011
module.exports = function plugin() {
1112
return function(style) {
12-
style.eachDecl(function transformDecl(dec) {
13-
if (!dec.value) {
13+
style.eachDecl(function transformDecl(decl) {
14+
if (!decl.value || decl.value.indexOf("color(") === -1) {
1415
return
1516
}
1617

17-
dec.value = transform(dec.value, dec.source)
18+
decl.value = helpers.try(function transformColorValue() {
19+
return transformColor(decl.value, decl.source)
20+
}, decl.source)
1821
})
1922
}
2023
}
2124

22-
/**
23-
* Transform colors to rgb() or rgba() on a declaration value
24-
*
25-
* @param {String} string
26-
* @return {String}
27-
*/
28-
function transform(string, source) {
29-
// order of transformation is important
30-
31-
try {
32-
if (string.indexOf("color(") > -1) {
33-
string = transformColor(string, source)
34-
}
35-
}
36-
catch (e) {
37-
throw new Error(gnuMessage(e.message, source))
38-
}
39-
40-
return string
41-
}
42-
4325
/**
4426
* Transform color() to rgb()
4527
*
@@ -55,19 +37,8 @@ function transformColor(string, source) {
5537
var fn = string.slice(index)
5638
var balancedMatches = balanced("(", ")", fn)
5739
if (!balancedMatches) {
58-
throw new SyntaxError(gnuMessage("Missing closing parentheses in '" + string + "'", source))
40+
throw new Error("Missing closing parentheses in '" + string + "'", source)
5941
}
6042

6143
return string.slice(0, index) + colorFn.convert("color(" + balancedMatches.body + ")") + transformColor(balancedMatches.post)
6244
}
63-
64-
65-
/**
66-
* return GNU style message
67-
*
68-
* @param {String} message
69-
* @param {Object} source
70-
*/
71-
function gnuMessage(message, source) {
72-
return (source ? (source.file ? source.file : "<css input>") + ":" + source.start.line + ":" + source.start.column + " " : "") + message
73-
}

package.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,8 @@
2323
],
2424
"dependencies": {
2525
"balanced-match": "^0.1.0",
26-
"css-color-function": "^1.1.1"
26+
"css-color-function": "^1.1.1",
27+
"postcss-message-helpers": "^1.1.0"
2728
},
2829
"devDependencies": {
2930
"jscs": "^1.6.2",

test/index.js

+3-1
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,9 @@ test("color()", function(t) {
2626
test("throw errors", function(t) {
2727
t.throws(function() {
2828
return postcss(plugin()).process(read(filename("fixtures/error"))).css
29-
}, /Unable to parse color from string/, "throws a readable error when a color can't be parsed")
29+
},
30+
/Unable to parse color from string/,
31+
"throws a readable error when a color can't be parsed")
3032

3133
t.end()
3234
})

0 commit comments

Comments
 (0)