Skip to content

Commit acda415

Browse files
committed
Fix search color function calling in decl value
1 parent 2b79521 commit acda415

File tree

3 files changed

+17
-2
lines changed

3 files changed

+17
-2
lines changed

index.js

+9-2
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ var balanced = require("balanced-match")
66
var colorFn = require("css-color-function")
77
var helpers = require("postcss-message-helpers")
88

9+
var colorFnRegExp = /(^|[^\w\-])color\(/;
10+
911
/**
1012
* PostCSS plugin to transform color()
1113
*/
@@ -30,11 +32,16 @@ module.exports = postcss.plugin("postcss-color-function", function() {
3032
* @return {String} converted declaration value to rgba()
3133
*/
3234
function transformColor(string, source) {
33-
var index = string.indexOf("color(")
34-
if (index == -1) {
35+
var index = string.search(colorFnRegExp)
36+
37+
if (index === -1) {
3538
return string
3639
}
3740

41+
// NOTE: regexp search beginning of line of non char symbol between `color(`.
42+
// Offset used for second case.
43+
index = index === 0 ? index : index + 1
44+
3845
var fn = string.slice(index)
3946
var balancedMatches = balanced("(", ")", fn)
4047
if (!balancedMatches) {

test/fixtures/color.css

+4
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,8 @@ body {
33
background-color: color(red tint(50%));
44
border-color: color(hsla(125, 50%, 50%, .4) hue(200));
55
border-top-color: color(hwb(270, 10%, 0%) contrast(50%));
6+
7+
border-bottom-color: hover-color(red);
8+
9+
border-right-color: hover-color(color(red tint(50%)));
610
}

test/fixtures/color.expected.css

+4
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,8 @@ body {
33
background-color: rgb(255, 128, 128);
44
border-color: rgba(64, 149, 191, 0.4);
55
border-top-color: rgb(248, 240, 255);
6+
7+
border-bottom-color: hover-color(red);
8+
9+
border-right-color: hover-color(rgb(255, 128, 128));
610
}

0 commit comments

Comments
 (0)