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
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
use itertools::Itertools;

use super::*;

mod traits;

/// A collection of css objects
///
/// Separate or merge as needed
#[derive(Debug, Clone, Default)]
pub struct CssBundle {
    inline: bool,
    items: BTreeSet<CssInstance>,
}

impl CssBundle {
    /// insert new css instance to the html tag
    pub fn insert(&mut self, item: CssInstance) -> bool {
        self.items.insert(item)
    }
    /// clear all css instance in this html tag
    pub fn clear(&mut self) {
        self.items.clear()
    }
    /// Try inline styles, keep the class name if that fails.
    ///
    /// # Returns
    ///
    /// - css classes
    ///
    /// ```html
    /// <img class="tw-1 tw-2"/>
    /// ```
    pub fn as_traced(&self) -> String {
        debug_assert!(!self.inline);
        self.items.iter().map(|css| css.selector.as_str()).collect::<Vec<_>>().join(" ")
    }
    /// Try inline styles, keep the class name if that fails.
    ///
    /// # Returns
    ///
    /// - `.0`: css classes rest, maybe empty
    /// - `.1`: css style
    ///
    /// ```html
    /// <img class="not-inlinable" style="k1:v1;k2:v2;"/>
    /// ```
    pub fn as_inlined(&self) -> (String, String) {
        debug_assert!(self.inline);
        let mut class = BTreeSet::new();
        let mut attribute = CssAttributes::default();
        for i in self.items.iter() {
            match i.inlinable {
                true => attribute.extend(i.attribute.clone().into_iter()),
                false => {
                    class.insert(i.selector.to_string());
                },
            }
        }
        let class = class.into_iter().join(" ");
        let attribute = attribute.to_string();
        (class, attribute)
    }
    /// # Returns
    ///
    /// - scoped class name
    ///
    /// ```html
    /// <img class="_b2JmdXNjYXRl"/>
    /// ```
    pub fn as_scope(&self) {
        todo!()
    }
    /// # Returns
    ///
    /// - data name without value
    ///
    /// ```html
    /// <img data-tw-b2JmdXNjYXRl/>
    /// ```
    ///
    /// ```css
    /// [data-tw-b2JmdXNjYXRl] {
    ///
    /// }
    /// ```
    pub fn as_data_key(&self) {
        todo!()
    }
    /// # Returns
    ///
    /// - scoped class name
    ///
    /// ```html
    /// <img data-tw="b2JmdXNjYXRl"/>
    /// ```
    ///
    /// ```css
    /// [data-tw="b2JmdXNjYXRl"] {
    ///
    /// }
    /// ```
    pub fn as_data_value(&self) {
        todo!()
    }
    /// Mark inline mode
    pub fn set_inline(&mut self, inline: bool) {
        self.inline = inline
    }
}