swc_css_compat/compiler/
mod.rs1use swc_common::{util::take::Take, Spanned, DUMMY_SP};
2use swc_css_ast::{
3 AbsoluteColorBase, AtRule, ComponentValue, CompoundSelector, MediaAnd, MediaCondition,
4 MediaConditionAllType, MediaConditionWithoutOr, MediaInParens, MediaQuery, Rule,
5 SupportsCondition,
6};
7use swc_css_visit::{VisitMut, VisitMutWith};
8
9use self::custom_media::CustomMediaHandler;
10use crate::feature::Features;
11
12mod color_alpha_parameter;
13mod color_hex_alpha;
14mod color_hwb;
15mod color_space_separated_parameters;
16mod custom_media;
17mod legacy_rgb_and_hsl;
18mod media_query_ranges;
19mod nesting;
20mod selector_not;
21
22#[derive(Debug)]
24pub struct Compiler {
25 #[allow(unused)]
26 c: Config,
27 custom_media: CustomMediaHandler,
28 in_supports_condition: bool,
29}
30
31#[derive(Debug)]
32pub struct Config {
33 pub process: Features,
35}
36
37impl Compiler {
38 pub fn new(config: Config) -> Self {
39 Self {
40 c: config,
41 custom_media: Default::default(),
42 in_supports_condition: Default::default(),
43 }
44 }
45}
46
47impl VisitMut for Compiler {
48 fn visit_mut_at_rule(&mut self, n: &mut AtRule) {
49 n.visit_mut_children_with(self);
50
51 if self.c.process.contains(Features::CUSTOM_MEDIA) {
52 self.custom_media.store_custom_media(n);
53 }
54 }
55
56 fn visit_mut_supports_condition(&mut self, n: &mut SupportsCondition) {
57 let old_in_support_condition = self.in_supports_condition;
58
59 self.in_supports_condition = true;
60
61 n.visit_mut_children_with(self);
62
63 self.in_supports_condition = old_in_support_condition;
64 }
65
66 fn visit_mut_media_query(&mut self, n: &mut MediaQuery) {
67 n.visit_mut_children_with(self);
68
69 if self.c.process.contains(Features::CUSTOM_MEDIA) {
70 self.custom_media.process_media_query(n);
71 }
72 }
73
74 fn visit_mut_media_condition(&mut self, n: &mut MediaCondition) {
75 n.visit_mut_children_with(self);
76
77 if self.c.process.contains(Features::CUSTOM_MEDIA) {
78 self.custom_media.process_media_condition(n);
79 }
80 }
81
82 fn visit_mut_media_condition_without_or(&mut self, n: &mut MediaConditionWithoutOr) {
83 n.visit_mut_children_with(self);
84
85 if self.c.process.contains(Features::CUSTOM_MEDIA) {
86 self.custom_media.process_media_condition_without_or(n);
87 }
88 }
89
90 fn visit_mut_rules(&mut self, n: &mut Vec<Rule>) {
91 if self.c.process.contains(Features::NESTING) {
92 let mut new = Vec::new();
93
94 for n in n.take() {
95 match n {
96 Rule::QualifiedRule(mut n) => {
97 let mut rules = self.extract_nested_rules(&mut n);
98
99 rules.visit_mut_with(self);
100
101 new.push(Rule::QualifiedRule(n));
102 new.extend(rules);
103 }
104 _ => {
105 new.push(n);
106 }
107 }
108 }
109
110 *n = new;
111 } else {
112 n.visit_mut_children_with(self);
113 }
114
115 if self.c.process.contains(Features::CUSTOM_MEDIA) {
116 self.custom_media.process_rules(n);
117 }
118 }
119
120 fn visit_mut_media_in_parens(&mut self, n: &mut MediaInParens) {
121 n.visit_mut_children_with(self);
122
123 if self.c.process.contains(Features::MEDIA_QUERY_RANGES) {
124 if let MediaInParens::Feature(media_feature) = n {
125 if let Some(legacy_media_feature) = self.get_legacy_media_feature(media_feature) {
126 match legacy_media_feature {
127 (legacy_media_feature, None) => {
128 *media_feature = Box::new(legacy_media_feature);
129 }
130 (left, Some(right)) => {
131 *n = MediaInParens::MediaCondition(MediaCondition {
132 span: n.span(),
133 conditions: vec![
134 MediaConditionAllType::MediaInParens(*Box::new(
135 MediaInParens::Feature(Box::new(left)),
136 )),
137 MediaConditionAllType::And(MediaAnd {
138 span: DUMMY_SP,
139 keyword: None,
140 condition: MediaInParens::Feature(Box::new(right)),
141 }),
142 ],
143 });
144 }
145 }
146 }
147 }
148 }
149 }
150
151 fn visit_mut_compound_selector(&mut self, n: &mut CompoundSelector) {
152 n.visit_mut_children_with(self);
153
154 if self.in_supports_condition {
155 return;
156 }
157
158 if self.c.process.contains(Features::SELECTOR_NOT) {
159 self.process_selector_not(n);
160 }
161 }
162
163 fn visit_mut_component_value(&mut self, n: &mut ComponentValue) {
164 n.visit_mut_children_with(self);
165
166 if self.in_supports_condition {
167 return;
168 }
169
170 if self.c.process.contains(Features::COLOR_HEX_ALPHA) {
171 self.process_color_hex_alpha(n);
172 }
173 }
174
175 fn visit_mut_absolute_color_base(&mut self, n: &mut AbsoluteColorBase) {
176 n.visit_mut_children_with(self);
177
178 if self.in_supports_condition {
179 return;
180 }
181
182 let process = self.c.process;
186
187 if process.contains(Features::COLOR_SPACE_SEPARATED_PARAMETERS) {
188 self.process_color_space_separated_function_notation(n);
189 }
190
191 if process.contains(Features::COLOR_ALPHA_PARAMETER) {
192 self.process_color_alpha_parameter(n);
193 }
194
195 if process.contains(Features::COLOR_LEGACY_RGB_AND_HSL) {
196 self.process_rgb_and_hsl(n);
197 }
198
199 if process.contains(Features::COLOR_HWB) {
200 self.process_color_hwb(n);
201 }
202 }
203}