1 /* This Source Code Form is subject to the terms of the Mozilla Public
2  * License, v. 2.0. If a copy of the MPL was not distributed with this
3  * file, You can obtain one at https://mozilla.org/MPL/2.0/. */
4 
5 //! Specified @page at-rule properties
6 
7 use crate::parser::{Parse, ParserContext};
8 use crate::values::generics;
9 use crate::values::generics::size::Size2D;
10 use crate::values::specified::length::NonNegativeLength;
11 use cssparser::Parser;
12 use style_traits::ParseError;
13 
14 pub use generics::page::Orientation;
15 pub use generics::page::PaperSize;
16 /// Specified value of the @page size descriptor
17 pub type PageSize = generics::page::PageSize<Size2D<NonNegativeLength>>;
18 
19 impl Parse for PageSize {
parse<'i, 't>( context: &ParserContext, input: &mut Parser<'i, 't>, ) -> Result<Self, ParseError<'i>>20     fn parse<'i, 't>(
21         context: &ParserContext,
22         input: &mut Parser<'i, 't>,
23     ) -> Result<Self, ParseError<'i>> {
24         // Try to parse as <page-size> [ <orientation> ]
25         if let Ok(paper_size) = input.try_parse(PaperSize::parse) {
26             if let Ok(orientation) = input.try_parse(Orientation::parse) {
27                 return Ok(PageSize::PaperSizeAndOrientation(paper_size, orientation));
28             }
29             return Ok(PageSize::PaperSize(paper_size));
30         }
31         // Try to parse as <orientation> [ <page-size> ]
32         if let Ok(orientation) = input.try_parse(Orientation::parse) {
33             if let Ok(paper_size) = input.try_parse(PaperSize::parse) {
34                 return Ok(PageSize::PaperSizeAndOrientation(paper_size, orientation));
35             }
36             return Ok(PageSize::Orientation(orientation));
37         }
38         // Try to parse dimensions
39         if let Ok(size) =
40             input.try_parse(|i| Size2D::parse_with(context, i, NonNegativeLength::parse))
41         {
42             return Ok(PageSize::Size(size));
43         }
44         // auto value
45         input.expect_ident_matching("auto")?;
46         Ok(PageSize::Auto)
47     }
48 }
49