swc_css_minifier/compressor/
url.rs

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
use swc_css_ast::*;

use super::Compressor;

impl Compressor {
    pub(super) fn compress_url(&self, url: &mut Url) {
        if url.name.value != "url" {
            return;
        }

        if let Some(modifiers) = &url.modifiers {
            if !modifiers.is_empty() {
                return;
            }
        }

        if let Some(UrlValue::Str(Str { value, span, .. })) = url.value.as_deref() {
            let mut counter = 0;

            for c in value.chars() {
                if counter == 2 {
                    return;
                }

                match c {
                    '(' | ')' | '"' | '\'' => {
                        counter += 1;
                    }
                    _ if c.is_whitespace() => {
                        counter += 1;
                    }
                    _ => {}
                }
            }

            url.value = Some(Box::new(UrlValue::Raw(UrlValueRaw {
                span: *span,
                value: value.clone(),
                raw: None,
            })));
        }
    }
}