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 http://mozilla.org/MPL/2.0/. */
4
5 use cssparser::{Parser, ParserInput};
6 use parsing::parse;
7 use style::context::QuirksMode;
8 use style::parser::{Parse, ParserContext};
9 use style::stylesheets::{CssRuleType, Origin};
10 use style::values::specified::length::{AbsoluteLength, Length, NoCalcLength};
11 use style_traits::{ParsingMode, ToCss};
12
13 #[test]
test_calc()14 fn test_calc() {
15 assert!(parse(Length::parse, "calc(1px+ 2px)").is_err());
16 assert!(parse(Length::parse, "calc(calc(1px) + calc(1px + 4px))").is_ok());
17 assert!(parse(Length::parse, "calc( 1px + 2px )").is_ok());
18 assert!(parse(Length::parse, "calc(1px + 2px )").is_ok());
19 assert!(parse(Length::parse, "calc( 1px + 2px)").is_ok());
20 assert!(parse(Length::parse, "calc( 1px + 2px / ( 1 + 2 - 1))").is_ok());
21 }
22
23 #[test]
test_length_literals()24 fn test_length_literals() {
25 assert_roundtrip_with_context!(Length::parse, "0.33px", "0.33px");
26 assert_roundtrip_with_context!(Length::parse, "0.33in", "0.33in");
27 assert_roundtrip_with_context!(Length::parse, "0.33cm", "0.33cm");
28 assert_roundtrip_with_context!(Length::parse, "0.33mm", "0.33mm");
29 assert_roundtrip_with_context!(Length::parse, "0.33q", "0.33q");
30 assert_roundtrip_with_context!(Length::parse, "0.33pt", "0.33pt");
31 assert_roundtrip_with_context!(Length::parse, "0.33pc", "0.33pc");
32 }
33
34 #[test]
test_parsing_modes()35 fn test_parsing_modes() {
36 // In default length mode, non-zero lengths must have a unit.
37 assert!(parse(Length::parse, "1").is_err());
38
39 // In SVG length mode, non-zero lengths are assumed to be px.
40 let url = ::servo_url::ServoUrl::parse("http://localhost").unwrap();
41 let context = ParserContext::new(Origin::Author, &url,
42 Some(CssRuleType::Style), ParsingMode::ALLOW_UNITLESS_LENGTH,
43 QuirksMode::NoQuirks);
44 let mut input = ParserInput::new("1");
45 let mut parser = Parser::new(&mut input);
46 let result = Length::parse(&context, &mut parser);
47 assert!(result.is_ok());
48 assert_eq!(result.unwrap(), Length::NoCalc(NoCalcLength::Absolute(AbsoluteLength::Px(1.))));
49 }
50