swc_css_compat/compiler/nesting.rs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305
use std::iter::once;
use swc_common::{util::take::Take, DUMMY_SP};
use swc_css_ast::*;
use crate::compiler::Compiler;
impl Compiler {
fn process_subclass_selectors(
&mut self,
prelude: &SelectorList,
subclass: &mut Vec<SubclassSelector>,
) {
for sel in subclass {
if let SubclassSelector::PseudoClass(PseudoClassSelector {
children: Some(children),
..
}) = sel
{
for c in children {
if let PseudoClassSelectorChildren::ForgivingSelectorList(c) = c {
let mut selectors = Vec::new();
for sel in &mut c.children {
match sel {
ForgivingComplexSelector::ComplexSelector(sel) => {
selectors.push(sel.clone());
}
ForgivingComplexSelector::ListOfComponentValues(_) => {
// Abort
return;
}
}
}
self.process_complex_selectors(prelude, &mut selectors);
*c = ForgivingSelectorList {
children: selectors
.into_iter()
.map(ForgivingComplexSelector::ComplexSelector)
.collect(),
..*c
};
}
}
}
}
}
fn process_complex_selectors(
&mut self,
prelude: &SelectorList,
selectors: &mut Vec<ComplexSelector>,
) {
let mut new_selectors = Vec::new();
'complex: for complex in selectors.take() {
for compound in &complex.children {
match compound {
ComplexSelectorChildren::CompoundSelector(compound) => {
if compound.nesting_selector.is_some() {
for prelude_children in &prelude.children {
let mut new = ComplexSelector {
span: Default::default(),
children: Default::default(),
};
for compound in &complex.children {
match compound {
ComplexSelectorChildren::CompoundSelector(compound) => {
self.append_compound(
prelude,
&mut new,
prelude_children,
compound,
);
}
ComplexSelectorChildren::Combinator(_) => {
new.children.push(compound.clone());
}
}
}
new_selectors.push(new);
}
continue 'complex;
}
}
ComplexSelectorChildren::Combinator(_) => {}
}
}
new_selectors.push(complex);
}
*selectors = new_selectors;
}
fn append_compound(
&mut self,
prelude: &SelectorList,
to: &mut ComplexSelector,
base: &ComplexSelector,
c: &CompoundSelector,
) {
if c.nesting_selector.is_some() {
let len = base.children.len();
to.children
.extend(
base.children
.iter()
.cloned()
.enumerate()
.map(|(idx, mut children)| {
if idx == len - 1 {
if let ComplexSelectorChildren::CompoundSelector(compound) =
&mut children
{
if c.type_selector.is_some() {
compound.type_selector.clone_from(&c.type_selector);
}
let mut subclass = c.subclass_selectors.clone();
self.process_subclass_selectors(prelude, &mut subclass);
compound.subclass_selectors.extend(subclass);
}
}
children
}),
);
} else {
to.children
.push(ComplexSelectorChildren::CompoundSelector(c.clone()));
}
}
fn relative_selector_list_to_selector_list(
&mut self,
base: &SelectorList,
relative_selector_list: &RelativeSelectorList,
) -> SelectorList {
let mut children = Vec::new();
for base_complex in &base.children {
for relative_selector in &relative_selector_list.children {
let mut complex_selector = ComplexSelector {
span: relative_selector.span,
children: Default::default(),
};
let is_non_relative = relative_selector.combinator.is_none()
&& relative_selector.selector.children.iter().any(|s| match s {
ComplexSelectorChildren::CompoundSelector(s) => {
s.nesting_selector.is_some()
}
_ => false,
});
if let Some(combinator) = &relative_selector.combinator {
complex_selector
.children
.extend(base_complex.children.clone());
complex_selector
.children
.push(ComplexSelectorChildren::Combinator(combinator.clone()))
} else if !is_non_relative {
complex_selector
.children
.extend(base_complex.children.clone());
complex_selector
.children
.push(ComplexSelectorChildren::Combinator(Combinator {
span: DUMMY_SP,
value: CombinatorValue::Descendant,
}))
}
for relative_complex_selector_children in &relative_selector.selector.children {
match relative_complex_selector_children {
ComplexSelectorChildren::CompoundSelector(compound) => {
self.append_compound(
base,
&mut complex_selector,
base_complex,
compound,
);
}
ComplexSelectorChildren::Combinator(combinator) => {
complex_selector
.children
.push(ComplexSelectorChildren::Combinator(combinator.clone()));
}
}
}
children.push(complex_selector);
}
}
SelectorList {
span: relative_selector_list.span,
children,
}
}
/// Prepend current selector
fn process_prelude(&mut self, base: &QualifiedRulePrelude, to: &mut QualifiedRulePrelude) {
if let (
QualifiedRulePrelude::SelectorList(base),
QualifiedRulePrelude::RelativeSelectorList(relative_selector_list),
) = (base, &to)
{
let selector_list =
self.relative_selector_list_to_selector_list(base, relative_selector_list);
*to = QualifiedRulePrelude::SelectorList(selector_list);
}
}
pub(crate) fn extract_nested_rules(&mut self, rule: &mut QualifiedRule) -> Vec<Rule> {
let mut nested_rules = Vec::new();
let mut block_values = Vec::new();
for value in rule.block.value.take() {
match value {
ComponentValue::QualifiedRule(mut nested) => {
self.process_prelude(&rule.prelude, &mut nested.prelude);
nested_rules.push(Rule::QualifiedRule(nested));
continue;
}
ComponentValue::AtRule(ref at_rule) => {
if let Some(
AtRulePrelude::MediaPrelude(..)
| AtRulePrelude::SupportsPrelude(..)
| AtRulePrelude::ContainerPrelude(..)
| AtRulePrelude::DocumentPrelude(..),
) = at_rule.prelude.as_deref()
{
if let Some(block) = &at_rule.block {
let mut decls_of_media = Vec::new();
let mut nested_of_media = Vec::new();
for n in &block.value {
match n {
ComponentValue::QualifiedRule(n) => {
let mut q = n.clone();
self.process_prelude(&rule.prelude, &mut q.prelude);
let rules = self.extract_nested_rules(&mut q);
nested_of_media.extend(
once(Rule::QualifiedRule(q))
.chain(rules)
.map(From::from),
);
}
_ => {
decls_of_media.push(n.clone());
}
}
}
if !decls_of_media.is_empty() {
let rule = Box::new(QualifiedRule {
span: DUMMY_SP,
prelude: rule.prelude.clone(),
block: SimpleBlock {
value: decls_of_media,
..block.clone()
},
});
nested_of_media.insert(0, ComponentValue::QualifiedRule(rule));
}
nested_rules.push(Rule::AtRule(Box::new(AtRule {
block: Some(SimpleBlock {
value: nested_of_media,
..block.clone()
}),
..*at_rule.clone()
})));
continue;
}
}
}
_ => {}
}
block_values.push(value);
}
rule.block.value = block_values;
nested_rules
}
}