tailwind_css_fixes/modules/typography/breaking/
mod.rs

1use super::*;
2
3#[doc=include_str!("readme.md")]
4#[derive(Debug, Clone)]
5pub struct TailwindBreak {
6    kind: WordBreak,
7}
8
9#[derive(Debug, Clone)]
10enum WordBreak {
11    Normal,
12    Words,
13    Standard(String),
14}
15
16impl<T> From<T> for TailwindBreak
17where
18    T: Into<String>,
19{
20    fn from(kind: T) -> Self {
21        Self { kind: WordBreak::Standard(kind.into()) }
22    }
23}
24
25impl Display for TailwindBreak {
26    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
27        match &self.kind {
28            WordBreak::Normal => write!(f, "break-normal"),
29            WordBreak::Words => write!(f, "break-words"),
30            WordBreak::Standard(s) => match s.as_str() {
31                "break-all" => write!(f, "break-all"),
32                _ => write!(f, "{}", s),
33            },
34        }
35    }
36}
37
38impl TailwindInstance for TailwindBreak {
39    fn attributes(&self, _: &TailwindBuilder) -> CssAttributes {
40        match &self.kind {
41            WordBreak::Normal => css_attributes! {
42                "overflow-wrap" => "normal",
43                "word-break" => "normal"
44            },
45            WordBreak::Words => css_attributes! {
46                "overflow-wrap" => "break-word"
47            },
48            WordBreak::Standard(s) => css_attributes! {
49                "word-break" => s
50            },
51        }
52    }
53}
54
55impl TailwindBreak {
56    /// https://tailwindcss.com/docs/word-break
57    pub fn parse(pattern: &[&str], arbitrary: &TailwindArbitrary) -> Result<Box<dyn TailwindInstance>> {
58        let kind = match pattern {
59            // https://tailwindcss.com/docs/break-before
60            ["before", rest @ ..] => TailwindBreakBefore::parse(rest, arbitrary)?.boxed(),
61            // https://tailwindcss.com/docs/break-inside
62            ["inside", rest @ ..] => TailwindBreakInside::parse(rest, arbitrary)?.boxed(),
63            // https://tailwindcss.com/docs/break-after
64            ["after", rest @ ..] => TailwindBreakAfter::parse(rest, arbitrary)?.boxed(),
65            // https://tailwindcss.com/docs/word-break
66            _ => Self::parse_self(pattern, arbitrary)?.boxed(),
67        };
68        Ok(kind)
69    }
70    fn parse_self(pattern: &[&str], arbitrary: &TailwindArbitrary) -> Result<Self> {
71        Ok(Self { kind: WordBreak::parse(pattern, arbitrary)? })
72    }
73}
74
75impl WordBreak {
76    pub fn parse(pattern: &[&str], _arbitrary: &TailwindArbitrary) -> Result<Self> {
77        let kind = match pattern {
78            ["normal"] => Self::Normal,
79            ["words"] => Self::Words,
80            ["all"] => Self::Standard("break-all".to_string()),
81            _ => {
82                let kind = pattern.join("-");
83                debug_assert!(Self::check_valid(&kind));
84                Self::Standard(kind)
85            },
86        };
87        Ok(kind)
88    }
89    /// https://developer.mozilla.org/en-US/docs/Web/CSS/word-break#syntax
90    pub fn check_valid(mode: &str) -> bool {
91        let set = BTreeSet::from_iter(vec!["break-all", "inherit", "initial", "keep-all", "normal", "revert", "unset"]);
92        set.contains(mode)
93    }
94}