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