Skip to main content

rspack_lightningcss/properties/
border.rs

1//! CSS properties related to borders.
2
3use super::border_image::*;
4use super::border_radius::*;
5use crate::compat::Feature;
6use crate::context::PropertyHandlerContext;
7use crate::declaration::{DeclarationBlock, DeclarationList};
8use crate::error::{ParserError, PrinterError};
9use crate::logical::PropertyCategory;
10use crate::macros::*;
11use crate::properties::custom::UnparsedProperty;
12use crate::properties::{Property, PropertyId};
13use crate::targets::Browsers;
14use crate::targets::Targets;
15use crate::traits::{FallbackValues, IsCompatible, Parse, PropertyHandler, Shorthand, ToCss};
16use crate::values::color::{ColorFallbackKind, CssColor};
17use crate::values::length::*;
18use crate::values::number::NonNegative;
19use crate::values::rect::Rect;
20use crate::values::size::Size2D;
21#[cfg(feature = "visitor")]
22use crate::visitor::Visit;
23use cssparser::*;
24
25/// A value for the [border-width](https://www.w3.org/TR/css-backgrounds-3/#border-width) property.
26#[derive(Debug, Clone, PartialEq, Parse, ToCss)]
27#[cfg_attr(feature = "visitor", derive(Visit))]
28#[cfg_attr(
29  feature = "serde",
30  derive(serde::Serialize, serde::Deserialize),
31  serde(tag = "type", content = "value", rename_all = "kebab-case")
32)]
33#[cfg_attr(feature = "jsonschema", derive(schemars::JsonSchema))]
34#[cfg_attr(feature = "into_owned", derive(rspack_lightningcss_static_self::IntoOwned))]
35pub enum BorderSideWidth {
36  /// A UA defined `thin` value.
37  Thin,
38  /// A UA defined `medium` value.
39  Medium,
40  /// A UA defined `thick` value.
41  Thick,
42  /// An explicit width.
43  Length(NonNegative<Length>),
44}
45
46impl Default for BorderSideWidth {
47  fn default() -> BorderSideWidth {
48    BorderSideWidth::Medium
49  }
50}
51
52impl IsCompatible for BorderSideWidth {
53  fn is_compatible(&self, browsers: Browsers) -> bool {
54    match self {
55      BorderSideWidth::Length(length) => length.is_compatible(browsers),
56      _ => true,
57    }
58  }
59}
60
61enum_property! {
62  /// A [`<line-style>`](https://drafts.csswg.org/css-backgrounds/#typedef-line-style) value, used in the `border-style` property.
63  pub enum LineStyle {
64    /// No border.
65    None,
66    /// Similar to `none` but with different rules for tables.
67    Hidden,
68    /// Looks as if the content on the inside of the border is sunken into the canvas.
69    Inset,
70    /// Looks as if it were carved in the canvas.
71    Groove,
72    /// Looks as if the content on the inside of the border is coming out of the canvas.
73    Outset,
74    /// Looks as if it were coming out of the canvas.
75    Ridge,
76    /// A series of round dots.
77    Dotted,
78    /// A series of square-ended dashes.
79    Dashed,
80    /// A single line segment.
81    Solid,
82    /// Two parallel solid lines with some space between them.
83    Double,
84  }
85}
86
87impl Default for LineStyle {
88  fn default() -> LineStyle {
89    LineStyle::None
90  }
91}
92
93impl IsCompatible for LineStyle {
94  fn is_compatible(&self, _browsers: Browsers) -> bool {
95    true
96  }
97}
98
99/// A generic type that represents the `border` and `outline` shorthand properties.
100#[derive(Debug, Clone, PartialEq)]
101#[cfg_attr(feature = "visitor", derive(Visit))]
102#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
103#[cfg_attr(feature = "jsonschema", derive(schemars::JsonSchema))]
104pub struct GenericBorder<S, const P: u8> {
105  /// The width of the border.
106  pub width: BorderSideWidth,
107  /// The border style.
108  pub style: S,
109  /// The border color.
110  pub color: CssColor,
111}
112
113#[cfg(feature = "into_owned")]
114impl<'any, S, const P: u8> rspack_lightningcss_static_self::IntoOwned<'any> for GenericBorder<S, P>
115where
116  S: rspack_lightningcss_static_self::IntoOwned<'any>,
117{
118  type Owned = GenericBorder<S::Owned, P>;
119  fn into_owned(self) -> Self::Owned {
120    GenericBorder {
121      width: self.width,
122      style: self.style.into_owned(),
123      color: self.color,
124    }
125  }
126}
127
128impl<S: Default, const P: u8> Default for GenericBorder<S, P> {
129  fn default() -> GenericBorder<S, P> {
130    GenericBorder {
131      width: BorderSideWidth::Medium,
132      style: S::default(),
133      color: CssColor::current_color(),
134    }
135  }
136}
137
138impl<'i, S: Parse<'i> + Default, const P: u8> Parse<'i> for GenericBorder<S, P> {
139  fn parse<'t>(input: &mut Parser<'i, 't>) -> Result<Self, ParseError<'i, ParserError<'i>>> {
140    // Order doesn't matter...
141    let mut color = None;
142    let mut style = None;
143    let mut width = None;
144    let mut any = false;
145    loop {
146      if width.is_none() {
147        if let Ok(value) = input.try_parse(|i| BorderSideWidth::parse(i)) {
148          width = Some(value);
149          any = true;
150        }
151      }
152      if style.is_none() {
153        if let Ok(value) = input.try_parse(S::parse) {
154          style = Some(value);
155          any = true;
156          continue;
157        }
158      }
159      if color.is_none() {
160        if let Ok(value) = input.try_parse(|i| CssColor::parse(i)) {
161          color = Some(value);
162          any = true;
163          continue;
164        }
165      }
166      break;
167    }
168    if any {
169      Ok(GenericBorder {
170        width: width.unwrap_or(BorderSideWidth::Medium),
171        style: style.unwrap_or_default(),
172        color: color.unwrap_or_else(|| CssColor::current_color()),
173      })
174    } else {
175      Err(input.new_custom_error(ParserError::InvalidDeclaration))
176    }
177  }
178}
179
180impl<S: ToCss + Default + PartialEq, const P: u8> ToCss for GenericBorder<S, P> {
181  fn to_css<PrinterT: crate::printer::PrinterTrait>(&self, dest: &mut PrinterT) -> Result<(), PrinterError> {
182    if *self == Self::default() {
183      self.style.to_css(dest)?;
184      return Ok(());
185    }
186
187    let mut needs_space = false;
188    if self.width != BorderSideWidth::default() {
189      self.width.to_css(dest)?;
190      needs_space = true;
191    }
192    if self.style != S::default() {
193      if needs_space {
194        dest.write_str(" ")?;
195      }
196      self.style.to_css(dest)?;
197      needs_space = true;
198    }
199    if self.color != CssColor::current_color() {
200      if needs_space {
201        dest.write_str(" ")?;
202      }
203      self.color.to_css(dest)?;
204    }
205    Ok(())
206  }
207}
208
209impl<S: Clone, const P: u8> FallbackValues for GenericBorder<S, P> {
210  fn get_fallbacks(&mut self, targets: Targets) -> Vec<Self> {
211    self
212      .color
213      .get_fallbacks(targets)
214      .into_iter()
215      .map(|color| GenericBorder {
216        color,
217        width: self.width.clone(),
218        style: self.style.clone(),
219      })
220      .collect()
221  }
222}
223
224/// A value for the [border-top](https://www.w3.org/TR/css-backgrounds-3/#propdef-border-top) shorthand property.
225pub type BorderTop = GenericBorder<LineStyle, 0>;
226/// A value for the [border-right](https://www.w3.org/TR/css-backgrounds-3/#propdef-border-right) shorthand property.
227pub type BorderRight = GenericBorder<LineStyle, 1>;
228/// A value for the [border-bottom](https://www.w3.org/TR/css-backgrounds-3/#propdef-border-bottom) shorthand property.
229pub type BorderBottom = GenericBorder<LineStyle, 2>;
230/// A value for the [border-left](https://www.w3.org/TR/css-backgrounds-3/#propdef-border-left) shorthand property.
231pub type BorderLeft = GenericBorder<LineStyle, 3>;
232/// A value for the [border-block-start](https://drafts.csswg.org/css-logical/#propdef-border-block-start) shorthand property.
233pub type BorderBlockStart = GenericBorder<LineStyle, 4>;
234/// A value for the [border-block-end](https://drafts.csswg.org/css-logical/#propdef-border-block-end) shorthand property.
235pub type BorderBlockEnd = GenericBorder<LineStyle, 5>;
236/// A value for the [border-inline-start](https://drafts.csswg.org/css-logical/#propdef-border-inline-start) shorthand property.
237pub type BorderInlineStart = GenericBorder<LineStyle, 6>;
238/// A value for the [border-inline-end](https://drafts.csswg.org/css-logical/#propdef-border-inline-end) shorthand property.
239pub type BorderInlineEnd = GenericBorder<LineStyle, 7>;
240/// A value for the [border-block](https://drafts.csswg.org/css-logical/#propdef-border-block) shorthand property.
241pub type BorderBlock = GenericBorder<LineStyle, 8>;
242/// A value for the [border-inline](https://drafts.csswg.org/css-logical/#propdef-border-inline) shorthand property.
243pub type BorderInline = GenericBorder<LineStyle, 9>;
244/// A value for the [border](https://www.w3.org/TR/css-backgrounds-3/#propdef-border) shorthand property.
245pub type Border = GenericBorder<LineStyle, 10>;
246
247impl_shorthand! {
248  BorderTop(BorderTop) {
249    width: [BorderTopWidth],
250    style: [BorderTopStyle],
251    color: [BorderTopColor],
252  }
253}
254
255impl_shorthand! {
256  BorderRight(BorderRight) {
257    width: [BorderRightWidth],
258    style: [BorderRightStyle],
259    color: [BorderRightColor],
260  }
261}
262
263impl_shorthand! {
264  BorderBottom(BorderBottom) {
265    width: [BorderBottomWidth],
266    style: [BorderBottomStyle],
267    color: [BorderBottomColor],
268  }
269}
270
271impl_shorthand! {
272  BorderLeft(BorderLeft) {
273    width: [BorderLeftWidth],
274    style: [BorderLeftStyle],
275    color: [BorderLeftColor],
276  }
277}
278
279impl_shorthand! {
280  BorderBlockStart(BorderBlockStart) {
281    width: [BorderBlockStartWidth],
282    style: [BorderBlockStartStyle],
283    color: [BorderBlockStartColor],
284  }
285}
286
287impl_shorthand! {
288  BorderBlockEnd(BorderBlockEnd) {
289    width: [BorderBlockEndWidth],
290    style: [BorderBlockEndStyle],
291    color: [BorderBlockEndColor],
292  }
293}
294
295impl_shorthand! {
296  BorderInlineStart(BorderInlineStart) {
297    width: [BorderInlineStartWidth],
298    style: [BorderInlineStartStyle],
299    color: [BorderInlineStartColor],
300  }
301}
302
303impl_shorthand! {
304  BorderInlineEnd(BorderInlineEnd) {
305    width: [BorderInlineEndWidth],
306    style: [BorderInlineEndStyle],
307    color: [BorderInlineEndColor],
308  }
309}
310
311impl_shorthand! {
312  BorderBlock(BorderBlock) {
313    width: [BorderBlockStartWidth, BorderBlockEndWidth],
314    style: [BorderBlockStartStyle, BorderBlockEndStyle],
315    color: [BorderBlockStartColor, BorderBlockEndColor],
316  }
317}
318
319impl_shorthand! {
320  BorderInline(BorderInline) {
321    width: [BorderInlineStartWidth, BorderInlineEndWidth],
322    style: [BorderInlineStartStyle, BorderInlineEndStyle],
323    color: [BorderInlineStartColor, BorderInlineEndColor],
324  }
325}
326
327impl_shorthand! {
328  Border(Border) {
329    width: [BorderTopWidth, BorderRightWidth, BorderBottomWidth, BorderLeftWidth],
330    style: [BorderTopStyle, BorderRightStyle, BorderBottomStyle, BorderLeftStyle],
331    color: [BorderTopColor, BorderRightColor, BorderBottomColor, BorderLeftColor],
332  }
333}
334
335size_shorthand! {
336  /// A value for the [border-block-color](https://drafts.csswg.org/css-logical/#propdef-border-block-color) shorthand property.
337  pub struct BorderBlockColor<CssColor> {
338    /// The block start value.
339    start: BorderBlockStartColor,
340    /// The block end value.
341    end: BorderBlockEndColor,
342  }
343}
344
345size_shorthand! {
346  /// A value for the [border-block-style](https://drafts.csswg.org/css-logical/#propdef-border-block-style) shorthand property.
347  pub struct BorderBlockStyle<LineStyle> {
348    /// The block start value.
349    start: BorderBlockStartStyle,
350    /// The block end value.
351    end: BorderBlockEndStyle,
352  }
353}
354
355size_shorthand! {
356  /// A value for the [border-block-width](https://drafts.csswg.org/css-logical/#propdef-border-block-width) shorthand property.
357  pub struct BorderBlockWidth<BorderSideWidth> {
358    /// The block start value.
359    start: BorderBlockStartWidth,
360    /// The block end value.
361    end: BorderBlockEndWidth,
362  }
363}
364
365size_shorthand! {
366  /// A value for the [border-inline-color](https://drafts.csswg.org/css-logical/#propdef-border-inline-color) shorthand property.
367  pub struct BorderInlineColor<CssColor> {
368    /// The inline start value.
369    start: BorderInlineStartColor,
370    /// The inline end value.
371    end: BorderInlineEndColor,
372  }
373}
374
375size_shorthand! {
376  /// A value for the [border-inline-style](https://drafts.csswg.org/css-logical/#propdef-border-inline-style) shorthand property.
377  pub struct BorderInlineStyle<LineStyle> {
378    /// The inline start value.
379    start: BorderInlineStartStyle,
380    /// The inline end value.
381    end: BorderInlineEndStyle,
382  }
383}
384
385size_shorthand! {
386  /// A value for the [border-inline-width](https://drafts.csswg.org/css-logical/#propdef-border-inline-width) shorthand property.
387  pub struct BorderInlineWidth<BorderSideWidth> {
388    /// The inline start value.
389    start: BorderInlineStartWidth,
390    /// The inline end value.
391    end: BorderInlineEndWidth,
392  }
393}
394
395rect_shorthand! {
396  /// A value for the [border-color](https://drafts.csswg.org/css-backgrounds/#propdef-border-color) shorthand property.
397  pub struct BorderColor<CssColor> {
398    BorderTopColor,
399    BorderRightColor,
400    BorderBottomColor,
401    BorderLeftColor
402  }
403}
404
405rect_shorthand! {
406  /// A value for the [border-style](https://drafts.csswg.org/css-backgrounds/#propdef-border-style) shorthand property.
407  pub struct BorderStyle<LineStyle> {
408    BorderTopStyle,
409    BorderRightStyle,
410    BorderBottomStyle,
411    BorderLeftStyle
412  }
413}
414
415rect_shorthand! {
416  /// A value for the [border-width](https://drafts.csswg.org/css-backgrounds/#propdef-border-width) shorthand property.
417  pub struct BorderWidth<BorderSideWidth> {
418    BorderTopWidth,
419    BorderRightWidth,
420    BorderBottomWidth,
421    BorderLeftWidth
422  }
423}
424
425macro_rules! impl_fallbacks {
426  ($t: ident $(, $name: ident)+) => {
427    impl FallbackValues for $t {
428      fn get_fallbacks(&mut self, targets: Targets) -> Vec<Self> {
429        let mut fallbacks = ColorFallbackKind::empty();
430        $(
431          fallbacks |= self.$name.get_necessary_fallbacks(targets);
432        )+
433
434        let mut res = Vec::new();
435        if fallbacks.contains(ColorFallbackKind::RGB) {
436          res.push($t {
437            $(
438              $name: self.$name.get_fallback(ColorFallbackKind::RGB),
439            )+
440          });
441        }
442
443        if fallbacks.contains(ColorFallbackKind::P3) {
444          res.push($t {
445            $(
446              $name: self.$name.get_fallback(ColorFallbackKind::P3),
447            )+
448          });
449        }
450
451        if fallbacks.contains(ColorFallbackKind::LAB) {
452          $(
453            self.$name = self.$name.get_fallback(ColorFallbackKind::LAB);
454          )+
455        }
456
457        res
458      }
459    }
460  }
461}
462
463impl_fallbacks!(BorderBlockColor, start, end);
464impl_fallbacks!(BorderInlineColor, start, end);
465impl_fallbacks!(BorderColor, top, right, bottom, left);
466
467#[derive(Default, Debug, PartialEq)]
468struct BorderShorthand {
469  pub width: Option<BorderSideWidth>,
470  pub style: Option<LineStyle>,
471  pub color: Option<CssColor>,
472}
473
474impl BorderShorthand {
475  pub fn set_border<const P: u8>(&mut self, border: &GenericBorder<LineStyle, P>) {
476    self.width = Some(border.width.clone());
477    self.style = Some(border.style.clone());
478    self.color = Some(border.color.clone());
479  }
480
481  pub fn is_valid(&self) -> bool {
482    self.width.is_some() && self.style.is_some() && self.color.is_some()
483  }
484
485  pub fn reset(&mut self) {
486    self.width = None;
487    self.style = None;
488    self.color = None;
489  }
490
491  pub fn to_border<const P: u8>(&self) -> GenericBorder<LineStyle, P> {
492    GenericBorder {
493      width: self.width.clone().unwrap(),
494      style: self.style.clone().unwrap(),
495      color: self.color.clone().unwrap(),
496    }
497  }
498}
499
500property_bitflags! {
501  #[derive(Debug, Default)]
502  struct BorderProperty: u32 {
503    const BorderTopColor = 1 << 0;
504    const BorderBottomColor = 1 << 1;
505    const BorderLeftColor = 1 << 2;
506    const BorderRightColor = 1 << 3;
507    const BorderBlockStartColor = 1 << 4;
508    const BorderBlockEndColor = 1 << 5;
509    const BorderBlockColor = Self::BorderBlockStartColor.bits() | Self::BorderBlockEndColor.bits();
510    const BorderInlineStartColor = 1 << 6;
511    const BorderInlineEndColor = 1 << 7;
512    const BorderInlineColor = Self::BorderInlineStartColor.bits() | Self::BorderInlineEndColor.bits();
513    const BorderTopWidth = 1 << 8;
514    const BorderBottomWidth = 1 << 9;
515    const BorderLeftWidth = 1 << 10;
516    const BorderRightWidth = 1 << 11;
517    const BorderBlockStartWidth = 1 << 12;
518    const BorderBlockEndWidth = 1 << 13;
519    const BorderBlockWidth = Self::BorderBlockStartWidth.bits() | Self::BorderBlockEndWidth.bits();
520    const BorderInlineStartWidth = 1 << 14;
521    const BorderInlineEndWidth = 1 << 15;
522    const BorderInlineWidth = Self::BorderInlineStartWidth.bits() | Self::BorderInlineEndWidth.bits();
523    const BorderTopStyle = 1 << 16;
524    const BorderBottomStyle = 1 << 17;
525    const BorderLeftStyle = 1 << 18;
526    const BorderRightStyle = 1 << 19;
527    const BorderBlockStartStyle = 1 << 20;
528    const BorderBlockEndStyle = 1 << 21;
529    const BorderBlockStyle = Self::BorderBlockStartStyle.bits() | Self::BorderBlockEndStyle.bits();
530    const BorderInlineStartStyle = 1 << 22;
531    const BorderInlineEndStyle = 1 << 23;
532    const BorderInlineStyle = Self::BorderInlineStartStyle.bits() | Self::BorderInlineEndStyle.bits();
533    const BorderTop = Self::BorderTopColor.bits() | Self::BorderTopWidth.bits() | Self::BorderTopStyle.bits();
534    const BorderBottom = Self::BorderBottomColor.bits() | Self::BorderBottomWidth.bits() | Self::BorderBottomStyle.bits();
535    const BorderLeft = Self::BorderLeftColor.bits() | Self::BorderLeftWidth.bits() | Self::BorderLeftStyle.bits();
536    const BorderRight = Self::BorderRightColor.bits() | Self::BorderRightWidth.bits() | Self::BorderRightStyle.bits();
537    const BorderBlockStart = Self::BorderBlockStartColor.bits() | Self::BorderBlockStartWidth.bits() | Self::BorderBlockStartStyle.bits();
538    const BorderBlockEnd = Self::BorderBlockEndColor.bits() | Self::BorderBlockEndWidth.bits() | Self::BorderBlockEndStyle.bits();
539    const BorderInlineStart = Self::BorderInlineStartColor.bits() | Self::BorderInlineStartWidth.bits() | Self::BorderInlineStartStyle.bits();
540    const BorderInlineEnd = Self::BorderInlineEndColor.bits() | Self::BorderInlineEndWidth.bits() | Self::BorderInlineEndStyle.bits();
541    const BorderBlock = Self::BorderBlockStart.bits() | Self::BorderBlockEnd.bits();
542    const BorderInline = Self::BorderInlineStart.bits() | Self::BorderInlineEnd.bits();
543    const BorderWidth = Self::BorderLeftWidth.bits() | Self::BorderRightWidth.bits() | Self::BorderTopWidth.bits() | Self::BorderBottomWidth.bits();
544    const BorderStyle = Self::BorderLeftStyle.bits() | Self::BorderRightStyle.bits() | Self::BorderTopStyle.bits() | Self::BorderBottomStyle.bits();
545    const BorderColor = Self::BorderLeftColor.bits() | Self::BorderRightColor.bits() | Self::BorderTopColor.bits() | Self::BorderBottomColor.bits();
546    const Border = Self::BorderWidth.bits() | Self::BorderStyle.bits() | Self::BorderColor.bits();
547  }
548}
549
550#[derive(Debug, Default)]
551pub(crate) struct BorderHandler<'i> {
552  border_top: BorderShorthand,
553  border_bottom: BorderShorthand,
554  border_left: BorderShorthand,
555  border_right: BorderShorthand,
556  border_block_start: BorderShorthand,
557  border_block_end: BorderShorthand,
558  border_inline_start: BorderShorthand,
559  border_inline_end: BorderShorthand,
560  category: PropertyCategory,
561  border_image_handler: BorderImageHandler<'i>,
562  border_radius_handler: BorderRadiusHandler<'i>,
563  flushed_properties: BorderProperty,
564  has_any: bool,
565}
566
567impl<'i> PropertyHandler<'i> for BorderHandler<'i> {
568  fn handle_property(
569    &mut self,
570    property: &Property<'i>,
571    dest: &mut DeclarationList<'i>,
572    context: &mut PropertyHandlerContext<'i, '_>,
573  ) -> bool {
574    use Property::*;
575
576    macro_rules! flush {
577      ($key: ident, $prop: ident, $val: expr, $category: ident) => {{
578        if PropertyCategory::$category != self.category {
579          self.flush(dest, context);
580        }
581
582        if self.$key.$prop.is_some() && self.$key.$prop.as_ref().unwrap() != $val && matches!(context.targets.browsers, Some(targets) if !$val.is_compatible(targets)) {
583          self.flush(dest, context);
584        }
585      }};
586    }
587
588    macro_rules! property {
589      ($key: ident, $prop: ident, $val: expr, $category: ident) => {{
590        flush!($key, $prop, $val, $category);
591        self.$key.$prop = Some($val.clone());
592        self.category = PropertyCategory::$category;
593        self.has_any = true;
594      }};
595    }
596
597    macro_rules! set_border {
598      ($key: ident, $val: ident, $category: ident) => {{
599        if PropertyCategory::$category != self.category {
600          self.flush(dest, context);
601        }
602        self.$key.set_border($val);
603        self.category = PropertyCategory::$category;
604        self.has_any = true;
605      }};
606    }
607
608    match &property {
609      BorderTopColor(val) => property!(border_top, color, val, Physical),
610      BorderBottomColor(val) => property!(border_bottom, color, val, Physical),
611      BorderLeftColor(val) => property!(border_left, color, val, Physical),
612      BorderRightColor(val) => property!(border_right, color, val, Physical),
613      BorderBlockStartColor(val) => property!(border_block_start, color, val, Logical),
614      BorderBlockEndColor(val) => property!(border_block_end, color, val, Logical),
615      BorderBlockColor(val) => {
616        property!(border_block_start, color, &val.start, Logical);
617        property!(border_block_end, color, &val.end, Logical);
618      }
619      BorderInlineStartColor(val) => property!(border_inline_start, color, val, Logical),
620      BorderInlineEndColor(val) => property!(border_inline_end, color, val, Logical),
621      BorderInlineColor(val) => {
622        property!(border_inline_start, color, &val.start, Logical);
623        property!(border_inline_end, color, &val.end, Logical);
624      }
625      BorderTopWidth(val) => property!(border_top, width, val, Physical),
626      BorderBottomWidth(val) => property!(border_bottom, width, val, Physical),
627      BorderLeftWidth(val) => property!(border_left, width, val, Physical),
628      BorderRightWidth(val) => property!(border_right, width, val, Physical),
629      BorderBlockStartWidth(val) => property!(border_block_start, width, val, Logical),
630      BorderBlockEndWidth(val) => property!(border_block_end, width, val, Logical),
631      BorderBlockWidth(val) => {
632        property!(border_block_start, width, &val.start, Logical);
633        property!(border_block_end, width, &val.end, Logical);
634      }
635      BorderInlineStartWidth(val) => property!(border_inline_start, width, val, Logical),
636      BorderInlineEndWidth(val) => property!(border_inline_end, width, val, Logical),
637      BorderInlineWidth(val) => {
638        property!(border_inline_start, width, &val.start, Logical);
639        property!(border_inline_end, width, &val.end, Logical);
640      }
641      BorderTopStyle(val) => property!(border_top, style, val, Physical),
642      BorderBottomStyle(val) => property!(border_bottom, style, val, Physical),
643      BorderLeftStyle(val) => property!(border_left, style, val, Physical),
644      BorderRightStyle(val) => property!(border_right, style, val, Physical),
645      BorderBlockStartStyle(val) => property!(border_block_start, style, val, Logical),
646      BorderBlockEndStyle(val) => property!(border_block_end, style, val, Logical),
647      BorderBlockStyle(val) => {
648        property!(border_block_start, style, &val.start, Logical);
649        property!(border_block_end, style, &val.end, Logical);
650      }
651      BorderInlineStartStyle(val) => property!(border_inline_start, style, val, Logical),
652      BorderInlineEndStyle(val) => property!(border_inline_end, style, val, Logical),
653      BorderInlineStyle(val) => {
654        property!(border_inline_start, style, &val.start, Logical);
655        property!(border_inline_end, style, &val.end, Logical);
656      }
657      BorderTop(val) => set_border!(border_top, val, Physical),
658      BorderBottom(val) => set_border!(border_bottom, val, Physical),
659      BorderLeft(val) => set_border!(border_left, val, Physical),
660      BorderRight(val) => set_border!(border_right, val, Physical),
661      BorderBlockStart(val) => set_border!(border_block_start, val, Logical),
662      BorderBlockEnd(val) => set_border!(border_block_end, val, Logical),
663      BorderInlineStart(val) => set_border!(border_inline_start, val, Logical),
664      BorderInlineEnd(val) => set_border!(border_inline_end, val, Logical),
665      BorderBlock(val) => {
666        set_border!(border_block_start, val, Logical);
667        set_border!(border_block_end, val, Logical);
668      }
669      BorderInline(val) => {
670        set_border!(border_inline_start, val, Logical);
671        set_border!(border_inline_end, val, Logical);
672      }
673      BorderWidth(val) => {
674        property!(border_top, width, &val.top, Physical);
675        property!(border_right, width, &val.right, Physical);
676        property!(border_bottom, width, &val.bottom, Physical);
677        property!(border_left, width, &val.left, Physical);
678        self.border_block_start.width = None;
679        self.border_block_end.width = None;
680        self.border_inline_start.width = None;
681        self.border_inline_end.width = None;
682        self.has_any = true;
683      }
684      BorderStyle(val) => {
685        property!(border_top, style, &val.top, Physical);
686        property!(border_right, style, &val.right, Physical);
687        property!(border_bottom, style, &val.bottom, Physical);
688        property!(border_left, style, &val.left, Physical);
689        self.border_block_start.style = None;
690        self.border_block_end.style = None;
691        self.border_inline_start.style = None;
692        self.border_inline_end.style = None;
693        self.has_any = true;
694      }
695      BorderColor(val) => {
696        property!(border_top, color, &val.top, Physical);
697        property!(border_right, color, &val.right, Physical);
698        property!(border_bottom, color, &val.bottom, Physical);
699        property!(border_left, color, &val.left, Physical);
700        self.border_block_start.color = None;
701        self.border_block_end.color = None;
702        self.border_inline_start.color = None;
703        self.border_inline_end.color = None;
704        self.has_any = true;
705      }
706      Border(val) => {
707        // dest.clear();
708        self.border_top.set_border(val);
709        self.border_bottom.set_border(val);
710        self.border_left.set_border(val);
711        self.border_right.set_border(val);
712        self.border_block_start.reset();
713        self.border_block_end.reset();
714        self.border_inline_start.reset();
715        self.border_inline_end.reset();
716
717        // Setting the `border` property resets `border-image`.
718        self.border_image_handler.reset();
719        self.has_any = true;
720      }
721      Unparsed(val) if is_border_property(&val.property_id) => {
722        self.flush(dest, context);
723        self.flush_unparsed(&val, dest, context);
724      }
725      _ => {
726        if self.border_image_handler.will_flush(property) {
727          self.flush(dest, context);
728        }
729
730        return self.border_image_handler.handle_property(property, dest, context)
731          || self.border_radius_handler.handle_property(property, dest, context);
732      }
733    }
734
735    true
736  }
737
738  fn finalize(&mut self, dest: &mut DeclarationList<'i>, context: &mut PropertyHandlerContext<'i, '_>) {
739    self.flush(dest, context);
740    self.flushed_properties = BorderProperty::empty();
741    self.border_image_handler.finalize(dest, context);
742    self.border_radius_handler.finalize(dest, context);
743  }
744}
745
746impl<'i> BorderHandler<'i> {
747  // Fallback pushes are uncommon, and outlining them avoids duplicating large `Property` moves.
748  #[inline(never)]
749  fn push_fallback(dest: &mut DeclarationList<'i>, property: Property<'i>) {
750    dest.push(property);
751  }
752
753  fn flush(&mut self, dest: &mut DeclarationList<'i>, context: &mut PropertyHandlerContext<'i, '_>) {
754    if !self.has_any {
755      return;
756    }
757
758    self.has_any = false;
759
760    let logical_supported = !context.should_compile_logical(Feature::LogicalBorders);
761    let logical_shorthand_supported = !context.should_compile_logical(Feature::LogicalBorderShorthand);
762    macro_rules! logical_prop {
763      ($ltr: ident, $ltr_key: ident, $rtl: ident, $rtl_key: ident, $val: expr) => {{
764        context.add_logical_rule(Property::$ltr($val.clone()), Property::$rtl($val.clone()));
765      }};
766    }
767
768    macro_rules! logical_prop_move {
769      ($ltr: ident, $ltr_key: ident, $rtl: ident, $rtl_key: ident, $val: expr) => {{
770        let val = $val;
771        context.add_logical_rule(Property::$ltr(val.clone()), Property::$rtl(val));
772      }};
773    }
774
775    macro_rules! push {
776      ($prop: ident, $val: expr) => {{
777        self.flushed_properties.insert(BorderProperty::$prop);
778        dest.push(Property::$prop($val));
779      }};
780    }
781
782    macro_rules! fallbacks {
783      ($prop: ident => $val: expr) => {{
784        let mut val = $val;
785        if !self.flushed_properties.contains(BorderProperty::$prop) {
786          let fallbacks = val.get_fallbacks(context.targets);
787          for fallback in fallbacks {
788            Self::push_fallback(dest, Property::$prop(fallback))
789          }
790        }
791        push!($prop, val);
792      }};
793    }
794
795    macro_rules! prop {
796      (BorderInlineStart => $val: expr) => {
797        if logical_supported {
798          fallbacks!(BorderInlineStart => $val);
799        } else {
800          logical_prop!(BorderLeft, border_left, BorderRight, border_right, $val);
801        }
802      };
803      (BorderInlineStartWidth => $val: expr) => {
804        if logical_supported {
805          push!(BorderInlineStartWidth, $val);
806        } else {
807          logical_prop_move!(BorderLeftWidth, border_left_width, BorderRightWidth, border_right_width, $val);
808        }
809      };
810      (BorderInlineStartColor => $val: expr) => {
811        if logical_supported {
812          fallbacks!(BorderInlineStartColor => $val);
813        } else {
814          logical_prop_move!(BorderLeftColor, border_left_color, BorderRightColor, border_right_color, $val);
815        }
816      };
817      (BorderInlineStartStyle => $val: expr) => {
818        if logical_supported {
819          push!(BorderInlineStartStyle, $val);
820        } else {
821          logical_prop_move!(BorderLeftStyle, border_left_style, BorderRightStyle, border_right_style, $val);
822        }
823      };
824      (BorderInlineEnd => $val: expr) => {
825        if logical_supported {
826          fallbacks!(BorderInlineEnd => $val);
827        } else {
828          logical_prop!(BorderRight, border_right, BorderLeft, border_left, $val);
829        }
830      };
831      (BorderInlineEndWidth => $val: expr) => {
832        if logical_supported {
833          push!(BorderInlineEndWidth, $val);
834        } else {
835          logical_prop_move!(BorderRightWidth, border_right_width, BorderLeftWidth, border_left_width, $val);
836        }
837      };
838      (BorderInlineEndColor => $val: expr) => {
839        if logical_supported {
840          fallbacks!(BorderInlineEndColor => $val);
841        } else {
842          logical_prop_move!(BorderRightColor, border_right_color, BorderLeftColor, border_left_color, $val);
843        }
844      };
845      (BorderInlineEndStyle => $val: expr) => {
846        if logical_supported {
847          push!(BorderInlineEndStyle, $val);
848        } else {
849          logical_prop_move!(BorderRightStyle, border_right_style, BorderLeftStyle, border_left_style, $val);
850        }
851      };
852      (BorderBlockStart => $val: expr) => {
853        if logical_supported {
854          fallbacks!(BorderBlockStart => $val);
855        } else {
856          fallbacks!(BorderTop => $val);
857        }
858      };
859      (BorderBlockStartWidth => $val: expr) => {
860        if logical_supported {
861          push!(BorderBlockStartWidth, $val);
862        } else {
863          push!(BorderTopWidth, $val);
864        }
865      };
866      (BorderBlockStartColor => $val: expr) => {
867        if logical_supported {
868          fallbacks!(BorderBlockStartColor => $val);
869        } else {
870          fallbacks!(BorderTopColor => $val);
871        }
872      };
873      (BorderBlockStartStyle => $val: expr) => {
874        if logical_supported {
875          push!(BorderBlockStartStyle, $val);
876        } else {
877          push!(BorderTopStyle, $val);
878        }
879      };
880      (BorderBlockEnd => $val: expr) => {
881        if logical_supported {
882          fallbacks!(BorderBlockEnd => $val);
883        } else {
884          fallbacks!(BorderBottom => $val);
885        }
886      };
887      (BorderBlockEndWidth => $val: expr) => {
888        if logical_supported {
889          push!(BorderBlockEndWidth, $val);
890        } else {
891          push!(BorderBottomWidth, $val);
892        }
893      };
894      (BorderBlockEndColor => $val: expr) => {
895        if logical_supported {
896          fallbacks!(BorderBlockEndColor => $val);
897        } else {
898          fallbacks!(BorderBottomColor => $val);
899        }
900      };
901      (BorderBlockEndStyle => $val: expr) => {
902        if logical_supported {
903          push!(BorderBlockEndStyle, $val);
904        } else {
905          push!(BorderBottomStyle, $val);
906        }
907      };
908      (BorderLeftColor => $val: expr) => {
909        fallbacks!(BorderLeftColor => $val);
910      };
911      (BorderRightColor => $val: expr) => {
912        fallbacks!(BorderRightColor => $val);
913      };
914      (BorderTopColor => $val: expr) => {
915        fallbacks!(BorderTopColor => $val);
916      };
917      (BorderBottomColor => $val: expr) => {
918        fallbacks!(BorderBottomColor => $val);
919      };
920      (BorderColor => $val: expr) => {
921        fallbacks!(BorderColor => $val);
922      };
923      (BorderBlockColor => $val: expr) => {
924        fallbacks!(BorderBlockColor => $val);
925      };
926      (BorderInlineColor => $val: expr) => {
927        fallbacks!(BorderInlineColor => $val);
928      };
929      (BorderLeft => $val: expr) => {
930        fallbacks!(BorderLeft => $val);
931      };
932      (BorderRight => $val: expr) => {
933        fallbacks!(BorderRight => $val);
934      };
935      (BorderTop => $val: expr) => {
936        fallbacks!(BorderTop => $val);
937      };
938      (BorderBottom => $val: expr) => {
939        fallbacks!(BorderBottom => $val);
940      };
941      (BorderBlockStart => $val: expr) => {
942        fallbacks!(BorderBlockStart => $val);
943      };
944      (BorderBlockEnd => $val: expr) => {
945        fallbacks!(BorderBlockEnd => $val);
946      };
947      (BorderInlineStart => $val: expr) => {
948        fallbacks!(BorderInlineStart => $val);
949      };
950      (BorderInlineEnd => $val: expr) => {
951        fallbacks!(BorderInlineEnd => $val);
952      };
953      (BorderInline => $val: expr) => {
954        fallbacks!(BorderInline => $val);
955      };
956      (BorderBlock => $val: expr) => {
957        fallbacks!(BorderBlock => $val);
958      };
959      (Border => $val: expr) => {
960        fallbacks!(Border => $val);
961      };
962      ($prop: ident => $val: expr) => {
963        push!($prop, $val);
964      };
965    }
966
967    macro_rules! flush_category {
968      (
969        $block_start_prop: ident,
970        $block_start_width: ident,
971        $block_start_style: ident,
972        $block_start_color: ident,
973        $block_start: expr,
974        $block_end_prop: ident,
975        $block_end_width: ident,
976        $block_end_style: ident,
977        $block_end_color: ident,
978        $block_end: expr,
979        $inline_start_prop: ident,
980        $inline_start_width: ident,
981        $inline_start_style: ident,
982        $inline_start_color: ident,
983        $inline_start: expr,
984        $inline_end_prop: ident,
985        $inline_end_width: ident,
986        $inline_end_style: ident,
987        $inline_end_color: ident,
988        $inline_end: expr,
989        $is_logical: expr
990      ) => {
991        macro_rules! shorthand {
992          ($prop: ident, $key: ident) => {{
993            let has_prop = $block_start.$key.is_some() && $block_end.$key.is_some() && $inline_start.$key.is_some() && $inline_end.$key.is_some();
994            if has_prop {
995              if !$is_logical || ($block_start.$key == $block_end.$key && $block_end.$key == $inline_start.$key && $inline_start.$key == $inline_end.$key) {
996                let rect = $prop {
997                  top: std::mem::take(&mut $block_start.$key).unwrap(),
998                  right: std::mem::take(&mut $inline_end.$key).unwrap(),
999                  bottom: std::mem::take(&mut $block_end.$key).unwrap(),
1000                  left: std::mem::take(&mut $inline_start.$key).unwrap()
1001                };
1002                prop!($prop => rect);
1003              }
1004            }
1005          }};
1006        }
1007
1008        macro_rules! logical_shorthand {
1009          ($prop: ident, $key: ident, $start: expr, $end: expr) => {{
1010            let has_prop = $start.$key.is_some() && $end.$key.is_some();
1011            if has_prop {
1012              prop!($prop => $prop {
1013                start: std::mem::take(&mut $start.$key).unwrap(),
1014                end: std::mem::take(&mut $end.$key).unwrap(),
1015              });
1016              $end.$key = None;
1017            }
1018            has_prop
1019          }};
1020        }
1021
1022        if $block_start.is_valid() && $block_end.is_valid() && $inline_start.is_valid() && $inline_end.is_valid() {
1023          let top_eq_bottom = $block_start == $block_end;
1024          let left_eq_right = $inline_start == $inline_end;
1025          let top_eq_left = $block_start == $inline_start;
1026          let top_eq_right = $block_start == $inline_end;
1027          let bottom_eq_left = $block_end == $inline_start;
1028          let bottom_eq_right = $block_end == $inline_end;
1029
1030          macro_rules! is_eq {
1031            ($key: ident) => {
1032              $block_start.$key == $block_end.$key &&
1033              $inline_start.$key == $inline_end.$key &&
1034              $inline_start.$key == $block_start.$key
1035            };
1036          }
1037
1038          macro_rules! prop_diff {
1039            ($border: expr, $fallback: expr, $border_fallback: literal) => {
1040              if !$is_logical && is_eq!(color) && is_eq!(style) {
1041                prop!(Border => $border.to_border());
1042                shorthand!(BorderWidth, width);
1043              } else if !$is_logical && is_eq!(width) && is_eq!(style) {
1044                prop!(Border => $border.to_border());
1045                shorthand!(BorderColor, color);
1046              } else if !$is_logical && is_eq!(width) && is_eq!(color) {
1047                prop!(Border => $border.to_border());
1048                shorthand!(BorderStyle, style);
1049              } else {
1050                if $border_fallback {
1051                  prop!(Border => $border.to_border());
1052                }
1053                $fallback
1054              }
1055            };
1056          }
1057
1058          macro_rules! side_diff {
1059            ($border: expr, $other: expr, $prop: ident, $width: ident, $style: ident, $color: ident) => {
1060              let eq_width = $border.width == $other.width;
1061              let eq_style = $border.style == $other.style;
1062              let eq_color = $border.color == $other.color;
1063
1064              // If only one of the sub-properties is different, only emit that.
1065              // Otherwise, emit the full border value.
1066              if eq_width && eq_style {
1067                prop!($color => $other.color.clone().unwrap());
1068              } else if eq_width && eq_color {
1069                prop!($style => $other.style.clone().unwrap());
1070              } else if eq_style && eq_color {
1071                prop!($width => $other.width.clone().unwrap());
1072              } else {
1073                prop!($prop => $other.to_border());
1074              }
1075            };
1076          }
1077
1078          if top_eq_bottom && top_eq_left && top_eq_right {
1079            prop!(Border => $block_start.to_border());
1080          } else if top_eq_bottom && top_eq_left {
1081            prop!(Border => $block_start.to_border());
1082            side_diff!($block_start, $inline_end, $inline_end_prop, $inline_end_width, $inline_end_style, $inline_end_color);
1083          } else if top_eq_bottom && top_eq_right {
1084            prop!(Border => $block_start.to_border());
1085            side_diff!($block_start, $inline_start, $inline_start_prop, $inline_start_width, $inline_start_style, $inline_start_color);
1086          } else if left_eq_right && bottom_eq_left {
1087            prop!(Border => $inline_start.to_border());
1088            side_diff!($inline_start, $block_start, $block_start_prop, $block_start_width, $block_start_style, $block_start_color);
1089          } else if left_eq_right && top_eq_left {
1090            prop!(Border => $inline_start.to_border());
1091            side_diff!($inline_start, $block_end, $block_end_prop, $block_end_width, $block_end_style, $block_end_color);
1092          } else if top_eq_bottom {
1093            prop_diff!($block_start, {
1094              // Try to use border-inline shorthands for the opposide direction if possible.
1095              let mut handled = false;
1096              if $is_logical {
1097                let mut diff = 0;
1098                if $inline_start.width != $block_start.width || $inline_end.width != $block_start.width {
1099                  diff += 1;
1100                }
1101                if $inline_start.style != $block_start.style || $inline_end.style != $block_start.style {
1102                  diff += 1;
1103                }
1104                if $inline_start.color != $block_start.color || $inline_end.color != $block_start.color {
1105                  diff += 1;
1106                }
1107
1108                if diff == 1 {
1109                  if $inline_start.width != $block_start.width {
1110                    prop!(BorderInlineWidth => BorderInlineWidth {
1111                      start: $inline_start.width.clone().unwrap(),
1112                      end: $inline_end.width.clone().unwrap(),
1113                    });
1114                    handled = true;
1115                  } else if $inline_start.style != $block_start.style {
1116                    prop!(BorderInlineStyle => BorderInlineStyle {
1117                      start: $inline_start.style.clone().unwrap(),
1118                      end: $inline_end.style.clone().unwrap()
1119                    });
1120                    handled = true;
1121                  } else if $inline_start.color != $block_start.color {
1122                    prop!(BorderInlineColor => BorderInlineColor {
1123                      start: $inline_start.color.clone().unwrap(),
1124                      end: $inline_end.color.clone().unwrap()
1125                    });
1126                    handled = true;
1127                  }
1128                } else if diff > 1 && $inline_start.width == $inline_end.width && $inline_start.style == $inline_end.style && $inline_start.color == $inline_end.color {
1129                  prop!(BorderInline => $inline_start.to_border());
1130                  handled = true;
1131                }
1132              }
1133
1134              if !handled {
1135                side_diff!($block_start, $inline_start, $inline_start_prop, $inline_start_width, $inline_start_style, $inline_start_color);
1136                side_diff!($block_start, $inline_end, $inline_end_prop, $inline_end_width, $inline_end_style, $inline_end_color);
1137              }
1138            }, true);
1139          } else if left_eq_right {
1140            prop_diff!($inline_start, {
1141              // We know already that top != bottom, so no need to try to use border-block.
1142              side_diff!($inline_start, $block_start, $block_start_prop, $block_start_width, $block_start_style, $block_start_color);
1143              side_diff!($inline_start, $block_end, $block_end_prop, $block_end_width, $block_end_style, $block_end_color);
1144            }, true);
1145          } else if bottom_eq_right {
1146            prop_diff!($block_end, {
1147              side_diff!($block_end, $block_start, $block_start_prop, $block_start_width, $block_start_style, $block_start_color);
1148              side_diff!($block_end, $inline_start, $inline_start_prop, $inline_start_width, $inline_start_style, $inline_start_color);
1149            }, true);
1150          } else {
1151            prop_diff!($block_start, {
1152              prop!($block_start_prop => $block_start.to_border());
1153              prop!($block_end_prop => $block_end.to_border());
1154              prop!($inline_start_prop => $inline_start.to_border());
1155              prop!($inline_end_prop => $inline_end.to_border());
1156            }, false);
1157          }
1158        } else {
1159          shorthand!(BorderStyle, style);
1160          shorthand!(BorderWidth, width);
1161          shorthand!(BorderColor, color);
1162
1163          macro_rules! side {
1164            ($val: expr, $shorthand: ident, $width: ident, $style: ident, $color: ident) => {
1165              if $val.is_valid() {
1166                prop!($shorthand => $val.to_border());
1167              } else {
1168                if let Some(style) = &$val.style {
1169                  prop!($style => style.clone());
1170                }
1171
1172                if let Some(width) = &$val.width {
1173                  prop!($width => width.clone());
1174                }
1175
1176                if let Some(color) = &$val.color {
1177                  prop!($color => color.clone());
1178                }
1179              }
1180            };
1181          }
1182
1183          if $is_logical && $block_start == $block_end && $block_start.is_valid() {
1184            if logical_supported {
1185              if logical_shorthand_supported {
1186                prop!(BorderBlock => $block_start.to_border());
1187              } else {
1188                prop!(BorderBlockStart => $block_start.to_border());
1189                prop!(BorderBlockEnd => $block_start.to_border());
1190              }
1191            } else {
1192              prop!(BorderTop => $block_start.to_border());
1193              prop!(BorderBottom => $block_start.to_border());
1194            }
1195          } else {
1196            if $is_logical && logical_shorthand_supported && !$block_start.is_valid() && !$block_end.is_valid() {
1197              logical_shorthand!(BorderBlockStyle, style, $block_start, $block_end);
1198              logical_shorthand!(BorderBlockWidth, width, $block_start, $block_end);
1199              logical_shorthand!(BorderBlockColor, color, $block_start, $block_end);
1200            }
1201
1202            side!($block_start, $block_start_prop, $block_start_width, $block_start_style, $block_start_color);
1203            side!($block_end, $block_end_prop, $block_end_width, $block_end_style, $block_end_color);
1204          }
1205
1206          if $is_logical && $inline_start == $inline_end && $inline_start.is_valid() {
1207            if logical_supported {
1208              if logical_shorthand_supported {
1209                prop!(BorderInline => $inline_start.to_border());
1210              } else {
1211                prop!(BorderInlineStart => $inline_start.to_border());
1212                prop!(BorderInlineEnd => $inline_start.to_border());
1213              }
1214            } else {
1215              prop!(BorderLeft => $inline_start.to_border());
1216              prop!(BorderRight => $inline_start.to_border());
1217            }
1218          } else {
1219            if $is_logical && !$inline_start.is_valid() && !$inline_end.is_valid() {
1220              if logical_shorthand_supported {
1221                logical_shorthand!(BorderInlineStyle, style, $inline_start, $inline_end);
1222                logical_shorthand!(BorderInlineWidth, width, $inline_start, $inline_end);
1223                logical_shorthand!(BorderInlineColor, color, $inline_start, $inline_end);
1224              } else {
1225                // If both values of an inline logical property are equal, then we can just convert them to physical properties.
1226                macro_rules! inline_prop {
1227                  ($key: ident, $left: ident, $right: ident) => {
1228                    if $inline_start.$key.is_some() && $inline_start.$key == $inline_end.$key {
1229                      prop!($left => std::mem::take(&mut $inline_start.$key).unwrap());
1230                      prop!($right => std::mem::take(&mut $inline_end.$key).unwrap());
1231                    }
1232                  }
1233                }
1234
1235                inline_prop!(style, BorderLeftStyle, BorderRightStyle);
1236                inline_prop!(width, BorderLeftWidth, BorderRightWidth);
1237                inline_prop!(color, BorderLeftColor, BorderRightColor);
1238              }
1239            }
1240
1241            side!($inline_start, $inline_start_prop, $inline_start_width, $inline_start_style, $inline_start_color);
1242            side!($inline_end, $inline_end_prop, $inline_end_width, $inline_end_style, $inline_end_color);
1243          }
1244        }
1245      };
1246    }
1247
1248    flush_category!(
1249      BorderTop,
1250      BorderTopWidth,
1251      BorderTopStyle,
1252      BorderTopColor,
1253      self.border_top,
1254      BorderBottom,
1255      BorderBottomWidth,
1256      BorderBottomStyle,
1257      BorderBottomColor,
1258      self.border_bottom,
1259      BorderLeft,
1260      BorderLeftWidth,
1261      BorderLeftStyle,
1262      BorderLeftColor,
1263      self.border_left,
1264      BorderRight,
1265      BorderRightWidth,
1266      BorderRightStyle,
1267      BorderRightColor,
1268      self.border_right,
1269      false
1270    );
1271
1272    flush_category!(
1273      BorderBlockStart,
1274      BorderBlockStartWidth,
1275      BorderBlockStartStyle,
1276      BorderBlockStartColor,
1277      self.border_block_start,
1278      BorderBlockEnd,
1279      BorderBlockEndWidth,
1280      BorderBlockEndStyle,
1281      BorderBlockEndColor,
1282      self.border_block_end,
1283      BorderInlineStart,
1284      BorderInlineStartWidth,
1285      BorderInlineStartStyle,
1286      BorderInlineStartColor,
1287      self.border_inline_start,
1288      BorderInlineEnd,
1289      BorderInlineEndWidth,
1290      BorderInlineEndStyle,
1291      BorderInlineEndColor,
1292      self.border_inline_end,
1293      true
1294    );
1295
1296    self.border_top.reset();
1297    self.border_bottom.reset();
1298    self.border_left.reset();
1299    self.border_right.reset();
1300    self.border_block_start.reset();
1301    self.border_block_end.reset();
1302    self.border_inline_start.reset();
1303    self.border_inline_end.reset();
1304  }
1305
1306  fn flush_unparsed(
1307    &mut self,
1308    unparsed: &UnparsedProperty<'i>,
1309    dest: &mut DeclarationList<'i>,
1310    context: &mut PropertyHandlerContext<'i, '_>,
1311  ) {
1312    let logical_supported = !context.should_compile_logical(Feature::LogicalBorders);
1313    if logical_supported {
1314      let mut unparsed = unparsed.clone();
1315      context.add_unparsed_fallbacks(&mut unparsed);
1316      self
1317        .flushed_properties
1318        .insert(BorderProperty::try_from(&unparsed.property_id).unwrap());
1319      dest.push(Property::Unparsed(unparsed));
1320      return;
1321    }
1322
1323    macro_rules! prop {
1324      ($id: ident) => {{
1325        let mut unparsed = unparsed.with_property_id(PropertyId::$id);
1326        context.add_unparsed_fallbacks(&mut unparsed);
1327        dest.push(Property::Unparsed(unparsed));
1328        self.flushed_properties.insert(BorderProperty::$id);
1329      }};
1330    }
1331
1332    macro_rules! logical_prop {
1333      ($ltr: ident, $ltr_key: ident, $rtl: ident, $rtl_key: ident) => {{
1334        context.add_logical_rule(
1335          Property::Unparsed(unparsed.with_property_id(PropertyId::$ltr)),
1336          Property::Unparsed(unparsed.with_property_id(PropertyId::$rtl)),
1337        );
1338      }};
1339    }
1340
1341    use PropertyId::*;
1342    match &unparsed.property_id {
1343      BorderInlineStart => logical_prop!(BorderLeft, border_left, BorderRight, border_right),
1344      BorderInlineStartWidth => {
1345        logical_prop!(BorderLeftWidth, border_left_width, BorderRightWidth, border_right_width)
1346      }
1347      BorderInlineStartColor => {
1348        logical_prop!(BorderLeftColor, border_left_color, BorderRightColor, border_right_color)
1349      }
1350      BorderInlineStartStyle => {
1351        logical_prop!(BorderLeftStyle, border_left_style, BorderRightStyle, border_right_style)
1352      }
1353      BorderInlineEnd => logical_prop!(BorderRight, border_right, BorderLeft, border_left),
1354      BorderInlineEndWidth => {
1355        logical_prop!(BorderRightWidth, border_right_width, BorderLeftWidth, border_left_width)
1356      }
1357      BorderInlineEndColor => {
1358        logical_prop!(BorderRightColor, border_right_color, BorderLeftColor, border_left_color)
1359      }
1360      BorderInlineEndStyle => {
1361        logical_prop!(BorderRightStyle, border_right_style, BorderLeftStyle, border_left_style)
1362      }
1363      BorderBlockStart => prop!(BorderTop),
1364      BorderBlockStartWidth => prop!(BorderTopWidth),
1365      BorderBlockStartColor => prop!(BorderTopColor),
1366      BorderBlockStartStyle => prop!(BorderTopStyle),
1367      BorderBlockEnd => prop!(BorderBottom),
1368      BorderBlockEndWidth => prop!(BorderBottomWidth),
1369      BorderBlockEndColor => prop!(BorderBottomColor),
1370      BorderBlockEndStyle => prop!(BorderBottomStyle),
1371      property_id => {
1372        let mut unparsed = unparsed.clone();
1373        context.add_unparsed_fallbacks(&mut unparsed);
1374        dest.push(Property::Unparsed(unparsed));
1375        self.flushed_properties.insert(BorderProperty::try_from(property_id).unwrap());
1376      }
1377    }
1378  }
1379}
1380
1381fn is_border_property(property_id: &PropertyId) -> bool {
1382  match property_id {
1383    PropertyId::BorderTopColor
1384    | PropertyId::BorderBottomColor
1385    | PropertyId::BorderLeftColor
1386    | PropertyId::BorderRightColor
1387    | PropertyId::BorderBlockStartColor
1388    | PropertyId::BorderBlockEndColor
1389    | PropertyId::BorderBlockColor
1390    | PropertyId::BorderInlineStartColor
1391    | PropertyId::BorderInlineEndColor
1392    | PropertyId::BorderInlineColor
1393    | PropertyId::BorderTopWidth
1394    | PropertyId::BorderBottomWidth
1395    | PropertyId::BorderLeftWidth
1396    | PropertyId::BorderRightWidth
1397    | PropertyId::BorderBlockStartWidth
1398    | PropertyId::BorderBlockEndWidth
1399    | PropertyId::BorderBlockWidth
1400    | PropertyId::BorderInlineStartWidth
1401    | PropertyId::BorderInlineEndWidth
1402    | PropertyId::BorderInlineWidth
1403    | PropertyId::BorderTopStyle
1404    | PropertyId::BorderBottomStyle
1405    | PropertyId::BorderLeftStyle
1406    | PropertyId::BorderRightStyle
1407    | PropertyId::BorderBlockStartStyle
1408    | PropertyId::BorderBlockEndStyle
1409    | PropertyId::BorderBlockStyle
1410    | PropertyId::BorderInlineStartStyle
1411    | PropertyId::BorderInlineEndStyle
1412    | PropertyId::BorderInlineStyle
1413    | PropertyId::BorderTop
1414    | PropertyId::BorderBottom
1415    | PropertyId::BorderLeft
1416    | PropertyId::BorderRight
1417    | PropertyId::BorderBlockStart
1418    | PropertyId::BorderBlockEnd
1419    | PropertyId::BorderInlineStart
1420    | PropertyId::BorderInlineEnd
1421    | PropertyId::BorderBlock
1422    | PropertyId::BorderInline
1423    | PropertyId::BorderWidth
1424    | PropertyId::BorderStyle
1425    | PropertyId::BorderColor
1426    | PropertyId::Border => true,
1427    _ => false,
1428  }
1429}