Import Upstream version 1.0.5

parents
{
"projectName": "postcss-minify-gradients",
"projectOwner": "ben-eb",
"files": [
"README.md"
],
"imageSize": 100,
"commit": false,
"contributors": [
{
"login": "ben-eb",
"name": "Ben Briggs",
"avatar_url": "https://avatars.githubusercontent.com/u/1282980?v=3",
"profile": "http://beneb.info",
"contributions": [
"code",
"doc",
"review",
"test"
]
},
{
"login": "TrySound",
"name": "Bogdan Chadkin",
"avatar_url": "https://avatars.githubusercontent.com/u/5635476?v=3",
"profile": "https://github.com/TrySound",
"contributions": [
"code"
]
},
{
"login": "huan086",
"name": null,
"avatar_url": "https://avatars.githubusercontent.com/u/1448788?v=3",
"profile": "https://github.com/huan086",
"contributions": [
"bug"
]
},
{
"login": "jaybekster",
"name": "Mikhail",
"avatar_url": "https://avatars.githubusercontent.com/u/2485494?v=3",
"profile": "https://github.com/jaybekster",
"contributions": [
"bug",
"code",
"test"
]
}
]
}
{
"presets": ["es2015-loose", "stage-0"],
"plugins": ["add-module-exports"],
"env": {
"development": {
"sourceMaps": "inline"
}
}
}
# editorconfig.org
root = true
[*]
indent_style = space
indent_size = 4
end_of_line = lf
charset = utf-8
trim_trailing_whitespace = true
insert_final_newline = true
dist
node_modules
npm-debug.log
sudo: false
language: node_js
matrix:
include:
- node_js: '5'
- node_js: '4'
- node_js: '0.12'
env: NO_ESLINT=true
script: "[[ $NO_ESLINT == true ]] && npm run test-012 || npm test"
# 1.0.5
* Resolves a crash on empty `linear-gradient()` functions.
# 1.0.4
* Resolves an issue where `0` was being removed from intermediate colour stops
if preceding colour stops contained `calc()` (thanks to @jaybekster).
# 1.0.3
* Resolves an issue where `0` was being incorrectly stripped from the final
colour stop value.
# 1.0.2
* Resolves an issue where the module would incorrectly parse floating
point numbers.
# 1.0.1
* Reduce function iterations (thanks to @TrySound).
# 1.0.0
* Initial release.
Copyright (c) Ben Briggs <beneb.info@gmail.com> (http://beneb.info)
Permission is hereby granted, free of charge, to any person
obtaining a copy of this software and associated documentation
files (the "Software"), to deal in the Software without
restriction, including without limitation the rights to use,
copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the
Software is furnished to do so, subject to the following
conditions:
The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
OTHER DEALINGS IN THE SOFTWARE.
# [postcss][postcss]-minify-gradients [![Build Status](https://travis-ci.org/ben-eb/postcss-minify-gradients.svg?branch=master)][ci] [![NPM version](https://badge.fury.io/js/postcss-minify-gradients.svg)][npm] [![Dependency Status](https://gemnasium.com/ben-eb/postcss-minify-gradients.svg)][deps]
> Minify gradient parameters with PostCSS.
## Install
With [npm](https://npmjs.org/package/postcss-minify-gradients) do:
```
npm install postcss-minify-gradients
```
## Example
Where possible, this module will minify gradient parameters. It can convert
linear gradient directional syntax to angles, remove the unnecessary `0%` and
`100%` start and end values, and minimise color stops that use the same length
values (the browser will adjust the value automatically).
### Input
```css
h1 {
background: linear-gradient(to bottom, #ffe500 0%, #ffe500 50%, #121 50%, #121 100%)
}
```
### Output
```css
h1 {
background: linear-gradient(180deg, #ffe500, #ffe500 50%, #121 0, #121)
}
```
## Usage
See the [PostCSS documentation](https://github.com/postcss/postcss#usage) for
examples for your environment.
## Contributors
Thanks goes to these wonderful people ([emoji key](https://github.com/kentcdodds/all-contributors#emoji-key)):
<!-- ALL-CONTRIBUTORS-LIST:START - Do not remove or modify this section -->
| [<img src="https://avatars.githubusercontent.com/u/1282980?v=3" width="100px;"/><br /><sub>Ben Briggs</sub>](http://beneb.info)<br />[💻](https://github.com/ben-eb/postcss-minify-gradients/commits?author=ben-eb) [📖](https://github.com/ben-eb/postcss-minify-gradients/commits?author=ben-eb) 👀 [⚠️](https://github.com/ben-eb/postcss-minify-gradients/commits?author=ben-eb) | [<img src="https://avatars.githubusercontent.com/u/5635476?v=3" width="100px;"/><br /><sub>Bogdan Chadkin</sub>](https://github.com/TrySound)<br />[💻](https://github.com/ben-eb/postcss-minify-gradients/commits?author=TrySound) | [<img src="https://avatars.githubusercontent.com/u/1448788?v=3" width="100px;"/><br /><sub></sub>](https://github.com/huan086)<br />[🐛](https://github.com/ben-eb/postcss-minify-gradients/issues?q=author%3Ahuan086) | [<img src="https://avatars.githubusercontent.com/u/2485494?v=3" width="100px;"/><br /><sub>Mikhail</sub>](https://github.com/jaybekster)<br />[🐛](https://github.com/ben-eb/postcss-minify-gradients/issues?q=author%3Ajaybekster) [💻](https://github.com/ben-eb/postcss-minify-gradients/commits?author=jaybekster) [⚠️](https://github.com/ben-eb/postcss-minify-gradients/commits?author=jaybekster) |
| :---: | :---: | :---: | :---: |
<!-- ALL-CONTRIBUTORS-LIST:END -->
This project follows the [all-contributors] specification. Contributions of
any kind welcome!
## License
MIT © [Ben Briggs](http://beneb.info)
[all-contributors]: https://github.com/kentcdodds/all-contributors
[ci]: https://travis-ci.org/ben-eb/postcss-minify-gradients
[deps]: https://gemnasium.com/ben-eb/postcss-minify-gradients
[npm]: http://badge.fury.io/js/postcss-minify-gradients
[postcss]: https://github.com/postcss/postcss
{
"name": "postcss-minify-gradients",
"version": "1.0.5",
"description": "Minify gradient parameters with PostCSS.",
"main": "dist/index.js",
"scripts": {
"contributorAdd": "all-contributors add",
"contributorGenerate": "all-contributors generate",
"pretest": "eslint src",
"prepublish": "del-cli dist && BABEL_ENV=publish babel src --out-dir dist --ignore /__tests__/",
"test": "ava src/__tests__",
"test-012": "ava src/__tests__"
},
"files": [
"LICENSE-MIT",
"dist"
],
"keywords": [
"css",
"postcss",
"postcss-plugin"
],
"license": "MIT",
"devDependencies": {
"all-contributors-cli": "^3.0.5",
"ava": "^0.16.0",
"babel-cli": "^6.3.17",
"babel-core": "^6.3.26",
"babel-plugin-add-module-exports": "^0.2.1",
"babel-preset-es2015": "^6.3.13",
"babel-preset-es2015-loose": "^7.0.0",
"babel-preset-stage-0": "^6.3.13",
"babel-register": "^6.9.0",
"del-cli": "^0.2.0",
"eslint": "^3.0.0",
"eslint-config-cssnano": "^3.0.0",
"eslint-plugin-babel": "^3.3.0",
"eslint-plugin-import": "^2.0.1"
},
"homepage": "https://github.com/ben-eb/postcss-minify-gradients",
"author": {
"name": "Ben Briggs",
"email": "beneb.info@gmail.com",
"url": "http://beneb.info"
},
"repository": "ben-eb/postcss-minify-gradients",
"dependencies": {
"postcss": "^5.0.12",
"postcss-value-parser": "^3.3.0"
},
"ava": {
"require": "babel-register"
},
"eslintConfig": {
"extends": "cssnano"
}
}
import test from 'ava';
import postcss from 'postcss';
import {name} from '../../package.json';
import plugin from '../';
const tests = [{
message: 'linear: should convert "to top" to 0deg',
fixture: 'background:linear-gradient(to top,#ffe500,#121)',
expected: 'background:linear-gradient(0deg,#ffe500,#121)',
}, {
message: 'linear: should convert "to right" to 90deg',
fixture: 'background:linear-gradient(to right,#ffe500,#121)',
expected: 'background:linear-gradient(90deg,#ffe500,#121)',
}, {
message: 'linear: should convert "to bottom" to 180deg',
fixture: 'background:linear-gradient(to bottom,#ffe500,#121)',
expected: 'background:linear-gradient(180deg,#ffe500,#121)',
}, {
message: 'linear: should convert "to left" to 270deg',
fixture: 'background:linear-gradient(to left,#ffe500,#121)',
expected: 'background:linear-gradient(270deg,#ffe500,#121)',
}, {
message: 'repeating-linear: should convert "to top" to 0deg',
fixture: 'background:repeating-linear-gradient(to top,#ffe500,#121)',
expected: 'background:repeating-linear-gradient(0deg,#ffe500,#121)',
}, {
message: 'repeating-linear: should convert "to right" to 90deg',
fixture: 'background:repeating-linear-gradient(to right,#ffe500,#121)',
expected: 'background:repeating-linear-gradient(90deg,#ffe500,#121)',
}, {
message: 'repeating-linear: should convert "to bottom" to 180deg',
fixture: 'background:repeating-linear-gradient(to bottom,#ffe500,#121)',
expected: 'background:repeating-linear-gradient(180deg,#ffe500,#121)',
}, {
message: 'repeating-linear: should convert "to left" to 270deg',
fixture: 'background:repeating-linear-gradient(to left,#ffe500,#121)',
expected: 'background:repeating-linear-gradient(270deg,#ffe500,#121)',
}, {
message: 'linear: should not convert "to top right" to an angle',
fixture: 'background:linear-gradient(to top right,#ffe500,#121)',
expected: 'background:linear-gradient(to top right,#ffe500,#121)',
}, {
message: 'linear: should not convert "to bottom left" to an angle',
fixture: 'background:linear-gradient(to bottom left,#ffe500,#121)',
expected: 'background:linear-gradient(to bottom left,#ffe500,#121)',
}, {
message: 'linear: should reduce length values if they are the same',
fixture: 'background:linear-gradient(45deg,#ffe500 50%,#121 50%)',
expected: 'background:linear-gradient(45deg,#ffe500 50%,#121 0)',
}, {
message: 'linear: should reduce length values if they are less',
fixture: 'background:linear-gradient(45deg,#ffe500 50%,#121 25%)',
expected: 'background:linear-gradient(45deg,#ffe500 50%,#121 0)',
}, {
message: 'linear: should not reduce length values with different units',
fixture: 'background:linear-gradient(45deg,#ffe500 25px,#121 20%)',
expected: 'background:linear-gradient(45deg,#ffe500 25px,#121 20%)',
}, {
message: 'linear: should remove the (unnecessary) start/end length values',
fixture: 'background:linear-gradient(#ffe500 0%,#121 100%)',
expected: 'background:linear-gradient(#ffe500,#121)',
}, {
message: 'repeating-radial: should reduce length values if they are the same',
fixture: 'background:repeating-radial-gradient(#121,#121 5px,#ffe500 5px,#ffe500 10px)',
expected: 'background:repeating-radial-gradient(#121,#121 5px,#ffe500 0,#ffe500 10px)',
}, {
message: 'should not mangle floating point numbers',
fixture: 'background:linear-gradient(#fff,#fff 2em,#ccc 2em,#ccc 2.1em,#fff 2.1em)',
expected: 'background:linear-gradient(#fff,#fff 2em,#ccc 0,#ccc 2.1em,#fff 0)',
}, {
message: 'should not remove the trailing zero if it is the last stop',
fixture: 'background: linear-gradient(90deg,transparent,#00aeef 0)',
expected: 'background: linear-gradient(90deg,transparent,#00aeef 0)',
}, {
message: 'should not remove point number if it its different type from a previous one',
fixture: 'background: linear-gradient(to left bottom,transparent calc(50% - 2px),#a7a7a8 0,#a7a7a8 calc(50% + 2px),transparent 0)',
expected: 'background: linear-gradient(to left bottom,transparent calc(50% - 2px),#a7a7a8 0,#a7a7a8 calc(50% + 2px),transparent 0)',
}, {
message: 'should not throw on empty linear gradients',
fixture: 'background: linear-gradient()',
expected: 'background: linear-gradient()',
}, {
message: 'should not throw on empty radial gradients',
fixture: 'background: radial-gradient()',
expected: 'background: radial-gradient()',
}];
tests.forEach(({message, fixture, expected, options = {}}) => {
test(message, t => {
return postcss(plugin(options)).process(fixture).then(({css}) => {
t.deepEqual(css, expected);
});
});
});
test('should use the postcss plugin api', t => {
t.truthy(plugin().postcssVersion, 'should be able to access version');
t.deepEqual(plugin().postcssPlugin, name, 'should be able to access name');
});
import postcss from 'postcss';
import valueParser, {unit} from 'postcss-value-parser';
const angles = {
top: '0deg',
right: '90deg',
bottom: '180deg',
left: '270deg',
};
function getArguments (node) {
return node.nodes.reduce((list, child) => {
if (child.type !== 'div') {
list[list.length - 1].push(child);
} else {
list.push([]);
}
return list;
}, [[]]);
}
function isLessThan (a, b) {
return a.unit === b.unit && parseFloat(a.number) >= parseFloat(b.number);
}
function optimise (decl) {
if (!~decl.value.indexOf('gradient')) {
return;
}
decl.value = valueParser(decl.value).walk(node => {
if (node.type !== 'function') {
return false;
}
if (
node.value === 'linear-gradient' ||
node.value === 'repeating-linear-gradient' ||
node.value === '-webkit-linear-gradient' ||
node.value === '-webkit-repeating-linear-gradient'
) {
if (!node.nodes.length) {
return false;
}
let args = getArguments(node);
if (node.nodes[0].value === 'to' && args[0].length === 3) {
node.nodes = node.nodes.slice(2);
node.nodes[0].value = angles[node.nodes[0].value];
}
let lastStop = null;
args.forEach((arg, index) => {
if (!arg[2]) {
return;
}
let isFinalStop = index === args.length - 1;
let thisStop = unit(arg[2].value);
if (lastStop === null) {
lastStop = thisStop;
if (!isFinalStop && lastStop && lastStop.number === '0' && lastStop.unit !== 'deg') {
arg[1].value = arg[2].value = '';
}
return;
}
if (isLessThan(lastStop, thisStop)) {
arg[2].value = 0;
}
lastStop = thisStop;
if (isFinalStop && arg[2].value === '100%') {
arg[1].value = arg[2].value = '';
}
});
return false;
}
if (
node.value === 'radial-gradient' ||
node.value === 'repeating-radial-gradient' ||
node.value === '-webkit-radial-gradient' ||
node.value === '-webkit-repeating-radial-gradient'
) {
if (!node.nodes.length) {
return false;
}
let args = getArguments(node);
let lastStop;
args.forEach(arg => {
if (!arg[2]) {
return;
}
let thisStop = unit(arg[2].value);
if (!lastStop) {
lastStop = thisStop;
return;
}
if (isLessThan(lastStop, thisStop)) {
arg[2].value = 0;
}
lastStop = thisStop;
});
return false;
}
}).toString();
}
export default postcss.plugin('postcss-minify-gradients', () => {
return css => css.walkDecls(optimise);
});
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment