Skip to content

Commit

Permalink
Prevent simplifying translate: none; and scale: none; (#703)
Browse files Browse the repository at this point in the history
  • Loading branch information
RobinMalfait committed May 14, 2024
1 parent d7aeff3 commit a4cc024
Show file tree
Hide file tree
Showing 3 changed files with 120 additions and 81 deletions.
78 changes: 43 additions & 35 deletions node/ast.d.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/* tslint:disable */
/* eslint-disable */
/**
* This file was automatically generated by json-schema-to-typescript.
* DO NOT MODIFY IT BY HAND. Instead, modify the source JSONSchema file,
Expand Down Expand Up @@ -5615,6 +5615,48 @@ export type Perspective =
type: "length";
value: Length;
};
/**
* A value for the [translate](https://drafts.csswg.org/css-transforms-2/#propdef-translate) property.
*/
export type Translate =
| "None"
| {
XYZ: {
/**
* The x translation.
*/
x: DimensionPercentageFor_LengthValue;
/**
* The y translation.
*/
y: DimensionPercentageFor_LengthValue;
/**
* The z translation.
*/
z: Length;
};
};
/**
* A value for the [scale](https://drafts.csswg.org/css-transforms-2/#propdef-scale) property.
*/
export type Scale =
| "None"
| {
XYZ: {
/**
* Scale on the x axis.
*/
x: NumberOrPercentage;
/**
* Scale on the y axis.
*/
y: NumberOrPercentage;
/**
* Scale on the z axis.
*/
z: NumberOrPercentage;
};
};
/**
* Defines how text case should be transformed in the [text-transform](https://www.w3.org/TR/2021/CRD-css-text-3-20210422/#text-transform-property) property.
*/
Expand Down Expand Up @@ -8519,23 +8561,6 @@ export interface Matrix3DForFloat {
m43: number;
m44: number;
}
/**
* A value for the [translate](https://drafts.csswg.org/css-transforms-2/#propdef-translate) property.
*/
export interface Translate {
/**
* The x translation.
*/
x: DimensionPercentageFor_LengthValue;
/**
* The y translation.
*/
y: DimensionPercentageFor_LengthValue;
/**
* The z translation.
*/
z: Length;
}
/**
* A value for the [rotate](https://drafts.csswg.org/css-transforms-2/#propdef-rotate) property.
*/
Expand All @@ -8557,23 +8582,6 @@ export interface Rotate {
*/
z: number;
}
/**
* A value for the [scale](https://drafts.csswg.org/css-transforms-2/#propdef-scale) property.
*/
export interface Scale {
/**
* Scale on the x axis.
*/
x: NumberOrPercentage;
/**
* Scale on the y axis.
*/
y: NumberOrPercentage;
/**
* Scale on the z axis.
*/
z: NumberOrPercentage;
}
/**
* A value for the [text-transform](https://www.w3.org/TR/2021/CRD-css-text-3-20210422/#text-transform-property) property.
*/
Expand Down
4 changes: 2 additions & 2 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11645,7 +11645,7 @@ mod tests {
minify_test(".foo { translate: 1px 0px 0px }", ".foo{translate:1px}");
minify_test(".foo { translate: 1px 2px 0px }", ".foo{translate:1px 2px}");
minify_test(".foo { translate: 1px 0px 2px }", ".foo{translate:1px 0 2px}");
minify_test(".foo { translate: none }", ".foo{translate:0}");
minify_test(".foo { translate: none }", ".foo{translate:none}");
minify_test(".foo { rotate: 10deg }", ".foo{rotate:10deg}");
minify_test(".foo { rotate: z 10deg }", ".foo{rotate:10deg}");
minify_test(".foo { rotate: 0 0 1 10deg }", ".foo{rotate:10deg}");
Expand All @@ -11659,7 +11659,7 @@ mod tests {
minify_test(".foo { scale: 1 }", ".foo{scale:1}");
minify_test(".foo { scale: 1 1 }", ".foo{scale:1}");
minify_test(".foo { scale: 1 1 1 }", ".foo{scale:1}");
minify_test(".foo { scale: none }", ".foo{scale:1}");
minify_test(".foo { scale: none }", ".foo{scale:none}");
minify_test(".foo { scale: 1 0 }", ".foo{scale:1 0}");
minify_test(".foo { scale: 1 0 1 }", ".foo{scale:1 0}");
minify_test(".foo { scale: 1 0 0 }", ".foo{scale:1 0 0}");
Expand Down
119 changes: 75 additions & 44 deletions src/properties/transform.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1457,23 +1457,25 @@ impl ToCss for Perspective {
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
#[cfg_attr(feature = "jsonschema", derive(schemars::JsonSchema))]
#[cfg_attr(feature = "into_owned", derive(static_self::IntoOwned))]
pub struct Translate {
/// The x translation.
pub x: LengthPercentage,
/// The y translation.
pub y: LengthPercentage,
/// The z translation.
pub z: Length,
pub enum Translate {
/// The "none" keyword.
None,

/// The x, y, and z translations.
XYZ {
/// The x translation.
x: LengthPercentage,
/// The y translation.
y: LengthPercentage,
/// The z translation.
z: Length,
},
}

impl<'i> Parse<'i> for Translate {
fn parse<'t>(input: &mut Parser<'i, 't>) -> Result<Self, ParseError<'i, ParserError<'i>>> {
if input.try_parse(|i| i.expect_ident_matching("none")).is_ok() {
return Ok(Translate {
x: LengthPercentage::zero(),
y: LengthPercentage::zero(),
z: Length::zero(),
});
return Ok(Translate::None);
}

let x = LengthPercentage::parse(input)?;
Expand All @@ -1484,7 +1486,7 @@ impl<'i> Parse<'i> for Translate {
None
};

Ok(Translate {
Ok(Translate::XYZ {
x,
y: y.unwrap_or(LengthPercentage::zero()),
z: z.unwrap_or(Length::zero()),
Expand All @@ -1497,23 +1499,36 @@ impl ToCss for Translate {
where
W: std::fmt::Write,
{
self.x.to_css(dest)?;
if !self.y.is_zero() || !self.z.is_zero() {
dest.write_char(' ')?;
self.y.to_css(dest)?;
if !self.z.is_zero() {
dest.write_char(' ')?;
self.z.to_css(dest)?;
match self {
Translate::None => {
dest.write_str("none")?;
}
}
Translate::XYZ { x, y, z } => {
x.to_css(dest)?;
if !y.is_zero() || !z.is_zero() {
dest.write_char(' ')?;
y.to_css(dest)?;
if !z.is_zero() {
dest.write_char(' ')?;
z.to_css(dest)?;
}
}
}
};

Ok(())
}
}

impl Translate {
/// Converts the translation to a transform function.
pub fn to_transform(&self) -> Transform {
Transform::Translate3d(self.x.clone(), self.y.clone(), self.z.clone())
match self {
Translate::None => {
Transform::Translate3d(LengthPercentage::zero(), LengthPercentage::zero(), Length::zero())
}
Translate::XYZ { x, y, z } => Transform::Translate3d(x.clone(), y.clone(), z.clone()),
}
}
}

Expand Down Expand Up @@ -1610,23 +1625,25 @@ impl Rotate {
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
#[cfg_attr(feature = "jsonschema", derive(schemars::JsonSchema))]
#[cfg_attr(feature = "into_owned", derive(static_self::IntoOwned))]
pub struct Scale {
/// Scale on the x axis.
pub x: NumberOrPercentage,
/// Scale on the y axis.
pub y: NumberOrPercentage,
/// Scale on the z axis.
pub z: NumberOrPercentage,
pub enum Scale {
/// The "none" keyword.
None,

/// Scale on the x, y, and z axis.
XYZ {
/// Scale on the x axis.
x: NumberOrPercentage,
/// Scale on the y axis.
y: NumberOrPercentage,
/// Scale on the z axis.
z: NumberOrPercentage,
},
}

impl<'i> Parse<'i> for Scale {
fn parse<'t>(input: &mut Parser<'i, 't>) -> Result<Self, ParseError<'i, ParserError<'i>>> {
if input.try_parse(|i| i.expect_ident_matching("none")).is_ok() {
return Ok(Scale {
x: NumberOrPercentage::Number(1.0),
y: NumberOrPercentage::Number(1.0),
z: NumberOrPercentage::Number(1.0),
});
return Ok(Scale::None);
}

let x = NumberOrPercentage::parse(input)?;
Expand All @@ -1637,7 +1654,7 @@ impl<'i> Parse<'i> for Scale {
None
};

Ok(Scale {
Ok(Scale::XYZ {
x: x.clone(),
y: y.unwrap_or(x),
z: z.unwrap_or(NumberOrPercentage::Number(1.0)),
Expand All @@ -1650,14 +1667,21 @@ impl ToCss for Scale {
where
W: std::fmt::Write,
{
self.x.to_css(dest)?;
let zv: f32 = (&self.z).into();
if self.y != self.x || zv != 1.0 {
dest.write_char(' ')?;
self.y.to_css(dest)?;
if zv != 1.0 {
dest.write_char(' ')?;
self.z.to_css(dest)?;
match self {
Scale::None => {
dest.write_str("none")?;
}
Scale::XYZ { x, y, z } => {
x.to_css(dest)?;
let zv: f32 = z.into();
if y != x || zv != 1.0 {
dest.write_char(' ')?;
y.to_css(dest)?;
if zv != 1.0 {
dest.write_char(' ')?;
z.to_css(dest)?;
}
}
}
}

Expand All @@ -1668,7 +1692,14 @@ impl ToCss for Scale {
impl Scale {
/// Converts the scale to a transform function.
pub fn to_transform(&self) -> Transform {
Transform::Scale3d(self.x.clone(), self.y.clone(), self.z.clone())
match self {
Scale::None => Transform::Scale3d(
NumberOrPercentage::Number(1.0),
NumberOrPercentage::Number(1.0),
NumberOrPercentage::Number(1.0),
),
Scale::XYZ { x, y, z } => Transform::Scale3d(x.clone(), y.clone(), z.clone()),
}
}
}

Expand Down

0 comments on commit a4cc024

Please sign in to comment.