tailwind_css_fixes/traits/
mod.rs

1use std::{
2    cmp::Ordering,
3    fmt::{Debug, Display, Formatter},
4    hash::{Hash, Hasher},
5};
6
7use crate::{CssAttributes, TailwindBuilder};
8
9pub mod instance;
10
11#[allow(unused_variables)]
12pub trait TailwindInstance: Display {
13    /// used to deduplication and marking
14    #[inline]
15    fn id(&self) -> String {
16        normalize_html_class_name(&self.to_string())
17    }
18    /// Is this instance inlineable within html style="" attribute?
19    /// - (no nested rules, no pseudo-classes, etc)
20    fn inlineable(&self) -> bool {
21        true
22    }
23    /// New tailwind instance
24    fn boxed(self) -> Box<dyn TailwindInstance>
25    where
26        Self: Sized,
27        Self: 'static,
28    {
29        Box::new(self)
30    }
31    /// Custom selector name
32    fn selectors(&self, ctx: &TailwindBuilder) -> String {
33        format!(".{}", self.id())
34    }
35    /// Attributes in css, representing contained CSS property-value(s)
36    fn attributes(&self, ctx: &TailwindBuilder) -> CssAttributes;
37    /// Additional css in bundle
38    fn additional(&self, ctx: &TailwindBuilder) -> String {
39        String::new()
40    }
41}
42
43/// Normalize classname to be used as a valid HTML classname.
44/// - Escapes non-alphanumeric characters with a backslash (`\`).
45/// - Replaces spaces with underscores (`_`).
46/// 
47/// Same as normalizing classname as CSS selector, but without escaping non-alphanumeric characters
48fn normalize_html_class_name(name: &str) -> String {
49    let mut out = String::new();
50    for c in name.chars() {
51        match c {
52            ' ' => out.push('_'),
53            r @ ('-' | '_') => out.push(r),
54            a if a.is_alphanumeric() => out.push(a),
55            _ => out.push(c),
56        }
57    }
58    out
59}