swc_css_minifier/compressor/
alpha_value.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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
use swc_common::DUMMY_SP;
use swc_css_ast::*;

use super::Compressor;

pub(super) fn compress_alpha_value(alpha_value: &mut AlphaValue) {
    match alpha_value {
        AlphaValue::Percentage(Percentage {
            value: number,
            span,
            ..
        }) if number.value % 10.0 == 0.0 => {
            let new_value = number.value / 100.0;

            *alpha_value = AlphaValue::Number(Number {
                span: *span,
                value: new_value,
                raw: None,
            });
        }
        AlphaValue::Number(Number { value, span, .. }) if *value < 0.1 && *value != 0.0 => {
            let new_value = *value * 100.0;

            *alpha_value = AlphaValue::Percentage(Percentage {
                span: *span,
                value: Number {
                    span: DUMMY_SP,
                    value: new_value,
                    raw: None,
                },
            });
        }
        _ => {}
    }
}

impl Compressor {
    pub(super) fn compress_alpha_value(&mut self, alpha_value: &mut AlphaValue) {
        compress_alpha_value(alpha_value);
    }

    pub(super) fn compress_alpha_value_in_component_value(
        &mut self,
        component_value: &mut ComponentValue,
    ) {
        if self.ctx.preserve_alpha_value {
            return;
        }

        match component_value {
            ComponentValue::Percentage(percentage) if percentage.value.value % 10.0 == 0.0 => {
                let new_value = percentage.value.value / 100.0;

                *component_value = ComponentValue::Number(Box::new(Number {
                    span: percentage.span,
                    value: new_value,
                    raw: None,
                }));
            }
            _ => {}
        }
    }
}