Skip to content

Commit c232861

Browse files
drewbourneMoOx
authored andcommitted
Changed: Add postcss message when color function contains a var() (postcss#36)
Custom properties cannot be resolved by `css-color-function`. This change skips color functions that contain `var(` and adds a message to the postcss `result.messages` with type 'skipped-color-function-with-custom-property'.
1 parent 453acee commit c232861

File tree

4 files changed

+71
-0
lines changed

4 files changed

+71
-0
lines changed

index.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,19 @@ module.exports = postcss.plugin("postcss-color-function", function() {
1616
return
1717
}
1818

19+
if (decl.value.indexOf("var(") !== -1) {
20+
result.messages.push({
21+
plugin: "postcss-color-function",
22+
type: "skipped-color-function-with-custom-property",
23+
word: decl.value,
24+
message:
25+
"Skipped color function with custom property `" +
26+
decl.value +
27+
"`"
28+
})
29+
return
30+
}
31+
1932
try {
2033
decl.value = helpers.try(function transformColorValue() {
2134
return transformColor(decl.value)
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
body {
2+
color: color(var(--red));
3+
color: color(var(--red) tint(50%));
4+
color: color(var(--red) tint(var(--tintPercent)));
5+
color: color(rgb(255, 0, 0) tint(var(--tintPercent)));
6+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
body {
2+
color: color(var(--red));
3+
color: color(var(--red) tint(50%));
4+
color: color(var(--red) tint(var(--tintPercent)));
5+
color: color(rgb(255, 0, 0) tint(var(--tintPercent)));
6+
}

test/index.js

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,3 +51,49 @@ test("logs warning when color() value cannot be parsed", function(t) {
5151
t.end()
5252
})
5353
})
54+
55+
test("logs message when color() contains var() custom property", function(t) {
56+
postcss(plugin()).process(read(filename("fixtures/color-with-custom-properties")))
57+
.then(function(result) {
58+
const expectedWords = [
59+
"color(var(--red))",
60+
"color(var(--red) tint(50%))",
61+
"color(var(--red) tint(var(--tintPercent)))",
62+
"color(rgb(255, 0, 0) tint(var(--tintPercent)))"
63+
]
64+
65+
t.equal(
66+
result.messages.length,
67+
expectedWords.length,
68+
"expected a message every time a color function is skipped"
69+
)
70+
71+
result.messages.forEach(function(message, i) {
72+
t.equal(
73+
message.type,
74+
"skipped-color-function-with-custom-property",
75+
"expected `message.type` to indicate reason for message"
76+
)
77+
78+
t.equal(
79+
message.plugin,
80+
"postcss-color-function",
81+
"expected `message.plugin` to match this plugin's name"
82+
)
83+
84+
t.equal(
85+
message.word,
86+
expectedWords[i],
87+
"expected `message.word` to contain declaration value"
88+
)
89+
90+
t.equal(
91+
message.message,
92+
"Skipped color function with custom property `" + expectedWords[i] + "`",
93+
"expected `message.message` to contain reason for message"
94+
)
95+
})
96+
97+
t.end()
98+
})
99+
})

0 commit comments

Comments
 (0)