Skip to content

Commit b593011

Browse files
ar53njonathantneal
authored andcommitted
Add preserveCustomProps option to preserve custom properties
1 parent 13b5c3f commit b593011

File tree

6 files changed

+68
-8
lines changed

6 files changed

+68
-8
lines changed

README.md

+4-3
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,10 @@
66

77
**⚠️ `color-mod()` has been removed from [Color Module Level 4 specification](https://www.w3.org/TR/css-color-4/#changes-from-20160705).**
88

9-
109
## Installation
1110

1211
```console
13-
$ npm install postcss-color-function
12+
npm install postcss-color-function
1413
```
1514

1615
## Usage
@@ -25,8 +24,10 @@ var colorFunction = require("postcss-color-function")
2524
var css = fs.readFileSync("input.css", "utf8")
2625

2726
// process css
27+
// set preserveCustomProps to `false` by default `true`
28+
//for delete declarations with custom properties
2829
var output = postcss()
29-
.use(colorFunction())
30+
.use(colorFunction({preserveCustomProps: true}))
3031
.process(css)
3132
.css
3233
```

index.js

+12-2
Original file line numberDiff line numberDiff line change
@@ -6,17 +6,27 @@ var parser = require("postcss-value-parser")
66
var colorFn = require("css-color-function")
77
var helpers = require("postcss-message-helpers")
88

9+
var defaultOptions = {
10+
preserveCustomProps: true,
11+
}
12+
913
/**
1014
* PostCSS plugin to transform color()
1115
*/
12-
module.exports = postcss.plugin("postcss-color-function", function() {
16+
module.exports = postcss.plugin("postcss-color-function", function(options) {
17+
options = Object.assign({}, defaultOptions, options)
18+
1319
return function(style, result) {
1420
style.walkDecls(function transformDecl(decl) {
1521
if (!decl.value || decl.value.indexOf("color(") === -1) {
1622
return
1723
}
1824

1925
if (decl.value.indexOf("var(") !== -1) {
26+
if (!options.preserveCustomProps) {
27+
decl.remove()
28+
return
29+
}
2030
result.messages.push({
2131
plugin: "postcss-color-function",
2232
type: "skipped-color-function-with-custom-property",
@@ -27,7 +37,7 @@ module.exports = postcss.plugin("postcss-color-function", function() {
2737
"`",
2838
})
2939
return
30-
}
40+
}
3141

3242
try {
3343
decl.value = helpers.try(function transformColorValue() {

package.json

+3-2
Original file line numberDiff line numberDiff line change
@@ -24,12 +24,13 @@
2424
},
2525
"devDependencies": {
2626
"eslint": "^3.19.0",
27+
"faucet": "0.0.1",
2728
"npmpub": "^3.1.0",
28-
"tape": "^3.0.0"
29+
"tape": "^4.10.1"
2930
},
3031
"scripts": {
3132
"lint": "eslint *.js index.js ./test/",
32-
"test": "npm run lint && tape test",
33+
"test": "npm run lint && tape test | faucet",
3334
"release": "npmpub"
3435
}
3536
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
body {
2+
color: color(var(--red));
3+
color: color(var(--red) tint(50%));
4+
background-color: color(red tint(50%));
5+
color: color(var(--red) tint(var(--tintPercent)));
6+
color: color(rgb(255, 0, 0) tint(var(--tintPercent)));
7+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
body {
2+
background-color: rgb(255, 128, 128);
3+
}

test/index.js

+39-1
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ function compareFixtures(t, name, msg, opts, postcssOpts) {
2020
var actual = postcss().use(plugin(opts))
2121
.process(read(postcssOpts.from), postcssOpts).css
2222
var expected = read(filename("fixtures/" + name + ".expected"))
23-
fs.writeFile(filename("fixtures/" + name + ".actual"), actual)
23+
fs.writeFileSync(filename("fixtures/" + name + ".actual"), actual)
2424
t.equal(actual, expected, msg)
2525
}
2626

@@ -106,3 +106,41 @@ test("logs message when color() contains var() custom property", function(t) {
106106
t.end()
107107
})
108108
})
109+
110+
test(
111+
"test delete custom properties with option preserveCustomProps `false`",
112+
function(t) {
113+
postcss(plugin({preserveCustomProps : false})).process(
114+
read(filename("fixtures/delete-custom-properties"))
115+
).then(function(result) {
116+
var expectedDeclaration = [{
117+
prop: "background-color",
118+
value: "rgb(255, 128, 128)",
119+
}]
120+
// check left rules in body after clear
121+
var declNodes = result.root.nodes[0].nodes
122+
t.equal(
123+
declNodes.length,
124+
expectedDeclaration.length,
125+
"expected " + expectedDeclaration.length +
126+
" declaration length but got " + declNodes.length
127+
)
128+
129+
t.equal(
130+
declNodes[0].prop,
131+
expectedDeclaration[0].prop,
132+
"expected declaration with "+ expectedDeclaration[0].prop +
133+
" property but got " + declNodes[0].prop
134+
)
135+
136+
t.equal(
137+
declNodes[0].value,
138+
expectedDeclaration[0].value,
139+
"expected declaration with "+ expectedDeclaration[0].value +
140+
" value but got " + declNodes[0].value
141+
)
142+
143+
t.end()
144+
})
145+
}
146+
)

0 commit comments

Comments
 (0)