style/values/specified/
page.rs

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 and named-page style properties
6
7use crate::parser::{Parse, ParserContext};
8use crate::values::generics::size::Size2D;
9use crate::values::specified::length::NonNegativeLength;
10use crate::values::{generics, CustomIdent};
11use cssparser::Parser;
12use style_traits::ParseError;
13
14pub use generics::page::PageOrientation;
15pub use generics::page::PageSizeOrientation;
16pub use generics::page::PaperSize;
17/// Specified value of the @page size descriptor
18pub type PageSize = generics::page::PageSize<Size2D<NonNegativeLength>>;
19
20impl Parse for PageSize {
21    fn parse<'i, 't>(
22        context: &ParserContext,
23        input: &mut Parser<'i, 't>,
24    ) -> Result<Self, ParseError<'i>> {
25        // Try to parse as <page-size> [ <orientation> ]
26        if let Ok(paper_size) = input.try_parse(PaperSize::parse) {
27            let orientation = input
28                .try_parse(PageSizeOrientation::parse)
29                .unwrap_or(PageSizeOrientation::Portrait);
30            return Ok(PageSize::PaperSize(paper_size, orientation));
31        }
32        // Try to parse as <orientation> [ <page-size> ]
33        if let Ok(orientation) = input.try_parse(PageSizeOrientation::parse) {
34            if let Ok(paper_size) = input.try_parse(PaperSize::parse) {
35                return Ok(PageSize::PaperSize(paper_size, orientation));
36            }
37            return Ok(PageSize::Orientation(orientation));
38        }
39        // Try to parse dimensions
40        if let Ok(size) =
41            input.try_parse(|i| Size2D::parse_with(context, i, NonNegativeLength::parse))
42        {
43            return Ok(PageSize::Size(size));
44        }
45        // auto value
46        input.expect_ident_matching("auto")?;
47        Ok(PageSize::Auto)
48    }
49}
50
51/// Page name value.
52///
53/// https://drafts.csswg.org/css-page-3/#using-named-pages
54#[derive(
55    Clone,
56    Debug,
57    MallocSizeOf,
58    PartialEq,
59    SpecifiedValueInfo,
60    ToCss,
61    ToComputedValue,
62    ToResolvedValue,
63    ToShmem,
64)]
65#[repr(C, u8)]
66pub enum PageName {
67    /// `auto` value.
68    Auto,
69    /// Page name value
70    PageName(CustomIdent),
71}
72
73impl Parse for PageName {
74    fn parse<'i, 't>(
75        _context: &ParserContext,
76        input: &mut Parser<'i, 't>,
77    ) -> Result<Self, ParseError<'i>> {
78        let location = input.current_source_location();
79        let ident = input.expect_ident()?;
80        Ok(match_ignore_ascii_case! { ident,
81            "auto" => PageName::auto(),
82            _ => PageName::PageName(CustomIdent::from_ident(location, ident, &[])?),
83        })
84    }
85}
86
87impl PageName {
88    /// `auto` value.
89    #[inline]
90    pub fn auto() -> Self {
91        PageName::Auto
92    }
93
94    /// Whether this is the `auto` value.
95    #[inline]
96    pub fn is_auto(&self) -> bool {
97        matches!(*self, PageName::Auto)
98    }
99}