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/parser): normalize dimension #6654

Merged
merged 3 commits into from
Dec 16, 2022
Merged
Show file tree
Hide file tree
Changes from all 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
2 changes: 1 addition & 1 deletion crates/swc_css_ast/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ mod value;
///
/// The type of value and patterns should be identical.
///
/// # Exmaples
/// # Examples
///
/// ```
/// use swc_atoms::JsWord;
Expand Down
2 changes: 1 addition & 1 deletion crates/swc_css_minifier/src/compressor/angle.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ impl Compressor {
return;
}

let from = match get_angle_type(&angle.unit.value.to_ascii_lowercase()) {
let from = match get_angle_type(&angle.unit.value) {
Some(angel_type) => angel_type,
None => return,
};
Expand Down
103 changes: 51 additions & 52 deletions crates/swc_css_minifier/src/compressor/calc_sum.rs
Original file line number Diff line number Diff line change
Expand Up @@ -291,8 +291,7 @@ impl CalcSumContext {
}

fn sum_length(&mut self, operator: Option<&CalcOperator>, operand: &CalcProduct, l: &Length) {
let unit = l.unit.value.to_ascii_lowercase();
if is_absolute_length(unit) {
if is_absolute_length(&l.unit.value) {
self.sum_absolute_length(operator, operand, l)
} else {
self.sum_other_length(operator, operand, l)
Expand All @@ -310,17 +309,17 @@ impl CalcSumContext {
operator: prev_operator,
operand: IndexedData { pos, data },
}) => {
let prev_unit = data.unit.value.to_ascii_lowercase();
let unit = l.unit.value.to_ascii_lowercase();
if let Some(result) = get_absolute_length_ratio(prev_unit, unit).and_then(|ratio| {
CalcSumContext::try_to_sum_values(
prev_operator.as_ref(),
operator,
data.value.value,
l.value.value,
Some(ratio),
)
}) {
if let Some(result) = get_absolute_length_ratio(&data.unit.value, &l.unit.value)
.and_then(|ratio| {
CalcSumContext::try_to_sum_values(
prev_operator.as_ref(),
operator,
data.value.value,
l.value.value,
Some(ratio),
)
})
{
data.value.value = result;

CalcSumContext::switch_sign_if_needed(
Expand Down Expand Up @@ -351,8 +350,7 @@ impl CalcSumContext {
operand: &CalcProduct,
l: &Length,
) {
let unit = l.unit.value.to_ascii_lowercase();
match &mut self.other_lengths.get_mut(&unit) {
match &mut self.other_lengths.get_mut(&l.unit.value) {
Some(IndexedOperatorAndOperand {
operator: prev_operator,
operand: IndexedData { pos, data },
Expand Down Expand Up @@ -384,7 +382,8 @@ impl CalcSumContext {
None => {
let indexed_data: IndexedOperatorAndOperand<Length> =
self.new_indexed_data(operator, operand, l.clone());
self.other_lengths.insert(unit, indexed_data);
self.other_lengths
.insert(l.unit.value.clone(), indexed_data);
}
}
}
Expand Down Expand Up @@ -430,17 +429,17 @@ impl CalcSumContext {
operator: prev_operator,
operand: IndexedData { pos, data },
}) => {
let prev_unit = data.unit.value.to_ascii_lowercase();
let unit = d.unit.value.to_ascii_lowercase();
if let Some(result) = get_duration_ratio(prev_unit, unit).and_then(|ratio| {
CalcSumContext::try_to_sum_values(
prev_operator.as_ref(),
operator,
data.value.value,
d.value.value,
Some(ratio),
)
}) {
if let Some(result) =
get_duration_ratio(&data.unit.value, &d.unit.value).and_then(|ratio| {
CalcSumContext::try_to_sum_values(
prev_operator.as_ref(),
operator,
data.value.value,
d.value.value,
Some(ratio),
)
})
{
data.value.value = result;

CalcSumContext::switch_sign_if_needed(
Expand Down Expand Up @@ -474,17 +473,17 @@ impl CalcSumContext {
operator: prev_operator,
operand: IndexedData { pos, data },
}) => {
let prev_unit = data.unit.value.to_ascii_lowercase();
let unit = f.unit.value.to_ascii_lowercase();
if let Some(result) = get_frequency_ratio(prev_unit, unit).and_then(|ratio| {
CalcSumContext::try_to_sum_values(
prev_operator.as_ref(),
operator,
data.value.value,
f.value.value,
Some(ratio),
)
}) {
if let Some(result) =
get_frequency_ratio(&data.unit.value, &f.unit.value).and_then(|ratio| {
CalcSumContext::try_to_sum_values(
prev_operator.as_ref(),
operator,
data.value.value,
f.value.value,
Some(ratio),
)
})
{
data.value.value = result;

CalcSumContext::switch_sign_if_needed(
Expand Down Expand Up @@ -518,17 +517,17 @@ impl CalcSumContext {
operator: prev_operator,
operand: IndexedData { pos, data },
}) => {
let prev_unit = data.unit.value.to_ascii_lowercase();
let unit = r.unit.value.to_ascii_lowercase();
if let Some(result) = get_resolution_ratio(prev_unit, unit).and_then(|ratio| {
CalcSumContext::try_to_sum_values(
prev_operator.as_ref(),
operator,
data.value.value,
r.value.value,
Some(ratio),
)
}) {
if let Some(result) = get_resolution_ratio(&data.unit.value, &r.unit.value)
.and_then(|ratio| {
CalcSumContext::try_to_sum_values(
prev_operator.as_ref(),
operator,
data.value.value,
r.value.value,
Some(ratio),
)
})
{
data.value.value = result;

CalcSumContext::switch_sign_if_needed(
Expand Down Expand Up @@ -591,8 +590,7 @@ impl CalcSumContext {
operand: &CalcProduct,
u: &UnknownDimension,
) {
let unit = u.unit.value.to_ascii_lowercase();
match &mut self.unknown_dimension.get_mut(&unit) {
match &mut self.unknown_dimension.get_mut(&u.unit.value) {
Some(IndexedOperatorAndOperand {
operator: prev_operator,
operand: IndexedData { pos, data },
Expand Down Expand Up @@ -625,7 +623,8 @@ impl CalcSumContext {
None => {
let indexed_data: IndexedOperatorAndOperand<UnknownDimension> =
self.new_indexed_data(operator, operand, u.clone());
self.unknown_dimension.insert(unit, indexed_data);
self.unknown_dimension
.insert(u.unit.value.clone(), indexed_data);
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion crates/swc_css_minifier/src/compressor/color.rs
Original file line number Diff line number Diff line change
Expand Up @@ -324,7 +324,7 @@ impl Compressor {
unit: Ident { value: unit, .. },
..
}) => {
let angel_type = match get_angle_type(&unit.to_ascii_lowercase()) {
let angel_type = match get_angle_type(unit) {
Some(angel_type) => angel_type,
_ => return None,
};
Expand Down
12 changes: 2 additions & 10 deletions crates/swc_css_minifier/src/compressor/declaration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -536,11 +536,7 @@ impl Compressor {
unit: unit_2,
..
}))),
) if value_1.value == value_2.value
&& unit_1.value.eq_ignore_ascii_case(&unit_2.value) =>
{
true
}
) if value_1.value == value_2.value && unit_1.value == unit_2.value => true,
(
Some(ComponentValue::Integer(box Integer { value: 0, .. })),
Some(ComponentValue::Integer(box Integer { value: 0, .. })),
Expand Down Expand Up @@ -576,11 +572,7 @@ impl Compressor {
unit: unit_2,
..
}))),
) if value_1.value == value_2.value
&& unit_1.value.eq_ignore_ascii_case(&unit_2.value) =>
{
true
}
) if value_1.value == value_2.value && unit_1.value == unit_2.value => true,
(
Some(ComponentValue::Percentage(box Percentage { value: value_1, .. })),
Some(ComponentValue::Percentage(box Percentage { value: value_2, .. })),
Expand Down
2 changes: 1 addition & 1 deletion crates/swc_css_minifier/src/compressor/frequency.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use super::Compressor;

impl Compressor {
pub(super) fn compress_frequency(&mut self, frequency: &mut Frequency) {
match frequency.unit.value.to_ascii_lowercase() {
match frequency.unit.value {
js_word!("hz")
if frequency.value.value > 0.0 && frequency.value.value % 1000.0 == 0.0 =>
{
Expand Down
41 changes: 20 additions & 21 deletions crates/swc_css_minifier/src/compressor/length.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ use swc_css_ast::*;
use super::Compressor;

impl Compressor {
fn convert_length(&mut self, value: f64, from_unit: JsWord, to_unit: JsWord) -> f64 {
match to_unit {
js_word!("cm") => match from_unit {
fn convert_length(&mut self, value: f64, from_unit: &JsWord, to_unit: &JsWord) -> f64 {
match *to_unit {
js_word!("cm") => match *from_unit {
js_word!("cm") => value,
js_word!("mm") => value / 10.0,
js_word!("q") => value / 40.0,
Expand All @@ -18,7 +18,7 @@ impl Compressor {
unreachable!()
}
},
js_word!("mm") => match from_unit {
js_word!("mm") => match *from_unit {
js_word!("cm") => 10.0 * value,
js_word!("mm") => value,
js_word!("q") => value / 4.0,
Expand All @@ -30,7 +30,7 @@ impl Compressor {
unreachable!()
}
},
js_word!("q") => match from_unit {
js_word!("q") => match *from_unit {
js_word!("cm") => 40.0 * value,
js_word!("mm") => 4.0 * value,
js_word!("q") => value,
Expand All @@ -42,7 +42,7 @@ impl Compressor {
unreachable!()
}
},
js_word!("in") => match from_unit {
js_word!("in") => match *from_unit {
js_word!("cm") => value / 2.54,
js_word!("mm") => value / 25.4,
js_word!("q") => value / 101.6,
Expand All @@ -54,7 +54,7 @@ impl Compressor {
unreachable!()
}
},
js_word!("pc") => match from_unit {
js_word!("pc") => match *from_unit {
js_word!("cm") => 6.0 / 2.54 * value,
js_word!("mm") => 6.0 / 25.4 * value,
js_word!("q") => 6.0 / 101.6 * value,
Expand All @@ -66,7 +66,7 @@ impl Compressor {
unreachable!()
}
},
js_word!("pt") => match from_unit {
js_word!("pt") => match *from_unit {
js_word!("cm") => 72.0 / 2.54 * value,
js_word!("mm") => 72.0 / 25.4 * value,
js_word!("q") => 72.0 / 101.6 * value,
Expand All @@ -78,7 +78,7 @@ impl Compressor {
unreachable!()
}
},
js_word!("px") => match from_unit {
js_word!("px") => match *from_unit {
js_word!("cm") => 96.0 / 2.54 * value,
js_word!("mm") => 96.0 / 25.4 * value,
js_word!("q") => 96.0 / 101.6 * value,
Expand Down Expand Up @@ -134,13 +134,12 @@ impl Compressor {
}

pub(super) fn compress_length(&mut self, length: &mut Length) {
let from = length.unit.value.to_ascii_lowercase();
let value = length.value.value;

match from {
match length.unit.value {
js_word!("cm") => {
if value % 2.54 == 0.0 {
let new_value = self.convert_length(value, from, js_word!("in"));
let new_value = self.convert_length(value, &length.unit.value, &js_word!("in"));

length.value = Number {
span: length.value.span,
Expand All @@ -153,7 +152,7 @@ impl Compressor {
raw: None,
};
} else if value <= 0.1 {
let new_value = self.convert_length(value, from, js_word!("mm"));
let new_value = self.convert_length(value, &length.unit.value, &js_word!("mm"));

length.value = Number {
span: length.value.span,
Expand All @@ -169,7 +168,7 @@ impl Compressor {
}
js_word!("mm") => {
if value % 25.4 == 0.0 {
let new_value = self.convert_length(value, from, js_word!("in"));
let new_value = self.convert_length(value, &length.unit.value, &js_word!("in"));

length.value = Number {
span: length.value.span,
Expand All @@ -182,7 +181,7 @@ impl Compressor {
raw: None,
};
} else if value % 10.0 == 0.0 {
let new_value = self.convert_length(value, from, js_word!("cm"));
let new_value = self.convert_length(value, &length.unit.value, &js_word!("cm"));

length.value = Number {
span: length.value.span,
Expand All @@ -198,7 +197,7 @@ impl Compressor {
}
js_word!("q") => {
if value > 80.0 && value % 40.0 == 0.0 {
let new_value = self.convert_length(value, from, js_word!("cm"));
let new_value = self.convert_length(value, &length.unit.value, &js_word!("cm"));

length.value = Number {
span: length.value.span,
Expand All @@ -211,7 +210,7 @@ impl Compressor {
raw: None,
};
} else if value % 101.6 == 0.0 {
let new_value = self.convert_length(value, from, js_word!("in"));
let new_value = self.convert_length(value, &length.unit.value, &js_word!("in"));

length.value = Number {
span: length.value.span,
Expand All @@ -227,7 +226,7 @@ impl Compressor {
}
js_word!("pc") => {
if value % 6.0 == 0.0 {
let new_value = self.convert_length(value, from, js_word!("in"));
let new_value = self.convert_length(value, &length.unit.value, &js_word!("in"));

length.value = Number {
span: length.value.span,
Expand All @@ -243,7 +242,7 @@ impl Compressor {
}
js_word!("pt") => {
if value % 72.0 == 0.0 {
let new_value = self.convert_length(value, from, js_word!("in"));
let new_value = self.convert_length(value, &length.unit.value, &js_word!("in"));

length.value = Number {
span: length.value.span,
Expand All @@ -256,7 +255,7 @@ impl Compressor {
raw: None,
};
} else if value % 12.0 == 0.0 {
let new_value = self.convert_length(value, from, js_word!("pc"));
let new_value = self.convert_length(value, &length.unit.value, &js_word!("pc"));

length.value = Number {
span: length.value.span,
Expand All @@ -269,7 +268,7 @@ impl Compressor {
raw: None,
};
} else if value % 0.75 == 0.0 {
let new_value = self.convert_length(value, from, js_word!("px"));
let new_value = self.convert_length(value, &length.unit.value, &js_word!("px"));

length.value = Number {
span: length.value.span,
Expand Down