-
Notifications
You must be signed in to change notification settings - Fork 192
/
Copy pathcontext.rs
234 lines (208 loc) · 6.65 KB
/
context.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
use std::collections::HashSet;
use crate::compat::Feature;
use crate::declaration::DeclarationBlock;
use crate::media_query::{
MediaCondition, MediaFeatureId, MediaFeatureName, MediaFeatureValue, MediaList, MediaQuery, MediaType,
QueryFeature,
};
use crate::properties::custom::UnparsedProperty;
use crate::properties::Property;
use crate::rules::media::MediaRule;
use crate::rules::supports::{SupportsCondition, SupportsRule};
use crate::rules::{style::StyleRule, CssRule, CssRuleList};
use crate::selector::{Direction, PseudoClass};
use crate::targets::Targets;
use crate::values::ident::Ident;
use crate::vendor_prefix::VendorPrefix;
use parcel_selectors::parser::Component;
#[derive(Debug)]
pub(crate) struct SupportsEntry<'i> {
pub condition: SupportsCondition<'i>,
pub declarations: Vec<Property<'i>>,
pub important_declarations: Vec<Property<'i>>,
}
#[derive(Debug, PartialEq)]
pub(crate) enum DeclarationContext {
None,
StyleRule,
Keyframes,
StyleAttribute,
}
#[derive(Debug)]
pub(crate) struct PropertyHandlerContext<'i, 'o> {
pub targets: Targets,
pub is_important: bool,
supports: Vec<SupportsEntry<'i>>,
ltr: Vec<Property<'i>>,
rtl: Vec<Property<'i>>,
dark: Vec<Property<'i>>,
pub context: DeclarationContext,
pub unused_symbols: &'o HashSet<String>,
}
impl<'i, 'o> PropertyHandlerContext<'i, 'o> {
pub fn new(targets: Targets, unused_symbols: &'o HashSet<String>) -> Self {
PropertyHandlerContext {
targets,
is_important: false,
supports: Vec::new(),
ltr: Vec::new(),
rtl: Vec::new(),
dark: Vec::new(),
context: DeclarationContext::None,
unused_symbols,
}
}
pub fn child(&self, context: DeclarationContext) -> Self {
PropertyHandlerContext {
targets: self.targets,
is_important: false,
supports: Vec::new(),
ltr: Vec::new(),
rtl: Vec::new(),
dark: Vec::new(),
context,
unused_symbols: self.unused_symbols,
}
}
pub fn should_compile_logical(&self, feature: Feature) -> bool {
// Don't convert logical properties in style attributes because
// our fallbacks rely on extra rules to define --ltr and --rtl.
if self.context == DeclarationContext::StyleAttribute {
return false;
}
self.targets.should_compile_logical(feature)
}
pub fn add_logical_rule(&mut self, ltr: Property<'i>, rtl: Property<'i>) {
self.ltr.push(ltr);
self.rtl.push(rtl);
}
pub fn add_dark_rule(&mut self, property: Property<'i>) {
self.dark.push(property);
}
pub fn get_additional_rules<T>(&self, style_rule: &StyleRule<'i, T>) -> Vec<CssRule<'i, T>> {
// TODO: :dir/:lang raises the specificity of the selector. Use :where to lower it?
let mut dest = Vec::new();
macro_rules! rule {
($dir: ident, $decls: ident) => {
let mut selectors = style_rule.selectors.clone();
for selector in &mut selectors.0 {
selector.append(Component::NonTSPseudoClass(PseudoClass::Dir {
direction: Direction::$dir,
}));
}
let rule = StyleRule {
selectors,
vendor_prefix: VendorPrefix::None,
declarations: DeclarationBlock {
declarations: self.$decls.clone(),
important_declarations: vec![],
},
rules: CssRuleList(vec![]),
loc: style_rule.loc.clone(),
};
dest.push(CssRule::Style(rule));
};
}
if !self.ltr.is_empty() {
rule!(Ltr, ltr);
}
if !self.rtl.is_empty() {
rule!(Rtl, rtl);
}
if !self.dark.is_empty() {
dest.push(CssRule::Media(MediaRule {
query: MediaList {
media_queries: vec![MediaQuery {
qualifier: None,
media_type: MediaType::All,
condition: Some(MediaCondition::Feature(QueryFeature::Plain {
name: MediaFeatureName::Standard(MediaFeatureId::PrefersColorScheme),
value: MediaFeatureValue::Ident(Ident("dark".into())),
})),
}],
},
rules: CssRuleList(vec![CssRule::Style(StyleRule {
selectors: style_rule.selectors.clone(),
vendor_prefix: VendorPrefix::None,
declarations: DeclarationBlock {
declarations: self.dark.clone(),
important_declarations: vec![],
},
rules: CssRuleList(vec![]),
loc: style_rule.loc.clone(),
})]),
loc: style_rule.loc.clone(),
}))
}
dest
}
pub fn add_conditional_property(&mut self, condition: SupportsCondition<'i>, property: Property<'i>) {
if self.context != DeclarationContext::StyleRule {
return;
}
if let Some(entry) = self.supports.iter_mut().find(|supports| condition == supports.condition) {
if self.is_important {
entry.important_declarations.push(property);
} else {
entry.declarations.push(property);
}
} else {
let mut important_declarations = Vec::new();
let mut declarations = Vec::new();
if self.is_important {
important_declarations.push(property);
} else {
declarations.push(property);
}
self.supports.push(SupportsEntry {
condition,
important_declarations,
declarations,
});
}
}
pub fn add_unparsed_fallbacks(&mut self, unparsed: &mut UnparsedProperty<'i>) {
if self.context != DeclarationContext::StyleRule && self.context != DeclarationContext::StyleAttribute {
return;
}
let fallbacks = unparsed.value.get_fallbacks(self.targets);
for (condition, fallback) in fallbacks {
self.add_conditional_property(
condition,
Property::Unparsed(UnparsedProperty {
property_id: unparsed.property_id.clone(),
value: fallback,
}),
);
}
}
pub fn get_supports_rules<T>(&self, style_rule: &StyleRule<'i, T>) -> Vec<CssRule<'i, T>> {
if self.supports.is_empty() {
return Vec::new();
}
let mut dest = Vec::new();
for entry in &self.supports {
dest.push(CssRule::Supports(SupportsRule {
condition: entry.condition.clone(),
rules: CssRuleList(vec![CssRule::Style(StyleRule {
selectors: style_rule.selectors.clone(),
vendor_prefix: VendorPrefix::None,
declarations: DeclarationBlock {
declarations: entry.declarations.clone(),
important_declarations: entry.important_declarations.clone(),
},
rules: CssRuleList(vec![]),
loc: style_rule.loc.clone(),
})]),
loc: style_rule.loc.clone(),
}));
}
dest
}
pub fn reset(&mut self) {
self.supports.clear();
self.ltr.clear();
self.rtl.clear();
self.dark.clear();
}
}