Skip to content

Commit

Permalink
fix(css/modules): Fix :global selectors without preceding whitespace (
Browse files Browse the repository at this point in the history
#8926)

**Related issue:**

 - Closes #8461
  • Loading branch information
kdy1 authored May 7, 2024
1 parent a0c8e1f commit 2405dc6
Show file tree
Hide file tree
Showing 18 changed files with 628 additions and 4 deletions.
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"
}
]
}
1 change: 1 addition & 0 deletions crates/swc_css_parser/tests/fixture/issue-8461/1/input.css
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 @@
{}
1 change: 1 addition & 0 deletions crates/swc_css_parser/tests/fixture/issue-8461/2/input.css
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

0 comments on commit 2405dc6

Please sign in to comment.