forked from parcel-bundler/lightningcss
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprefix_handler.rs
More file actions
180 lines (164 loc) · 5.39 KB
/
prefix_handler.rs
File metadata and controls
180 lines (164 loc) · 5.39 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
#![allow(non_snake_case)]
use super::{Property, PropertyId};
use crate::context::PropertyHandlerContext;
use crate::declaration::DeclarationList;
use crate::prefixes::Feature;
use crate::traits::{FallbackValues, IsCompatible, PropertyHandler};
use crate::vendor_prefix::VendorPrefix;
macro_rules! define_prefixes {
(
$( $name: ident, )+
) => {
#[derive(Default)]
pub(crate) struct PrefixHandler {
$(
$name: Option<usize>,
)+
}
impl<'i> PropertyHandler<'i> for PrefixHandler {
fn handle_property(&mut self, property: &Property<'i>, dest: &mut DeclarationList<'i>, context: &mut PropertyHandlerContext) -> bool {
match property {
$(
Property::$name(val, prefix) => {
if let Some(i) = self.$name {
if let Some(decl) = dest.get_mut(i) {
if let Property::$name(cur, prefixes) = decl {
// If the value is the same, update the prefix.
// If the prefix is the same, then update the value.
if val == cur || prefixes.contains(*prefix) {
*cur = val.clone();
*prefixes |= *prefix;
*prefixes = context.targets.prefixes(*prefixes, Feature::$name);
return true
}
}
}
}
// Update the prefixes based on the targets.
let prefixes = context.targets.prefixes(*prefix, Feature::$name);
// Store the index of the property, so we can update it later.
self.$name = Some(dest.len());
dest.push(Property::$name(val.clone(), prefixes))
}
)+
_ => return false
}
true
}
fn finalize(&mut self, _: &mut DeclarationList, _: &mut PropertyHandlerContext) {}
}
};
}
define_prefixes! {
TransformOrigin,
TransformStyle,
BackfaceVisibility,
Perspective,
PerspectiveOrigin,
BoxSizing,
TabSize,
Hyphens,
TextAlignLast,
TextDecorationSkipInk,
TextOverflow,
UserSelect,
Appearance,
ClipPath,
BoxDecorationBreak,
TextSizeAdjust,
}
macro_rules! define_fallbacks {
(
$( $name: ident$(($p: ident))?, )+
) => {
paste::paste! {
#[derive(Default)]
pub(crate) struct FallbackHandler {
$(
[<$name:snake>]: Option<usize>
),+
}
}
impl<'i> PropertyHandler<'i> for FallbackHandler {
fn handle_property(&mut self, property: &Property<'i>, dest: &mut DeclarationList<'i>, context: &mut PropertyHandlerContext<'i, '_>) -> bool {
match property {
$(
Property::$name(val $(, mut $p)?) => {
let mut val = val.clone();
$(
$p = context.targets.prefixes($p, Feature::$name);
)?
if paste::paste! { self.[<$name:snake>] }.is_none() {
let fallbacks = val.get_fallbacks(context.targets);
#[allow(unused_variables)]
let has_fallbacks = !fallbacks.is_empty();
for fallback in fallbacks {
dest.push(Property::$name(fallback $(, $p)?))
}
$(
if has_fallbacks && $p.contains(VendorPrefix::None) {
$p = VendorPrefix::None;
}
)?
}
if paste::paste! { self.[<$name:snake>] }.is_none() || matches!(context.targets.browsers, Some(targets) if !val.is_compatible(targets)) {
paste::paste! { self.[<$name:snake>] = Some(dest.len()) };
dest.push(Property::$name(val $(, $p)?));
} else if let Some(index) = paste::paste! { self.[<$name:snake>] } {
dest[index] = Property::$name(val $(, $p)?);
}
}
)+
Property::Unparsed(val) => {
let (mut unparsed, index) = match val.property_id {
$(
PropertyId::$name$(($p))? => {
macro_rules! get_prefixed {
($vp: ident) => {
if $vp.contains(VendorPrefix::None) {
val.get_prefixed(context.targets, Feature::$name)
} else {
val.clone()
}
};
() => {
val.clone()
};
}
let val = get_prefixed!($($p)?);
(val, paste::paste! { &mut self.[<$name:snake>] })
}
)+
_ => return false
};
// Unparsed properties are always "valid", meaning they always override previous declarations.
context.add_unparsed_fallbacks(&mut unparsed);
if let Some(index) = *index {
dest[index] = Property::Unparsed(unparsed);
} else {
*index = Some(dest.len());
dest.push(Property::Unparsed(unparsed));
}
}
_ => return false
}
true
}
fn finalize(&mut self, _: &mut DeclarationList, _: &mut PropertyHandlerContext) {
$(
paste::paste! { self.[<$name:snake>] = None };
)+
}
}
};
}
define_fallbacks! {
Color,
TextShadow,
Filter(prefix),
BackdropFilter(prefix),
Fill,
Stroke,
CaretColor,
Caret,
}