Skip to content

Commit 11ab2f4

Browse files
author
Marc Robichaud
committed
Retain spaces before color().
Replaced balanced-match with post-css-value-parser
1 parent 0852e8c commit 11ab2f4

File tree

4 files changed

+16
-21
lines changed

4 files changed

+16
-21
lines changed

index.js

+10-19
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
* Module dependencies.
33
*/
44
var postcss = require("postcss")
5-
var balanced = require("balanced-match")
5+
var parser = require("postcss-value-parser")
66
var colorFn = require("css-color-function")
77
var helpers = require("postcss-message-helpers")
88

@@ -17,7 +17,7 @@ module.exports = postcss.plugin("postcss-color-function", function() {
1717
}
1818

1919
decl.value = helpers.try(function transformColorValue() {
20-
return transformColor(decl.value, decl.source)
20+
return transformColor(decl.value)
2121
}, decl.source)
2222
})
2323
}
@@ -29,22 +29,13 @@ module.exports = postcss.plugin("postcss-color-function", function() {
2929
* @param {String} string declaration value
3030
* @return {String} converted declaration value to rgba()
3131
*/
32-
function transformColor(string, source) {
33-
var index = string.search(/(^|[^\w\-])color\(/)
32+
function transformColor(string) {
33+
return parser(string).walk(function(node) {
34+
if (node.type !== "function" || node.value !== "color") {
35+
return
36+
}
3437

35-
if (index === -1) {
36-
return string
37-
}
38-
39-
// NOTE: regexp search beginning of line of non char symbol before `color(`.
40-
// Offset used for second case.
41-
index = index === 0 ? index : index + 1
42-
43-
var fn = string.slice(index)
44-
var balancedMatches = balanced("(", ")", fn)
45-
if (!balancedMatches) {
46-
throw new Error("Missing closing parentheses in '" + string + "'", source)
47-
}
48-
49-
return string.slice(0, index) + colorFn.convert("color(" + balancedMatches.body + ")") + transformColor(balancedMatches.post)
38+
node.value = colorFn.convert(parser.stringify(node))
39+
node.type = "word"
40+
}).toString()
5041
}

package.json

+2-2
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,10 @@
2020
"index.js"
2121
],
2222
"dependencies": {
23-
"balanced-match": "^0.1.0",
2423
"css-color-function": "^1.2.0",
2524
"postcss": "^5.0.4",
26-
"postcss-message-helpers": "^2.0.0"
25+
"postcss-message-helpers": "^2.0.0",
26+
"postcss-value-parser": "^3.3.0"
2727
},
2828
"devDependencies": {
2929
"jscs": "^1.6.2",

test/fixtures/color.css

+2
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,6 @@ body {
77
border-bottom-color: hover-color(red);
88

99
border-right-color: hover-color(color(red tint(50%)));
10+
11+
border-color: color(#999 a(0.8)) color(#999 a(0.8));
1012
}

test/fixtures/color.expected.css

+2
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,6 @@ body {
77
border-bottom-color: hover-color(red);
88

99
border-right-color: hover-color(rgb(255, 128, 128));
10+
11+
border-color: rgba(153, 153, 153, 0.8) rgba(153, 153, 153, 0.8);
1012
}

0 commit comments

Comments
 (0)