Skip to content

Commit 39d250b

Browse files
authored
postcss-hwb-function : maintenance (#1289)
1 parent d821470 commit 39d250b

17 files changed

+211
-93
lines changed

package-lock.json

+2-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

plugin-packs/postcss-preset-env/test/progressive-custom-properties.expect.css

+2-1
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,8 @@
4040
}
4141
}
4242

43-
@supports (color: hwb(0 0% 0%)) {:root {
43+
@supports (color: hwb(0 0% 0%)) {
44+
:root {
4445

4546
--hwb: hwb(20 10% 10%);
4647
}
+33-21
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,30 @@
1-
# PostCSS HWB Function [<img src="https://postcss.github.io/postcss/logo.svg" alt="PostCSS Logo" width="90" height="90" align="right">][postcss]
1+
# PostCSS HWB Function [<img src="https://postcss.github.io/postcss/logo.svg" alt="PostCSS Logo" width="90" height="90" align="right">][PostCSS]
22

3-
[<img alt="npm version" src="https://img.shields.io/npm/v/@csstools/postcss-hwb-function.svg" height="20">][npm-url]
4-
[<img alt="Build Status" src="https://github.com/csstools/postcss-plugins/workflows/test/badge.svg" height="20">][cli-url]
5-
[<img alt="Discord" src="https://shields.io/badge/Discord-5865F2?logo=discord&logoColor=white">][discord]
6-
<br><br>
7-
[<img alt="Baseline Status" src="https://cssdb.org/images/badges-baseline/hwb-function.svg" height="20">][css-url]
8-
[<img alt="CSS Standard Status" src="https://cssdb.org/images/badges/hwb-function.svg" height="20">][css-url]
3+
[<img alt="npm version" src="https://img.shields.io/npm/v/@csstools/postcss-hwb-function.svg" height="20">][npm-url] [<img alt="Build Status" src="https://github.com/csstools/postcss-plugins/workflows/test/badge.svg" height="20">][cli-url] [<img alt="Discord" src="https://shields.io/badge/Discord-5865F2?logo=discord&logoColor=white">][discord]<br><br>[<img alt="Baseline Status" src="https://cssdb.org/images/badges-baseline/hwb-function.svg" height="20">][css-url] [<img alt="CSS Standard Status" src="https://cssdb.org/images/badges/hwb-function.svg" height="20">][css-url]
94

10-
[PostCSS HWB Function] lets you use `hwb` color functions in
11-
CSS, following the [CSS Color] specification.
5+
```bash
6+
npm install @csstools/postcss-hwb-function --save-dev
7+
```
8+
9+
[PostCSS HWB Function] lets you use the `hwb()` color function in CSS, following [CSS Color Module 4].
1210

1311
```pcss
1412
a {
1513
color: hwb(194 0% 0%);
14+
}
15+
16+
b {
1617
color: hwb(194 0% 0% / .5);
1718
}
1819
1920
/* becomes */
2021
2122
a {
22-
color: rgb(0, 195, 255);
23-
color: rgba(0, 195, 255, .5);
23+
color: rgb(0, 196, 255);
24+
}
25+
26+
b {
27+
color: rgba(0, 196, 255, 0.5);
2428
}
2529
```
2630

@@ -39,21 +43,26 @@ const postcss = require('postcss');
3943
const postcssHWBFunction = require('@csstools/postcss-hwb-function');
4044

4145
postcss([
42-
postcssHWBFunction(/* pluginOptions */)
46+
postcssHWBFunction(/* pluginOptions */)
4347
]).process(YOUR_CSS /*, processOptions */);
4448
```
4549

4650
[PostCSS HWB Function] runs in all Node environments, with special
4751
instructions for:
4852

49-
| [Node](INSTALL.md#node) | [PostCSS CLI](INSTALL.md#postcss-cli) | [Webpack](INSTALL.md#webpack) | [Gulp](INSTALL.md#gulp) | [Grunt](INSTALL.md#grunt) |
50-
| --- | --- | --- | --- | --- |
53+
- [Node](INSTALL.md#node)
54+
- [PostCSS CLI](INSTALL.md#postcss-cli)
55+
- [PostCSS Load Config](INSTALL.md#postcss-load-config)
56+
- [Webpack](INSTALL.md#webpack)
57+
- [Next.js](INSTALL.md#nextjs)
58+
- [Gulp](INSTALL.md#gulp)
59+
- [Grunt](INSTALL.md#grunt)
5160

5261
## Options
5362

5463
### preserve
5564

56-
The `preserve` option determines whether the original functional color notation
65+
The `preserve` option determines whether the original notation
5766
is preserved. By default, it is not preserved.
5867

5968
```js
@@ -63,15 +72,21 @@ postcssHWBFunction({ preserve: true })
6372
```pcss
6473
a {
6574
color: hwb(194 0% 0%);
75+
}
76+
77+
b {
6678
color: hwb(194 0% 0% / .5);
6779
}
6880
6981
/* becomes */
7082
7183
a {
72-
color: rgb(0, 195, 255);
84+
color: rgb(0, 196, 255);
7385
color: hwb(194 0% 0%);
74-
color: rgba(0, 195, 255, .5);
86+
}
87+
88+
b {
89+
color: rgba(0, 196, 255, 0.5);
7590
color: hwb(194 0% 0% / .5);
7691
}
7792
```
@@ -81,9 +96,6 @@ a {
8196
[discord]: https://discord.gg/bUadyRwkJS
8297
[npm-url]: https://www.npmjs.com/package/@csstools/postcss-hwb-function
8398

84-
[CSS Color]: https://drafts.csswg.org/css-color/#the-hwb-notation
85-
[Gulp PostCSS]: https://github.com/postcss/gulp-postcss
86-
[Grunt PostCSS]: https://github.com/nDmitry/grunt-postcss
8799
[PostCSS]: https://github.com/postcss/postcss
88-
[PostCSS Loader]: https://github.com/postcss/postcss-loader
89100
[PostCSS HWB Function]: https://github.com/csstools/postcss-plugins/tree/main/plugins/postcss-hwb-function
101+
[CSS Color Module 4]: https://www.w3.org/TR/css-color-4/#the-hwb-notation
+1-1
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
"use strict";var e=require("@csstools/css-color-parser"),t=require("@csstools/css-parser-algorithms"),r=require("@csstools/css-tokenizer");const s="(color: hwb(0 0% 0%))";function hasSupportsAtRuleAncestor(e){let t=e.parent;for(;t;)if("atrule"===t.type){if("supports"===t.name.toLowerCase()&&t.params.includes(s))return!0;t=t.parent}else t=t.parent;return!1}function hasFallback(e){const t=e.parent;if(!t)return!1;const r=e.prop.toLowerCase(),s=t.index(e);for(let e=0;e<s;e++){const s=t.nodes[e];if("decl"===s.type&&s.prop.toLowerCase()===r)return!0}return!1}const o=/hwb\(/i,a=/^hwb$/i,postcssPlugin=n=>{const l="preserve"in Object(n)&&Boolean(n?.preserve);return{postcssPlugin:"postcss-hwb-function",Declaration:(n,{postcss:p})=>{const c=n.value;if(!o.test(c))return;if(l&&hasSupportsAtRuleAncestor(n))return;if(hasFallback(n))return;const u=t.replaceComponentValues(t.parseCommaSeparatedListOfComponentValues(r.tokenize({css:c})),(r=>{if(!t.isFunctionNode(r)||!a.test(r.getName()))return;const s=e.color(r);return s&&!(s.syntaxFlags.has(e.SyntaxFlag.Experimental)||s.syntaxFlags.has(e.SyntaxFlag.HasNoneKeywords)||s.syntaxFlags.has(e.SyntaxFlag.RelativeColorSyntax))?e.serializeRGB(s):void 0})),i=t.stringify(u);if(i!==c)if(n.variable&&l&&n.parent){const e=n.parent,t=p.atRule({name:"supports",params:s,source:n.source}),r=e.clone();r.removeAll(),r.append(n.clone()),t.append(r),insertAtSupportsAfterCorrectRule(t,e,s),n.replaceWith(n.clone({value:i}))}else l?n.cloneBefore({value:i}):n.replaceWith(n.clone({value:i}))}}};function insertAtSupportsAfterCorrectRule(e,t,r){let s=t,o=t.next();for(;s&&o&&"atrule"===o.type&&"supports"===o.name.toLowerCase()&&o.params===r;)s=o,o=o.next();s.after(e)}postcssPlugin.postcss=!0,module.exports=postcssPlugin;
1+
"use strict";var s=require("@csstools/postcss-progressive-custom-properties"),e=require("@csstools/css-tokenizer"),r=require("@csstools/css-color-parser"),t=require("@csstools/css-parser-algorithms");function hasFallback(s){const e=s.parent;if(!e)return!1;const r=s.prop.toLowerCase(),t=e.index(s);for(let s=0;s<t;s++){const t=e.nodes[s];if("decl"===t.type&&t.prop.toLowerCase()===r)return!0}return!1}function hasSupportsAtRuleAncestor(s){let e=s.parent;for(;e;)if("atrule"===e.type){if("supports"===e.name.toLowerCase()&&e.params.includes("(color: hwb(0 0% 0%))"))return!0;e=e.parent}else e=e.parent;return!1}const o=/hwb\(/i,n=/^hwb$/i,basePlugin=s=>({postcssPlugin:"postcss-hwb-function",Declaration:a=>{const i=a.value;if(!o.test(i))return;if(hasFallback(a))return;if(hasSupportsAtRuleAncestor(a))return;const c=t.replaceComponentValues(t.parseCommaSeparatedListOfComponentValues(e.tokenize({css:i})),(s=>{if(!t.isFunctionNode(s)||!n.test(s.getName()))return;const e=r.color(s);return e&&!(e.syntaxFlags.has(r.SyntaxFlag.Experimental)||e.syntaxFlags.has(r.SyntaxFlag.HasNoneKeywords)||e.syntaxFlags.has(r.SyntaxFlag.RelativeColorSyntax))?r.serializeRGB(e):void 0})),l=t.stringify(c);l!==i&&(a.cloneBefore({value:l}),s?.preserve||a.remove())}});basePlugin.postcss=!0;const postcssPlugin=e=>{const r=Object.assign({preserve:!1,enableProgressiveCustomProperties:!0},e);return r.enableProgressiveCustomProperties&&r.preserve?{postcssPlugin:"postcss-hwb-function",plugins:[s(),basePlugin(r)]}:basePlugin(r)};postcssPlugin.postcss=!0,module.exports=postcssPlugin;

plugins/postcss-hwb-function/dist/index.d.ts

+8
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,17 @@
11
import type { PluginCreator } from 'postcss';
22

3+
/** postcss-hwb-function plugin options */
4+
export declare type basePluginOptions = {
5+
/** Preserve the original notation. default: false */
6+
preserve?: boolean;
7+
};
8+
39
/** postcss-hwb-function plugin options */
410
export declare type pluginOptions = {
511
/** Preserve the original notation. default: false */
612
preserve?: boolean;
13+
/** Enable "@csstools/postcss-progressive-custom-properties". default: true */
14+
enableProgressiveCustomProperties?: boolean;
715
};
816

917
/** Transform hwb() functions in CSS. */
+1-1
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
import{color as e,SyntaxFlag as t,serializeRGB as r}from"@csstools/css-color-parser";import{replaceComponentValues as s,parseCommaSeparatedListOfComponentValues as o,isFunctionNode as n,stringify as a}from"@csstools/css-parser-algorithms";import{tokenize as p}from"@csstools/css-tokenizer";const l="(color: hwb(0 0% 0%))";function hasSupportsAtRuleAncestor(e){let t=e.parent;for(;t;)if("atrule"===t.type){if("supports"===t.name.toLowerCase()&&t.params.includes(l))return!0;t=t.parent}else t=t.parent;return!1}function hasFallback(e){const t=e.parent;if(!t)return!1;const r=e.prop.toLowerCase(),s=t.index(e);for(let e=0;e<s;e++){const s=t.nodes[e];if("decl"===s.type&&s.prop.toLowerCase()===r)return!0}return!1}const c=/hwb\(/i,u=/^hwb$/i,postcssPlugin=i=>{const f="preserve"in Object(i)&&Boolean(i?.preserve);return{postcssPlugin:"postcss-hwb-function",Declaration:(i,{postcss:m})=>{const h=i.value;if(!c.test(h))return;if(f&&hasSupportsAtRuleAncestor(i))return;if(hasFallback(i))return;const v=s(o(p({css:h})),(s=>{if(!n(s)||!u.test(s.getName()))return;const o=e(s);return o&&!(o.syntaxFlags.has(t.Experimental)||o.syntaxFlags.has(t.HasNoneKeywords)||o.syntaxFlags.has(t.RelativeColorSyntax))?r(o):void 0})),d=a(v);if(d!==h)if(i.variable&&f&&i.parent){const e=i.parent,t=m.atRule({name:"supports",params:l,source:i.source}),r=e.clone();r.removeAll(),r.append(i.clone()),t.append(r),insertAtSupportsAfterCorrectRule(t,e,l),i.replaceWith(i.clone({value:d}))}else f?i.cloneBefore({value:d}):i.replaceWith(i.clone({value:d}))}}};function insertAtSupportsAfterCorrectRule(e,t,r){let s=t,o=t.next();for(;s&&o&&"atrule"===o.type&&"supports"===o.name.toLowerCase()&&o.params===r;)s=o,o=o.next();s.after(e)}postcssPlugin.postcss=!0;export{postcssPlugin as default};
1+
import s from"@csstools/postcss-progressive-custom-properties";import{tokenize as e}from"@csstools/css-tokenizer";import{color as r,SyntaxFlag as t,serializeRGB as o}from"@csstools/css-color-parser";import{replaceComponentValues as n,parseCommaSeparatedListOfComponentValues as a,isFunctionNode as p,stringify as c}from"@csstools/css-parser-algorithms";function hasFallback(s){const e=s.parent;if(!e)return!1;const r=s.prop.toLowerCase(),t=e.index(s);for(let s=0;s<t;s++){const t=e.nodes[s];if("decl"===t.type&&t.prop.toLowerCase()===r)return!0}return!1}function hasSupportsAtRuleAncestor(s){let e=s.parent;for(;e;)if("atrule"===e.type){if("supports"===e.name.toLowerCase()&&e.params.includes("(color: hwb(0 0% 0%))"))return!0;e=e.parent}else e=e.parent;return!1}const i=/hwb\(/i,l=/^hwb$/i,basePlugin=s=>({postcssPlugin:"postcss-hwb-function",Declaration:u=>{const f=u.value;if(!i.test(f))return;if(hasFallback(u))return;if(hasSupportsAtRuleAncestor(u))return;const m=n(a(e({css:f})),(s=>{if(!p(s)||!l.test(s.getName()))return;const e=r(s);return e&&!(e.syntaxFlags.has(t.Experimental)||e.syntaxFlags.has(t.HasNoneKeywords)||e.syntaxFlags.has(t.RelativeColorSyntax))?o(e):void 0})),h=c(m);h!==f&&(u.cloneBefore({value:h}),s?.preserve||u.remove())}});basePlugin.postcss=!0;const postcssPlugin=e=>{const r=Object.assign({preserve:!1,enableProgressiveCustomProperties:!0},e);return r.enableProgressiveCustomProperties&&r.preserve?{postcssPlugin:"postcss-hwb-function",plugins:[s(),basePlugin(r)]}:basePlugin(r)};postcssPlugin.postcss=!0;export{postcssPlugin as default};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
<!-- Available Variables: -->
2+
<!-- <humanReadableName> PostCSS Your Plugin -->
3+
<!-- <exportName> postcssYourPlugin -->
4+
<!-- <packageName> @csstools/postcss-your-plugin -->
5+
<!-- <packageVersion> 1.0.0 -->
6+
<!-- <packagePath> plugins/postcss-your-plugin -->
7+
<!-- <cssdbId> your-feature -->
8+
<!-- <specUrl> https://www.w3.org/TR/css-color-4/#funcdef-color -->
9+
<!-- <example.css> file contents for examples/example.css -->
10+
<!-- <header> -->
11+
<!-- <usage> usage instructions -->
12+
<!-- <envSupport> -->
13+
<!-- <corsWarning> -->
14+
<!-- <linkList> -->
15+
<!-- <parallelBuildsNotice> -->
16+
<!-- to generate : npm run docs -->
17+
18+
<header>
19+
20+
[<humanReadableName>] lets you use the `hwb()` color function in CSS, following [CSS Color Module 4].
21+
22+
```pcss
23+
<example.css>
24+
25+
/* becomes */
26+
27+
<example.expect.css>
28+
```
29+
30+
<usage>
31+
32+
<envSupport>
33+
34+
## Options
35+
36+
### preserve
37+
38+
The `preserve` option determines whether the original notation
39+
is preserved. By default, it is not preserved.
40+
41+
```js
42+
<exportName>({ preserve: true })
43+
```
44+
45+
```pcss
46+
<example.css>
47+
48+
/* becomes */
49+
50+
<example.preserve-true.expect.css>
51+
```
52+
53+
<linkList>
54+
[CSS Color Module 4]: <specUrl>

plugins/postcss-hwb-function/package.json

+18-12
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,17 @@
22
"name": "@csstools/postcss-hwb-function",
33
"description": "Use hwb() color functions in CSS",
44
"version": "3.0.8",
5-
"author": "Jonathan Neal <jonathantneal@hotmail.com>",
5+
"contributors": [
6+
{
7+
"name": "Antonio Laguna",
8+
"email": "antonio@laguna.es",
9+
"url": "https://antonio.laguna.es"
10+
},
11+
{
12+
"name": "Romain Menke",
13+
"email": "romainmenke@gmail.com"
14+
}
15+
],
616
"license": "MIT-0",
717
"funding": [
818
{
@@ -33,15 +43,15 @@
3343
},
3444
"files": [
3545
"CHANGELOG.md",
36-
"INSTALL.md",
3746
"LICENSE.md",
3847
"README.md",
3948
"dist"
4049
],
4150
"dependencies": {
4251
"@csstools/css-color-parser": "^1.5.1",
4352
"@csstools/css-parser-algorithms": "^2.5.0",
44-
"@csstools/css-tokenizer": "^2.2.3"
53+
"@csstools/css-tokenizer": "^2.2.3",
54+
"@csstools/postcss-progressive-custom-properties": "^3.0.3"
4555
},
4656
"peerDependencies": {
4757
"postcss": "^8.4"
@@ -51,7 +61,7 @@
5161
},
5262
"scripts": {
5363
"build": "rollup -c ../../rollup/default.mjs",
54-
"docs": "node ../../.github/bin/generate-docs/install.mjs",
64+
"docs": "node ../../.github/bin/generate-docs/install.mjs && node ../../.github/bin/generate-docs/readme.mjs",
5565
"lint": "node ../../.github/bin/format-package-json.mjs",
5666
"prepublishOnly": "npm run build && npm run test",
5767
"test": "node --test",
@@ -66,24 +76,20 @@
6676
"bugs": "https://github.com/csstools/postcss-plugins/issues",
6777
"keywords": [
6878
"color",
69-
"comma",
7079
"css",
7180
"design",
72-
"functional",
73-
"hsl",
74-
"hsla",
7581
"hwb",
7682
"notation",
7783
"postcss",
7884
"postcss-plugin",
79-
"rgb",
80-
"rgba",
81-
"space",
8285
"syntax"
8386
],
8487
"csstools": {
88+
"cssdbId": "hwb-function",
8589
"exportName": "postcssHWBFunction",
86-
"humanReadableName": "PostCSS HWB Function"
90+
"humanReadableName": "PostCSS HWB Function",
91+
"specUrl": "https://www.w3.org/TR/css-color-4/#the-hwb-notation"
92+
8793
},
8894
"volta": {
8995
"extends": "../../package.json"

plugins/postcss-hwb-function/src/has-supports-at-rule-ancestor.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import type { Node, AtRule } from 'postcss';
22

3-
export const atSupportsHwbParams = '(color: hwb(0 0% 0%))';
3+
const atSupportsHwbParams = '(color: hwb(0 0% 0%))';
44

55
export function hasSupportsAtRuleAncestor(node: Node): boolean {
66
let parent = node.parent;

0 commit comments

Comments
 (0)