postcss-pseudo-where-fallback 0.4.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/LICENSE +21 -0
- package/README.md +185 -0
- package/dist/index.cjs +92 -0
- package/dist/index.mjs +63 -0
- package/package.json +54 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Zoli Szabó
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,185 @@
|
|
|
1
|
+
# postcss-pseudo-where-fallback
|
|
2
|
+
|
|
3
|
+
A PostCSS plugin that provides fallbacks for the `:where()` CSS pseudo-class selector.
|
|
4
|
+
|
|
5
|
+
## Why?
|
|
6
|
+
|
|
7
|
+
The `:where()` pseudo-class is a modern CSS feature that allows grouping selectors with zero specificity. However, older browsers don't support it. This plugin automatically generates fallback selectors for better browser compatibility.
|
|
8
|
+
|
|
9
|
+
## Installation
|
|
10
|
+
|
|
11
|
+
```bash
|
|
12
|
+
npm install postcss-pseudo-where-fallback --save-dev
|
|
13
|
+
```
|
|
14
|
+
|
|
15
|
+
## Usage
|
|
16
|
+
|
|
17
|
+
### PostCSS Config
|
|
18
|
+
|
|
19
|
+
```js
|
|
20
|
+
// postcss.config.js
|
|
21
|
+
import postcssPseudoWhereFallback from 'postcss-pseudo-where-fallback';
|
|
22
|
+
|
|
23
|
+
export default {
|
|
24
|
+
plugins: [
|
|
25
|
+
postcssPseudoWhereFallback()
|
|
26
|
+
]
|
|
27
|
+
};
|
|
28
|
+
```
|
|
29
|
+
|
|
30
|
+
### With PostCSS CLI
|
|
31
|
+
|
|
32
|
+
```js
|
|
33
|
+
// postcss.config.cjs
|
|
34
|
+
module.exports = {
|
|
35
|
+
plugins: [
|
|
36
|
+
require('postcss-pseudo-where-fallback')()
|
|
37
|
+
]
|
|
38
|
+
};
|
|
39
|
+
```
|
|
40
|
+
|
|
41
|
+
### Programmatic Usage
|
|
42
|
+
|
|
43
|
+
```js
|
|
44
|
+
import postcss from 'postcss';
|
|
45
|
+
import postcssPseudoWhereFallback from 'postcss-pseudo-where-fallback';
|
|
46
|
+
|
|
47
|
+
const result = await postcss([
|
|
48
|
+
postcssPseudoWhereFallback()
|
|
49
|
+
]).process(css, { from: 'input.css', to: 'output.css' });
|
|
50
|
+
```
|
|
51
|
+
|
|
52
|
+
## Example
|
|
53
|
+
|
|
54
|
+
### Input
|
|
55
|
+
|
|
56
|
+
```css
|
|
57
|
+
:where(.foo, .bar) {
|
|
58
|
+
color: red;
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
h1:where(.title, .heading) {
|
|
62
|
+
font-size: 2rem;
|
|
63
|
+
}
|
|
64
|
+
```
|
|
65
|
+
|
|
66
|
+
### Output
|
|
67
|
+
|
|
68
|
+
```css
|
|
69
|
+
:where(.foo, .bar) {
|
|
70
|
+
color: red;
|
|
71
|
+
}
|
|
72
|
+
@supports not selector(:where(*)) {
|
|
73
|
+
.foo, .bar {
|
|
74
|
+
color: red;
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
h1:where(.title, .heading) {
|
|
79
|
+
font-size: 2rem;
|
|
80
|
+
}
|
|
81
|
+
@supports not selector(:where(*)) {
|
|
82
|
+
h1.title, h1.heading {
|
|
83
|
+
font-size: 2rem;
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
```
|
|
87
|
+
|
|
88
|
+
The plugin keeps the original `:where()` selector for modern browsers (which will use it with zero specificity), and adds a fallback wrapped in `@supports not selector(:where(*))` for older browsers that don't support `:where()`. This ensures:
|
|
89
|
+
|
|
90
|
+
- **Modern browsers**: Use the `:where()` selector with zero specificity
|
|
91
|
+
- **Older browsers with `@supports`**: Ignore the invalid `:where()` selector and use the fallback with normal specificity
|
|
92
|
+
- **Very old browsers** (no `@supports` support): Ignore both the `:where()` and `@supports` blocks, resulting in no styles (these are pre-2013 browsers)
|
|
93
|
+
|
|
94
|
+
## More Examples
|
|
95
|
+
|
|
96
|
+
### Selector Lists with Mixed Types
|
|
97
|
+
|
|
98
|
+
Input:
|
|
99
|
+
```css
|
|
100
|
+
a, :where(b) {
|
|
101
|
+
color: red;
|
|
102
|
+
}
|
|
103
|
+
```
|
|
104
|
+
|
|
105
|
+
Output:
|
|
106
|
+
```css
|
|
107
|
+
a, :where(b) {
|
|
108
|
+
color: red;
|
|
109
|
+
}
|
|
110
|
+
@supports not selector(:where(*)) {
|
|
111
|
+
b {
|
|
112
|
+
color: red;
|
|
113
|
+
}
|
|
114
|
+
}
|
|
115
|
+
```
|
|
116
|
+
|
|
117
|
+
Note: The fallback only includes expanded `:where()` selectors. Regular selectors like `a` are already valid and don't need to be repeated.
|
|
118
|
+
|
|
119
|
+
### Attribute Selectors
|
|
120
|
+
|
|
121
|
+
Input:
|
|
122
|
+
```css
|
|
123
|
+
input:where([type='button'], [type='submit'], [type='reset']) {
|
|
124
|
+
cursor: pointer;
|
|
125
|
+
}
|
|
126
|
+
```
|
|
127
|
+
|
|
128
|
+
Output:
|
|
129
|
+
```css
|
|
130
|
+
input:where([type='button'], [type='submit'], [type='reset']) {
|
|
131
|
+
cursor: pointer;
|
|
132
|
+
}
|
|
133
|
+
@supports not selector(:where(*)) {
|
|
134
|
+
input[type='button'], input[type='submit'], input[type='reset'] {
|
|
135
|
+
cursor: pointer;
|
|
136
|
+
}
|
|
137
|
+
}
|
|
138
|
+
```
|
|
139
|
+
|
|
140
|
+
### Complex Selectors
|
|
141
|
+
|
|
142
|
+
Input:
|
|
143
|
+
```css
|
|
144
|
+
.container :where(.foo, .bar) .item {
|
|
145
|
+
padding: 10px;
|
|
146
|
+
}
|
|
147
|
+
```
|
|
148
|
+
|
|
149
|
+
Output:
|
|
150
|
+
```css
|
|
151
|
+
.container :where(.foo, .bar) .item {
|
|
152
|
+
padding: 10px;
|
|
153
|
+
}
|
|
154
|
+
@supports not selector(:where(*)) {
|
|
155
|
+
.container .foo .item, .container .bar .item {
|
|
156
|
+
padding: 10px;
|
|
157
|
+
}
|
|
158
|
+
}
|
|
159
|
+
```
|
|
160
|
+
|
|
161
|
+
## Options
|
|
162
|
+
|
|
163
|
+
This plugin currently does not accept any options. Simply use it without arguments:
|
|
164
|
+
|
|
165
|
+
```js
|
|
166
|
+
postcssPluginPseudoWhereFallback()
|
|
167
|
+
```
|
|
168
|
+
|
|
169
|
+
## Browser Support
|
|
170
|
+
|
|
171
|
+
This plugin helps support browsers that don't have native `:where()` support, including:
|
|
172
|
+
|
|
173
|
+
- Internet Explorer 11
|
|
174
|
+
- Edge < 88
|
|
175
|
+
- Firefox < 78
|
|
176
|
+
- Chrome < 88
|
|
177
|
+
- Safari < 14
|
|
178
|
+
|
|
179
|
+
## Contributing
|
|
180
|
+
|
|
181
|
+
Contributions are welcome! Please feel free to submit a Pull Request.
|
|
182
|
+
|
|
183
|
+
## License
|
|
184
|
+
|
|
185
|
+
[MIT](./LICENSE)
|
package/dist/index.cjs
ADDED
|
@@ -0,0 +1,92 @@
|
|
|
1
|
+
var __create = Object.create;
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
+
var __getProtoOf = Object.getPrototypeOf;
|
|
6
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
7
|
+
var __export = (target, all) => {
|
|
8
|
+
for (var name in all)
|
|
9
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
10
|
+
};
|
|
11
|
+
var __copyProps = (to, from, except, desc) => {
|
|
12
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
13
|
+
for (let key of __getOwnPropNames(from))
|
|
14
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
15
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
16
|
+
}
|
|
17
|
+
return to;
|
|
18
|
+
};
|
|
19
|
+
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
|
|
20
|
+
// If the importer is in node compatibility mode or this is not an ESM
|
|
21
|
+
// file that has been converted to a CommonJS file using a Babel-
|
|
22
|
+
// compatible transform (i.e. "__esModule" has not been set), then set
|
|
23
|
+
// "default" to the CommonJS "module.exports" for node compatibility.
|
|
24
|
+
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
|
|
25
|
+
mod
|
|
26
|
+
));
|
|
27
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
28
|
+
var index_exports = {};
|
|
29
|
+
__export(index_exports, {
|
|
30
|
+
default: () => index_default
|
|
31
|
+
});
|
|
32
|
+
module.exports = __toCommonJS(index_exports);
|
|
33
|
+
var import_postcss = __toESM(require("postcss"), 1);
|
|
34
|
+
var import_postcss_selector_parser = __toESM(require("postcss-selector-parser"), 1);
|
|
35
|
+
const plugin = () => {
|
|
36
|
+
return {
|
|
37
|
+
postcssPlugin: "postcss-pseudo-where-fallback",
|
|
38
|
+
Once(root) {
|
|
39
|
+
const rulesToProcess = [];
|
|
40
|
+
root.walkRules((rule) => {
|
|
41
|
+
if (rule.selector && rule.selector.includes(":where(")) {
|
|
42
|
+
rulesToProcess.push(rule);
|
|
43
|
+
}
|
|
44
|
+
});
|
|
45
|
+
rulesToProcess.forEach((rule) => {
|
|
46
|
+
const fallbackSelectors = [];
|
|
47
|
+
(0, import_postcss_selector_parser.default)((selectors) => {
|
|
48
|
+
selectors.each((selector) => {
|
|
49
|
+
selector.walkPseudos((pseudo) => {
|
|
50
|
+
if (pseudo.value === ":where" && pseudo.nodes) {
|
|
51
|
+
const parent = pseudo.parent;
|
|
52
|
+
const index = parent.index(pseudo);
|
|
53
|
+
const prefix = parent.nodes.slice(0, index);
|
|
54
|
+
const suffix = parent.nodes.slice(index + 1);
|
|
55
|
+
pseudo.nodes.forEach((whereSelector) => {
|
|
56
|
+
let selectorString = "";
|
|
57
|
+
prefix.forEach((node) => {
|
|
58
|
+
selectorString += node.toString();
|
|
59
|
+
});
|
|
60
|
+
whereSelector.nodes.forEach((node, i) => {
|
|
61
|
+
const nodeStr = node.toString();
|
|
62
|
+
if (i === 0) {
|
|
63
|
+
selectorString += nodeStr.trimStart();
|
|
64
|
+
} else {
|
|
65
|
+
selectorString += nodeStr;
|
|
66
|
+
}
|
|
67
|
+
});
|
|
68
|
+
suffix.forEach((node) => {
|
|
69
|
+
selectorString += node.toString();
|
|
70
|
+
});
|
|
71
|
+
fallbackSelectors.push(selectorString);
|
|
72
|
+
});
|
|
73
|
+
}
|
|
74
|
+
});
|
|
75
|
+
});
|
|
76
|
+
}).processSync(rule.selector);
|
|
77
|
+
const fallbackRule = rule.clone({
|
|
78
|
+
selector: fallbackSelectors.map((s) => s.trim()).join(", ")
|
|
79
|
+
});
|
|
80
|
+
const fallbackSupports = import_postcss.default.atRule({
|
|
81
|
+
name: "supports",
|
|
82
|
+
params: "not selector(:where(*))",
|
|
83
|
+
source: rule.source
|
|
84
|
+
});
|
|
85
|
+
fallbackSupports.append(fallbackRule);
|
|
86
|
+
rule.after(fallbackSupports);
|
|
87
|
+
});
|
|
88
|
+
}
|
|
89
|
+
};
|
|
90
|
+
};
|
|
91
|
+
plugin.postcss = true;
|
|
92
|
+
var index_default = plugin;
|
package/dist/index.mjs
ADDED
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
import postcss from "postcss";
|
|
2
|
+
import selectorParser from "postcss-selector-parser";
|
|
3
|
+
const plugin = () => {
|
|
4
|
+
return {
|
|
5
|
+
postcssPlugin: "postcss-pseudo-where-fallback",
|
|
6
|
+
Once(root) {
|
|
7
|
+
const rulesToProcess = [];
|
|
8
|
+
root.walkRules((rule) => {
|
|
9
|
+
if (rule.selector && rule.selector.includes(":where(")) {
|
|
10
|
+
rulesToProcess.push(rule);
|
|
11
|
+
}
|
|
12
|
+
});
|
|
13
|
+
rulesToProcess.forEach((rule) => {
|
|
14
|
+
const fallbackSelectors = [];
|
|
15
|
+
selectorParser((selectors) => {
|
|
16
|
+
selectors.each((selector) => {
|
|
17
|
+
selector.walkPseudos((pseudo) => {
|
|
18
|
+
if (pseudo.value === ":where" && pseudo.nodes) {
|
|
19
|
+
const parent = pseudo.parent;
|
|
20
|
+
const index = parent.index(pseudo);
|
|
21
|
+
const prefix = parent.nodes.slice(0, index);
|
|
22
|
+
const suffix = parent.nodes.slice(index + 1);
|
|
23
|
+
pseudo.nodes.forEach((whereSelector) => {
|
|
24
|
+
let selectorString = "";
|
|
25
|
+
prefix.forEach((node) => {
|
|
26
|
+
selectorString += node.toString();
|
|
27
|
+
});
|
|
28
|
+
whereSelector.nodes.forEach((node, i) => {
|
|
29
|
+
const nodeStr = node.toString();
|
|
30
|
+
if (i === 0) {
|
|
31
|
+
selectorString += nodeStr.trimStart();
|
|
32
|
+
} else {
|
|
33
|
+
selectorString += nodeStr;
|
|
34
|
+
}
|
|
35
|
+
});
|
|
36
|
+
suffix.forEach((node) => {
|
|
37
|
+
selectorString += node.toString();
|
|
38
|
+
});
|
|
39
|
+
fallbackSelectors.push(selectorString);
|
|
40
|
+
});
|
|
41
|
+
}
|
|
42
|
+
});
|
|
43
|
+
});
|
|
44
|
+
}).processSync(rule.selector);
|
|
45
|
+
const fallbackRule = rule.clone({
|
|
46
|
+
selector: fallbackSelectors.map((s) => s.trim()).join(", ")
|
|
47
|
+
});
|
|
48
|
+
const fallbackSupports = postcss.atRule({
|
|
49
|
+
name: "supports",
|
|
50
|
+
params: "not selector(:where(*))",
|
|
51
|
+
source: rule.source
|
|
52
|
+
});
|
|
53
|
+
fallbackSupports.append(fallbackRule);
|
|
54
|
+
rule.after(fallbackSupports);
|
|
55
|
+
});
|
|
56
|
+
}
|
|
57
|
+
};
|
|
58
|
+
};
|
|
59
|
+
plugin.postcss = true;
|
|
60
|
+
var index_default = plugin;
|
|
61
|
+
export {
|
|
62
|
+
index_default as default
|
|
63
|
+
};
|
package/package.json
ADDED
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "postcss-pseudo-where-fallback",
|
|
3
|
+
"version": "0.4.1",
|
|
4
|
+
"description": "PostCSS plugin to provide fallbacks for :where() pseudo-class",
|
|
5
|
+
"main": "dist/index.cjs",
|
|
6
|
+
"module": "dist/index.mjs",
|
|
7
|
+
"exports": {
|
|
8
|
+
".": {
|
|
9
|
+
"import": "./dist/index.mjs",
|
|
10
|
+
"require": "./dist/index.cjs"
|
|
11
|
+
}
|
|
12
|
+
},
|
|
13
|
+
"type": "module",
|
|
14
|
+
"files": [
|
|
15
|
+
"dist",
|
|
16
|
+
"README.md",
|
|
17
|
+
"LICENSE"
|
|
18
|
+
],
|
|
19
|
+
"scripts": {
|
|
20
|
+
"build": "esbuild src/index.mjs --format=esm --outfile=dist/index.mjs && esbuild src/index.mjs --format=cjs --outfile=dist/index.cjs",
|
|
21
|
+
"test": "node --test",
|
|
22
|
+
"prepublishOnly": "npm run build"
|
|
23
|
+
},
|
|
24
|
+
"keywords": [
|
|
25
|
+
"postcss",
|
|
26
|
+
"postcss-plugin",
|
|
27
|
+
"css",
|
|
28
|
+
"where",
|
|
29
|
+
"pseudo-class",
|
|
30
|
+
"fallback",
|
|
31
|
+
"polyfill"
|
|
32
|
+
],
|
|
33
|
+
"author": "Zoli Szabó",
|
|
34
|
+
"license": "MIT",
|
|
35
|
+
"repository": {
|
|
36
|
+
"type": "git",
|
|
37
|
+
"url": "https://github.com/zoliszabo/postcss-pseudo-where-fallback.git"
|
|
38
|
+
},
|
|
39
|
+
"bugs": {
|
|
40
|
+
"url": "https://github.com/zoliszabo/postcss-pseudo-where-fallback/issues"
|
|
41
|
+
},
|
|
42
|
+
"peerDependencies": {
|
|
43
|
+
"postcss": "^8.0.0"
|
|
44
|
+
},
|
|
45
|
+
"devDependencies": {
|
|
46
|
+
"@csstools/postcss-tape": "^7.0.0",
|
|
47
|
+
"esbuild": "^0.27.2",
|
|
48
|
+
"postcss": "^8.4.0",
|
|
49
|
+
"postcss-selector-parser": "^7.1.1"
|
|
50
|
+
},
|
|
51
|
+
"engines": {
|
|
52
|
+
"node": ">=14.0.0"
|
|
53
|
+
}
|
|
54
|
+
}
|