lewp_css/domain/at_rules/viewport/viewport_length.rs
1// This file is part of css. It is subject to the license terms in the COPYRIGHT file found in the top-level directory of this distribution and at https://raw.githubusercontent.com/lemonrock/css/master/COPYRIGHT. No part of predicator, including this file, may be copied, modified, propagated, or distributed except according to the terms contained in the COPYRIGHT file.
2// Copyright © 2017 The developers of css. See the COPYRIGHT file in the top-level directory of this distribution and at https://raw.githubusercontent.com/lemonrock/css/master/COPYRIGHT.
3
4use {
5 crate::{
6 domain::{
7 expressions::CalculablePropertyValue,
8 numbers::CssUnsignedNumber,
9 units::{LengthOrPercentageUnit, Unit},
10 },
11 parsers::ParserContext,
12 CustomParseError,
13 },
14 cssparser::{ParseError, Parser, ToCss},
15 std::fmt,
16 ViewportLength::*,
17};
18
19/// ViewportLength is a length | percentage | auto
20/// See <http://dev.w3.org/csswg/css-device-adapt/#min-max-width-desc>
21/// extend-to-zoom is explicitly not supported as it does not occur in CSS, only when converting from HTML's meta name="viewport" tag (see <http://dev.w3.org/csswg/css-device-adapt/#extend-to-zoom>)
22#[derive(Debug, Clone, Ord, PartialOrd, Eq, PartialEq, Hash)]
23#[allow(missing_docs)]
24pub enum ViewportLength {
25 /// Automatic length
26 auto,
27
28 /// invariant or calculated non-negative length or non-negative percentage
29 value(CalculablePropertyValue<LengthOrPercentageUnit<CssUnsignedNumber>>),
30}
31
32impl ToCss for ViewportLength {
33 fn to_css<W: fmt::Write>(&self, dest: &mut W) -> fmt::Result {
34 match *self {
35 auto => dest.write_str("auto"),
36
37 value(ref numeric_value) => numeric_value.to_css(dest),
38 }
39 }
40}
41
42impl ViewportLength {
43 pub(crate) fn parse<'i, 't>(
44 context: &ParserContext,
45 input: &mut Parser<'i, 't>,
46 ) -> Result<Self, ParseError<'i, CustomParseError<'i>>> {
47 use self::ViewportLength::*;
48
49 if input.r#try(|i| i.expect_ident_matching("auto")).is_ok() {
50 return Ok(auto);
51 }
52
53 Ok(value(
54 LengthOrPercentageUnit::parse_one_outside_calc_function(
55 context, input,
56 )?,
57 ))
58 }
59}