postcss-color-functional-notation 1.0.0 → 2.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/CHANGELOG.md CHANGED
@@ -1,5 +1,22 @@
1
1
  # Changes to PostCSS Color Functional Notation
2
2
 
3
+ ### 2.0.1 (September 18, 2018)
4
+
5
+ - Updated: PostCSS Values Parser 2 (patch for this project)
6
+
7
+ ### 2.0.0 (September 17, 2018)
8
+
9
+ - Updated: Support for PostCSS v7+
10
+ - Updated: Support for Node 6+
11
+
12
+ ### 1.0.2 (July 13, 2018)
13
+
14
+ - Fixed: Poorly detected hsl() and rgb() now resolve correctly
15
+
16
+ ### 1.0.1 (May 11, 2018)
17
+
18
+ - Fixed: A non-percentage 0 works alongside other percentages
19
+
3
20
  ### 1.0.0 (May 7, 2018)
4
21
 
5
22
  - Initial version
package/README.md CHANGED
@@ -3,7 +3,6 @@
3
3
  [![NPM Version][npm-img]][npm-url]
4
4
  [![CSS Standard Status][css-img]][css-url]
5
5
  [![Build Status][cli-img]][cli-url]
6
- [![Windows Build Status][win-img]][win-url]
7
6
  [![Support Chat][git-img]][git-url]
8
7
 
9
8
  [PostCSS Color Functional Notation] lets you use space and slash separated
@@ -12,143 +11,52 @@ color notation in CSS, following the [CSS Color] specification.
12
11
  ```pcss
13
12
  :root {
14
13
  --firebrick: rgb(178 34 34);
15
- --firebrick-a50: color: rgb(70% 13.5% 13.5% / 50%);
16
- --firebrick-hsl: color: hsla(0 68% 42%);
17
- --firebrick-hsl-a50: color: hsl(0 68% 42% / 50%);
14
+ --firebrick-a50: rgb(70% 13.5% 13.5% / 50%);
15
+ --firebrick-hsl: hsla(0 68% 42%);
16
+ --firebrick-hsl-a50: hsl(0 68% 42% / 50%);
18
17
  }
19
18
 
20
19
  /* becomes */
21
20
 
22
21
  :root {
23
22
  --firebrick: rgb(178, 34, 34);
24
- --firebrick-a50: color: rgba(178, 34, 34, .5);
25
- --firebrick-hsl: color: hsl(0, 68%, 42%);
26
- --firebrick-hsl-a50: color: hsla(0, 68%, 42%, .5);
23
+ --firebrick-a50: rgba(178, 34, 34, .5);
24
+ --firebrick-hsl: hsl(0, 68%, 42%);
25
+ --firebrick-hsl-a50: hsla(0, 68%, 42%, .5);
27
26
  }
28
27
  ```
29
28
 
30
29
  ## Usage
31
30
 
32
- Add [PostCSS Color Functional Notation] to your build tool:
31
+ Add [PostCSS Color Functional Notation] to your project:
33
32
 
34
33
  ```bash
35
34
  npm install postcss-color-functional-notation --save-dev
36
35
  ```
37
36
 
38
- #### Node
39
-
40
37
  Use [PostCSS Color Functional Notation] to process your CSS:
41
38
 
42
39
  ```js
43
- import postcssColorFunctionalNotation from 'postcss-color-functional-notation';
44
-
45
- postcssColorFunctionalNotation.process(YOUR_CSS, /* processOptions */, /* pluginOptions */);
46
- ```
47
-
48
- #### PostCSS
40
+ const postcssColorFunctionalNotation = require('postcss-color-functional-notation');
49
41
 
50
- Add [PostCSS] to your build tool:
51
-
52
- ```bash
53
- npm install postcss --save-dev
42
+ postcssColorFunctionalNotation.process(YOUR_CSS /*, processOptions, pluginOptions */);
54
43
  ```
55
44
 
56
- Use [PostCSS Color Functional Notation] as a plugin:
45
+ Or use it as a [PostCSS] plugin:
57
46
 
58
47
  ```js
59
- import postcss from 'gulp-postcss';
60
- import postcssColorFunctionalNotation from 'postcss-color-functional-notation';
48
+ const postcss = require('postcss');
49
+ const postcssColorFunctionalNotation = require('postcss-color-functional-notation');
61
50
 
62
51
  postcss([
63
52
  postcssColorFunctionalNotation(/* pluginOptions */)
64
- ]).process(YOUR_CSS);
65
- ```
66
-
67
- #### Webpack
68
-
69
- Add [PostCSS Loader] to your build tool:
70
-
71
- ```bash
72
- npm install postcss-loader --save-dev
73
- ```
74
-
75
- Use [PostCSS Color Functional Notation] in your Webpack configuration:
76
-
77
- ```js
78
- import postcssColorFunctionalNotation from 'postcss-color-functional-notation';
79
-
80
- module.exports = {
81
- module: {
82
- rules: [
83
- {
84
- test: /\.css$/,
85
- use: [
86
- 'style-loader',
87
- { loader: 'css-loader', options: { importLoaders: 1 } },
88
- { loader: 'postcss-loader', options: {
89
- ident: 'postcss',
90
- plugins: () => [
91
- postcssColorFunctionalNotation(/* pluginOptions */)
92
- ]
93
- } }
94
- ]
95
- }
96
- ]
97
- }
98
- }
53
+ ]).process(YOUR_CSS /*, processOptions */);
99
54
  ```
100
55
 
101
- #### Gulp
56
+ [PostCSS Color Functional Notation] runs in all Node environments, with special instructions for:
102
57
 
103
- Add [Gulp PostCSS] to your build tool:
104
-
105
- ```bash
106
- npm install gulp-postcss --save-dev
107
- ```
108
-
109
- Use [PostCSS Color Functional Notation] in your Gulpfile:
110
-
111
- ```js
112
- import postcss from 'gulp-postcss';
113
- import postcssColorFunctionalNotation from 'postcss-color-functional-notation';
114
-
115
- gulp.task('css', () => gulp.src('./src/*.css').pipe(
116
- postcss([
117
- postcssColorFunctionalNotation(/* pluginOptions */)
118
- ])
119
- ).pipe(
120
- gulp.dest('.')
121
- ));
122
- ```
123
-
124
- #### Grunt
125
-
126
- Add [Grunt PostCSS] to your build tool:
127
-
128
- ```bash
129
- npm install grunt-postcss --save-dev
130
- ```
131
-
132
- Use [PostCSS Color Functional Notation] in your Gruntfile:
133
-
134
- ```js
135
- import postcssColorFunctionalNotation from 'postcss-color-functional-notation';
136
-
137
- grunt.loadNpmTasks('grunt-postcss');
138
-
139
- grunt.initConfig({
140
- postcss: {
141
- options: {
142
- use: [
143
- postcssColorFunctionalNotation(/* pluginOptions */)
144
- ]
145
- },
146
- dist: {
147
- src: '*.css'
148
- }
149
- }
150
- });
151
- ```
58
+ | [Node](INSTALL.md#node) | [PostCSS CLI](INSTALL.md#postcss-cli) | [Webpack](INSTALL.md#webpack) | [Create React App](INSTALL.md#create-react-app) | [Gulp](INSTALL.md#gulp) | [Grunt](INSTALL.md#grunt) |
59
+ | --- | --- | --- | --- | --- | --- |
152
60
 
153
61
  ## Options
154
62
 
@@ -164,9 +72,9 @@ postcssImageSetFunction({ preserve: true })
164
72
  ```pcss
165
73
  :root {
166
74
  --firebrick: rgb(178 34 34);
167
- --firebrick-a50: color: rgb(70% 13.5% 13.5% / 50%);
168
- --firebrick-hsl: color: hsla(0 68% 42%);
169
- --firebrick-hsl-a50: color: hsl(0 68% 42% / 50%);
75
+ --firebrick-a50: rgb(70% 13.5% 13.5% / 50%);
76
+ --firebrick-hsl: hsla(0 68% 42%);
77
+ --firebrick-hsl-a50: hsl(0 68% 42% / 50%);
170
78
  }
171
79
 
172
80
  /* becomes */
@@ -174,12 +82,12 @@ postcssImageSetFunction({ preserve: true })
174
82
  :root {
175
83
  --firebrick: rgb(178, 34, 34);
176
84
  --firebrick: rgb(178 34 34);
177
- --firebrick-a50: color: rgba(178, 34, 34, .5);
178
- --firebrick-a50: color: rgb(70% 13.5% 13.5% / 50%);
179
- --firebrick-hsl: color: hsl(0, 68%, 42%);
180
- --firebrick-hsl: color: hsla(0 68% 42%);
181
- --firebrick-hsl-a50: color: hsla(0, 68%, 42%, .5);
182
- --firebrick-hsl-a50: color: hsl(0 68% 42% / 50%);
85
+ --firebrick-a50: rgba(178, 34, 34, .5);
86
+ --firebrick-a50: rgb(70% 13.5% 13.5% / 50%);
87
+ --firebrick-hsl: hsl(0, 68%, 42%);
88
+ --firebrick-hsl: hsla(0 68% 42%);
89
+ --firebrick-hsl-a50: hsla(0, 68%, 42%, .5);
90
+ --firebrick-hsl-a50: hsl(0 68% 42% / 50%);
183
91
  }
184
92
  ```
185
93
 
@@ -191,8 +99,6 @@ postcssImageSetFunction({ preserve: true })
191
99
  [git-url]: https://gitter.im/postcss/postcss
192
100
  [npm-img]: https://img.shields.io/npm/v/postcss-color-functional-notation.svg
193
101
  [npm-url]: https://www.npmjs.com/package/postcss-color-functional-notation
194
- [win-img]: https://img.shields.io/appveyor/ci/jonathantneal/postcss-color-functional-notation.svg
195
- [win-url]: https://ci.appveyor.com/project/jonathantneal/postcss-color-functional-notation
196
102
 
197
103
  [CSS Color]: https://drafts.csswg.org/css-color/#ref-for-funcdef-rgb%E2%91%A1%E2%91%A0
198
104
  [Gulp PostCSS]: https://github.com/postcss/gulp-postcss
package/index.cjs.js CHANGED
@@ -3,127 +3,114 @@
3
3
  function _interopDefault (ex) { return (ex && (typeof ex === 'object') && 'default' in ex) ? ex['default'] : ex; }
4
4
 
5
5
  var postcss = _interopDefault(require('postcss'));
6
- var parser = _interopDefault(require('postcss-values-parser'));
7
-
8
- var colorAnyRegExp = /(^|[^\w-])(hsla?|rgba?)\(/i;
9
- var colorRegExp = /^(hsla?|rgba?)$/i;
10
-
11
- var index = postcss.plugin('postcss-color-functional-notation', function (opts) {
12
- var preserve = 'preserve' in Object(opts) ? Boolean(opts.preserve) : false;
13
-
14
- return function (root) {
15
- root.walkDecls(function (decl) {
16
- var value = decl.value;
17
-
18
-
19
- if (colorAnyRegExp.test(value)) {
20
- var ast = parser(value).parse();
21
-
22
- ast.walkType('func', function (node) {
23
- if (colorRegExp.test(node.value)) {
24
- var children = node.nodes.slice(1, -1);
25
- var isFunctionalHSL = matchFunctionalHSL(children);
26
- var isFunctionalRGB1 = matchFunctionalRGB1(children);
27
- var isFunctionalRGB2 = matchFunctionalRGB2(children);
28
-
29
- if (isFunctionalHSL || isFunctionalRGB1 || isFunctionalRGB2) {
30
- var slashNode = children[3];
31
- var alphaNode = children[4];
32
-
33
- if (alphaNode) {
34
- if (isPercentage(alphaNode) && !isCalc(alphaNode)) {
35
- alphaNode.unit = '';
36
- alphaNode.value = String(alphaNode.value / 100);
37
- }
38
-
39
- if (isHslRgb(node)) {
40
- node.value += 'a';
41
- }
42
- } else if (isHslaRgba(node)) {
43
- node.value = node.value.slice(0, -1);
44
- }
45
-
46
- if (isSlash(slashNode)) {
47
- slashNode.replaceWith(newComma());
48
- }
49
-
50
- if (isFunctionalRGB2) {
51
- children[0].unit = children[1].unit = children[2].unit = '';
52
-
53
- children[0].value = String(Math.floor(children[0].value * 255 / 100));
54
- children[1].value = String(Math.floor(children[1].value * 255 / 100));
55
- children[2].value = String(Math.floor(children[2].value * 255 / 100));
56
- }
57
-
58
- node.nodes.splice(3, 0, [newComma()]);
59
- node.nodes.splice(2, 0, [newComma()]);
60
- }
61
- }
62
- });
63
-
64
- var newValue = String(ast);
65
-
66
- if (preserve) {
67
- decl.cloneBefore({ value: newValue });
68
- } else {
69
- decl.value = newValue;
70
- }
71
- }
72
- });
73
- };
6
+ var valuesParser = _interopDefault(require('postcss-values-parser'));
7
+
8
+ var index = postcss.plugin('postcss-color-functional-notation', opts => {
9
+ const preserve = 'preserve' in Object(opts) ? Boolean(opts.preserve) : false;
10
+ return root => {
11
+ root.walkDecls(decl => {
12
+ const originalValue = decl.value;
13
+
14
+ if (colorAnyRegExp.test(originalValue)) {
15
+ const valueAST = valuesParser(originalValue).parse();
16
+ valueAST.walkType('func', node => {
17
+ if (colorRegExp.test(node.value)) {
18
+ const children = node.nodes.slice(1, -1);
19
+ const isFunctionalHSL = matchFunctionalHSL(node, children);
20
+ const isFunctionalRGB1 = matchFunctionalRGB1(node, children);
21
+ const isFunctionalRGB2 = matchFunctionalRGB2(node, children);
22
+
23
+ if (isFunctionalHSL || isFunctionalRGB1 || isFunctionalRGB2) {
24
+ const slashNode = children[3];
25
+ const alphaNode = children[4];
26
+
27
+ if (alphaNode) {
28
+ if (isPercentage(alphaNode) && !isCalc(alphaNode)) {
29
+ alphaNode.unit = '';
30
+ alphaNode.value = String(alphaNode.value / 100);
31
+ }
32
+
33
+ if (isHslRgb(node)) {
34
+ node.value += 'a';
35
+ }
36
+ } else if (isHslaRgba(node)) {
37
+ node.value = node.value.slice(0, -1);
38
+ }
39
+
40
+ if (slashNode && isSlash(slashNode)) {
41
+ slashNode.replaceWith(newComma());
42
+ }
43
+
44
+ if (isFunctionalRGB2) {
45
+ children[0].unit = children[1].unit = children[2].unit = '';
46
+ children[0].value = String(Math.floor(children[0].value * 255 / 100));
47
+ children[1].value = String(Math.floor(children[1].value * 255 / 100));
48
+ children[2].value = String(Math.floor(children[2].value * 255 / 100));
49
+ }
50
+
51
+ node.nodes.splice(3, 0, [newComma()]);
52
+ node.nodes.splice(2, 0, [newComma()]);
53
+ }
54
+ }
55
+ });
56
+ const modifiedValue = String(valueAST);
57
+
58
+ if (modifiedValue !== originalValue) {
59
+ if (preserve) {
60
+ decl.cloneBefore({
61
+ value: modifiedValue
62
+ });
63
+ } else {
64
+ decl.value = modifiedValue;
65
+ }
66
+ }
67
+ }
68
+ });
69
+ };
74
70
  });
71
+ const alphaUnitMatch = /^%?$/i;
72
+ const calcFuncMatch = /^calc$/i;
73
+ const colorAnyRegExp = /(^|[^\w-])(hsla?|rgba?)\(/i;
74
+ const colorRegExp = /^(hsla?|rgba?)$/i;
75
+ const hslishRegExp = /^hsla?$/i;
76
+ const hslRgbFuncMatch = /^(hsl|rgb)$/i;
77
+ const hslaRgbaFuncMatch = /^(hsla|rgba)$/i;
78
+ const hueUnitMatch = /^(deg|grad|rad|turn)?$/i;
79
+ const rgbishRegExp = /^rgba?$/i;
75
80
 
76
- var alphaUnitMatch = /^%?$/i;
77
- var calcFuncMatch = /^calc$/i;
78
- var hueUnitMatch = /^(deg|grad|rad|turn)?$/i;
79
- var hslRgbFuncMatch = /^(hsl|rgb)$/i;
80
- var hslaRgbaFuncMatch = /^(hsla|rgba)$/i;
81
- var isAlphaValue = function isAlphaValue(node) {
82
- return isCalc(node) || Object(node).type === 'number' && alphaUnitMatch.test(node.unit);
83
- };
84
- var isCalc = function isCalc(node) {
85
- return Object(node).type === 'func' && calcFuncMatch.test(node.value);
86
- };
87
- var isHue = function isHue(node) {
88
- return isCalc(node) || Object(node).type === 'number' && hueUnitMatch.test(node.unit);
89
- };
90
- var isNumber = function isNumber(node) {
91
- return isCalc(node) || Object(node).type === 'number' && node.unit === '';
92
- };
93
- var isPercentage = function isPercentage(node) {
94
- return isCalc(node) || Object(node).type === 'number' && node.unit === '%';
95
- };
96
- var isHslRgb = function isHslRgb(node) {
97
- return Object(node).type === 'func' && hslRgbFuncMatch.test(node.value);
98
- };
99
- var isHslaRgba = function isHslaRgba(node) {
100
- return Object(node).type === 'func' && hslaRgbaFuncMatch.test(node.value);
101
- };
102
- var isSlash = function isSlash(node) {
103
- return Object(node).type === 'operator' && node.value === '/';
104
- };
105
- var functionalHSLMatch = [isHue, isPercentage, isPercentage, isSlash, isAlphaValue];
106
- var functionalRGB1Match = [isNumber, isNumber, isNumber, isSlash, isAlphaValue];
107
- var functionalRGB2Match = [isPercentage, isPercentage, isPercentage, isSlash, isAlphaValue];
108
-
109
- var matchFunctionalHSL = function matchFunctionalHSL(children) {
110
- return children.every(function (child, index) {
111
- return typeof functionalHSLMatch[index] === 'function' && functionalHSLMatch[index](child);
112
- });
113
- };
114
- var matchFunctionalRGB1 = function matchFunctionalRGB1(children) {
115
- return children.every(function (child, index) {
116
- return typeof functionalRGB1Match[index] === 'function' && functionalRGB1Match[index](child);
117
- });
118
- };
119
- var matchFunctionalRGB2 = function matchFunctionalRGB2(children) {
120
- return children.every(function (child, index) {
121
- return typeof functionalRGB2Match[index] === 'function' && functionalRGB2Match[index](child);
122
- });
123
- };
124
-
125
- var newComma = function newComma() {
126
- return parser.comma({ value: ',' });
127
- };
81
+ const isAlphaValue = node => isCalc(node) || node.type === 'number' && alphaUnitMatch.test(node.unit);
82
+
83
+ const isCalc = node => node.type === 'func' && calcFuncMatch.test(node.value);
84
+
85
+ const isHue = node => isCalc(node) || node.type === 'number' && hueUnitMatch.test(node.unit);
86
+
87
+ const isNumber = node => isCalc(node) || node.type === 'number' && node.unit === '';
88
+
89
+ const isPercentage = node => isCalc(node) || node.type === 'number' && (node.unit === '%' || node.unit === '' && node.value === '0');
90
+
91
+ const isHslish = node => node.type === 'func' && hslishRegExp.test(node.value);
92
+
93
+ const isHslRgb = node => node.type === 'func' && hslRgbFuncMatch.test(node.value);
94
+
95
+ const isHslaRgba = node => node.type === 'func' && hslaRgbaFuncMatch.test(node.value);
96
+
97
+ const isRgbish = node => node.type === 'func' && rgbishRegExp.test(node.value);
98
+
99
+ const isSlash = node => node.type === 'operator' && node.value === '/';
100
+
101
+ const functionalHSLMatch = [isHue, isPercentage, isPercentage, isSlash, isAlphaValue];
102
+ const functionalRGB1Match = [isNumber, isNumber, isNumber, isSlash, isAlphaValue];
103
+ const functionalRGB2Match = [isPercentage, isPercentage, isPercentage, isSlash, isAlphaValue];
104
+
105
+ const matchFunctionalHSL = (node, children) => isHslish(node) && children.every((child, index) => typeof functionalHSLMatch[index] === 'function' && functionalHSLMatch[index](child));
106
+
107
+ const matchFunctionalRGB1 = (node, children) => isRgbish(node) && children.every((child, index) => typeof functionalRGB1Match[index] === 'function' && functionalRGB1Match[index](child));
108
+
109
+ const matchFunctionalRGB2 = (node, children) => isRgbish(node) && children.every((child, index) => typeof functionalRGB2Match[index] === 'function' && functionalRGB2Match[index](child));
110
+
111
+ const newComma = () => valuesParser.comma({
112
+ value: ','
113
+ });
128
114
 
129
115
  module.exports = index;
116
+ //# sourceMappingURL=index.cjs.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.cjs.js","sources":["index.js"],"sourcesContent":["import postcss from 'postcss';\nimport valuesParser from 'postcss-values-parser';\n\nexport default postcss.plugin('postcss-color-functional-notation', opts => {\n\tconst preserve = 'preserve' in Object(opts) ? Boolean(opts.preserve) : false;\n\n\treturn root => {\n\t\troot.walkDecls(decl => {\n\t\t\tconst { value: originalValue } = decl;\n\n\t\t\tif (colorAnyRegExp.test(originalValue)) {\n\t\t\t\tconst valueAST = valuesParser(originalValue).parse();\n\n\t\t\t\tvalueAST.walkType('func', node => {\n\t\t\t\t\tif (colorRegExp.test(node.value)) {\n\t\t\t\t\t\tconst children = node.nodes.slice(1, -1);\n\t\t\t\t\t\tconst isFunctionalHSL = matchFunctionalHSL(node, children);\n\t\t\t\t\t\tconst isFunctionalRGB1 = matchFunctionalRGB1(node, children);\n\t\t\t\t\t\tconst isFunctionalRGB2 = matchFunctionalRGB2(node, children);\n\n\t\t\t\t\t\tif (isFunctionalHSL || isFunctionalRGB1 || isFunctionalRGB2) {\n\t\t\t\t\t\t\tconst slashNode = children[3];\n\t\t\t\t\t\t\tconst alphaNode = children[4];\n\n\t\t\t\t\t\t\tif (alphaNode) {\n\t\t\t\t\t\t\t\tif (isPercentage(alphaNode) && !isCalc(alphaNode)) {\n\t\t\t\t\t\t\t\t\talphaNode.unit = '';\n\t\t\t\t\t\t\t\t\talphaNode.value = String(alphaNode.value / 100);\n\t\t\t\t\t\t\t\t}\n\n\t\t\t\t\t\t\t\tif (isHslRgb(node)) {\n\t\t\t\t\t\t\t\t\tnode.value += 'a';\n\t\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\t} else if (isHslaRgba(node)) {\n\t\t\t\t\t\t\t\tnode.value = node.value.slice(0, -1);\n\t\t\t\t\t\t\t}\n\n\t\t\t\t\t\t\tif (slashNode && isSlash(slashNode)) {\n\t\t\t\t\t\t\t\tslashNode.replaceWith( newComma() );\n\t\t\t\t\t\t\t}\n\n\t\t\t\t\t\t\tif (isFunctionalRGB2) {\n\t\t\t\t\t\t\t\tchildren[0].unit = children[1].unit = children[2].unit = '';\n\n\t\t\t\t\t\t\t\tchildren[0].value = String(Math.floor(children[0].value * 255 / 100));\n\t\t\t\t\t\t\t\tchildren[1].value = String(Math.floor(children[1].value * 255 / 100));\n\t\t\t\t\t\t\t\tchildren[2].value = String(Math.floor(children[2].value * 255 / 100));\n\t\t\t\t\t\t\t}\n\n\t\t\t\t\t\t\tnode.nodes.splice(3, 0, [ newComma() ]);\n\t\t\t\t\t\t\tnode.nodes.splice(2, 0, [ newComma() ]);\n\t\t\t\t\t\t}\n\t\t\t\t\t}\n\t\t\t\t});\n\n\t\t\t\tconst modifiedValue = String(valueAST);\n\n\t\t\t\tif (modifiedValue !== originalValue) {\n\t\t\t\t\tif (preserve) {\n\t\t\t\t\t\tdecl.cloneBefore({ value: modifiedValue });\n\t\t\t\t\t} else {\n\t\t\t\t\t\tdecl.value = modifiedValue;\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t}\n\t\t});\n\t};\n});\n\nconst alphaUnitMatch = /^%?$/i;\nconst calcFuncMatch = /^calc$/i;\nconst colorAnyRegExp = /(^|[^\\w-])(hsla?|rgba?)\\(/i;\nconst colorRegExp = /^(hsla?|rgba?)$/i;\nconst hslishRegExp = /^hsla?$/i;\nconst hslRgbFuncMatch = /^(hsl|rgb)$/i;\nconst hslaRgbaFuncMatch = /^(hsla|rgba)$/i;\nconst hueUnitMatch = /^(deg|grad|rad|turn)?$/i;\nconst rgbishRegExp = /^rgba?$/i;\nconst isAlphaValue = node => isCalc(node) || node.type === 'number' && alphaUnitMatch.test(node.unit);\nconst isCalc = node => node.type === 'func' && calcFuncMatch.test(node.value);\nconst isHue = node => isCalc(node) || node.type === 'number' && hueUnitMatch.test(node.unit);\nconst isNumber = node => isCalc(node) || node.type === 'number' && node.unit === '';\nconst isPercentage = node => isCalc(node) || node.type === 'number' && (node.unit === '%' || node.unit === '' && node.value === '0');\nconst isHslish = node => node.type === 'func' && hslishRegExp.test(node.value);\nconst isHslRgb = node => node.type === 'func' && hslRgbFuncMatch.test(node.value);\nconst isHslaRgba = node => node.type === 'func' && hslaRgbaFuncMatch.test(node.value);\nconst isRgbish = node => node.type === 'func' && rgbishRegExp.test(node.value);\nconst isSlash = node => node.type === 'operator' && node.value === '/';\nconst functionalHSLMatch = [isHue, isPercentage, isPercentage, isSlash, isAlphaValue];\nconst functionalRGB1Match = [isNumber, isNumber, isNumber, isSlash, isAlphaValue];\nconst functionalRGB2Match = [isPercentage, isPercentage, isPercentage, isSlash, isAlphaValue];\n\nconst matchFunctionalHSL = (node, children) => isHslish(node) && children.every(\n\t(child, index) => typeof functionalHSLMatch[index] === 'function' && functionalHSLMatch[index](child)\n);\nconst matchFunctionalRGB1 = (node, children) => isRgbish(node) && children.every(\n\t(child, index) => typeof functionalRGB1Match[index] === 'function' && functionalRGB1Match[index](child)\n);\nconst matchFunctionalRGB2 = (node, children) => isRgbish(node) && children.every(\n\t(child, index) => typeof functionalRGB2Match[index] === 'function' && functionalRGB2Match[index](child)\n);\n\nconst newComma = () => valuesParser.comma({ value: ',' })\n"],"names":["postcss","plugin","opts","preserve","Object","Boolean","root","walkDecls","decl","originalValue","value","colorAnyRegExp","test","valueAST","valuesParser","parse","walkType","node","colorRegExp","children","nodes","slice","isFunctionalHSL","matchFunctionalHSL","isFunctionalRGB1","matchFunctionalRGB1","isFunctionalRGB2","matchFunctionalRGB2","slashNode","alphaNode","isPercentage","isCalc","unit","String","isHslRgb","isHslaRgba","isSlash","replaceWith","newComma","Math","floor","splice","modifiedValue","cloneBefore","alphaUnitMatch","calcFuncMatch","hslishRegExp","hslRgbFuncMatch","hslaRgbaFuncMatch","hueUnitMatch","rgbishRegExp","isAlphaValue","type","isHue","isNumber","isHslish","isRgbish","functionalHSLMatch","functionalRGB1Match","functionalRGB2Match","every","child","index","comma"],"mappings":";;;;;;;AAGA,YAAeA,OAAO,CAACC,MAAR,CAAe,mCAAf,EAAoDC,IAAI,IAAI;QACpEC,QAAQ,GAAG,cAAcC,MAAM,CAACF,IAAD,CAApB,GAA6BG,OAAO,CAACH,IAAI,CAACC,QAAN,CAApC,GAAsD,KAAvE;SAEOG,IAAI,IAAI;IACdA,IAAI,CAACC,SAAL,CAAeC,IAAI,IAAI;YACPC,aADO,GACWD,IADX,CACdE,KADc;;UAGlBC,cAAc,CAACC,IAAf,CAAoBH,aAApB,CAAJ,EAAwC;cACjCI,QAAQ,GAAGC,YAAY,CAACL,aAAD,CAAZ,CAA4BM,KAA5B,EAAjB;QAEAF,QAAQ,CAACG,QAAT,CAAkB,MAAlB,EAA0BC,IAAI,IAAI;cAC7BC,WAAW,CAACN,IAAZ,CAAiBK,IAAI,CAACP,KAAtB,CAAJ,EAAkC;kBAC3BS,QAAQ,GAAGF,IAAI,CAACG,KAAL,CAAWC,KAAX,CAAiB,CAAjB,EAAoB,CAAC,CAArB,CAAjB;kBACMC,eAAe,GAAGC,kBAAkB,CAACN,IAAD,EAAOE,QAAP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DH,mBAAmB,CAACG,KAAD,CAAnB,CAA2BD,KAA3B,CADL,CAAlE;;AAIA,MAAMvB,QAAQ,GAAG,MAAMxB,YAAY,CAACiD,KAAb,CAAmB;EAAErD,KAAK,EAAE;CAA5B,CAAvB;;;;"}
package/index.es.mjs ADDED
@@ -0,0 +1,112 @@
1
+ import postcss from 'postcss';
2
+ import valuesParser from 'postcss-values-parser';
3
+
4
+ var index = postcss.plugin('postcss-color-functional-notation', opts => {
5
+ const preserve = 'preserve' in Object(opts) ? Boolean(opts.preserve) : false;
6
+ return root => {
7
+ root.walkDecls(decl => {
8
+ const originalValue = decl.value;
9
+
10
+ if (colorAnyRegExp.test(originalValue)) {
11
+ const valueAST = valuesParser(originalValue).parse();
12
+ valueAST.walkType('func', node => {
13
+ if (colorRegExp.test(node.value)) {
14
+ const children = node.nodes.slice(1, -1);
15
+ const isFunctionalHSL = matchFunctionalHSL(node, children);
16
+ const isFunctionalRGB1 = matchFunctionalRGB1(node, children);
17
+ const isFunctionalRGB2 = matchFunctionalRGB2(node, children);
18
+
19
+ if (isFunctionalHSL || isFunctionalRGB1 || isFunctionalRGB2) {
20
+ const slashNode = children[3];
21
+ const alphaNode = children[4];
22
+
23
+ if (alphaNode) {
24
+ if (isPercentage(alphaNode) && !isCalc(alphaNode)) {
25
+ alphaNode.unit = '';
26
+ alphaNode.value = String(alphaNode.value / 100);
27
+ }
28
+
29
+ if (isHslRgb(node)) {
30
+ node.value += 'a';
31
+ }
32
+ } else if (isHslaRgba(node)) {
33
+ node.value = node.value.slice(0, -1);
34
+ }
35
+
36
+ if (slashNode && isSlash(slashNode)) {
37
+ slashNode.replaceWith(newComma());
38
+ }
39
+
40
+ if (isFunctionalRGB2) {
41
+ children[0].unit = children[1].unit = children[2].unit = '';
42
+ children[0].value = String(Math.floor(children[0].value * 255 / 100));
43
+ children[1].value = String(Math.floor(children[1].value * 255 / 100));
44
+ children[2].value = String(Math.floor(children[2].value * 255 / 100));
45
+ }
46
+
47
+ node.nodes.splice(3, 0, [newComma()]);
48
+ node.nodes.splice(2, 0, [newComma()]);
49
+ }
50
+ }
51
+ });
52
+ const modifiedValue = String(valueAST);
53
+
54
+ if (modifiedValue !== originalValue) {
55
+ if (preserve) {
56
+ decl.cloneBefore({
57
+ value: modifiedValue
58
+ });
59
+ } else {
60
+ decl.value = modifiedValue;
61
+ }
62
+ }
63
+ }
64
+ });
65
+ };
66
+ });
67
+ const alphaUnitMatch = /^%?$/i;
68
+ const calcFuncMatch = /^calc$/i;
69
+ const colorAnyRegExp = /(^|[^\w-])(hsla?|rgba?)\(/i;
70
+ const colorRegExp = /^(hsla?|rgba?)$/i;
71
+ const hslishRegExp = /^hsla?$/i;
72
+ const hslRgbFuncMatch = /^(hsl|rgb)$/i;
73
+ const hslaRgbaFuncMatch = /^(hsla|rgba)$/i;
74
+ const hueUnitMatch = /^(deg|grad|rad|turn)?$/i;
75
+ const rgbishRegExp = /^rgba?$/i;
76
+
77
+ const isAlphaValue = node => isCalc(node) || node.type === 'number' && alphaUnitMatch.test(node.unit);
78
+
79
+ const isCalc = node => node.type === 'func' && calcFuncMatch.test(node.value);
80
+
81
+ const isHue = node => isCalc(node) || node.type === 'number' && hueUnitMatch.test(node.unit);
82
+
83
+ const isNumber = node => isCalc(node) || node.type === 'number' && node.unit === '';
84
+
85
+ const isPercentage = node => isCalc(node) || node.type === 'number' && (node.unit === '%' || node.unit === '' && node.value === '0');
86
+
87
+ const isHslish = node => node.type === 'func' && hslishRegExp.test(node.value);
88
+
89
+ const isHslRgb = node => node.type === 'func' && hslRgbFuncMatch.test(node.value);
90
+
91
+ const isHslaRgba = node => node.type === 'func' && hslaRgbaFuncMatch.test(node.value);
92
+
93
+ const isRgbish = node => node.type === 'func' && rgbishRegExp.test(node.value);
94
+
95
+ const isSlash = node => node.type === 'operator' && node.value === '/';
96
+
97
+ const functionalHSLMatch = [isHue, isPercentage, isPercentage, isSlash, isAlphaValue];
98
+ const functionalRGB1Match = [isNumber, isNumber, isNumber, isSlash, isAlphaValue];
99
+ const functionalRGB2Match = [isPercentage, isPercentage, isPercentage, isSlash, isAlphaValue];
100
+
101
+ const matchFunctionalHSL = (node, children) => isHslish(node) && children.every((child, index) => typeof functionalHSLMatch[index] === 'function' && functionalHSLMatch[index](child));
102
+
103
+ const matchFunctionalRGB1 = (node, children) => isRgbish(node) && children.every((child, index) => typeof functionalRGB1Match[index] === 'function' && functionalRGB1Match[index](child));
104
+
105
+ const matchFunctionalRGB2 = (node, children) => isRgbish(node) && children.every((child, index) => typeof functionalRGB2Match[index] === 'function' && functionalRGB2Match[index](child));
106
+
107
+ const newComma = () => valuesParser.comma({
108
+ value: ','
109
+ });
110
+
111
+ export default index;
112
+ //# sourceMappingURL=index.es.mjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.es.mjs","sources":["index.js"],"sourcesContent":["import postcss from 'postcss';\nimport valuesParser from 'postcss-values-parser';\n\nexport default postcss.plugin('postcss-color-functional-notation', opts => {\n\tconst preserve = 'preserve' in Object(opts) ? Boolean(opts.preserve) : false;\n\n\treturn root => {\n\t\troot.walkDecls(decl => {\n\t\t\tconst { value: originalValue } = decl;\n\n\t\t\tif (colorAnyRegExp.test(originalValue)) {\n\t\t\t\tconst valueAST = valuesParser(originalValue).parse();\n\n\t\t\t\tvalueAST.walkType('func', node => {\n\t\t\t\t\tif (colorRegExp.test(node.value)) {\n\t\t\t\t\t\tconst children = node.nodes.slice(1, -1);\n\t\t\t\t\t\tconst isFunctionalHSL = matchFunctionalHSL(node, children);\n\t\t\t\t\t\tconst isFunctionalRGB1 = matchFunctionalRGB1(node, children);\n\t\t\t\t\t\tconst isFunctionalRGB2 = matchFunctionalRGB2(node, children);\n\n\t\t\t\t\t\tif (isFunctionalHSL || isFunctionalRGB1 || isFunctionalRGB2) {\n\t\t\t\t\t\t\tconst slashNode = children[3];\n\t\t\t\t\t\t\tconst alphaNode = children[4];\n\n\t\t\t\t\t\t\tif (alphaNode) {\n\t\t\t\t\t\t\t\tif (isPercentage(alphaNode) && !isCalc(alphaNode)) {\n\t\t\t\t\t\t\t\t\talphaNode.unit = '';\n\t\t\t\t\t\t\t\t\talphaNode.value = String(alphaNode.value / 100);\n\t\t\t\t\t\t\t\t}\n\n\t\t\t\t\t\t\t\tif (isHslRgb(node)) {\n\t\t\t\t\t\t\t\t\tnode.value += 'a';\n\t\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\t} else if (isHslaRgba(node)) {\n\t\t\t\t\t\t\t\tnode.value = node.value.slice(0, -1);\n\t\t\t\t\t\t\t}\n\n\t\t\t\t\t\t\tif (slashNode && isSlash(slashNode)) {\n\t\t\t\t\t\t\t\tslashNode.replaceWith( newComma() );\n\t\t\t\t\t\t\t}\n\n\t\t\t\t\t\t\tif (isFunctionalRGB2) {\n\t\t\t\t\t\t\t\tchildren[0].unit = children[1].unit = children[2].unit = '';\n\n\t\t\t\t\t\t\t\tchildren[0].value = String(Math.floor(children[0].value * 255 / 100));\n\t\t\t\t\t\t\t\tchildren[1].value = String(Math.floor(children[1].value * 255 / 100));\n\t\t\t\t\t\t\t\tchildren[2].value = String(Math.floor(children[2].value * 255 / 100));\n\t\t\t\t\t\t\t}\n\n\t\t\t\t\t\t\tnode.nodes.splice(3, 0, [ newComma() ]);\n\t\t\t\t\t\t\tnode.nodes.splice(2, 0, [ newComma() ]);\n\t\t\t\t\t\t}\n\t\t\t\t\t}\n\t\t\t\t});\n\n\t\t\t\tconst modifiedValue = String(valueAST);\n\n\t\t\t\tif (modifiedValue !== originalValue) {\n\t\t\t\t\tif (preserve) {\n\t\t\t\t\t\tdecl.cloneBefore({ value: modifiedValue });\n\t\t\t\t\t} else {\n\t\t\t\t\t\tdecl.value = modifiedValue;\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t}\n\t\t});\n\t};\n});\n\nconst alphaUnitMatch = /^%?$/i;\nconst calcFuncMatch = /^calc$/i;\nconst colorAnyRegExp = /(^|[^\\w-])(hsla?|rgba?)\\(/i;\nconst colorRegExp = /^(hsla?|rgba?)$/i;\nconst hslishRegExp = /^hsla?$/i;\nconst hslRgbFuncMatch = /^(hsl|rgb)$/i;\nconst hslaRgbaFuncMatch = /^(hsla|rgba)$/i;\nconst hueUnitMatch = /^(deg|grad|rad|turn)?$/i;\nconst rgbishRegExp = /^rgba?$/i;\nconst isAlphaValue = node => isCalc(node) || node.type === 'number' && alphaUnitMatch.test(node.unit);\nconst isCalc = node => node.type === 'func' && calcFuncMatch.test(node.value);\nconst isHue = node => isCalc(node) || node.type === 'number' && hueUnitMatch.test(node.unit);\nconst isNumber = node => isCalc(node) || node.type === 'number' && node.unit === '';\nconst isPercentage = node => isCalc(node) || node.type === 'number' && (node.unit === '%' || node.unit === '' && node.value === '0');\nconst isHslish = node => node.type === 'func' && hslishRegExp.test(node.value);\nconst isHslRgb = node => node.type === 'func' && hslRgbFuncMatch.test(node.value);\nconst isHslaRgba = node => node.type === 'func' && hslaRgbaFuncMatch.test(node.value);\nconst isRgbish = node => node.type === 'func' && rgbishRegExp.test(node.value);\nconst isSlash = node => node.type === 'operator' && node.value === '/';\nconst functionalHSLMatch = [isHue, isPercentage, isPercentage, isSlash, isAlphaValue];\nconst functionalRGB1Match = [isNumber, isNumber, isNumber, isSlash, isAlphaValue];\nconst functionalRGB2Match = [isPercentage, isPercentage, isPercentage, isSlash, isAlphaValue];\n\nconst matchFunctionalHSL = (node, children) => isHslish(node) && children.every(\n\t(child, index) => typeof functionalHSLMatch[index] === 'function' && functionalHSLMatch[index](child)\n);\nconst matchFunctionalRGB1 = (node, children) => isRgbish(node) && children.every(\n\t(child, index) => typeof functionalRGB1Match[index] === 'function' && functionalRGB1Match[index](child)\n);\nconst matchFunctionalRGB2 = (node, children) => isRgbish(node) && children.every(\n\t(child, index) => typeof functionalRGB2Match[index] === 'function' && functionalRGB2Match[index](child)\n);\n\nconst newComma = () => valuesParser.comma({ value: ',' })\n"],"names":["postcss","plugin","opts","preserve","Object","Boolean","root","walkDecls","decl","originalValue","value","colorAnyRegExp","test","valueAST","valuesParser","parse","walkType","node","colorRegExp","children","nodes","slice","isFunctionalHSL","matchFunctionalHSL","isFunctionalRGB1","matchFunctionalRGB1","isFunctionalRGB2","matchFunctionalRGB2","slashNode","alphaNode","isPercentage","isCalc","unit","String","isHslRgb","isHslaRgba","isSlash","replaceWith","newComma","Math","floor","splice","modifiedValue","cloneBefore","alphaUnitMatch","calcFuncMatch","hslishRegExp","hslRgbFuncMatch","hslaRgbaFuncMatch","hueUnitMatch","rgbishRegExp","isAlphaValue","type","isHue","isNumber","isHslish","isRgbish","functionalHSLMatch","functionalRGB1Match","functionalRGB2Match","every","child","index","comma"],"mappings":";;;AAGA,YAAeA,OAAO,CAACC,MAAR,CAAe,mCAAf,EAAoDC,IAAI,IAAI;QACpEC,QAAQ,GAAG,cAAcC,MAAM,CAACF,IAAD,CAApB,GAA6BG,OAAO,CAACH,IAAI,CAACC,QAAN,CAApC,GAAsD,KAAvE;SAEOG,IAAI,IAAI;IACdA,IAAI,CAACC,SAAL,CAAeC,IAAI,IAAI;YACPC,aADO,GACWD,IADX,CACdE,KADc;;UAGlBC,cAAc,CAACC,IAAf,CAAoBH,aAApB,CAAJ,EAAwC;cACjCI,QAAQ,GAAGC,YAAY,CAACL,aAAD,CAAZ,CAA4BM,KAA5B,EAAjB;QAEAF,QAAQ,CAACG,QAAT,CAAkB,MAAlB,EAA0BC,IAAI,IAAI;cAC7BC,WAAW,CAACN,IAAZ,CAAiBK,IAAI,CAACP,KAAtB,CAAJ,EAAkC;kBAC3BS,QAAQ,GAAGF,IAAI,CAACG,KAAL,CAAWC,KAAX,CAAiB,CAAjB,EAAoB,CAAC,CAArB,CAAjB;kBACMC,eAAe,GAAGC,kBAAkB,CAACN,IAAD,EAAOE,QAAP,CAA1C;kBACMK,gBAAgB,GAAGC,mBAAmB,CAACR,IAAD,EAAOE,QAAP,CAA5C;kBACMO,gBAAgB,GAAGC,mBAAmB,CAACV,IAAD,EAAOE,QAAP,CAA5C;;gBAEIG,eAAe,IA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B;;;;"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "postcss-color-functional-notation",
3
- "version": "1.0.0",
3
+ "version": "2.0.1",
4
4
  "description": "Use space and slash separated color notation in CSS",
5
5
  "author": "Jonathan Neal <jonathantneal@hotmail.com>",
6
6
  "license": "CC0-1.0",
@@ -8,10 +8,12 @@
8
8
  "homepage": "https://github.com/jonathantneal/postcss-color-functional-notation#readme",
9
9
  "bugs": "https://github.com/jonathantneal/postcss-color-functional-notation/issues",
10
10
  "main": "index.cjs.js",
11
- "module": "index.es.js",
11
+ "module": "index.es.mjs",
12
12
  "files": [
13
13
  "index.cjs.js",
14
- "index.es.js"
14
+ "index.cjs.js.map",
15
+ "index.es.mjs",
16
+ "index.es.mjs.map"
15
17
  ],
16
18
  "scripts": {
17
19
  "prepublishOnly": "npm test",
@@ -22,22 +24,22 @@
22
24
  "test:tape": "postcss-tape"
23
25
  },
24
26
  "engines": {
25
- "node": ">=4.0.0"
27
+ "node": ">=6.0.0"
26
28
  },
27
29
  "dependencies": {
28
- "postcss": "^6.0.22",
29
- "postcss-values-parser": "^1.5.0"
30
+ "postcss": "^7.0.2",
31
+ "postcss-values-parser": "^2.0.0"
30
32
  },
31
33
  "devDependencies": {
32
- "babel-core": "^6.26.3",
33
- "babel-eslint": "^8.2.3",
34
- "babel-preset-env": "^1.6.1",
35
- "eslint": "^4.19.1",
34
+ "@babel/core": "^7.1.0",
35
+ "@babel/preset-env": "^7.1.0",
36
+ "babel-eslint": "^9.0.0",
37
+ "eslint": "^5.6.0",
36
38
  "eslint-config-dev": "^2.0.0",
37
39
  "postcss-tape": "^2.2.0",
38
40
  "pre-commit": "^1.2.2",
39
- "rollup": "^0.58.2",
40
- "rollup-plugin-babel": "^3.0.4"
41
+ "rollup": "^0.66.0",
42
+ "rollup-plugin-babel": "^4.0.3"
41
43
  },
42
44
  "eslintConfig": {
43
45
  "extends": "dev",
package/index.es.js DELETED
@@ -1,125 +0,0 @@
1
- import postcss from 'postcss';
2
- import parser from 'postcss-values-parser';
3
-
4
- var colorAnyRegExp = /(^|[^\w-])(hsla?|rgba?)\(/i;
5
- var colorRegExp = /^(hsla?|rgba?)$/i;
6
-
7
- var index = postcss.plugin('postcss-color-functional-notation', function (opts) {
8
- var preserve = 'preserve' in Object(opts) ? Boolean(opts.preserve) : false;
9
-
10
- return function (root) {
11
- root.walkDecls(function (decl) {
12
- var value = decl.value;
13
-
14
-
15
- if (colorAnyRegExp.test(value)) {
16
- var ast = parser(value).parse();
17
-
18
- ast.walkType('func', function (node) {
19
- if (colorRegExp.test(node.value)) {
20
- var children = node.nodes.slice(1, -1);
21
- var isFunctionalHSL = matchFunctionalHSL(children);
22
- var isFunctionalRGB1 = matchFunctionalRGB1(children);
23
- var isFunctionalRGB2 = matchFunctionalRGB2(children);
24
-
25
- if (isFunctionalHSL || isFunctionalRGB1 || isFunctionalRGB2) {
26
- var slashNode = children[3];
27
- var alphaNode = children[4];
28
-
29
- if (alphaNode) {
30
- if (isPercentage(alphaNode) && !isCalc(alphaNode)) {
31
- alphaNode.unit = '';
32
- alphaNode.value = String(alphaNode.value / 100);
33
- }
34
-
35
- if (isHslRgb(node)) {
36
- node.value += 'a';
37
- }
38
- } else if (isHslaRgba(node)) {
39
- node.value = node.value.slice(0, -1);
40
- }
41
-
42
- if (isSlash(slashNode)) {
43
- slashNode.replaceWith(newComma());
44
- }
45
-
46
- if (isFunctionalRGB2) {
47
- children[0].unit = children[1].unit = children[2].unit = '';
48
-
49
- children[0].value = String(Math.floor(children[0].value * 255 / 100));
50
- children[1].value = String(Math.floor(children[1].value * 255 / 100));
51
- children[2].value = String(Math.floor(children[2].value * 255 / 100));
52
- }
53
-
54
- node.nodes.splice(3, 0, [newComma()]);
55
- node.nodes.splice(2, 0, [newComma()]);
56
- }
57
- }
58
- });
59
-
60
- var newValue = String(ast);
61
-
62
- if (preserve) {
63
- decl.cloneBefore({ value: newValue });
64
- } else {
65
- decl.value = newValue;
66
- }
67
- }
68
- });
69
- };
70
- });
71
-
72
- var alphaUnitMatch = /^%?$/i;
73
- var calcFuncMatch = /^calc$/i;
74
- var hueUnitMatch = /^(deg|grad|rad|turn)?$/i;
75
- var hslRgbFuncMatch = /^(hsl|rgb)$/i;
76
- var hslaRgbaFuncMatch = /^(hsla|rgba)$/i;
77
- var isAlphaValue = function isAlphaValue(node) {
78
- return isCalc(node) || Object(node).type === 'number' && alphaUnitMatch.test(node.unit);
79
- };
80
- var isCalc = function isCalc(node) {
81
- return Object(node).type === 'func' && calcFuncMatch.test(node.value);
82
- };
83
- var isHue = function isHue(node) {
84
- return isCalc(node) || Object(node).type === 'number' && hueUnitMatch.test(node.unit);
85
- };
86
- var isNumber = function isNumber(node) {
87
- return isCalc(node) || Object(node).type === 'number' && node.unit === '';
88
- };
89
- var isPercentage = function isPercentage(node) {
90
- return isCalc(node) || Object(node).type === 'number' && node.unit === '%';
91
- };
92
- var isHslRgb = function isHslRgb(node) {
93
- return Object(node).type === 'func' && hslRgbFuncMatch.test(node.value);
94
- };
95
- var isHslaRgba = function isHslaRgba(node) {
96
- return Object(node).type === 'func' && hslaRgbaFuncMatch.test(node.value);
97
- };
98
- var isSlash = function isSlash(node) {
99
- return Object(node).type === 'operator' && node.value === '/';
100
- };
101
- var functionalHSLMatch = [isHue, isPercentage, isPercentage, isSlash, isAlphaValue];
102
- var functionalRGB1Match = [isNumber, isNumber, isNumber, isSlash, isAlphaValue];
103
- var functionalRGB2Match = [isPercentage, isPercentage, isPercentage, isSlash, isAlphaValue];
104
-
105
- var matchFunctionalHSL = function matchFunctionalHSL(children) {
106
- return children.every(function (child, index) {
107
- return typeof functionalHSLMatch[index] === 'function' && functionalHSLMatch[index](child);
108
- });
109
- };
110
- var matchFunctionalRGB1 = function matchFunctionalRGB1(children) {
111
- return children.every(function (child, index) {
112
- return typeof functionalRGB1Match[index] === 'function' && functionalRGB1Match[index](child);
113
- });
114
- };
115
- var matchFunctionalRGB2 = function matchFunctionalRGB2(children) {
116
- return children.every(function (child, index) {
117
- return typeof functionalRGB2Match[index] === 'function' && functionalRGB2Match[index](child);
118
- });
119
- };
120
-
121
- var newComma = function newComma() {
122
- return parser.comma({ value: ',' });
123
- };
124
-
125
- export default index;