Skip to content

Commit 113134b

Browse files
committed
Allow :where() selector in options
1 parent 2c67643 commit 113134b

File tree

5 files changed

+37
-2
lines changed

5 files changed

+37
-2
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
# Changes to PostCSS Editor Styles
22

3+
### 0.5.0 (March 7, 2024)
4+
5+
- Allow using `:where()` pseudo class in options to account for every `:where(*)` selectors.
6+
37
### 0.4.0 (March 7, 2024)
48

59
- Fixed a bug where some tags weren't getting scoped ([#5](https://github.com/m-e-h/postcss-editor-styles/pull/5)) thanks to @animeshk874

index.js

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,13 +39,33 @@ module.exports = (options = {}) => {
3939

4040
const opts = { ...defaults, ...options };
4141

42+
// Detect if there is a :where() pseudo class in the selectors.
43+
const hasWherePseudo = (optionsVal, selectors) => {
44+
let optionHasWhere = false;
45+
46+
if (typeof optionsVal === 'string') {
47+
optionHasWhere = ':where()' === optionsVal;
48+
} else if (Array.isArray(optionsVal)) {
49+
optionHasWhere = optionsVal.find((el) => el === ':where()');
50+
}
51+
52+
if (!optionHasWhere) {
53+
return false;
54+
}
55+
56+
return selectors.find((el) => el.startsWith(':where('));
57+
}
58+
4259
const firstOrLastSelector = (optsArray, selectorArray) => {
4360
let firstSelector = selectorArray[0];
4461
let lastSelector = selectorArray[selectorArray.length - 1];
62+
const whereMatch = hasWherePseudo(optsArray, [firstSelector, lastSelector]);
4563

4664
var selectorIn = [];
4765

48-
if (-1 !== optsArray.indexOf(firstSelector)) {
66+
if(whereMatch) {
67+
selectorIn.push(whereMatch);
68+
} else if (-1 !== optsArray.indexOf(firstSelector)) {
4969
selectorIn.push(firstSelector);
5070
} else if (-1 !== optsArray.indexOf(lastSelector)) {
5171
selectorIn.push(lastSelector);

test/_tape.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,10 @@ const plugin = require('../index.js');
33

44
postcssTape(plugin)({
55
basic: {
6-
message: 'supports basic usage'
6+
message: 'supports basic usage',
7+
options: {
8+
ignore: [':root', ':where()'],
9+
}
710
},
811
remove: {
912
message: 'supports remove usage'

test/basic.css

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
:where(.wp-site-blocks) li {
2+
border-bottom: 1px solid #e1e4e8;
3+
}
4+
15
:root {
26
--color-text: #24292e;
37
}

test/basic.expect.css

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
:where(.wp-site-blocks) li {
2+
border-bottom: 1px solid #e1e4e8;
3+
}
4+
15
:root {
26
--color-text: #24292e;
37
}

0 commit comments

Comments
 (0)