Skip to content

Commit 215083e

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

File tree

3 files changed

+18
-3
lines changed

3 files changed

+18
-3
lines changed

index.js

+10-3
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,15 @@ 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
*/
1214
module.exports = postcss.plugin("postcss-color-function", function() {
1315
return function(style) {
1416
style.eachDecl(function transformDecl(decl) {
15-
if (!decl.value || decl.value.indexOf("color(") === -1) {
17+
if (!decl.value || !colorFnRegExp.test(decl.value)) {
1618
return
1719
}
1820

@@ -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)