@@ -10,6 +10,7 @@ use crate::printer::Printer;
1010use crate :: properties:: { Property , PropertyId } ;
1111use crate :: traits:: { IsCompatible , Parse , PropertyHandler , ToCss } ;
1212use crate :: values:: length:: LengthPercentage ;
13+ use crate :: values:: ratio:: Ratio ;
1314use crate :: vendor_prefix:: VendorPrefix ;
1415#[ cfg( feature = "visitor" ) ]
1516use 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+
296348property_bitflags ! {
297349 #[ derive( Default ) ]
298350 struct SizeProperty : u16 {
0 commit comments