swc_css_minifier/compressor/
container.rs

1use swc_css_ast::*;
2
3use super::Compressor;
4use crate::compressor::math::{is_calc_function_name, transform_calc_value_into_component_value};
5
6impl Compressor {
7    pub(super) fn compress_calc_sum_in_size_feature_value(&mut self, n: &mut SizeFeatureValue) {
8        match n {
9            SizeFeatureValue::Function(Function { name, value, .. })
10                if is_calc_function_name(name) && value.len() == 1 =>
11            {
12                match &value[0] {
13                    ComponentValue::CalcSum(calc_sum) if calc_sum.expressions.len() == 1 => {
14                        match &calc_sum.expressions[0] {
15                            CalcProductOrOperator::Product(CalcProduct {
16                                expressions: calc_product_expressions,
17                                ..
18                            }) if calc_product_expressions.len() == 1 => {
19                                if let CalcValueOrOperator::Value(calc_value) =
20                                    &calc_product_expressions[0]
21                                {
22                                    match transform_calc_value_into_component_value(calc_value) {
23                                        Some(ComponentValue::Function(function)) => {
24                                            *n = SizeFeatureValue::Function(*function);
25                                        }
26                                        Some(ComponentValue::Dimension(dimension)) => {
27                                            *n = SizeFeatureValue::Dimension(*dimension);
28                                        }
29                                        Some(ComponentValue::Number(number)) => {
30                                            *n = SizeFeatureValue::Number(*number);
31                                        }
32                                        _ => {}
33                                    }
34                                }
35                            }
36                            _ => {}
37                        }
38                    }
39                    _ => {}
40                }
41            }
42            _ => {}
43        }
44    }
45
46    pub(super) fn compress_size_feature_value_length(&mut self, n: &mut SizeFeatureValue) {
47        if let SizeFeatureValue::Dimension(Dimension::Length(length)) = n {
48            if let Some(number) = self.length_to_zero(length) {
49                *n = SizeFeatureValue::Number(number)
50            }
51        }
52    }
53}