Skip to main content

takumi_css/style/properties/
overflow_wrap.rs

1use std::fmt;
2
3use cssparser::{Parser, Token, match_ignore_ascii_case};
4
5use crate::style::{
6  CssToken, FromCss, MakeComputed, ParseResult, ToCss, tw::TailwindPropertyParser, unexpected_token,
7};
8
9/// Controls how text should be overflowed.
10#[derive(Debug, Default, Copy, Clone, PartialEq)]
11pub struct OverflowWrap(parley::OverflowWrap);
12
13impl TailwindPropertyParser for OverflowWrap {
14  fn parse_tw(token: &str) -> Option<Self> {
15    Self::from_str(token).ok()
16  }
17}
18
19impl<'i> FromCss<'i> for OverflowWrap {
20  fn from_css(input: &mut Parser<'i, '_>) -> ParseResult<'i, Self> {
21    let location = input.current_source_location();
22    let token = input.next()?;
23
24    let Token::Ident(ident) = token else {
25      return Err(unexpected_token!(location, token));
26    };
27
28    match_ignore_ascii_case! {&ident,
29      "normal" => Ok(Self(parley::OverflowWrap::Normal)),
30      "anywhere" => Ok(Self(parley::OverflowWrap::Anywhere)),
31      "break-word" => Ok(Self(parley::OverflowWrap::BreakWord)),
32      _ => Err(unexpected_token!(location, token)),
33    }
34  }
35
36  const VALID_TOKENS: &'static [CssToken] = &[
37    CssToken::Keyword("normal"),
38    CssToken::Keyword("anywhere"),
39    CssToken::Keyword("break-word"),
40  ];
41}
42
43impl MakeComputed for OverflowWrap {}
44
45impl ToCss for OverflowWrap {
46  fn to_css<W: fmt::Write>(&self, dest: &mut W) -> fmt::Result {
47    match self.0 {
48      parley::OverflowWrap::Normal => dest.write_str("normal"),
49      parley::OverflowWrap::Anywhere => dest.write_str("anywhere"),
50      parley::OverflowWrap::BreakWord => dest.write_str("break-word"),
51    }
52  }
53}
54
55impl From<OverflowWrap> for parley::OverflowWrap {
56  fn from(value: OverflowWrap) -> Self {
57    value.0
58  }
59}