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
use swc_css_ast::*;
use swc_css_visit::{VisitMut, VisitMutWith};

pub struct TextReplacer<'a> {
    from: &'a str,
    to: &'a str,
}

impl VisitMut for TextReplacer<'_> {
    fn visit_mut_text(&mut self, n: &mut Text) {
        n.visit_mut_children_with(self);

        if &*n.value == self.from {
            n.value = self.to.into();
            n.raw = self.to.into();
        }
    }
}

pub fn replace_text<N>(node: &mut N, from: &str, to: &str)
where
    N: for<'aa> VisitMutWith<TextReplacer<'aa>>,
{
    node.visit_mut_with(&mut TextReplacer { from, to });
}