forked from parcel-bundler/lightningcss
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcontext.rs
More file actions
183 lines (161 loc) · 5.13 KB
/
context.rs
File metadata and controls
183 lines (161 loc) · 5.13 KB
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
use std::collections::HashSet;
use crate::compat::Feature;
use crate::declaration::DeclarationBlock;
use crate::properties::custom::UnparsedProperty;
use crate::properties::Property;
use crate::rules::supports::{SupportsCondition, SupportsRule};
use crate::rules::{style::StyleRule, CssRule, CssRuleList};
use crate::selector::{Direction, PseudoClass};
use crate::targets::Browsers;
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: Option<Browsers>,
pub is_important: bool,
supports: Vec<SupportsEntry<'i>>,
ltr: Vec<Property<'i>>,
rtl: Vec<Property<'i>>,
pub context: DeclarationContext,
pub unused_symbols: &'o HashSet<String>,
}
impl<'i, 'o> PropertyHandlerContext<'i, 'o> {
pub fn new(targets: Option<Browsers>, unused_symbols: &'o HashSet<String>) -> Self {
PropertyHandlerContext {
targets,
is_important: false,
supports: Vec::new(),
ltr: Vec::new(),
rtl: Vec::new(),
context: DeclarationContext::None,
unused_symbols,
}
}
pub fn is_supported(&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 true;
}
if let Some(targets) = self.targets {
feature.is_compatible(targets)
} else {
true
}
}
pub fn add_logical_rule(&mut self, ltr: Property<'i>, rtl: Property<'i>) {
self.ltr.push(ltr);
self.rtl.push(rtl);
}
pub fn get_logical_rules<T>(&mut 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: std::mem::take(&mut self.$decls),
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);
}
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;
}
if let Some(targets) = self.targets {
let fallbacks = unparsed.value.get_fallbacks(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>(&mut self, style_rule: &StyleRule<'i, T>) -> Vec<CssRule<'i, T>> {
if self.supports.is_empty() {
return Vec::new();
}
let mut dest = Vec::new();
let supports = std::mem::take(&mut self.supports);
for entry in supports {
dest.push(CssRule::Supports(SupportsRule {
condition: entry.condition,
rules: CssRuleList(vec![CssRule::Style(StyleRule {
selectors: style_rule.selectors.clone(),
vendor_prefix: VendorPrefix::None,
declarations: DeclarationBlock {
declarations: entry.declarations,
important_declarations: entry.important_declarations,
},
rules: CssRuleList(vec![]),
loc: style_rule.loc.clone(),
})]),
loc: style_rule.loc.clone(),
}));
}
dest
}
}