Skip to content
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

fix(css/modules): Fix :global selectors without preceding whitespace #8926

Merged
merged 12 commits into from
May 7, 2024
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
5 changes: 2 additions & 3 deletions crates/swc_css_modules/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -704,9 +704,8 @@ fn prepend_left_subclass_selectors(
{
c.subclass_selectors.splice(0..0, sels.drain(..sel_index));

if sels.len() > sel_index {
c.subclass_selectors
.extend(sels[..sel_index + 1].iter().cloned());
if !sels.is_empty() {
c.subclass_selectors.extend(sels[..].iter().cloned());
}

sel_index = 0;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
.__local__a.b .c.__local__d {
.__local__a.b.__local__d .c.__local__d {
color: red;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
.__local__foo.bar.__local__baz {}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
.foo:global(.bar).baz {}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"baz": [
{
"name": "__local__baz",
"type": "local"
}
],
"foo": [
{
"name": "__local__foo",
"type": "local"
}
]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
.__local__foo .bar.__local__baz {}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
.foo :global(.bar).baz {}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"baz": [
{
"name": "__local__baz",
"type": "local"
}
],
"foo": [
{
"name": "__local__foo",
"type": "local"
}
]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
.foo:global(.bar).baz {}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{}
151 changes: 151 additions & 0 deletions crates/swc_css_parser/tests/fixture/issue-8461/1/output.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,151 @@
{
"type": "Stylesheet",
"span": {
"start": 1,
"end": 25,
"ctxt": 0
},
"rules": [
{
"type": "QualifiedRule",
"span": {
"start": 1,
"end": 25,
"ctxt": 0
},
"prelude": {
"type": "SelectorList",
"span": {
"start": 1,
"end": 22,
"ctxt": 0
},
"children": [
{
"type": "ComplexSelector",
"span": {
"start": 1,
"end": 22,
"ctxt": 0
},
"children": [
{
"type": "CompoundSelector",
"span": {
"start": 1,
"end": 22,
"ctxt": 0
},
"nestingSelector": null,
"typeSelector": null,
"subclassSelectors": [
{
"type": "ClassSelector",
"span": {
"start": 1,
"end": 5,
"ctxt": 0
},
"text": {
"type": "Ident",
"span": {
"start": 2,
"end": 5,
"ctxt": 0
},
"value": "foo",
"raw": "foo"
}
},
{
"type": "PseudoClassSelector",
"span": {
"start": 5,
"end": 18,
"ctxt": 0
},
"name": {
"type": "Ident",
"span": {
"start": 6,
"end": 12,
"ctxt": 0
},
"value": "global",
"raw": "global"
},
"children": [
{
"type": "PreservedToken",
"span": {
"start": 13,
"end": 14,
"ctxt": 0
},
"token": {
"Delim": {
"value": "."
}
}
},
{
"type": "PreservedToken",
"span": {
"start": 14,
"end": 17,
"ctxt": 0
},
"token": {
"Ident": {
"value": "bar",
"raw": "bar"
}
}
}
]
},
{
"type": "ClassSelector",
"span": {
"start": 18,
"end": 22,
"ctxt": 0
},
"text": {
"type": "Ident",
"span": {
"start": 19,
"end": 22,
"ctxt": 0
},
"value": "baz",
"raw": "baz"
}
}
]
}
]
}
]
},
"block": {
"type": "SimpleBlock",
"span": {
"start": 23,
"end": 25,
"ctxt": 0
},
"name": {
"type": "PreservedToken",
"span": {
"start": 23,
"end": 24,
"ctxt": 0
},
"token": "LBrace"
},
"value": []
}
}
]
}
126 changes: 126 additions & 0 deletions crates/swc_css_parser/tests/fixture/issue-8461/1/span.swc-stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,126 @@

x Stylesheet
,-[$DIR/tests/fixture/issue-8461/1/input.css:1:1]
1 | .foo:global(.bar).baz {}
: ^^^^^^^^^^^^^^^^^^^^^^^^
`----

x Rule
,-[$DIR/tests/fixture/issue-8461/1/input.css:1:1]
1 | .foo:global(.bar).baz {}
: ^^^^^^^^^^^^^^^^^^^^^^^^
`----

x QualifiedRule
,-[$DIR/tests/fixture/issue-8461/1/input.css:1:1]
1 | .foo:global(.bar).baz {}
: ^^^^^^^^^^^^^^^^^^^^^^^^
`----

x SelectorList
,-[$DIR/tests/fixture/issue-8461/1/input.css:1:1]
1 | .foo:global(.bar).baz {}
: ^^^^^^^^^^^^^^^^^^^^^
`----

x ComplexSelector
,-[$DIR/tests/fixture/issue-8461/1/input.css:1:1]
1 | .foo:global(.bar).baz {}
: ^^^^^^^^^^^^^^^^^^^^^
`----

x CompoundSelector
,-[$DIR/tests/fixture/issue-8461/1/input.css:1:1]
1 | .foo:global(.bar).baz {}
: ^^^^^^^^^^^^^^^^^^^^^
`----

x SubclassSelector
,-[$DIR/tests/fixture/issue-8461/1/input.css:1:1]
1 | .foo:global(.bar).baz {}
: ^^^^
`----

x ClassSelector
,-[$DIR/tests/fixture/issue-8461/1/input.css:1:1]
1 | .foo:global(.bar).baz {}
: ^^^^
`----

x Ident
,-[$DIR/tests/fixture/issue-8461/1/input.css:1:1]
1 | .foo:global(.bar).baz {}
: ^^^
`----

x SubclassSelector
,-[$DIR/tests/fixture/issue-8461/1/input.css:1:1]
1 | .foo:global(.bar).baz {}
: ^^^^^^^^^^^^^
`----

x PseudoClassSelector
,-[$DIR/tests/fixture/issue-8461/1/input.css:1:1]
1 | .foo:global(.bar).baz {}
: ^^^^^^^^^^^^^
`----

x Ident
,-[$DIR/tests/fixture/issue-8461/1/input.css:1:1]
1 | .foo:global(.bar).baz {}
: ^^^^^^
`----

x PseudoClassSelectorChildren
,-[$DIR/tests/fixture/issue-8461/1/input.css:1:1]
1 | .foo:global(.bar).baz {}
: ^
`----

x Delim { value: '.' }
,-[$DIR/tests/fixture/issue-8461/1/input.css:1:1]
1 | .foo:global(.bar).baz {}
: ^
`----

x PseudoClassSelectorChildren
,-[$DIR/tests/fixture/issue-8461/1/input.css:1:1]
1 | .foo:global(.bar).baz {}
: ^^^
`----

x Ident { value: "bar", raw: "bar" }
,-[$DIR/tests/fixture/issue-8461/1/input.css:1:1]
1 | .foo:global(.bar).baz {}
: ^^^
`----

x SubclassSelector
,-[$DIR/tests/fixture/issue-8461/1/input.css:1:1]
1 | .foo:global(.bar).baz {}
: ^^^^
`----

x ClassSelector
,-[$DIR/tests/fixture/issue-8461/1/input.css:1:1]
1 | .foo:global(.bar).baz {}
: ^^^^
`----

x Ident
,-[$DIR/tests/fixture/issue-8461/1/input.css:1:1]
1 | .foo:global(.bar).baz {}
: ^^^
`----

x SimpleBlock
,-[$DIR/tests/fixture/issue-8461/1/input.css:1:1]
1 | .foo:global(.bar).baz {}
: ^^
`----

x LBrace
,-[$DIR/tests/fixture/issue-8461/1/input.css:1:1]
1 | .foo:global(.bar).baz {}
: ^
`----
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
.foo :global(.bar).baz {}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{}
Loading
Loading