Skip to main content

rspack_lightningcss/properties/
overflow.rs

1//! CSS properties related to overflow.
2
3use 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  /// An [overflow](https://www.w3.org/TR/css-overflow-3/#overflow-properties) keyword
16  /// as used in the `overflow-x`, `overflow-y`, and `overflow` properties.
17  pub enum OverflowKeyword {
18    /// Overflowing content is visible.
19    Visible,
20    /// Overflowing content is hidden. Programmatic scrolling is allowed.
21    Hidden,
22    /// Overflowing content is clipped. Programmatic scrolling is not allowed.
23    Clip,
24    /// The element is scrollable.
25    Scroll,
26    /// Overflowing content scrolls if needed.
27    Auto,
28  }
29}
30
31define_shorthand! {
32  /// A value for the [overflow](https://www.w3.org/TR/css-overflow-3/#overflow-properties) shorthand property.
33  pub struct Overflow {
34    /// The overflow mode for the x direction.
35    x: OverflowX(OverflowKeyword),
36    /// The overflow mode for the y direction.
37    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  /// A value for the [text-overflow](https://www.w3.org/TR/css-overflow-3/#text-overflow) property.
62  pub enum TextOverflow {
63    /// Overflowing text is clipped.
64    Clip,
65    /// Overflowing text is truncated with an ellipsis.
66    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      // Only use shorthand syntax if the x and y values are the
117      // same or the two-value syntax is supported by all targets.
118      (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}