feat(css/modules): Support :local and :global (#6045)

**Description:**

This PR adds support for `:local` and `:global` directives.
This commit is contained in:
Donny/강동윤 2022-10-05 16:33:40 +09:00 committed by GitHub
parent 5618702554
commit 10d0f8e001
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 30 additions and 24 deletions

View File

@ -84,6 +84,8 @@ struct Data {
renamed_to_orig: FxHashMap<JsWord, JsWord>,
orig_to_renamed: FxHashMap<JsWord, JsWord>,
is_global_mode: bool,
}
impl<C> VisitMut for Compiler<C>
@ -258,6 +260,8 @@ where
let mut new_children = Vec::with_capacity(n.children.len());
let old_is_global_mode = self.data.is_global_mode;
self.data.is_global_mode = false;
'complex: for mut n in n.children.take() {
match &mut n {
ComplexSelectorChildren::CompoundSelector(sel) => {
@ -266,13 +270,15 @@ where
for sel in &mut sel.subclass_selectors {
match sel {
SubclassSelector::Class(..) | SubclassSelector::Id(..) => {
process_local(
&mut self.config,
&mut self.result,
&mut self.data.orig_to_renamed,
&mut self.data.renamed_to_orig,
sel,
);
if !self.data.is_global_mode {
process_local(
&mut self.config,
&mut self.result,
&mut self.data.orig_to_renamed,
&mut self.data.renamed_to_orig,
sel,
);
}
}
SubclassSelector::PseudoClass(class_sel) => {
match &*class_sel.name.value {
@ -292,7 +298,9 @@ where
sel.visit_mut_with(self);
new_children.extend(sel.children);
continue 'complex;
} else {
self.data.is_global_mode = false;
continue 'complex;
}
}
@ -311,6 +319,9 @@ where
new_children.extend(sel.children);
continue 'complex;
} else {
self.data.is_global_mode = true;
continue 'complex;
}
}
@ -331,6 +342,7 @@ where
}
n.children = new_children;
self.data.is_global_mode = old_is_global_mode;
}
fn visit_mut_complex_selectors(&mut self, n: &mut Vec<ComplexSelector>) {

View File

@ -33,7 +33,7 @@
left: 20px;
}
}
:global .__local__d1 {
.d1 {
animation: __local__d1;
animation: __local__d2, __local__d3, __local__d4;
}

View File

@ -1,6 +1,6 @@
.__local__abc .__local__def {
color: red;
}
:local .__local__ghi .__local__jkl {
.__local__ghi .__local__jkl {
color: blue;
}

View File

@ -1,5 +1,5 @@
.__local__c1 :local .__local__c2 .__local__c3 :global .__local__c4 :local .__local__c5,
.__local__c6 :local .__local__c7 {
.__local__c1 .__local__c2 .__local__c3 .c4 .__local__c5,
.__local__c6 .__local__c7 {
background: red;
}
.__local__c8 {

View File

@ -1,10 +1,4 @@
{
"c4": [
{
"type": "local",
"name": "__local__c4"
}
],
"c5": [
{
"type": "local",
@ -23,6 +17,12 @@
"name": "__local__c7"
}
],
"c8": [
{
"type": "local",
"name": "__local__c8"
}
],
"c1": [
{
"type": "local",
@ -40,11 +40,5 @@
"type": "local",
"name": "__local__c3"
}
],
"c8": [
{
"type": "local",
"name": "__local__c8"
}
]
}