Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(css/minifier): Handle nested calc() #6153

Merged
merged 3 commits into from
Oct 17, 2022
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
58 changes: 28 additions & 30 deletions crates/swc_css_minifier/src/compressor/calc_sum.rs
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,20 @@ impl CalcSumContext {
let mut sum = s.clone();
self.nested_fold(operator, &mut sum);
}
CalcValueOrOperator::Value(CalcValue::Function(Function {
name, value, ..
})) if is_calc_function_name(name) && value.len() == 1 => {
match &value[0] {
ComponentValue::CalcSum(calc_sum) => {
let mut sum = calc_sum.clone();
self.nested_fold(operator, &mut sum);
}
_ => {
// Other cases (constant, function...), just push the data
self.push(operator, operand);
}
}
}
_ => {
// Other cases (constant, function...), just push the data
self.push(operator, operand);
Expand Down Expand Up @@ -970,39 +984,23 @@ impl Compressor {
ComponentValue::CalcSum(CalcSum {
expressions: calc_sum_expressions,
..
}) if calc_sum_expressions.len() == 1 => {
match &calc_sum_expressions[0] {
CalcProductOrOperator::Product(CalcProduct {
expressions: calc_product_expressions,
..
}) if calc_product_expressions.len() == 1 => {
match &calc_product_expressions[0] {
CalcValueOrOperator::Value(CalcValue::Sum(_)) => {
// Do nothing, we cannot transform a
// CalcSum into a ComponentValue
}
CalcValueOrOperator::Value(CalcValue::Constant(_)) => {
// https://www.w3.org/TR/css-values-4/#calc-constants
// "These keywords are only usable
// within a calculation"
// "If used outside of a calculation,
// they’re treated like any other
// keyword"
}
// `calc` and other math functions can be used in `@supports` to
// check availability, we should leave them as is
CalcValueOrOperator::Value(calc_value)
if !self.in_supports_conidition =>
{
*component_value =
transform_calc_value_into_component_value(calc_value);
}
_ => {}
}) if calc_sum_expressions.len() == 1 => match &calc_sum_expressions[0] {
CalcProductOrOperator::Product(CalcProduct {
expressions: calc_product_expressions,
..
}) if calc_product_expressions.len() == 1 => {
if let CalcValueOrOperator::Value(calc_value) =
&calc_product_expressions[0]
{
if let Some(cv) =
transform_calc_value_into_component_value(calc_value)
{
*component_value = cv;
}
}
_ => {}
}
}
_ => {}
},
_ => {}
}
}
Expand Down
50 changes: 17 additions & 33 deletions crates/swc_css_minifier/src/compressor/container.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,46 +13,30 @@ impl Compressor {
ComponentValue::CalcSum(CalcSum {
expressions: calc_sum_expressions,
..
}) if calc_sum_expressions.len() == 1 => {
match &calc_sum_expressions[0] {
CalcProductOrOperator::Product(CalcProduct {
expressions: calc_product_expressions,
..
}) if calc_product_expressions.len() == 1 => {
match &calc_product_expressions[0] {
CalcValueOrOperator::Value(CalcValue::Sum(_)) => {
// Do nothing, we cannot transform a
// CalcSum into a ComponentValue
}) if calc_sum_expressions.len() == 1 => match &calc_sum_expressions[0] {
CalcProductOrOperator::Product(CalcProduct {
expressions: calc_product_expressions,
..
}) if calc_product_expressions.len() == 1 => {
if let CalcValueOrOperator::Value(calc_value) =
&calc_product_expressions[0]
{
match transform_calc_value_into_component_value(calc_value) {
Some(ComponentValue::Function(function)) => {
*n = SizeFeatureValue::Function(function);
}
CalcValueOrOperator::Value(CalcValue::Constant(_)) => {
// https://www.w3.org/TR/css-values-4/#calc-constants
// "These keywords are only usable
// within a calculation"
// "If used outside of a calculation,
// they’re treated like any other
// keyword"
Some(ComponentValue::Dimension(dimension)) => {
*n = SizeFeatureValue::Dimension(dimension);
}
CalcValueOrOperator::Value(calc_value) => {
match transform_calc_value_into_component_value(calc_value)
{
ComponentValue::Function(function) => {
*n = SizeFeatureValue::Function(function);
}
ComponentValue::Dimension(dimension) => {
*n = SizeFeatureValue::Dimension(dimension);
}
ComponentValue::Number(number) => {
*n = SizeFeatureValue::Number(number);
}
_ => {}
}
Some(ComponentValue::Number(number)) => {
*n = SizeFeatureValue::Number(number);
}
_ => {}
}
}
_ => {}
}
}
_ => {}
},
_ => {}
}
}
Expand Down
56 changes: 30 additions & 26 deletions crates/swc_css_minifier/src/compressor/math/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,72 +7,76 @@ pub fn is_calc_function_name(ident: &Ident) -> bool {
|| ident.value.to_ascii_lowercase() == js_word!("-moz-calc")
}

pub fn transform_calc_value_into_component_value(calc_value: &CalcValue) -> ComponentValue {
pub fn transform_calc_value_into_component_value(calc_value: &CalcValue) -> Option<ComponentValue> {
match &calc_value {
CalcValue::Number(n) => ComponentValue::Number(n.clone()),
CalcValue::Number(n) => Some(ComponentValue::Number(n.clone())),
CalcValue::Dimension(Dimension::Length(l)) => {
ComponentValue::Dimension(Dimension::Length(Length {
Some(ComponentValue::Dimension(Dimension::Length(Length {
span: l.span,
value: l.value.clone(),
unit: l.unit.clone(),
}))
})))
}
CalcValue::Dimension(Dimension::Angle(a)) => {
ComponentValue::Dimension(Dimension::Angle(Angle {
Some(ComponentValue::Dimension(Dimension::Angle(Angle {
span: a.span,
value: a.value.clone(),
unit: a.unit.clone(),
}))
})))
}
CalcValue::Dimension(Dimension::Time(t)) => {
ComponentValue::Dimension(Dimension::Time(Time {
Some(ComponentValue::Dimension(Dimension::Time(Time {
span: t.span,
value: t.value.clone(),
unit: t.unit.clone(),
}))
})))
}
CalcValue::Dimension(Dimension::Frequency(f)) => {
ComponentValue::Dimension(Dimension::Frequency(Frequency {
Some(ComponentValue::Dimension(Dimension::Frequency(Frequency {
span: f.span,
value: f.value.clone(),
unit: f.unit.clone(),
}))
})))
}
CalcValue::Dimension(Dimension::Resolution(r)) => {
ComponentValue::Dimension(Dimension::Resolution(Resolution {
CalcValue::Dimension(Dimension::Resolution(r)) => Some(ComponentValue::Dimension(
Dimension::Resolution(Resolution {
span: r.span,
value: r.value.clone(),
unit: r.unit.clone(),
}))
}
}),
)),
CalcValue::Dimension(Dimension::Flex(f)) => {
ComponentValue::Dimension(Dimension::Flex(Flex {
Some(ComponentValue::Dimension(Dimension::Flex(Flex {
span: f.span,
value: f.value.clone(),
unit: f.unit.clone(),
}))
})))
}
CalcValue::Dimension(Dimension::UnknownDimension(u)) => {
ComponentValue::Dimension(Dimension::UnknownDimension(UnknownDimension {
CalcValue::Dimension(Dimension::UnknownDimension(u)) => Some(ComponentValue::Dimension(
Dimension::UnknownDimension(UnknownDimension {
span: u.span,
value: u.value.clone(),
unit: u.unit.clone(),
}))
}
CalcValue::Percentage(p) => ComponentValue::Percentage(Percentage {
}),
)),
CalcValue::Percentage(p) => Some(ComponentValue::Percentage(Percentage {
span: p.span,
value: p.value.clone(),
}),
CalcValue::Function(f) => ComponentValue::Function(Function {
})),
CalcValue::Function(f) => Some(ComponentValue::Function(Function {
span: f.span,
name: f.name.clone(),
value: f.value.to_vec(),
}),
})),
CalcValue::Constant(_) => {
unreachable!("CalcValue::Constant cannot be transformed into a ComponentValue per spec")
// https://www.w3.org/TR/css-values-4/#calc-constants
// "These keywords are only usable within a calculation"
// "If used outside of a calculation, they’re treated like any other keyword"
None
}
CalcValue::Sum(_) => {
unreachable!("CalcValue::Sum cannot be transformed into a ComponentValue")
// Do nothing, we cannot transform a CalcSum into a ComponentValue
None
}
}
}
50 changes: 17 additions & 33 deletions crates/swc_css_minifier/src/compressor/media.rs
Original file line number Diff line number Diff line change
Expand Up @@ -339,46 +339,30 @@ impl Compressor {
ComponentValue::CalcSum(CalcSum {
expressions: calc_sum_expressions,
..
}) if calc_sum_expressions.len() == 1 => {
match &calc_sum_expressions[0] {
CalcProductOrOperator::Product(CalcProduct {
expressions: calc_product_expressions,
..
}) if calc_product_expressions.len() == 1 => {
match &calc_product_expressions[0] {
CalcValueOrOperator::Value(CalcValue::Sum(_)) => {
// Do nothing, we cannot transform a
// CalcSum into a ComponentValue
}) if calc_sum_expressions.len() == 1 => match &calc_sum_expressions[0] {
CalcProductOrOperator::Product(CalcProduct {
expressions: calc_product_expressions,
..
}) if calc_product_expressions.len() == 1 => {
if let CalcValueOrOperator::Value(calc_value) =
&calc_product_expressions[0]
{
match transform_calc_value_into_component_value(calc_value) {
Some(ComponentValue::Function(function)) => {
*n = MediaFeatureValue::Function(function);
}
CalcValueOrOperator::Value(CalcValue::Constant(_)) => {
// https://www.w3.org/TR/css-values-4/#calc-constants
// "These keywords are only usable
// within a calculation"
// "If used outside of a calculation,
// they’re treated like any other
// keyword"
Some(ComponentValue::Dimension(dimension)) => {
*n = MediaFeatureValue::Dimension(dimension);
}
CalcValueOrOperator::Value(calc_value) => {
match transform_calc_value_into_component_value(calc_value)
{
ComponentValue::Function(function) => {
*n = MediaFeatureValue::Function(function);
}
ComponentValue::Dimension(dimension) => {
*n = MediaFeatureValue::Dimension(dimension);
}
ComponentValue::Number(number) => {
*n = MediaFeatureValue::Number(number);
}
_ => {}
}
Some(ComponentValue::Number(number)) => {
*n = MediaFeatureValue::Number(number);
}
_ => {}
}
}
_ => {}
}
}
_ => {}
},
_ => {}
}
}
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.