logo
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
use super::*;

#[doc=include_str!("readme.md")]
#[derive(Debug, Clone)]
pub struct TailwindBreak {
    kind: WordBreak,
}

#[derive(Debug, Clone)]
enum WordBreak {
    Normal,
    Words,
    Standard(String),
}

impl<T> From<T> for TailwindBreak
where
    T: Into<String>,
{
    fn from(kind: T) -> Self {
        Self { kind: WordBreak::Standard(kind.into()) }
    }
}

impl Display for TailwindBreak {
    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
        match &self.kind {
            WordBreak::Normal => write!(f, "break-normal"),
            WordBreak::Words => write!(f, "break-words"),
            WordBreak::Standard(s) => match s.as_str() {
                "break-all" => write!(f, "break-all"),
                _ => write!(f, "{}", s),
            },
        }
    }
}

impl TailwindInstance for TailwindBreak {
    fn attributes(&self, _: &TailwindBuilder) -> BTreeSet<CssAttribute> {
        match &self.kind {
            WordBreak::Normal => css_attributes! {
                "overflow-wrap" => "normal",
                "word-break" => "normal"
            },
            WordBreak::Words => css_attributes! {
                "overflow-wrap" => "break-word"
            },
            WordBreak::Standard(s) => css_attributes! {
                "word-break" => s
            },
        }
    }
}

impl TailwindBreak {
    /// https://tailwindcss.com/docs/word-break
    pub fn parse(pattern: &[&str], arbitrary: &TailwindArbitrary) -> Result<Box<dyn TailwindInstance>> {
        let kind = match pattern {
            // https://tailwindcss.com/docs/break-before
            ["before", rest @ ..] => TailwindBreakBefore::parse(rest, arbitrary)?.boxed(),
            // https://tailwindcss.com/docs/break-inside
            ["inside", rest @ ..] => TailwindBreakInside::parse(rest, arbitrary)?.boxed(),
            // https://tailwindcss.com/docs/break-after
            ["after", rest @ ..] => TailwindBreakAfter::parse(rest, arbitrary)?.boxed(),
            // https://tailwindcss.com/docs/word-break
            _ => Self::parse_self(pattern, arbitrary)?.boxed(),
        };
        Ok(kind)
    }
    fn parse_self(pattern: &[&str], arbitrary: &TailwindArbitrary) -> Result<Self> {
        Ok(Self { kind: WordBreak::parse(pattern, arbitrary)? })
    }
}

impl WordBreak {
    pub fn parse(pattern: &[&str], arbitrary: &TailwindArbitrary) -> Result<Self> {
        debug_assert!(arbitrary.is_none(), "forbidden arbitrary after break");
        let kind = match pattern {
            ["normal"] => Self::Normal,
            ["words"] => Self::Words,
            ["all"] => Self::Standard("break-all".to_string()),
            _ => {
                let kind = pattern.join("-");
                debug_assert!(Self::check_valid(&kind));
                Self::Standard(kind)
            },
        };
        Ok(kind)
    }
    /// https://developer.mozilla.org/en-US/docs/Web/CSS/word-break#syntax
    pub fn check_valid(mode: &str) -> bool {
        let set = BTreeSet::from_iter(vec!["break-all", "inherit", "initial", "keep-all", "normal", "revert", "unset"]);
        set.contains(mode)
    }
}