postcss-color-functional-notation 5.0.2 → 6.0.0

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,22 +1,46 @@
1
1
  # Changes to PostCSS Color Functional Notation
2
2
 
3
- ### 5.0.2 (February 6, 2023)
3
+ ### 6.0.0
4
+
5
+ _July 3, 2023_
6
+
7
+ - Change license to `MIT-0` ([read more about this change in the blog post](https://preset-env.cssdb.org/blog/license-change/))
8
+ - Updated [`@csstools/postcss-progressive-custom-properties`](/plugins/postcss-progressive-custom-properties) to [`3.0.0`](/plugins/postcss-progressive-custom-properties/CHANGELOG.md#300) (major)
9
+
10
+ ### 5.1.0
11
+
12
+ _June 1, 2023_
13
+
14
+ - Added `@csstools/postcss-progressive-custom-properties` for improved support of custom properties.
15
+ - Updated `@csstools/postcss-progressive-custom-properties` to `2.3.0` (minor)
16
+
17
+ ### 5.0.2
18
+
19
+ _February 6, 2023_
4
20
 
5
21
  - Reduce the amount of duplicate fallback CSS.
6
22
 
7
- ### 5.0.1 (January 28, 2023)
23
+ ### 5.0.1
24
+
25
+ _January 28, 2023_
8
26
 
9
27
  - Improve `types` declaration in `package.json`
10
28
 
11
- ### 5.0.0 (January 24, 2023)
29
+ ### 5.0.0
30
+
31
+ _January 24, 2023_
12
32
 
13
33
  - Updated: Support for Node v14+ (major).
14
34
 
15
- ### 4.2.4 (July 8, 2022)
35
+ ### 4.2.4
36
+
37
+ _July 8, 2022_
16
38
 
17
39
  - Fix case insensitive matching.
18
40
 
19
- ### 4.2.3 (May 19, 2022)
41
+ ### 4.2.3
42
+
43
+ _May 19, 2022_
20
44
 
21
45
  - Handle modern channel values in legacy notation (comma separated)
22
46
 
@@ -32,70 +56,100 @@
32
56
  }
33
57
  ```
34
58
 
35
- ### 4.2.2 (February 5, 2022)
59
+ ### 4.2.2
60
+
61
+ _February 5, 2022_
36
62
 
37
63
  - Improved `es module` and `commonjs` compatibility
38
64
 
39
- ### 4.2.1 (January 2, 2022)
65
+ ### 4.2.1
66
+
67
+ _January 2, 2022_
40
68
 
41
69
  - Removed Sourcemaps from package tarball.
42
70
  - Moved CLI to CLI Package. See [announcement](https://github.com/csstools/postcss-plugins/discussions/121).
43
71
 
44
- ### 4.2.0 (December 27, 2021)
72
+ ### 4.2.0
73
+
74
+ _December 27, 2021_
45
75
 
46
76
  - Added: support for Alpha value as the fourth argument in comma separated values notation.
47
77
 
48
- ### 4.1.0 (December 15, 2021)
78
+ ### 4.1.0
79
+
80
+ _December 15, 2021_
49
81
 
50
82
  - Added: support for Alpha value as a CSS variable in `rgb()` and `rgba()`.
51
83
 
52
- ### 4.0.2 (December 13, 2021)
84
+ ### 4.0.2
85
+
86
+ _December 13, 2021_
53
87
 
54
88
  - Changed: now uses `postcss-value-parser` for parsing.
55
89
  - Updated: documentation
56
90
  - Added: support for CSS variables with `preserve: true` option.
57
91
 
58
- ### 4.0.1 (November 18, 2021)
92
+ ### 4.0.1
93
+
94
+ _November 18, 2021_
59
95
 
60
96
  - Added: Safeguards against postcss-values-parser potentially throwing an error.
61
97
  - Updated: postcss-value-parser to 6.0.1 (patch)
62
98
 
63
- ### 4.0.0 (September 17, 2021)
99
+ ### 4.0.0
100
+
101
+ _September 17, 2021_
64
102
 
65
103
  - Updated: Support for PostCS 8+ (major).
66
104
  - Updated: Support for Node 12+ (major).
67
105
 
68
- ### 3.0.2 (April 25, 2020)
106
+ ### 3.0.2
107
+
108
+ _April 25, 2020_
69
109
 
70
110
  - Updated: Publish
71
111
 
72
- ### 3.0.1 (April 25, 2020)
112
+ ### 3.0.1
113
+
114
+ _April 25, 2020_
73
115
 
74
116
  - Updated: Using `walkType` to evade walker bug in `postcss-values-parser`
75
117
 
76
- ### 3.0.0 (April 25, 2020)
118
+ ### 3.0.0
119
+
120
+ _April 25, 2020_
77
121
 
78
122
  - Updated: Support for Node 10+
79
123
  - Updated: `postcss` to 7.0.27 (patch)
80
124
  - Updated: `postcss-values-parser` to 3.2.0 (minor)
81
125
 
82
- ### 2.0.1 (September 18, 2018)
126
+ ### 2.0.1
127
+
128
+ _September 18, 2018_
83
129
 
84
130
  - Updated: PostCSS Values Parser 2 (patch for this project)
85
131
 
86
- ### 2.0.0 (September 17, 2018)
132
+ ### 2.0.0
133
+
134
+ _September 17, 2018_
87
135
 
88
136
  - Updated: Support for PostCSS v7+
89
137
  - Updated: Support for Node 6+
90
138
 
91
- ### 1.0.2 (July 13, 2018)
139
+ ### 1.0.2
140
+
141
+ _July 13, 2018_
92
142
 
93
143
  - Fixed: Poorly detected hsl() and rgb() now resolve correctly
94
144
 
95
- ### 1.0.1 (May 11, 2018)
145
+ ### 1.0.1
146
+
147
+ _May 11, 2018_
96
148
 
97
149
  - Fixed: A non-percentage 0 works alongside other percentages
98
150
 
99
- ### 1.0.0 (May 7, 2018)
151
+ ### 1.0.0
152
+
153
+ _May 7, 2018_
100
154
 
101
155
  - Initial version
package/LICENSE.md CHANGED
@@ -1,108 +1,18 @@
1
- # CC0 1.0 Universal
2
-
3
- ## Statement of Purpose
4
-
5
- The laws of most jurisdictions throughout the world automatically confer
6
- exclusive Copyright and Related Rights (defined below) upon the creator and
7
- subsequent owner(s) (each and all, an “owner”) of an original work of
8
- authorship and/or a database (each, a “Work”).
9
-
10
- Certain owners wish to permanently relinquish those rights to a Work for the
11
- purpose of contributing to a commons of creative, cultural and scientific works
12
- (“Commons”) that the public can reliably and without fear of later claims of
13
- infringement build upon, modify, incorporate in other works, reuse and
14
- redistribute as freely as possible in any form whatsoever and for any purposes,
15
- including without limitation commercial purposes. These owners may contribute
16
- to the Commons to promote the ideal of a free culture and the further
17
- production of creative, cultural and scientific works, or to gain reputation or
18
- greater distribution for their Work in part through the use and efforts of
19
- others.
20
-
21
- For these and/or other purposes and motivations, and without any expectation of
22
- additional consideration or compensation, the person associating CC0 with a
23
- Work (the “Affirmer”), to the extent that he or she is an owner of Copyright
24
- and Related Rights in the Work, voluntarily elects to apply CC0 to the Work and
25
- publicly distribute the Work under its terms, with knowledge of his or her
26
- Copyright and Related Rights in the Work and the meaning and intended legal
27
- effect of CC0 on those rights.
28
-
29
- 1. Copyright and Related Rights. A Work made available under CC0 may be
30
- protected by copyright and related or neighboring rights (“Copyright and
31
- Related Rights”). Copyright and Related Rights include, but are not limited
32
- to, the following:
33
- 1. the right to reproduce, adapt, distribute, perform, display, communicate,
34
- and translate a Work;
35
- 2. moral rights retained by the original author(s) and/or performer(s);
36
- 3. publicity and privacy rights pertaining to a person’s image or likeness
37
- depicted in a Work;
38
- 4. rights protecting against unfair competition in regards to a Work,
39
- subject to the limitations in paragraph 4(i), below;
40
- 5. rights protecting the extraction, dissemination, use and reuse of data in
41
- a Work;
42
- 6. database rights (such as those arising under Directive 96/9/EC of the
43
- European Parliament and of the Council of 11 March 1996 on the legal
44
- protection of databases, and under any national implementation thereof,
45
- including any amended or successor version of such directive); and
46
- 7. other similar, equivalent or corresponding rights throughout the world
47
- based on applicable law or treaty, and any national implementations
48
- thereof.
49
-
50
- 2. Waiver. To the greatest extent permitted by, but not in contravention of,
51
- applicable law, Affirmer hereby overtly, fully, permanently, irrevocably and
52
- unconditionally waives, abandons, and surrenders all of Affirmer’s Copyright
53
- and Related Rights and associated claims and causes of action, whether now
54
- known or unknown (including existing as well as future claims and causes of
55
- action), in the Work (i) in all territories worldwide, (ii) for the maximum
56
- duration provided by applicable law or treaty (including future time
57
- extensions), (iii) in any current or future medium and for any number of
58
- copies, and (iv) for any purpose whatsoever, including without limitation
59
- commercial, advertising or promotional purposes (the “Waiver”). Affirmer
60
- makes the Waiver for the benefit of each member of the public at large and
61
- to the detriment of Affirmer’s heirs and successors, fully intending that
62
- such Waiver shall not be subject to revocation, rescission, cancellation,
63
- termination, or any other legal or equitable action to disrupt the quiet
64
- enjoyment of the Work by the public as contemplated by Affirmer’s express
65
- Statement of Purpose.
66
-
67
- 3. Public License Fallback. Should any part of the Waiver for any reason be
68
- judged legally invalid or ineffective under applicable law, then the Waiver
69
- shall be preserved to the maximum extent permitted taking into account
70
- Affirmer’s express Statement of Purpose. In addition, to the extent the
71
- Waiver is so judged Affirmer hereby grants to each affected person a
72
- royalty-free, non transferable, non sublicensable, non exclusive,
73
- irrevocable and unconditional license to exercise Affirmer’s Copyright and
74
- Related Rights in the Work (i) in all territories worldwide, (ii) for the
75
- maximum duration provided by applicable law or treaty (including future time
76
- extensions), (iii) in any current or future medium and for any number of
77
- copies, and (iv) for any purpose whatsoever, including without limitation
78
- commercial, advertising or promotional purposes (the “License”). The License
79
- shall be deemed effective as of the date CC0 was applied by Affirmer to the
80
- Work. Should any part of the License for any reason be judged legally
81
- invalid or ineffective under applicable law, such partial invalidity or
82
- ineffectiveness shall not invalidate the remainder of the License, and in
83
- such case Affirmer hereby affirms that he or she will not (i) exercise any
84
- of his or her remaining Copyright and Related Rights in the Work or (ii)
85
- assert any associated claims and causes of action with respect to the Work,
86
- in either case contrary to Affirmer’s express Statement of Purpose.
87
-
88
- 4. Limitations and Disclaimers.
89
- 1. No trademark or patent rights held by Affirmer are waived, abandoned,
90
- surrendered, licensed or otherwise affected by this document.
91
- 2. Affirmer offers the Work as-is and makes no representations or warranties
92
- of any kind concerning the Work, express, implied, statutory or
93
- otherwise, including without limitation warranties of title,
94
- merchantability, fitness for a particular purpose, non infringement, or
95
- the absence of latent or other defects, accuracy, or the present or
96
- absence of errors, whether or not discoverable, all to the greatest
97
- extent permissible under applicable law.
98
- 3. Affirmer disclaims responsibility for clearing rights of other persons
99
- that may apply to the Work or any use thereof, including without
100
- limitation any person’s Copyright and Related Rights in the Work.
101
- Further, Affirmer disclaims responsibility for obtaining any necessary
102
- consents, permissions or other rights required for any use of the Work.
103
- 4. Affirmer understands and acknowledges that Creative Commons is not a
104
- party to this document and has no duty or obligation with respect to this
105
- CC0 or use of the Work.
106
-
107
- For more information, please see
108
- http://creativecommons.org/publicdomain/zero/1.0/.
1
+ MIT No Attribution (MIT-0)
2
+
3
+ Copyright © CSSTools Contributors
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy of
6
+ this software and associated documentation files (the “Software”), to deal in
7
+ the Software without restriction, including without limitation the rights to
8
+ use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
9
+ of the Software, and to permit persons to whom the Software is furnished to do
10
+ so.
11
+
12
+ THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
13
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
14
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
15
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
16
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
17
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
18
+ SOFTWARE.
package/README.md CHANGED
@@ -58,7 +58,7 @@ The `preserve` option determines whether the original functional color notation
58
58
  is preserved. By default, it is not preserved.
59
59
 
60
60
  ```js
61
- postcssImageSetFunction({ preserve: true })
61
+ postcssColorFunctionalNotation({ preserve: true })
62
62
  ```
63
63
 
64
64
  ```pcss
@@ -83,6 +83,17 @@ postcssImageSetFunction({ preserve: true })
83
83
  }
84
84
  ```
85
85
 
86
+ ### enableProgressiveCustomProperties
87
+
88
+ The `enableProgressiveCustomProperties` option determines whether the original notation
89
+ is wrapped with `@supports` when used in Custom Properties. By default, it is enabled.
90
+
91
+ ⚠️ We only recommend disabling this when you set `preserve` to `false` or if you bring your own fix for Custom Properties. See what the plugin does in its [README](https://github.com/csstools/postcss-plugins/tree/main/plugins/postcss-progressive-custom-properties#readme).
92
+
93
+ ```js
94
+ postcssColorFunctionalNotation({ enableProgressiveCustomProperties: false })
95
+ ```
96
+
86
97
  [cli-url]: https://github.com/csstools/postcss-plugins/actions/workflows/test.yml?query=workflow/test
87
98
  [css-url]: https://cssdb.org/#color-functional-notation
88
99
  [discord]: https://discord.gg/bUadyRwkJS
package/dist/index.cjs CHANGED
@@ -1 +1 @@
1
- "use strict";var e=require("postcss-value-parser");function onCSSFunction(e){const r=e.value.toLowerCase();if(!needsConversion("rgb"===r||"rgba"===r,e.nodes))return;const n=convertOldSyntaxToNewSyntaxBeforeTransform(e.nodes).slice().filter((e=>"comment"!==e.type&&"space"!==e.type));let t=null;if("hsl"===r||"hsla"===r?t=hslFunctionContents(n):"rgb"!==r&&"rgba"!==r||(t=rgbFunctionContents(n)),!t)return;if(n.length>3&&(!t.slash||!t.alpha))return;transformAlpha(e,t.slash,t.alpha);const[o,u]=channelNodes(t);e.nodes.splice(e.nodes.indexOf(o)+1,0,{sourceIndex:0,sourceEndIndex:1,value:",",type:"div",before:"",after:""}),e.nodes.splice(e.nodes.indexOf(u)+1,0,{sourceIndex:0,sourceEndIndex:1,value:",",type:"div",before:"",after:""})}function isNumericNode(r){if(!r||"word"!==r.type)return!1;if(!canParseAsUnit(r))return!1;const n=e.unit(r.value);return!!n&&!!n.number}function isNumericNodeHueLike(r){if(!r||"word"!==r.type)return!1;if(!canParseAsUnit(r))return!1;const n=e.unit(r.value);if(!n)return!1;const t=n.unit.toLowerCase();return!!n.number&&("deg"===t||"grad"===t||"rad"===t||"turn"===t||""===n.unit)}function isNumericNodePercentageOrNumber(r){if(!r||"word"!==r.type)return!1;if(!canParseAsUnit(r))return!1;const n=e.unit(r.value);return!!n&&("%"===n.unit||""===n.unit)}function isCalcNode(e){return e&&"function"===e.type&&"calc"===e.value.toLowerCase()}function isVarNode(e){return e&&"function"===e.type&&"var"===e.value.toLowerCase()}function isSlashNode(e){return e&&"div"===e.type&&"/"===e.value}function hslFunctionContents(r){if(!isNumericNodeHueLike(r[0]))return null;if(!isNumericNodePercentageOrNumber(r[1]))return null;if(!isNumericNodePercentageOrNumber(r[2]))return null;const n={h:e.unit(r[0].value),hNode:r[0],s:e.unit(r[1].value),sNode:r[1],l:e.unit(r[2].value),lNode:r[2]};return normalizeHueNode(n.h),""!==n.h.unit?null:(n.hNode.value=n.h.number,isSlashNode(r[3])&&(n.slash=r[3]),(isNumericNodePercentageOrNumber(r[4])||isCalcNode(r[4])||isVarNode(r[4]))&&(n.alpha=r[4]),n)}function rgbFunctionContents(r){if(!isNumericNodePercentageOrNumber(r[0]))return null;if(!isNumericNodePercentageOrNumber(r[1]))return null;if(!isNumericNodePercentageOrNumber(r[2]))return null;const n={r:e.unit(r[0].value),rNode:r[0],g:e.unit(r[1].value),gNode:r[1],b:e.unit(r[2].value),bNode:r[2]};return"%"===n.r.unit&&(n.r.number=String(Math.floor(Number(n.r.number)/100*255)),n.rNode.value=n.r.number),"%"===n.g.unit&&(n.g.number=String(Math.floor(Number(n.g.number)/100*255)),n.gNode.value=n.g.number),"%"===n.b.unit&&(n.b.number=String(Math.floor(Number(n.b.number)/100*255)),n.bNode.value=n.b.number),isSlashNode(r[3])&&(n.slash=r[3]),(isNumericNodePercentageOrNumber(r[4])||isCalcNode(r[4])||isVarNode(r[4]))&&(n.alpha=r[4]),n}function isRgb(e){return void 0!==e.r}function channelNodes(e){return isRgb(e)?[e.rNode,e.gNode,e.bNode]:[e.hNode,e.sNode,e.lNode]}function transformAlpha(r,n,t){if("hsl"===r.value.toLowerCase()||"hsla"===r.value.toLowerCase()?r.value="hsl":"rgb"!==r.value.toLowerCase()&&"rgba"!==r.value.toLowerCase()||(r.value="rgb"),!n||!t)return;if("hsl"===r.value.toLowerCase()?r.value="hsla":r.value="rgba",n.value=",",n.before="",!isNumericNode(t))return;const o=e.unit(t.value);o&&"%"===o.unit&&(o.number=String(parseFloat(o.number)/100),t.value=String(o.number))}function normalizeHueNode(e){switch(e.unit.toLowerCase()){case"deg":return void(e.unit="");case"rad":return e.unit="",void(e.number=Math.round(180*parseFloat(e.number)/Math.PI).toString());case"grad":return e.unit="",void(e.number=Math.round(.9*parseFloat(e.number)).toString());case"turn":return e.unit="",void(e.number=Math.round(360*parseFloat(e.number)).toString())}}function canParseAsUnit(r){if(!r||!r.value)return!1;try{return!1!==e.unit(r.value)}catch(e){return!1}}function convertOldSyntaxToNewSyntaxBeforeTransform(e){let r=0;for(let n=0;n<e.length;n++){const t=e[n];if("div"===t.type&&","===t.value){if(r<2&&(t.value=" ",t.type="space"),2===r&&(t.value="/"),r>2)return;r++}}return e}function needsConversion(e,r){let n=!1,t=!1,o=!1;const u=r.slice().filter((e=>"comment"!==e.type&&"space"!==e.type));for(let a=0;a<u.length;a++){const s=u[a];if("word"===s.type&&"from"===s.value.toLowerCase())return!1;"div"!==s.type||","!==s.value?e&&"word"===s.type&&s.value.endsWith("%")?o=!0:a===r.length-1&&"word"===s.type&&s.value.endsWith("%")&&(t=!0):n=!0}return!(!n||!t&&!o)||!n}function hasSupportsAtRuleAncestor(e){let r=e.parent;for(;r;)if("atrule"===r.type){if("supports"===r.name&&-1!==r.params.toLowerCase().indexOf("(color: rgb(0 0 0 / 0.5)) and (color: hsl(0 0% 0% / 0.5))"))return!0;r=r.parent}else r=r.parent;return!1}function hasFallback(e){const r=e.parent;if(!r)return!1;const n=e.prop.toLowerCase(),t=r.index(e);for(let e=0;e<t;e++){const t=r.nodes[e];if("decl"===t.type&&t.prop.toLowerCase()===n)return!0}return!1}const postcssPlugin=r=>{const n="preserve"in Object(r)&&Boolean(r.preserve);return{postcssPlugin:"postcss-color-functional-notation",Declaration:(r,{result:t,postcss:o})=>{const u=r.value,a=u.toLowerCase();if(!(a.includes("rgb")||a.includes("rgba")||a.includes("hsl")||a.includes("hsla")))return;if(hasFallback(r))return;if(hasSupportsAtRuleAncestor(r))return;let s;try{s=e(u)}catch(e){r.warn(t,`Failed to parse value '${u}' as a hsl or rgb function. Leaving the original value intact.`)}if(void 0===s)return;s.walk((e=>{if(!e.type||"function"!==e.type)return;const r=e.value.toLowerCase();"hsl"!==r&&"hsla"!==r&&"rgb"!==r&&"rgba"!==r||onCSSFunction(e)}));const i=String(s);if(i!==u)if(n&&r.variable){const e=r.parent,n="(color: rgb(0 0 0 / 0.5)) and (color: hsl(0 0% 0% / 0.5))",t=o.atRule({name:"supports",params:n,source:r.source}),u=e.clone();u.removeAll(),u.append(r.clone()),t.append(u);let a=e,s=e.next();for(;a&&s&&"atrule"===s.type&&"supports"===s.name&&s.params===n;)a=s,s=s.next();a.after(t),r.replaceWith(r.clone({value:i}))}else n?r.cloneBefore({value:i}):r.replaceWith(r.clone({value:i}))}}};postcssPlugin.postcss=!0,module.exports=postcssPlugin;
1
+ "use strict";var e=require("@csstools/postcss-progressive-custom-properties"),r=require("postcss-value-parser");function onCSSFunction(e){const r=e.value.toLowerCase();if(!needsConversion("rgb"===r||"rgba"===r,e.nodes))return;const n=convertOldSyntaxToNewSyntaxBeforeTransform(e.nodes).slice().filter((e=>"comment"!==e.type&&"space"!==e.type));let t=null;if("hsl"===r||"hsla"===r?t=hslFunctionContents(n):"rgb"!==r&&"rgba"!==r||(t=rgbFunctionContents(n)),!t)return;if(n.length>3&&(!t.slash||!t.alpha))return;transformAlpha(e,t.slash,t.alpha);const[o,u]=channelNodes(t);e.nodes.splice(e.nodes.indexOf(o)+1,0,{sourceIndex:0,sourceEndIndex:1,value:",",type:"div",before:"",after:""}),e.nodes.splice(e.nodes.indexOf(u)+1,0,{sourceIndex:0,sourceEndIndex:1,value:",",type:"div",before:"",after:""})}function isNumericNode(e){if(!e||"word"!==e.type)return!1;if(!canParseAsUnit(e))return!1;const n=r.unit(e.value);return!!n&&!!n.number}function isNumericNodeHueLike(e){if(!e||"word"!==e.type)return!1;if(!canParseAsUnit(e))return!1;const n=r.unit(e.value);if(!n)return!1;const t=n.unit.toLowerCase();return!!n.number&&("deg"===t||"grad"===t||"rad"===t||"turn"===t||""===n.unit)}function isNumericNodePercentageOrNumber(e){if(!e||"word"!==e.type)return!1;if(!canParseAsUnit(e))return!1;const n=r.unit(e.value);return!!n&&("%"===n.unit||""===n.unit)}function isCalcNode(e){return e&&"function"===e.type&&"calc"===e.value.toLowerCase()}function isVarNode(e){return e&&"function"===e.type&&"var"===e.value.toLowerCase()}function isSlashNode(e){return e&&"div"===e.type&&"/"===e.value}function hslFunctionContents(e){if(!isNumericNodeHueLike(e[0]))return null;if(!isNumericNodePercentageOrNumber(e[1]))return null;if(!isNumericNodePercentageOrNumber(e[2]))return null;const n={h:r.unit(e[0].value),hNode:e[0],s:r.unit(e[1].value),sNode:e[1],l:r.unit(e[2].value),lNode:e[2]};return normalizeHueNode(n.h),""!==n.h.unit?null:(n.hNode.value=n.h.number,isSlashNode(e[3])&&(n.slash=e[3]),(isNumericNodePercentageOrNumber(e[4])||isCalcNode(e[4])||isVarNode(e[4]))&&(n.alpha=e[4]),n)}function rgbFunctionContents(e){if(!isNumericNodePercentageOrNumber(e[0]))return null;if(!isNumericNodePercentageOrNumber(e[1]))return null;if(!isNumericNodePercentageOrNumber(e[2]))return null;const n={r:r.unit(e[0].value),rNode:e[0],g:r.unit(e[1].value),gNode:e[1],b:r.unit(e[2].value),bNode:e[2]};return"%"===n.r.unit&&(n.r.number=String(Math.floor(Number(n.r.number)/100*255)),n.rNode.value=n.r.number),"%"===n.g.unit&&(n.g.number=String(Math.floor(Number(n.g.number)/100*255)),n.gNode.value=n.g.number),"%"===n.b.unit&&(n.b.number=String(Math.floor(Number(n.b.number)/100*255)),n.bNode.value=n.b.number),isSlashNode(e[3])&&(n.slash=e[3]),(isNumericNodePercentageOrNumber(e[4])||isCalcNode(e[4])||isVarNode(e[4]))&&(n.alpha=e[4]),n}function isRgb(e){return void 0!==e.r}function channelNodes(e){return isRgb(e)?[e.rNode,e.gNode,e.bNode]:[e.hNode,e.sNode,e.lNode]}function transformAlpha(e,n,t){if("hsl"===e.value.toLowerCase()||"hsla"===e.value.toLowerCase()?e.value="hsl":"rgb"!==e.value.toLowerCase()&&"rgba"!==e.value.toLowerCase()||(e.value="rgb"),!n||!t)return;if("hsl"===e.value.toLowerCase()?e.value="hsla":e.value="rgba",n.value=",",n.before="",!isNumericNode(t))return;const o=r.unit(t.value);o&&"%"===o.unit&&(o.number=String(parseFloat(o.number)/100),t.value=String(o.number))}function normalizeHueNode(e){switch(e.unit.toLowerCase()){case"deg":return void(e.unit="");case"rad":return e.unit="",void(e.number=Math.round(180*parseFloat(e.number)/Math.PI).toString());case"grad":return e.unit="",void(e.number=Math.round(.9*parseFloat(e.number)).toString());case"turn":return e.unit="",void(e.number=Math.round(360*parseFloat(e.number)).toString())}}function canParseAsUnit(e){if(!e||!e.value)return!1;try{return!1!==r.unit(e.value)}catch(e){return!1}}function convertOldSyntaxToNewSyntaxBeforeTransform(e){let r=0;for(let n=0;n<e.length;n++){const t=e[n];if("div"===t.type&&","===t.value){if(r<2&&(t.value=" ",t.type="space"),2===r&&(t.value="/"),r>2)return[];r++}}return e}function needsConversion(e,r){let n=!1,t=!1,o=!1;const u=r.slice().filter((e=>"comment"!==e.type&&"space"!==e.type));for(let s=0;s<u.length;s++){const a=u[s];if("word"===a.type&&"from"===a.value.toLowerCase())return!1;"div"!==a.type||","!==a.value?e&&"word"===a.type&&a.value.endsWith("%")?o=!0:s===r.length-1&&"word"===a.type&&a.value.endsWith("%")&&(t=!0):n=!0}return!(!n||!t&&!o)||!n}const n=/(\(color: rgb(a?)\(0 0 0 \/ 0)|(\(color: hsl(a?)\(0 0% 0% \/ 0)/i;function hasSupportsAtRuleAncestor(e){let r=e.parent;for(;r;)if("atrule"===r.type){if("supports"===r.name&&n.test(r.params))return!0;r=r.parent}else r=r.parent;return!1}function hasFallback(e){const r=e.parent;if(!r)return!1;const n=e.prop.toLowerCase(),t=r.index(e);for(let e=0;e<t;e++){const t=r.nodes[e];if("decl"===t.type&&t.prop.toLowerCase()===n)return!0}return!1}const basePlugin=e=>({postcssPlugin:"postcss-color-function",Declaration:(n,{result:t})=>{const o=n.value,u=o.toLowerCase();if(!(u.includes("rgb")||u.includes("rgba")||u.includes("hsl")||u.includes("hsla")))return;if(hasFallback(n))return;if(hasSupportsAtRuleAncestor(n))return;let s;try{s=r(o)}catch(e){n.warn(t,`Failed to parse value '${o}' as a hsl or rgb function. Leaving the original value intact.`)}if(void 0===s)return;s.walk((e=>{if(!e.type||"function"!==e.type)return;const r=e.value.toLowerCase();"hsl"!==r&&"hsla"!==r&&"rgb"!==r&&"rgba"!==r||onCSSFunction(e)}));const a=String(s);a!==o&&(n.cloneBefore({value:a}),null!=e&&e.preserve||n.remove())}});basePlugin.postcss=!0;const postcssPlugin=r=>{const n=Object.assign({preserve:!1,enableProgressiveCustomProperties:!0},r);return n.enableProgressiveCustomProperties&&n.preserve?{postcssPlugin:"postcss-color-function",plugins:[e(),basePlugin(n)]}:basePlugin(n)};postcssPlugin.postcss=!0,module.exports=postcssPlugin;
package/dist/index.d.ts CHANGED
@@ -3,7 +3,8 @@ import type { PluginCreator } from 'postcss';
3
3
  export type pluginOptions = {
4
4
  /** Preserve the original notation. default: false */
5
5
  preserve?: boolean;
6
+ /** Enable "@csstools/postcss-progressive-custom-properties". default: true */
7
+ enableProgressiveCustomProperties?: boolean;
6
8
  };
7
- /** Transform lab() and lch() functions in CSS. */
8
9
  declare const postcssPlugin: PluginCreator<pluginOptions>;
9
10
  export default postcssPlugin;
package/dist/index.mjs CHANGED
@@ -1 +1 @@
1
- import e from"postcss-value-parser";function onCSSFunction(e){const r=e.value.toLowerCase();if(!needsConversion("rgb"===r||"rgba"===r,e.nodes))return;const n=convertOldSyntaxToNewSyntaxBeforeTransform(e.nodes).slice().filter((e=>"comment"!==e.type&&"space"!==e.type));let t=null;if("hsl"===r||"hsla"===r?t=hslFunctionContents(n):"rgb"!==r&&"rgba"!==r||(t=rgbFunctionContents(n)),!t)return;if(n.length>3&&(!t.slash||!t.alpha))return;transformAlpha(e,t.slash,t.alpha);const[o,u]=channelNodes(t);e.nodes.splice(e.nodes.indexOf(o)+1,0,{sourceIndex:0,sourceEndIndex:1,value:",",type:"div",before:"",after:""}),e.nodes.splice(e.nodes.indexOf(u)+1,0,{sourceIndex:0,sourceEndIndex:1,value:",",type:"div",before:"",after:""})}function isNumericNode(r){if(!r||"word"!==r.type)return!1;if(!canParseAsUnit(r))return!1;const n=e.unit(r.value);return!!n&&!!n.number}function isNumericNodeHueLike(r){if(!r||"word"!==r.type)return!1;if(!canParseAsUnit(r))return!1;const n=e.unit(r.value);if(!n)return!1;const t=n.unit.toLowerCase();return!!n.number&&("deg"===t||"grad"===t||"rad"===t||"turn"===t||""===n.unit)}function isNumericNodePercentageOrNumber(r){if(!r||"word"!==r.type)return!1;if(!canParseAsUnit(r))return!1;const n=e.unit(r.value);return!!n&&("%"===n.unit||""===n.unit)}function isCalcNode(e){return e&&"function"===e.type&&"calc"===e.value.toLowerCase()}function isVarNode(e){return e&&"function"===e.type&&"var"===e.value.toLowerCase()}function isSlashNode(e){return e&&"div"===e.type&&"/"===e.value}function hslFunctionContents(r){if(!isNumericNodeHueLike(r[0]))return null;if(!isNumericNodePercentageOrNumber(r[1]))return null;if(!isNumericNodePercentageOrNumber(r[2]))return null;const n={h:e.unit(r[0].value),hNode:r[0],s:e.unit(r[1].value),sNode:r[1],l:e.unit(r[2].value),lNode:r[2]};return normalizeHueNode(n.h),""!==n.h.unit?null:(n.hNode.value=n.h.number,isSlashNode(r[3])&&(n.slash=r[3]),(isNumericNodePercentageOrNumber(r[4])||isCalcNode(r[4])||isVarNode(r[4]))&&(n.alpha=r[4]),n)}function rgbFunctionContents(r){if(!isNumericNodePercentageOrNumber(r[0]))return null;if(!isNumericNodePercentageOrNumber(r[1]))return null;if(!isNumericNodePercentageOrNumber(r[2]))return null;const n={r:e.unit(r[0].value),rNode:r[0],g:e.unit(r[1].value),gNode:r[1],b:e.unit(r[2].value),bNode:r[2]};return"%"===n.r.unit&&(n.r.number=String(Math.floor(Number(n.r.number)/100*255)),n.rNode.value=n.r.number),"%"===n.g.unit&&(n.g.number=String(Math.floor(Number(n.g.number)/100*255)),n.gNode.value=n.g.number),"%"===n.b.unit&&(n.b.number=String(Math.floor(Number(n.b.number)/100*255)),n.bNode.value=n.b.number),isSlashNode(r[3])&&(n.slash=r[3]),(isNumericNodePercentageOrNumber(r[4])||isCalcNode(r[4])||isVarNode(r[4]))&&(n.alpha=r[4]),n}function isRgb(e){return void 0!==e.r}function channelNodes(e){return isRgb(e)?[e.rNode,e.gNode,e.bNode]:[e.hNode,e.sNode,e.lNode]}function transformAlpha(r,n,t){if("hsl"===r.value.toLowerCase()||"hsla"===r.value.toLowerCase()?r.value="hsl":"rgb"!==r.value.toLowerCase()&&"rgba"!==r.value.toLowerCase()||(r.value="rgb"),!n||!t)return;if("hsl"===r.value.toLowerCase()?r.value="hsla":r.value="rgba",n.value=",",n.before="",!isNumericNode(t))return;const o=e.unit(t.value);o&&"%"===o.unit&&(o.number=String(parseFloat(o.number)/100),t.value=String(o.number))}function normalizeHueNode(e){switch(e.unit.toLowerCase()){case"deg":return void(e.unit="");case"rad":return e.unit="",void(e.number=Math.round(180*parseFloat(e.number)/Math.PI).toString());case"grad":return e.unit="",void(e.number=Math.round(.9*parseFloat(e.number)).toString());case"turn":return e.unit="",void(e.number=Math.round(360*parseFloat(e.number)).toString())}}function canParseAsUnit(r){if(!r||!r.value)return!1;try{return!1!==e.unit(r.value)}catch(e){return!1}}function convertOldSyntaxToNewSyntaxBeforeTransform(e){let r=0;for(let n=0;n<e.length;n++){const t=e[n];if("div"===t.type&&","===t.value){if(r<2&&(t.value=" ",t.type="space"),2===r&&(t.value="/"),r>2)return;r++}}return e}function needsConversion(e,r){let n=!1,t=!1,o=!1;const u=r.slice().filter((e=>"comment"!==e.type&&"space"!==e.type));for(let a=0;a<u.length;a++){const s=u[a];if("word"===s.type&&"from"===s.value.toLowerCase())return!1;"div"!==s.type||","!==s.value?e&&"word"===s.type&&s.value.endsWith("%")?o=!0:a===r.length-1&&"word"===s.type&&s.value.endsWith("%")&&(t=!0):n=!0}return!(!n||!t&&!o)||!n}function hasSupportsAtRuleAncestor(e){let r=e.parent;for(;r;)if("atrule"===r.type){if("supports"===r.name&&-1!==r.params.toLowerCase().indexOf("(color: rgb(0 0 0 / 0.5)) and (color: hsl(0 0% 0% / 0.5))"))return!0;r=r.parent}else r=r.parent;return!1}function hasFallback(e){const r=e.parent;if(!r)return!1;const n=e.prop.toLowerCase(),t=r.index(e);for(let e=0;e<t;e++){const t=r.nodes[e];if("decl"===t.type&&t.prop.toLowerCase()===n)return!0}return!1}const postcssPlugin=r=>{const n="preserve"in Object(r)&&Boolean(r.preserve);return{postcssPlugin:"postcss-color-functional-notation",Declaration:(r,{result:t,postcss:o})=>{const u=r.value,a=u.toLowerCase();if(!(a.includes("rgb")||a.includes("rgba")||a.includes("hsl")||a.includes("hsla")))return;if(hasFallback(r))return;if(hasSupportsAtRuleAncestor(r))return;let s;try{s=e(u)}catch(e){r.warn(t,`Failed to parse value '${u}' as a hsl or rgb function. Leaving the original value intact.`)}if(void 0===s)return;s.walk((e=>{if(!e.type||"function"!==e.type)return;const r=e.value.toLowerCase();"hsl"!==r&&"hsla"!==r&&"rgb"!==r&&"rgba"!==r||onCSSFunction(e)}));const i=String(s);if(i!==u)if(n&&r.variable){const e=r.parent,n="(color: rgb(0 0 0 / 0.5)) and (color: hsl(0 0% 0% / 0.5))",t=o.atRule({name:"supports",params:n,source:r.source}),u=e.clone();u.removeAll(),u.append(r.clone()),t.append(u);let a=e,s=e.next();for(;a&&s&&"atrule"===s.type&&"supports"===s.name&&s.params===n;)a=s,s=s.next();a.after(t),r.replaceWith(r.clone({value:i}))}else n?r.cloneBefore({value:i}):r.replaceWith(r.clone({value:i}))}}};postcssPlugin.postcss=!0;export{postcssPlugin as default};
1
+ import e from"@csstools/postcss-progressive-custom-properties";import r from"postcss-value-parser";function onCSSFunction(e){const r=e.value.toLowerCase();if(!needsConversion("rgb"===r||"rgba"===r,e.nodes))return;const n=convertOldSyntaxToNewSyntaxBeforeTransform(e.nodes).slice().filter((e=>"comment"!==e.type&&"space"!==e.type));let t=null;if("hsl"===r||"hsla"===r?t=hslFunctionContents(n):"rgb"!==r&&"rgba"!==r||(t=rgbFunctionContents(n)),!t)return;if(n.length>3&&(!t.slash||!t.alpha))return;transformAlpha(e,t.slash,t.alpha);const[o,u]=channelNodes(t);e.nodes.splice(e.nodes.indexOf(o)+1,0,{sourceIndex:0,sourceEndIndex:1,value:",",type:"div",before:"",after:""}),e.nodes.splice(e.nodes.indexOf(u)+1,0,{sourceIndex:0,sourceEndIndex:1,value:",",type:"div",before:"",after:""})}function isNumericNode(e){if(!e||"word"!==e.type)return!1;if(!canParseAsUnit(e))return!1;const n=r.unit(e.value);return!!n&&!!n.number}function isNumericNodeHueLike(e){if(!e||"word"!==e.type)return!1;if(!canParseAsUnit(e))return!1;const n=r.unit(e.value);if(!n)return!1;const t=n.unit.toLowerCase();return!!n.number&&("deg"===t||"grad"===t||"rad"===t||"turn"===t||""===n.unit)}function isNumericNodePercentageOrNumber(e){if(!e||"word"!==e.type)return!1;if(!canParseAsUnit(e))return!1;const n=r.unit(e.value);return!!n&&("%"===n.unit||""===n.unit)}function isCalcNode(e){return e&&"function"===e.type&&"calc"===e.value.toLowerCase()}function isVarNode(e){return e&&"function"===e.type&&"var"===e.value.toLowerCase()}function isSlashNode(e){return e&&"div"===e.type&&"/"===e.value}function hslFunctionContents(e){if(!isNumericNodeHueLike(e[0]))return null;if(!isNumericNodePercentageOrNumber(e[1]))return null;if(!isNumericNodePercentageOrNumber(e[2]))return null;const n={h:r.unit(e[0].value),hNode:e[0],s:r.unit(e[1].value),sNode:e[1],l:r.unit(e[2].value),lNode:e[2]};return normalizeHueNode(n.h),""!==n.h.unit?null:(n.hNode.value=n.h.number,isSlashNode(e[3])&&(n.slash=e[3]),(isNumericNodePercentageOrNumber(e[4])||isCalcNode(e[4])||isVarNode(e[4]))&&(n.alpha=e[4]),n)}function rgbFunctionContents(e){if(!isNumericNodePercentageOrNumber(e[0]))return null;if(!isNumericNodePercentageOrNumber(e[1]))return null;if(!isNumericNodePercentageOrNumber(e[2]))return null;const n={r:r.unit(e[0].value),rNode:e[0],g:r.unit(e[1].value),gNode:e[1],b:r.unit(e[2].value),bNode:e[2]};return"%"===n.r.unit&&(n.r.number=String(Math.floor(Number(n.r.number)/100*255)),n.rNode.value=n.r.number),"%"===n.g.unit&&(n.g.number=String(Math.floor(Number(n.g.number)/100*255)),n.gNode.value=n.g.number),"%"===n.b.unit&&(n.b.number=String(Math.floor(Number(n.b.number)/100*255)),n.bNode.value=n.b.number),isSlashNode(e[3])&&(n.slash=e[3]),(isNumericNodePercentageOrNumber(e[4])||isCalcNode(e[4])||isVarNode(e[4]))&&(n.alpha=e[4]),n}function isRgb(e){return void 0!==e.r}function channelNodes(e){return isRgb(e)?[e.rNode,e.gNode,e.bNode]:[e.hNode,e.sNode,e.lNode]}function transformAlpha(e,n,t){if("hsl"===e.value.toLowerCase()||"hsla"===e.value.toLowerCase()?e.value="hsl":"rgb"!==e.value.toLowerCase()&&"rgba"!==e.value.toLowerCase()||(e.value="rgb"),!n||!t)return;if("hsl"===e.value.toLowerCase()?e.value="hsla":e.value="rgba",n.value=",",n.before="",!isNumericNode(t))return;const o=r.unit(t.value);o&&"%"===o.unit&&(o.number=String(parseFloat(o.number)/100),t.value=String(o.number))}function normalizeHueNode(e){switch(e.unit.toLowerCase()){case"deg":return void(e.unit="");case"rad":return e.unit="",void(e.number=Math.round(180*parseFloat(e.number)/Math.PI).toString());case"grad":return e.unit="",void(e.number=Math.round(.9*parseFloat(e.number)).toString());case"turn":return e.unit="",void(e.number=Math.round(360*parseFloat(e.number)).toString())}}function canParseAsUnit(e){if(!e||!e.value)return!1;try{return!1!==r.unit(e.value)}catch(e){return!1}}function convertOldSyntaxToNewSyntaxBeforeTransform(e){let r=0;for(let n=0;n<e.length;n++){const t=e[n];if("div"===t.type&&","===t.value){if(r<2&&(t.value=" ",t.type="space"),2===r&&(t.value="/"),r>2)return[];r++}}return e}function needsConversion(e,r){let n=!1,t=!1,o=!1;const u=r.slice().filter((e=>"comment"!==e.type&&"space"!==e.type));for(let s=0;s<u.length;s++){const a=u[s];if("word"===a.type&&"from"===a.value.toLowerCase())return!1;"div"!==a.type||","!==a.value?e&&"word"===a.type&&a.value.endsWith("%")?o=!0:s===r.length-1&&"word"===a.type&&a.value.endsWith("%")&&(t=!0):n=!0}return!(!n||!t&&!o)||!n}const n=/(\(color: rgb(a?)\(0 0 0 \/ 0)|(\(color: hsl(a?)\(0 0% 0% \/ 0)/i;function hasSupportsAtRuleAncestor(e){let r=e.parent;for(;r;)if("atrule"===r.type){if("supports"===r.name&&n.test(r.params))return!0;r=r.parent}else r=r.parent;return!1}function hasFallback(e){const r=e.parent;if(!r)return!1;const n=e.prop.toLowerCase(),t=r.index(e);for(let e=0;e<t;e++){const t=r.nodes[e];if("decl"===t.type&&t.prop.toLowerCase()===n)return!0}return!1}const basePlugin=e=>({postcssPlugin:"postcss-color-function",Declaration:(n,{result:t})=>{const o=n.value,u=o.toLowerCase();if(!(u.includes("rgb")||u.includes("rgba")||u.includes("hsl")||u.includes("hsla")))return;if(hasFallback(n))return;if(hasSupportsAtRuleAncestor(n))return;let s;try{s=r(o)}catch(e){n.warn(t,`Failed to parse value '${o}' as a hsl or rgb function. Leaving the original value intact.`)}if(void 0===s)return;s.walk((e=>{if(!e.type||"function"!==e.type)return;const r=e.value.toLowerCase();"hsl"!==r&&"hsla"!==r&&"rgb"!==r&&"rgba"!==r||onCSSFunction(e)}));const a=String(s);a!==o&&(n.cloneBefore({value:a}),null!=e&&e.preserve||n.remove())}});basePlugin.postcss=!0;const postcssPlugin=r=>{const n=Object.assign({preserve:!1,enableProgressiveCustomProperties:!0},r);return n.enableProgressiveCustomProperties&&n.preserve?{postcssPlugin:"postcss-color-function",plugins:[e(),basePlugin(n)]}:basePlugin(n)};postcssPlugin.postcss=!0;export{postcssPlugin as default};
package/package.json CHANGED
@@ -1,13 +1,19 @@
1
1
  {
2
2
  "name": "postcss-color-functional-notation",
3
3
  "description": "Use space and slash separated color notation in CSS",
4
- "version": "5.0.2",
4
+ "version": "6.0.0",
5
5
  "author": "Jonathan Neal <jonathantneal@hotmail.com>",
6
- "license": "CC0-1.0",
7
- "funding": {
8
- "type": "opencollective",
9
- "url": "https://opencollective.com/csstools"
10
- },
6
+ "license": "MIT-0",
7
+ "funding": [
8
+ {
9
+ "type": "github",
10
+ "url": "https://github.com/sponsors/csstools"
11
+ },
12
+ {
13
+ "type": "opencollective",
14
+ "url": "https://opencollective.com/csstools"
15
+ }
16
+ ],
11
17
  "engines": {
12
18
  "node": "^14 || ^16 || >=18"
13
19
  },
@@ -29,22 +35,21 @@
29
35
  "dist"
30
36
  ],
31
37
  "dependencies": {
38
+ "@csstools/postcss-progressive-custom-properties": "^3.0.0",
32
39
  "postcss-value-parser": "^4.2.0"
33
40
  },
34
41
  "peerDependencies": {
35
42
  "postcss": "^8.4"
36
43
  },
44
+ "devDependencies": {
45
+ "@csstools/postcss-tape": "*"
46
+ },
37
47
  "scripts": {
38
- "prebuild": "npm run clean",
39
48
  "build": "rollup -c ../../rollup/default.mjs",
40
- "clean": "node -e \"fs.rmSync('./dist', { recursive: true, force: true }); fs.mkdirSync('./dist');\"",
41
49
  "docs": "node ../../.github/bin/generate-docs/install.mjs",
42
- "lint": "npm run lint:eslint && npm run lint:package-json",
43
- "lint:eslint": "eslint ./src --ext .js --ext .ts --ext .mjs --no-error-on-unmatched-pattern",
44
- "lint:package-json": "node ../../.github/bin/format-package-json.mjs",
45
- "prepublishOnly": "npm run clean && npm run build && npm run test",
46
- "test": "node .tape.mjs && npm run test:exports",
47
- "test:exports": "node ./test/_import.mjs && node ./test/_require.cjs",
50
+ "lint": "node ../../.github/bin/format-package-json.mjs",
51
+ "prepublishOnly": "npm run build && npm run test",
52
+ "test": "node .tape.mjs && node ./test/_import.mjs && node ./test/_require.cjs",
48
53
  "test:rewrite-expects": "REWRITE_EXPECTS=true node .tape.mjs"
49
54
  },
50
55
  "homepage": "https://github.com/csstools/postcss-plugins/tree/main/plugins/postcss-color-functional-notation#readme",