~eanyanwu/toph

312e936c135be6127ef2818f0ff4fbb60eb106e4 — Eze 3 months ago f3db9f3
Revert variable suffixing
2 files changed, 7 insertions(+), 32 deletions(-)

M src/node/variable.rs
M src/node/visitor.rs
M src/node/variable.rs => src/node/variable.rs +5 -24
@@ 1,22 1,10 @@
use crate::encode;
use fastrand;
use std::collections::BTreeMap;
use std::fmt::{Display, Write};

/// The value associated with a variable + a random suffix that will be appended
/// to the variable name to make it unique to Node instance
///
/// This addition is necessary because in the case of a nested component, the
/// ancestor variable value gets  overridden in descendants.
#[derive(Debug, Clone, PartialEq, Eq, Default)]
pub struct Entry {
    pub value: String,
    pub suffix: u32,
}

/// Map of custom CSS variable name to value
#[derive(Debug, Clone, PartialEq, Eq, Default)]
pub struct CSSVariableMap(BTreeMap<&'static str, Entry>);
pub struct CSSVariableMap(BTreeMap<&'static str, String>);

impl CSSVariableMap {
    /// Add a new custom CSS variable to the map


@@ 24,14 12,7 @@ impl CSSVariableMap {
    /// The value will be attribute encoded  
    pub fn insert(&mut self, key: &'static str, value: &str) {
        let encoded = encode::attr(value);
        let suffix = fastrand::u32(0..u32::MAX);
        self.0.insert(
            key,
            Entry {
                value: encoded,
                suffix,
            },
        );
        self.0.insert(key, encoded);
    }

    /// Create a new variable map


@@ 46,8 27,8 @@ impl CSSVariableMap {
}

impl<'a> IntoIterator for &'a CSSVariableMap {
    type Item = (&'a &'static str, &'a Entry);
    type IntoIter = std::collections::btree_map::Iter<'a, &'static str, Entry>;
    type Item = (&'a &'static str, &'a String);
    type IntoIter = std::collections::btree_map::Iter<'a, &'static str, String>;
    fn into_iter(self) -> Self::IntoIter {
        self.0.iter()
    }


@@ 60,7 41,7 @@ impl Display for CSSVariableMap {
        }
        let mut result = String::new();
        for (k, v) in self.0.iter() {
            write!(result, "--{}-{}: {};", k, v.suffix, v.value)?;
            write!(result, "--{}: {};", k, v)?;
        }
        write!(f, "{}", result)
    }

M src/node/visitor.rs => src/node/visitor.rs +2 -8
@@ 229,7 229,7 @@ impl NodeVisitor for AssetInserter {
// A visitor that collects all css & js snippets from the Node tree
// Using btreeset because the iteration order is defined, which makes it possible to test
pub struct SnippetCollector {
    pub css: BTreeSet<String>,
    pub css: BTreeSet<&'static str>,
    pub js: BTreeSet<&'static str>,
}



@@ 249,13 249,7 @@ impl NodeVisitor for &mut SnippetCollector {
        for asset in el.assets.iter_mut() {
            match asset {
                Asset::StyleSheet(css) => {
                    let mut localized_css = String::from(*css);
                    for (k, v) in el.variables.into_iter() {
                        let pattern = format!("var(--{})", k);
                        let replacement = format!("var(--{}-{})", k, v.suffix);
                        localized_css = localized_css.replace(&pattern, &replacement);
                    }
                    self.css.insert(localized_css);
                    self.css.insert(css);
                }
                Asset::JavaScript(js) => {
                    self.js.insert(js);