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 with .class:global(.class2) #6900

Merged
merged 6 commits into from Feb 8, 2023
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
37 changes: 34 additions & 3 deletions crates/swc_css_modules/src/lib.rs
Expand Up @@ -374,7 +374,7 @@ where

'complex: for mut n in n.children.take() {
if let ComplexSelectorChildren::CompoundSelector(selector) = &mut n {
for sel in &mut selector.subclass_selectors {
for (sel_index, sel) in selector.subclass_selectors.iter_mut().enumerate() {
match sel {
SubclassSelector::Class(..) | SubclassSelector::Id(..) => {
if !self.data.is_global_mode {
Expand Down Expand Up @@ -402,7 +402,17 @@ where

complex_selector.visit_mut_with(self);

new_children.extend(complex_selector.children.clone());
let mut complex_selector_children =
complex_selector.children.clone();
prepend_left_subclass_selectors(
&mut complex_selector_children,
selector
.subclass_selectors
.split_at(sel_index)
.0
.to_vec(),
);
new_children.extend(complex_selector_children);

self.data.is_global_mode = old_is_global_mode;
self.data.is_in_local_pseudo_class = old_inside;
Expand All @@ -419,7 +429,17 @@ where
complex_selector,
)) = children.get_mut(0)
{
new_children.extend(complex_selector.children.clone());
let mut complex_selector_children =
complex_selector.children.clone();
prepend_left_subclass_selectors(
&mut complex_selector_children,
selector
.subclass_selectors
.split_at(sel_index)
.0
.to_vec(),
);
new_children.extend(complex_selector_children);
}
} else {
self.data.is_global_mode = true;
Expand Down Expand Up @@ -523,3 +543,14 @@ fn process_local<C>(
SubclassSelector::PseudoElement(_) => {}
}
}

fn prepend_left_subclass_selectors(
complex_selector_children: &mut [ComplexSelectorChildren],
left_sels: Vec<SubclassSelector>,
) {
if let Some(ComplexSelectorChildren::CompoundSelector(first)) =
complex_selector_children.get_mut(0)
{
first.subclass_selectors = [left_sels, first.subclass_selectors.take()].concat();
}
}
Expand Up @@ -5,3 +5,9 @@
.global {
color: blue;
}
.__local__local.global {
color: green;
}
.__local__local.__local__local {
color: yellow;
}
Expand Up @@ -6,3 +6,11 @@
:global(.global) {
color: blue;
}

.local:global(.global) {
color: green;
}

.local:local(.local) {
color: yellow;
}
Expand Up @@ -52,7 +52,7 @@ div:not(.__local__a) {
.__local__😓 .__local__a {
color: red;
}
.__local__a {
.__local__😓.__local__a {
color: red;
}
.__local__a > .__local__b > .__local__c {
Expand Down