Skip to content

fix: handle scoped :global and :local in nested pseudo #58

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
merged 1 commit into from
May 18, 2023
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
16 changes: 13 additions & 3 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -149,9 +149,19 @@ function localizeNode(rule, mode, localAliasMap) {
hasLocals: false,
explicit: context.explicit,
};
newNodes = node.map((childNode) =>
transform(childNode, childContext)
);
newNodes = node.map((childNode) => {
const newContext = {
...childContext,
enforceNoSpacing: false,
};

const result = transform(childNode, newContext);

childContext.global = newContext.global;
childContext.hasLocals = newContext.hasLocals;

return result;
});

node = node.clone();
node.nodes = normalizeNodeArray(newNodes);
Expand Down
50 changes: 50 additions & 0 deletions test/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -276,6 +276,51 @@ const tests = [
input: ":global .foo { animation: foo; animation-name: bar; }",
expected: ".foo { animation: foo; animation-name: bar; }",
},
{
name: "handle nested global",
input: ":global .a:not(:global .b) {}",
expected: ".a:not(.b) {}",
},
{
name: "handle nested global #1",
input: ":global .a:not(:global .b:not(:global .c)) {}",
expected: ".a:not(.b:not(.c)) {}",
},
{
name: "handle nested global #2",
input: ":local .a:not(:not(:not(:global .c))) {}",
expected: ":local(.a):not(:not(:not(.c))) {}",
},
{
name: "handle nested global #3",
input: ":global .a:not(:global .b, :global .c) {}",
expected: ".a:not(.b, .c) {}",
},
{
name: "handle nested global #4",
input: ":local .a:not(:global .b, :local .c) {}",
expected: ":local(.a):not(.b, :local(.c)) {}",
},
{
name: "handle nested global #5",
input: ":global .a:not(:local .b, :global .c) {}",
expected: ".a:not(:local(.b), .c) {}",
},
{
name: "handle nested global #6",
input: ":global .a:not(.b, .c) {}",
expected: ".a:not(.b, .c) {}",
},
{
name: "handle nested global #7",
input: ":local .a:not(.b, .c) {}",
expected: ":local(.a):not(:local(.b), :local(.c)) {}",
},
{
name: "handle nested global #8",
input: ":global .a:not(:local .b, .c) {}",
expected: ".a:not(:local(.b), :local(.c)) {}",
},
{
name: "handle a complex animation rule",
input:
Expand Down Expand Up @@ -739,6 +784,11 @@ const tests = [
input: ":global(#) {}",
error: /Invalid class or id selector syntax/,
},
{
name: "throw on invalid global class usage",
input: ":global(.a:not(:global .b, :global .c)) {}",
error: /A :global is not allowed inside of a :global/,
},
/*
Bug in postcss-selector-parser
{
Expand Down