From 59e8bf49761c9f2434dcfb18153f93234d3d69a7 Mon Sep 17 00:00:00 2001 From: Alexander Akait <4567934+alexander-akait@users.noreply.github.com> Date: Mon, 12 Dec 2022 07:27:13 +0300 Subject: [PATCH] fix(css/minifier): Fix compression of timing functions (#6618) --- .../src/compressor/easing_function.rs | 48 +++++++++++++------ .../compress-easing-function/output.min.css | 2 +- 2 files changed, 35 insertions(+), 15 deletions(-) diff --git a/crates/swc_css_minifier/src/compressor/easing_function.rs b/crates/swc_css_minifier/src/compressor/easing_function.rs index e6195ec79807..5bfdd7f6a359 100644 --- a/crates/swc_css_minifier/src/compressor/easing_function.rs +++ b/crates/swc_css_minifier/src/compressor/easing_function.rs @@ -14,41 +14,61 @@ impl Compressor { && function_value.len() == 7 => { if let ( - ComponentValue::Number(box Number { value: first, .. }), - ComponentValue::Number(box Number { value: second, .. }), - ComponentValue::Number(box Number { value: third, .. }), - ComponentValue::Number(box Number { value: fourth, .. }), + first, + second, + third, + ComponentValue::Integer(box Integer { value: fourth, .. }), ) = ( &function_value[0], &function_value[2], &function_value[4], &function_value[6], ) { - if *first == 0.0 && *second == 0.0 && *third == 1.0 && *fourth == 1.0 { + if matches!(first, ComponentValue::Integer(box Integer { value, .. }) if *value == 0) + && matches!(second, ComponentValue::Integer(box Integer { value, .. }) if *value == 0) + && matches!(third, ComponentValue::Integer(box Integer { value, .. }) if *value == 1) + && *fourth == 1 + { *component_value = ComponentValue::Ident(Box::new(Ident { span: *span, value: js_word!("linear"), raw: None, })) - } else if *first == 0.25 && *second == 0.1 && *third == 0.25 && *fourth == 1.0 { + } else if matches!(first, ComponentValue::Number(box Number { value, .. }) if *value == 0.25) + && matches!(second, ComponentValue::Number(box Number { value, .. }) if *value == 0.1) + && matches!(third, ComponentValue::Number(box Number { value, .. }) if *value == 0.25) + && *fourth == 1 + { *component_value = ComponentValue::Ident(Box::new(Ident { span: *span, value: js_word!("ease"), raw: None, })) - } else if *first == 0.42 && *second == 0.0 && *third == 1.0 && *fourth == 1.0 { + } else if matches!(first, ComponentValue::Number(box Number { value: first, .. }) if *first == 0.42) + && matches!(second, ComponentValue::Integer(box Integer { value, .. }) if *value == 0) + && matches!(third, ComponentValue::Integer(box Integer { value, .. }) if *value == 1) + && *fourth == 1 + { *component_value = ComponentValue::Ident(Box::new(Ident { span: *span, value: js_word!("ease-in"), raw: None, })) - } else if *first == 0.0 && *second == 0.0 && *third == 0.58 && *fourth == 1.0 { + } else if matches!(first, ComponentValue::Integer(box Integer { value: first, .. }) if *first == 0) + && matches!(second, ComponentValue::Integer(box Integer { value, .. }) if *value == 0) + && matches!(third, ComponentValue::Number(box Number { value, .. }) if *value == 0.58) + && *fourth == 1 + { *component_value = ComponentValue::Ident(Box::new(Ident { span: *span, value: js_word!("ease-out"), raw: None, })) - } else if *first == 0.42 && *second == 0.0 && *third == 0.58 && *fourth == 1.0 { + } else if matches!(first, ComponentValue::Number(box Number { value: first, .. }) if *first == 0.42) + && matches!(second, ComponentValue::Integer(box Integer { value, .. }) if *value == 0) + && matches!(third, ComponentValue::Number(box Number { value, .. }) if *value == 0.58) + && *fourth == 1 + { *component_value = ComponentValue::Ident(Box::new(Ident { span: *span, value: js_word!("ease-in-out"), @@ -66,14 +86,14 @@ impl Compressor { { match (&function_value[0], &function_value[2]) { ( - ComponentValue::Number(box Number { + ComponentValue::Integer(box Integer { value: number_value, .. }), ComponentValue::Ident(box Ident { value: ident_value, .. }), - ) if *number_value == 1.0 => match ident_value.to_ascii_lowercase() { + ) if *number_value == 1 => match ident_value.to_ascii_lowercase() { js_word!("start") | js_word!("jump-start") => { *component_value = ComponentValue::Ident(Box::new(Ident { span: *span, @@ -91,7 +111,7 @@ impl Compressor { _ => {} }, ( - ComponentValue::Number(box Number { .. }), + ComponentValue::Integer(box Integer { .. }), ComponentValue::Ident(box Ident { value: ident_value, .. }), @@ -103,13 +123,13 @@ impl Compressor { })) } ( - ComponentValue::Number(number), + ComponentValue::Integer(number), ComponentValue::Ident(box Ident { value: ident_value, .. }), ) => match ident_value.to_ascii_lowercase() { js_word!("end") | js_word!("jump-end") => { - *function_value = vec![ComponentValue::Number(number.clone())]; + *function_value = vec![ComponentValue::Integer(number.clone())]; } _ => {} }, diff --git a/crates/swc_css_minifier/tests/fixture/compress-easing-function/output.min.css b/crates/swc_css_minifier/tests/fixture/compress-easing-function/output.min.css index fb3739f4dbe5..86de16d3d7ee 100644 --- a/crates/swc_css_minifier/tests/fixture/compress-easing-function/output.min.css +++ b/crates/swc_css_minifier/tests/fixture/compress-easing-function/output.min.css @@ -1 +1 @@ -.class-1{animation:fade 3s cubic-bezier(.25,.1,.25,1)}.class-2{animation-timing-function:cubic-bezier(.25,.1,.25,1)}.class-3{transition:fade 3s cubic-bezier(.25,.1,.25,1)}.class-4{transition-timing-function:cubic-bezier(.25,.1,.25,1)}.class-5{transition-timing-function:cubic-bezier(.25,.1,.25,1)}.class-6{foo:cubic-bezier(.25,.1,.25,1,0,0,0,0)}.class-7{transition-timing-function:cubic-bezier(0,0,1,1)}.class-8{transition-timing-function:cubic-bezier(.25,.1,.25,1)}.class-9{transition-timing-function:cubic-bezier(.42,0,1,1)}.class-10{transition-timing-function:cubic-bezier(0,0,.58,1)}.class-11{transition-timing-function:cubic-bezier(.42,0,.58,1)}.class-12{transition-timing-function:steps(1,start);transition-timing-function:steps(1,START)}.class-13{transition-timing-function:steps(1,start)}.class-14{transition-timing-function:steps(1,jump-start)}.class-15{transition-timing-function:steps(1,JUMP-START)}.class-16{transition-timing-function:steps(1,jump-start)}.class-17{transition-timing-function:steps(1,end)}.class-18{transition-timing-function:steps(1,END)}.class-19{transition-timing-function:steps(1,end)}.class-20{transition-timing-function:steps(1,jump-end)}.class-21{transition-timing-function:steps(1,JUMP-END)}.class-22{transition-timing-function:steps(1,jump-end)}.class-23{transition-timing-function:steps(1)}.class-24{transition-timing-functionf:steps(1,end)}.class-25{transition-timing-function:steps(1)}.class-26{transition-timing-function:steps(5,start)}.class-27{transition-timing-function:steps(5,jump-start)}.class-28{transition-timing-function:steps(10,end)}.class-29{transition-timing-function:steps(10,jump-end)}.class-30{transition-timing-function:steps(15)}.class-31{transition-timing-function:cubic-bezier(0,0,1,1)}.class-32,.class-33{transition-timing-function:cubic-bezier(.25,.1,.25,1)}.class-34{animation-timing-function:cubic-bezier(0,0,1,1),cubic-bezier(0,0,1,1),steps(1,start)}.class-35{animation-timing-function:cubic-bezier()}.class-36{animation-timing-function:steps()}.class-37{animation-timing-function:cubic-bezier(var(--foo),var(--bar),var(--baz),var(--foz))}.class-38{animation-timing-function:cubic-bezier(.25,var(--foo),.25,1)} +.class-1{animation:fade 3s ease}.class-2{animation-timing-function:ease}.class-3{transition:fade 3s ease}.class-4,.class-5{transition-timing-function:ease}.class-6{foo:cubic-bezier(.25,.1,.25,1,0,0,0,0)}.class-7{transition-timing-function:linear}.class-8{transition-timing-function:ease}.class-9{transition-timing-function:ease-in}.class-10{transition-timing-function:ease-out}.class-11{transition-timing-function:ease-in-out}.class-12,.class-13,.class-14,.class-15,.class-16{transition-timing-function:step-start}.class-17,.class-18,.class-19,.class-20,.class-21,.class-22{transition-timing-function:step-end}.class-23{transition-timing-function:steps(1)}.class-24{transition-timing-functionf:step-end}.class-25{transition-timing-function:steps(1)}.class-26,.class-27{transition-timing-function:steps(5,start)}.class-28,.class-29{transition-timing-function:steps(10)}.class-30{transition-timing-function:steps(15)}.class-31{transition-timing-function:linear}.class-32,.class-33{transition-timing-function:ease}.class-34{animation-timing-function:linear,linear,step-start}.class-35{animation-timing-function:cubic-bezier()}.class-36{animation-timing-function:steps()}.class-37{animation-timing-function:cubic-bezier(var(--foo),var(--bar),var(--baz),var(--foz))}.class-38{animation-timing-function:cubic-bezier(.25,var(--foo),.25,1)}