Skip to content

postcss-nesting: fix pseudo's and allow ampersand everywhere #683

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions experimental/postcss-nesting/.tape.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -92,4 +92,7 @@ postcssTape(plugin)({
'relative-selectors': {
message: 'examples of relative selectors',
},
'ampersand-everywhere': {
message: 'allow & to be used everywhere',
}
});
15 changes: 15 additions & 0 deletions experimental/postcss-nesting/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,20 @@
# Changes to PostCSS Nesting Experimental

### Unreleased

- match Chrome's behavior for pseudo elements.
- add support for `&` everywhere.

```css
.foo {
::before {}
}

/* becomes */

.foo ::before {}
```

### 1.0.0 (October 29, 2022)

Implement the 28 October version of [css-nestng-1](https://drafts.csswg.org/css-nesting/)
Expand Down
15 changes: 12 additions & 3 deletions experimental/postcss-nesting/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,18 +23,27 @@ a, b {
}
}

& {
color: pink;
}


/* becomes */

a, b {
color: red;
}

:is(a,b) c, :is(a,b) d {
color: white;
}
color: white;
}

:is(e) :is(a,b) {
color: yellow;
color: yellow;
}

:scope {
color: pink;
}
```

Expand Down
5 changes: 5 additions & 0 deletions experimental/postcss-nesting/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,16 @@
import type { PluginCreator } from 'postcss';
import ampersandToScope from './lib/ampersand-to-scope.js';
import walk from './lib/walk.js';

const creator: PluginCreator<never> = () => {
return {
postcssPlugin: 'postcss-nesting',
Rule(rule, { result }) {
walk(rule, result);

if (rule.selector.trim().includes('&')) {
ampersandToScope(rule);
}
},
};
};
Expand Down
25 changes: 25 additions & 0 deletions experimental/postcss-nesting/src/lib/ampersand-to-scope.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import type { Container, Node, Rule } from 'postcss';
import parser from 'postcss-selector-parser';

export default function ampersandToScope(rule: Rule) {
let parent: Container<Node> = rule.parent;

while (parent) {
if (parent.type === 'rule') {
return;
}

parent = parent.parent;
}
Comment on lines +7 to +13
Copy link
Member Author

@romainmenke romainmenke Nov 3, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We want to be sure that & was "detected" in a rule that isn't itself nested.

Nested CSS can exist after transforms when it was invalid.


const selectorAST = parser().astSync(rule.selector);
if (!selectorAST) {
return;
}

selectorAST.walkNesting((nesting) => {
nesting.replaceWith(parser.pseudo({value: ':scope'}));
});

rule.selector = selectorAST.toString();
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,6 @@ export default function mergeSelectors(node: Node, postcssResult: Result, fromSe
return false;
}

// see : https://github.com/w3c/csswg-drafts/issues/7979
if (parser.isPseudoElement(maybeCombinator)) {
startsWithCombinator = true;
return false;
}
Comment on lines -34 to -38
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am following what Chrome did and I agree with their analysis : w3c/csswg-drafts#7979 (comment)


return false;
});

Expand Down
28 changes: 28 additions & 0 deletions experimental/postcss-nesting/test/ampersand-everywhere.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
& {
order: 1;
}

.foo & {
order: 2;
}


.foo:has(&, :not(&)) {
order: 3;
}

&& + & {
order: 4;
}

@media screen {
& {
order: 5;
}
}

@container (.foo) {
& {
order: 6;
}
}
28 changes: 28 additions & 0 deletions experimental/postcss-nesting/test/ampersand-everywhere.expect.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
:scope {
order: 1;
}

.foo :scope {
order: 2;
}


.foo:has(:scope, :not(:scope)) {
order: 3;
}

:scope:scope + :scope {
order: 4;
}

@media screen {
:scope {
order: 5;
}
}

@container (.foo) {
:scope {
order: 6;
}
}
4 changes: 4 additions & 0 deletions experimental/postcss-nesting/test/examples/example.css
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,7 @@ a, b {
color: yellow;
}
}

& {
color: pink;
}
6 changes: 6 additions & 0 deletions experimental/postcss-nesting/test/examples/example.expect.css
Original file line number Diff line number Diff line change
@@ -1,9 +1,15 @@
a, b {
color: red;
}

:is(a,b) c, :is(a,b) d {
color: white;
}

:is(e) :is(a,b) {
color: yellow;
}

:scope {
color: pink;
}
2 changes: 1 addition & 1 deletion experimental/postcss-nesting/test/ignore.expect.css
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ a, b {
order: 2;
}

& e {
:scope e {
order: 3;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
}


:is(.foo)::before {
:is(.foo) ::before {
order: 5;
}

Expand All @@ -44,6 +44,6 @@
}


:is(.foo)::before, :is(.foo) .other {
:is(.foo) ::before, :is(.foo) .other {
order: 15;
}