Skip to content

Commit 3360c5a

Browse files
committed
Parse the aspect-ratio property
Fixes parcel-bundler#494
1 parent 368aecd commit 3360c5a

File tree

3 files changed

+59
-1
lines changed

3 files changed

+59
-1
lines changed

src/lib.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3860,6 +3860,11 @@ mod tests {
38603860
},
38613861
);
38623862
}
3863+
3864+
minify_test(".foo { aspect-ratio: auto }", ".foo{aspect-ratio:auto}");
3865+
minify_test(".foo { aspect-ratio: 2 / 3 }", ".foo{aspect-ratio:2/3}");
3866+
minify_test(".foo { aspect-ratio: auto 2 / 3 }", ".foo{aspect-ratio:auto 2/3}");
3867+
minify_test(".foo { aspect-ratio: 2 / 3 auto }", ".foo{aspect-ratio:auto 2/3}");
38633868
}
38643869

38653870
#[test]

src/properties/mod.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,7 @@ use crate::parser::starts_with_ignore_ascii_case;
127127
use crate::parser::ParserOptions;
128128
use crate::prefixes::Feature;
129129
use crate::printer::{Printer, PrinterOptions};
130-
use crate::targets::{Targets};
130+
use crate::targets::Targets;
131131
use crate::traits::{Parse, ParseWithOptions, Shorthand, ToCss};
132132
use crate::values::number::{CSSInteger, CSSNumber};
133133
use crate::values::string::CowArcStr;
@@ -1184,6 +1184,7 @@ define_properties! {
11841184
"max-block-size": MaxBlockSize(MaxSize) [logical_group: MaxSize, category: Logical],
11851185
"max-inline-size": MaxInlineSize(MaxSize) [logical_group: MaxSize, category: Logical],
11861186
"box-sizing": BoxSizing(BoxSizing, VendorPrefix) / WebKit / Moz,
1187+
"aspect-ratio": AspectRatio(AspectRatio),
11871188

11881189
"overflow": Overflow(Overflow) shorthand: true,
11891190
"overflow-x": OverflowX(OverflowKeyword),

src/properties/size.rs

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ use crate::printer::Printer;
1010
use crate::properties::{Property, PropertyId};
1111
use crate::traits::{IsCompatible, Parse, PropertyHandler, ToCss};
1212
use crate::values::length::LengthPercentage;
13+
use crate::values::ratio::Ratio;
1314
use crate::vendor_prefix::VendorPrefix;
1415
#[cfg(feature = "visitor")]
1516
use crate::visitor::Visit;
@@ -293,6 +294,57 @@ enum_property! {
293294
}
294295
}
295296

297+
/// A value for the [aspect-ratio](https://drafts.csswg.org/css-sizing-4/#aspect-ratio) property.
298+
#[derive(Debug, Clone, PartialEq)]
299+
#[cfg_attr(feature = "visitor", derive(Visit))]
300+
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
301+
#[cfg_attr(feature = "jsonschema", derive(schemars::JsonSchema))]
302+
pub struct AspectRatio {
303+
/// The `auto` keyword.
304+
pub auto: bool,
305+
/// A preferred aspect ratio for the box, specified as width / height.
306+
pub ratio: Option<Ratio>,
307+
}
308+
309+
impl<'i> Parse<'i> for AspectRatio {
310+
fn parse<'t>(input: &mut Parser<'i, 't>) -> Result<Self, ParseError<'i, ParserError<'i>>> {
311+
let location = input.current_source_location();
312+
let mut auto = input.try_parse(|i| i.expect_ident_matching("auto"));
313+
let ratio = input.try_parse(Ratio::parse);
314+
if auto.is_err() {
315+
auto = input.try_parse(|i| i.expect_ident_matching("auto"));
316+
}
317+
if auto.is_err() && ratio.is_err() {
318+
return Err(location.new_custom_error(ParserError::InvalidValue));
319+
}
320+
321+
Ok(AspectRatio {
322+
auto: auto.is_ok(),
323+
ratio: ratio.ok(),
324+
})
325+
}
326+
}
327+
328+
impl ToCss for AspectRatio {
329+
fn to_css<W>(&self, dest: &mut Printer<W>) -> Result<(), PrinterError>
330+
where
331+
W: std::fmt::Write,
332+
{
333+
if self.auto {
334+
dest.write_str("auto")?;
335+
}
336+
337+
if let Some(ratio) = &self.ratio {
338+
if self.auto {
339+
dest.write_char(' ')?;
340+
}
341+
ratio.to_css(dest)?;
342+
}
343+
344+
Ok(())
345+
}
346+
}
347+
296348
property_bitflags! {
297349
#[derive(Default)]
298350
struct SizeProperty: u16 {

0 commit comments

Comments
 (0)