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
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
//! The CSS border radius property.

use crate::compat;
use crate::context::PropertyHandlerContext;
use crate::declaration::{DeclarationBlock, DeclarationList};
use crate::error::{ParserError, PrinterError};
use crate::logical::PropertyCategory;
use crate::macros::define_shorthand;
use crate::prefixes::Feature;
use crate::printer::Printer;
use crate::properties::{Property, PropertyId, VendorPrefix};
use crate::targets::Browsers;
use crate::traits::{Parse, PropertyHandler, Shorthand, ToCss, Zero};
use crate::values::length::*;
use crate::values::rect::Rect;
use crate::values::size::Size2D;
use cssparser::*;

define_shorthand! {
  /// A value for the [border-radius](https://www.w3.org/TR/css-backgrounds-3/#border-radius) property.
  pub struct BorderRadius(VendorPrefix) {
    /// The x and y radius values for the top left corner.
    top_left: BorderTopLeftRadius(Size2D<LengthPercentage>, VendorPrefix),
    /// The x and y radius values for the top right corner.
    top_right: BorderTopRightRadius(Size2D<LengthPercentage>, VendorPrefix),
    /// The x and y radius values for the bottom left corner.
    bottom_left: BorderBottomLeftRadius(Size2D<LengthPercentage>, VendorPrefix),
    /// The x and y radius values for the bottom right corner.
    bottom_right: BorderBottomRightRadius(Size2D<LengthPercentage>, VendorPrefix),
  }
}

impl Default for BorderRadius {
  fn default() -> BorderRadius {
    let zero = Size2D(LengthPercentage::zero(), LengthPercentage::zero());
    BorderRadius {
      top_left: zero.clone(),
      top_right: zero.clone(),
      bottom_left: zero.clone(),
      bottom_right: zero,
    }
  }
}

impl<'i> Parse<'i> for BorderRadius {
  fn parse<'t>(input: &mut Parser<'i, 't>) -> Result<Self, ParseError<'i, ParserError<'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>) -> Result<(), PrinterError>
  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<'i> {
  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)>,
  start_start: Option<Property<'i>>,
  start_end: Option<Property<'i>>,
  end_start: Option<Property<'i>>,
  end_end: Option<Property<'i>>,
  category: PropertyCategory,
  has_any: bool,
}

impl<'i> BorderRadiusHandler<'i> {
  pub fn new(targets: Option<Browsers>) -> Self {
    BorderRadiusHandler {
      targets,
      ..BorderRadiusHandler::default()
    }
  }
}

impl<'i> PropertyHandler<'i> for BorderRadiusHandler<'i> {
  fn handle_property(
    &mut self,
    property: &Property<'i>,
    dest: &mut DeclarationList<'i>,
    context: &mut PropertyHandlerContext<'i, '_>,
  ) -> bool {
    use Property::*;

    macro_rules! maybe_flush {
      ($prop: ident, $val: expr, $vp: expr) => {{
        // 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, context);
          }
        }
      }};
    }

    macro_rules! property {
      ($prop: ident, $val: expr, $vp: ident) => {{
        if self.category != PropertyCategory::Physical {
          self.flush(dest, context);
        }

        maybe_flush!($prop, $val, $vp);

        // 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));
          self.has_any = true;
        }

        self.category = PropertyCategory::Physical;
      }};
    }

    macro_rules! logical_property {
      ($prop: ident) => {{
        if self.category != PropertyCategory::Logical {
          self.flush(dest, context);
        }

        self.$prop = Some(property.clone());
        self.category = PropertyCategory::Logical;
        self.has_any = true;
      }};
    }

    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(_) => logical_property!(start_start),
      BorderStartEndRadius(_) => logical_property!(start_end),
      BorderEndStartRadius(_) => logical_property!(end_start),
      BorderEndEndRadius(_) => logical_property!(end_end),
      BorderRadius(val, vp) => {
        self.start_start = None;
        self.start_end = None;
        self.end_start = None;
        self.end_end = None;
        maybe_flush!(top_left, &val.top_left, vp);
        maybe_flush!(top_right, &val.top_right, vp);
        maybe_flush!(bottom_left, &val.bottom_left, vp);
        maybe_flush!(bottom_right, &val.bottom_right, vp);
        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);
      }
      Unparsed(val) if is_border_radius_property(&val.property_id) => {
        // Even if we weren't able to parse the value (e.g. due to var() references),
        // we can still add vendor prefixes to the property itself.
        match &val.property_id {
          PropertyId::BorderStartStartRadius => logical_property!(start_start),
          PropertyId::BorderStartEndRadius => logical_property!(start_end),
          PropertyId::BorderEndStartRadius => logical_property!(end_start),
          PropertyId::BorderEndEndRadius => logical_property!(end_end),
          _ => {
            self.flush(dest, context);
            dest.push(Property::Unparsed(
              val.get_prefixed(self.targets, Feature::BorderRadius),
            ));
          }
        }
      }
      _ => return false,
    }

    true
  }

  fn finalize(&mut self, dest: &mut DeclarationList<'i>, context: &mut PropertyHandlerContext<'i, '_>) {
    self.flush(dest, context);
  }
}

impl<'i> BorderRadiusHandler<'i> {
  fn flush(&mut self, dest: &mut DeclarationList<'i>, context: &mut PropertyHandlerContext<'i, '_>) {
    if !self.has_any {
      return;
    }

    self.has_any = false;

    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);
    let start_start = std::mem::take(&mut self.start_start);
    let start_end = std::mem::take(&mut self.start_end);
    let end_start = std::mem::take(&mut self.end_start);
    let end_end = std::mem::take(&mut self.end_end);

    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))
          }
        }
      };
    }

    let logical_supported = context.is_supported(compat::Feature::LogicalBorderRadius);

    macro_rules! logical_property {
      ($prop: ident, $key: ident, $ltr: ident, $rtl: ident) => {
        if let Some(val) = $key {
          if logical_supported {
            dest.push(val);
          } else {
            let vp = if let Some(targets) = self.targets {
              Feature::$ltr.prefixes_for(targets)
            } else {
              VendorPrefix::None
            };

            match val {
              Property::BorderStartStartRadius(val)
              | Property::BorderStartEndRadius(val)
              | Property::BorderEndStartRadius(val)
              | Property::BorderEndEndRadius(val) => {
                context.add_logical_rule(Property::$ltr(val.clone(), vp), Property::$rtl(val, vp));
              }
              Property::Unparsed(val) => {
                context.add_logical_rule(
                  Property::Unparsed(val.with_property_id(PropertyId::$ltr(vp))),
                  Property::Unparsed(val.with_property_id(PropertyId::$rtl(vp))),
                );
              }
              _ => {}
            }
          }
        }
      };
    }

    single_property!(BorderTopLeftRadius, top_left);
    single_property!(BorderTopRightRadius, top_right);
    single_property!(BorderBottomLeftRadius, bottom_left);
    single_property!(BorderBottomRightRadius, bottom_right);
    logical_property!(
      BorderStartStartRadius,
      start_start,
      BorderTopLeftRadius,
      BorderTopRightRadius
    );
    logical_property!(
      BorderStartEndRadius,
      start_end,
      BorderTopRightRadius,
      BorderTopLeftRadius
    );
    logical_property!(
      BorderEndStartRadius,
      end_start,
      BorderBottomLeftRadius,
      BorderBottomRightRadius
    );
    logical_property!(
      BorderEndEndRadius,
      end_end,
      BorderBottomRightRadius,
      BorderBottomLeftRadius
    );
  }
}

#[inline]
fn is_border_radius_property(property_id: &PropertyId) -> bool {
  if is_logical_border_radius_property(property_id) {
    return true;
  }

  match property_id {
    PropertyId::BorderTopLeftRadius(_)
    | PropertyId::BorderTopRightRadius(_)
    | PropertyId::BorderBottomLeftRadius(_)
    | PropertyId::BorderBottomRightRadius(_)
    | PropertyId::BorderRadius(_) => true,
    _ => false,
  }
}

#[inline]
fn is_logical_border_radius_property(property_id: &PropertyId) -> bool {
  match property_id {
    PropertyId::BorderStartStartRadius
    | PropertyId::BorderStartEndRadius
    | PropertyId::BorderEndStartRadius
    | PropertyId::BorderEndEndRadius => true,
    _ => false,
  }
}