diff --git a/CHANGELOG.md b/CHANGELOG.md index a407228..d814ea6 100755 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,14 @@ +# 4.1.0 - 2019-04-01 + +- Added: `preserveCustomProps` option to preserve custom properties +- Updated: `postcss` to 6.0.23 (patch) +- Updated: `postcss-value-parser` to 3.3.1 (patch) + +# 4.0.1 - 2017-11-03 + +- Fixed: bug when using the `tint`, `shade`, and `contrast` adjusters along with the `alpha` adjuster +([#33](https://github.com/postcss/postcss-color-function/pull/33) - @tylergaw) + # 4.0.0 - 2017-05-15 - Added: compatibility with postcss v6.x @@ -8,7 +19,7 @@ - Changed: send postcss warning when color function cannot be parsed instead of throwing ([#35](https://github.com/postcss/postcss-color-function/pull/35) - @drewbourne) - Changed: send a postcss message when color function contains a var() -([#36](https://github.com/postcss/postcss-color-function/pull/36) - @drewbourne)
 +([#36](https://github.com/postcss/postcss-color-function/pull/36) - @drewbourne) # 2.0.1 - 2016-03-15 diff --git a/README.md b/README.md index 6081353..d0ad387 100755 --- a/README.md +++ b/README.md @@ -1,11 +1,21 @@ -# postcss-color-function [![CSS Standard Status](https://jonathantneal.github.io/css-db/badge/css-color-modifying-colors.svg)](https://jonathantneal.github.io/css-db/#css-color-modifying-colors) [![Build Status](https://travis-ci.org/postcss/postcss-color-function.svg)](https://travis-ci.org/postcss/postcss-color-function) +# postcss-color-function [![Build Status](https://travis-ci.org/postcss/postcss-color-function.svg)](https://travis-ci.org/postcss/postcss-color-function) -> [PostCSS](https://github.com/postcss/postcss) plugin to transform [W3C CSS color function][specs] to more compatible CSS. +[PostCSS](https://github.com/postcss/postcss) plugin to transform CSS color function from editor draft of 'Color Module Level 4' specification to more compatible CSS. + +## Deprecated + +**⚠️ `color()` was changed to `color-mod()`. See [postcss-color-mod-function](https://github.com/jonathantneal/postcss-color-mod-function).** + +> There is a + [`color-mod`](https://github.com/jonathantneal/postcss-color-mod-function) + implementation. + +**⚠️ `color-mod()` has been removed from [Color Module Level 4 specification](https://www.w3.org/TR/css-color-4/#changes-from-20160705).** ## Installation ```console -$ npm install postcss-color-function +npm install postcss-color-function ``` ## Usage @@ -20,8 +30,10 @@ var colorFunction = require("postcss-color-function") var css = fs.readFileSync("input.css", "utf8") // process css +// set preserveCustomProps to `false` by default `true` +//for delete declarations with custom properties var output = postcss() - .use(colorFunction()) + .use(colorFunction({preserveCustomProps: true})) .process(css) .css ``` @@ -75,8 +87,6 @@ Notes: - can be used on every value on any property, - some values can use add/subtract/scale modifiers or a direct value. -[Read the specs][specs] for more information. - ### Examples ```css @@ -119,5 +129,3 @@ color(red shade(20%)) ## [Changelog](CHANGELOG.md) ## [License](LICENSE) - -[specs]: http://dev.w3.org/csswg/css-color/#modifying-colors diff --git a/index.js b/index.js index c045b73..905dd88 100755 --- a/index.js +++ b/index.js @@ -6,10 +6,16 @@ var parser = require("postcss-value-parser") var colorFn = require("css-color-function") var helpers = require("postcss-message-helpers") +var defaultOptions = { + preserveCustomProps: true, +} + /** * PostCSS plugin to transform color() */ -module.exports = postcss.plugin("postcss-color-function", function() { +module.exports = postcss.plugin("postcss-color-function", function(options) { + options = Object.assign({}, defaultOptions, options) + return function(style, result) { style.walkDecls(function transformDecl(decl) { if (!decl.value || decl.value.indexOf("color(") === -1) { @@ -17,6 +23,10 @@ module.exports = postcss.plugin("postcss-color-function", function() { } if (decl.value.indexOf("var(") !== -1) { + if (!options.preserveCustomProps) { + decl.remove() + return + } result.messages.push({ plugin: "postcss-color-function", type: "skipped-color-function-with-custom-property", @@ -27,7 +37,7 @@ module.exports = postcss.plugin("postcss-color-function", function() { "`", }) return - } + } try { decl.value = helpers.try(function transformColorValue() { diff --git a/package.json b/package.json index 9025c91..b6c5fa9 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "postcss-color-function", - "version": "4.0.0", + "version": "4.1.0", "description": "PostCSS plugin to transform W3C CSS color function to more compatible CSS.", "keywords": [ "css", @@ -17,19 +17,20 @@ "index.js" ], "dependencies": { - "css-color-function": "^1.3.0", - "postcss": "^6.0.1", + "css-color-function": "~1.3.3", + "postcss": "^6.0.23", "postcss-message-helpers": "^2.0.0", - "postcss-value-parser": "^3.3.0" + "postcss-value-parser": "^3.3.1" }, "devDependencies": { "eslint": "^3.19.0", + "faucet": "0.0.1", "npmpub": "^3.1.0", - "tape": "^3.0.0" + "tape": "^4.10.1" }, "scripts": { "lint": "eslint *.js index.js ./test/", - "test": "npm run lint && tape test", + "test": "npm run lint && tape test | faucet", "release": "npmpub" } } diff --git a/test/fixtures/color.css b/test/fixtures/color.css index 4ee21ac..335dc73 100644 --- a/test/fixtures/color.css +++ b/test/fixtures/color.css @@ -10,3 +10,9 @@ body { border-color: color(#999 a(0.8)) color(#999 a(0.8)); } + +.withAlpha { + color: color(black a(80%) contrast(99%)); + background-color: color(red a(50%) shade(50%)); + border-color: color(red a(80%) tint(50%)); +} diff --git a/test/fixtures/color.expected.css b/test/fixtures/color.expected.css index 01cea60..f525b85 100644 --- a/test/fixtures/color.expected.css +++ b/test/fixtures/color.expected.css @@ -10,3 +10,9 @@ body { border-color: rgba(153, 153, 153, 0.8) rgba(153, 153, 153, 0.8); } + +.withAlpha { + color: rgba(255, 255, 255, 0.8); + background-color: rgba(128, 0, 0, 0.5); + border-color: rgba(255, 128, 128, 0.8); +} diff --git a/test/fixtures/delete-custom-properties.css b/test/fixtures/delete-custom-properties.css new file mode 100644 index 0000000..1754afb --- /dev/null +++ b/test/fixtures/delete-custom-properties.css @@ -0,0 +1,7 @@ +body { + color: color(var(--red)); + color: color(var(--red) tint(50%)); + background-color: color(red tint(50%)); + color: color(var(--red) tint(var(--tintPercent))); + color: color(rgb(255, 0, 0) tint(var(--tintPercent))); +} diff --git a/test/fixtures/delete-custom-properties.expected.css b/test/fixtures/delete-custom-properties.expected.css new file mode 100644 index 0000000..21d0172 --- /dev/null +++ b/test/fixtures/delete-custom-properties.expected.css @@ -0,0 +1,3 @@ +body { + background-color: rgb(255, 128, 128); +} diff --git a/test/index.js b/test/index.js index d2c4801..e882c40 100755 --- a/test/index.js +++ b/test/index.js @@ -20,7 +20,7 @@ function compareFixtures(t, name, msg, opts, postcssOpts) { var actual = postcss().use(plugin(opts)) .process(read(postcssOpts.from), postcssOpts).css var expected = read(filename("fixtures/" + name + ".expected")) - fs.writeFile(filename("fixtures/" + name + ".actual"), actual) + fs.writeFileSync(filename("fixtures/" + name + ".actual"), actual) t.equal(actual, expected, msg) } @@ -106,3 +106,41 @@ test("logs message when color() contains var() custom property", function(t) { t.end() }) }) + +test( + "test delete custom properties with option preserveCustomProps `false`", + function(t) { + postcss(plugin({preserveCustomProps : false})).process( + read(filename("fixtures/delete-custom-properties")) + ).then(function(result) { + var expectedDeclaration = [{ + prop: "background-color", + value: "rgb(255, 128, 128)", + }] + // check left rules in body after clear + var declNodes = result.root.nodes[0].nodes + t.equal( + declNodes.length, + expectedDeclaration.length, + "expected " + expectedDeclaration.length + + " declaration length but got " + declNodes.length + ) + + t.equal( + declNodes[0].prop, + expectedDeclaration[0].prop, + "expected declaration with "+ expectedDeclaration[0].prop + + " property but got " + declNodes[0].prop + ) + + t.equal( + declNodes[0].value, + expectedDeclaration[0].value, + "expected declaration with "+ expectedDeclaration[0].value + + " value but got " + declNodes[0].value + ) + + t.end() + }) + } +)