forked from parcel-bundler/lightningcss
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathborder_radius.rs
More file actions
175 lines (156 loc) · 5.78 KB
/
border_radius.rs
File metadata and controls
175 lines (156 loc) · 5.78 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
use crate::values::length::*;
use crate::values::size::Size2D;
use cssparser::*;
use crate::traits::{Parse, ToCss, PropertyHandler};
use crate::targets::Browsers;
use crate::prefixes::Feature;
use crate::properties::{Property, VendorPrefix};
use crate::declaration::DeclarationList;
use crate::values::rect::Rect;
use crate::printer::Printer;
#[derive(Debug, Clone, PartialEq)]
pub struct BorderRadius {
pub top_left: Size2D<LengthPercentage>,
pub top_right: Size2D<LengthPercentage>,
pub bottom_left: Size2D<LengthPercentage>,
pub bottom_right: Size2D<LengthPercentage>
}
impl Parse for BorderRadius {
fn parse<'i, 't>(input: &mut Parser<'i, 't>) -> Result<Self, ParseError<'i, ()>> {
let widths: Rect<LengthPercentage> = Rect::parse(input)?;
let heights = if input.try_parse(|input| input.expect_delim('/')).is_ok() {
Rect::parse(input)?
} else {
widths.clone()
};
Ok(BorderRadius {
top_left: Size2D(widths.0, heights.0),
top_right: Size2D(widths.1, heights.1),
bottom_left: Size2D(widths.2, heights.2),
bottom_right: Size2D(widths.3, heights.3)
})
}
}
impl ToCss for BorderRadius {
fn to_css<W>(&self, dest: &mut Printer<W>) -> std::fmt::Result where W: std::fmt::Write {
let widths = Rect::new(&self.top_left.0, &self.top_right.0, &self.bottom_left.0, &self.bottom_right.0);
let heights = Rect::new(&self.top_left.1, &self.top_right.1, &self.bottom_left.1, &self.bottom_right.1);
widths.to_css(dest)?;
if widths != heights {
dest.delim('/', true)?;
heights.to_css(dest)?;
}
Ok(())
}
}
#[derive(Default, Debug)]
pub(crate) struct BorderRadiusHandler {
targets: Option<Browsers>,
top_left: Option<(Size2D<LengthPercentage>, VendorPrefix)>,
top_right: Option<(Size2D<LengthPercentage>, VendorPrefix)>,
bottom_left: Option<(Size2D<LengthPercentage>, VendorPrefix)>,
bottom_right: Option<(Size2D<LengthPercentage>, VendorPrefix)>,
logical: Vec<Property>,
decls: Vec<Property>
}
impl BorderRadiusHandler {
pub fn new(targets: Option<Browsers>) -> BorderRadiusHandler {
BorderRadiusHandler {
targets,
..BorderRadiusHandler::default()
}
}
}
impl PropertyHandler for BorderRadiusHandler {
fn handle_property(&mut self, property: &Property, dest: &mut DeclarationList) -> bool {
use Property::*;
macro_rules! property {
($prop: ident, $val: expr, $vp: ident) => {{
// If two vendor prefixes for the same property have different
// values, we need to flush what we have immediately to preserve order.
if let Some((val, prefixes)) = &self.$prop {
if val != $val && !prefixes.contains(*$vp) {
self.flush(dest);
}
}
// Otherwise, update the value and add the prefix.
if let Some((val, prefixes)) = &mut self.$prop {
*val = $val.clone();
*prefixes |= *$vp;
} else {
self.$prop = Some(($val.clone(), *$vp))
}
}};
}
match property {
BorderTopLeftRadius(val, vp) => property!(top_left, val, vp),
BorderTopRightRadius(val, vp) => property!(top_right, val, vp),
BorderBottomLeftRadius(val, vp) => property!(bottom_left, val, vp),
BorderBottomRightRadius(val, vp) => property!(bottom_right, val, vp),
BorderStartStartRadius(_) | BorderStartEndRadius(_) | BorderEndStartRadius(_) | BorderEndEndRadius(_) => {
self.flush(dest);
self.logical.push(property.clone());
}
BorderRadius(val, vp) => {
self.logical.clear();
property!(top_left, &val.top_left, vp);
property!(top_right, &val.top_right, vp);
property!(bottom_left, &val.bottom_left, vp);
property!(bottom_right, &val.bottom_right, vp);
}
_ => return false
}
true
}
fn finalize(&mut self, dest: &mut DeclarationList) {
self.flush(dest);
}
}
impl BorderRadiusHandler {
fn flush(&mut self, dest: &mut DeclarationList) {
let mut top_left = std::mem::take(&mut self.top_left);
let mut top_right = std::mem::take(&mut self.top_right);
let mut bottom_left = std::mem::take(&mut self.bottom_left);
let mut bottom_right = std::mem::take(&mut self.bottom_right);
dest.extend(&mut self.logical);
if let (Some((top_left, tl_prefix)), Some((top_right, tr_prefix)), Some((bottom_left, bl_prefix)), Some((bottom_right, br_prefix))) = (&mut top_left, &mut top_right, &mut bottom_left, &mut bottom_right) {
let intersection = *tl_prefix & *tr_prefix & *bl_prefix & *br_prefix;
if !intersection.is_empty() {
let mut prefix = intersection;
if prefix.contains(VendorPrefix::None) {
if let Some(targets) = self.targets {
prefix = Feature::BorderRadius.prefixes_for(targets)
}
}
dest.push(Property::BorderRadius(BorderRadius {
top_left: top_left.clone(),
top_right: top_right.clone(),
bottom_left: bottom_left.clone(),
bottom_right: bottom_right.clone(),
}, prefix));
tl_prefix.remove(intersection);
tr_prefix.remove(intersection);
bl_prefix.remove(intersection);
br_prefix.remove(intersection);
}
}
macro_rules! single_property {
($prop: ident, $key: ident) => {
if let Some((val, mut vp)) = $key {
if !vp.is_empty() {
if vp.contains(VendorPrefix::None) {
if let Some(targets) = self.targets {
vp = Feature::$prop.prefixes_for(targets)
}
}
dest.push(Property::$prop(val, vp))
}
}
};
}
single_property!(BorderTopLeftRadius, top_left);
single_property!(BorderTopRightRadius, top_right);
single_property!(BorderBottomLeftRadius, bottom_left);
single_property!(BorderBottomRightRadius, bottom_right);
}
}