Skip to main content

takumi_css/style/properties/
text_overflow.rs

1use std::fmt;
2
3use cssparser::{Parser, match_ignore_ascii_case};
4
5use crate::style::{
6  CssSyntaxKind, CssToken, FromCss, MakeComputed, ParseResult, ToCss, properties::write_css_string,
7};
8
9/// Defines how text should be overflowed.
10///
11/// This enum determines how text should be handled when it exceeds the container width.
12#[derive(Debug, Clone, PartialEq, Default)]
13#[non_exhaustive]
14pub enum TextOverflow {
15  /// Text is simply clipped at the overflow edge with no visual indication
16  #[default]
17  Clip,
18  /// Text is truncated with an ellipsis (…) at the end when it overflows
19  Ellipsis,
20  /// Text is truncated with a custom string at the end when it overflows
21  Custom(String),
22}
23
24impl MakeComputed for TextOverflow {}
25
26impl<'i> FromCss<'i> for TextOverflow {
27  fn from_css(input: &mut Parser<'i, '_>) -> ParseResult<'i, Self> {
28    let string = input.expect_ident_or_string()?;
29
30    match_ignore_ascii_case! {string,
31      "clip" => Ok(TextOverflow::Clip),
32      "ellipsis" => Ok(TextOverflow::Ellipsis),
33      _ => Ok(TextOverflow::Custom(string.to_string())),
34    }
35  }
36
37  const VALID_TOKENS: &'static [CssToken] = &[
38    CssToken::Keyword("clip"),
39    CssToken::Keyword("ellipsis"),
40    CssToken::Syntax(CssSyntaxKind::String),
41  ];
42}
43
44impl ToCss for TextOverflow {
45  fn to_css<W: fmt::Write>(&self, dest: &mut W) -> fmt::Result {
46    match self {
47      Self::Clip => dest.write_str("clip"),
48      Self::Ellipsis => dest.write_str("ellipsis"),
49      Self::Custom(s) => write_css_string(dest, s),
50    }
51  }
52}