rspack_lightningcss/properties/
overflow.rs1use super::{Property, PropertyId};
4use crate::compat::Feature;
5use crate::context::PropertyHandlerContext;
6use crate::declaration::{DeclarationBlock, DeclarationList};
7use crate::error::{ParserError, PrinterError};
8use crate::macros::{define_shorthand, enum_property};
9use crate::traits::{Parse, PropertyHandler, Shorthand, ToCss};
10#[cfg(feature = "visitor")]
11use crate::visitor::Visit;
12use cssparser::*;
13
14enum_property! {
15 pub enum OverflowKeyword {
18 Visible,
20 Hidden,
22 Clip,
24 Scroll,
26 Auto,
28 }
29}
30
31define_shorthand! {
32 pub struct Overflow {
34 x: OverflowX(OverflowKeyword),
36 y: OverflowY(OverflowKeyword),
38 }
39}
40
41impl<'i> Parse<'i> for Overflow {
42 fn parse<'t>(input: &mut Parser<'i, 't>) -> Result<Self, ParseError<'i, ParserError<'i>>> {
43 let x = OverflowKeyword::parse(input)?;
44 let y = input.try_parse(OverflowKeyword::parse).unwrap_or_else(|_| x.clone());
45 Ok(Overflow { x, y })
46 }
47}
48
49impl ToCss for Overflow {
50 fn to_css<PrinterT: crate::printer::PrinterTrait>(&self, dest: &mut PrinterT) -> Result<(), PrinterError> {
51 self.x.to_css(dest)?;
52 if self.y != self.x {
53 dest.write_char(' ')?;
54 self.y.to_css(dest)?;
55 }
56 Ok(())
57 }
58}
59
60enum_property! {
61 pub enum TextOverflow {
63 Clip,
65 Ellipsis,
67 }
68}
69
70#[derive(Default)]
71pub(crate) struct OverflowHandler {
72 x: Option<OverflowKeyword>,
73 y: Option<OverflowKeyword>,
74}
75
76impl<'i> PropertyHandler<'i> for OverflowHandler {
77 fn handle_property(
78 &mut self,
79 property: &Property<'i>,
80 dest: &mut DeclarationList<'i>,
81 context: &mut PropertyHandlerContext<'i, '_>,
82 ) -> bool {
83 use Property::*;
84
85 match property {
86 OverflowX(val) => self.x = Some(*val),
87 OverflowY(val) => self.y = Some(*val),
88 Overflow(val) => {
89 self.x = Some(val.x);
90 self.y = Some(val.y);
91 }
92 Unparsed(val)
93 if matches!(
94 val.property_id,
95 PropertyId::OverflowX | PropertyId::OverflowY | PropertyId::Overflow
96 ) =>
97 {
98 self.finalize(dest, context);
99 dest.push(property.clone());
100 }
101 _ => return false,
102 }
103
104 true
105 }
106
107 fn finalize(&mut self, dest: &mut DeclarationList, context: &mut PropertyHandlerContext<'i, '_>) {
108 if self.x.is_none() && self.y.is_none() {
109 return;
110 }
111
112 let x = std::mem::take(&mut self.x);
113 let y = std::mem::take(&mut self.y);
114
115 match (x, y) {
116 (Some(x), Some(y)) if x == y || context.targets.is_compatible(Feature::OverflowShorthand) => {
119 dest.push(Property::Overflow(Overflow { x, y }))
120 }
121 _ => {
122 if let Some(x) = x {
123 dest.push(Property::OverflowX(x))
124 }
125
126 if let Some(y) = y {
127 dest.push(Property::OverflowY(y))
128 }
129 }
130 }
131 }
132}