summaryrefslogtreecommitdiffstats
path: root/servo/tests/unit/style/parsing/position.rs
blob: ce73c7a0d2dba34ed1b1d757d7756594cd4aa0bf (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at https://mozilla.org/MPL/2.0/. */

use parsing::{parse, parse_entirely};
use style::parser::Parse;
use style::values::specified::position::*;
use style_traits::ToCss;

#[test]
fn test_position() {
    // Serialization is not actually specced
    // though these are the values expected by basic-shape
    // https://github.com/w3c/csswg-drafts/issues/368
    assert_roundtrip_with_context!(Position::parse, "center", "center center");
    assert_roundtrip_with_context!(Position::parse, "top left", "left top");
    assert_roundtrip_with_context!(Position::parse, "left top", "left top");
    assert_roundtrip_with_context!(Position::parse, "top right", "right top");
    assert_roundtrip_with_context!(Position::parse, "right top", "right top");
    assert_roundtrip_with_context!(Position::parse, "bottom left", "left bottom");
    assert_roundtrip_with_context!(Position::parse, "left bottom", "left bottom");
    assert_roundtrip_with_context!(Position::parse, "left center", "left center");
    assert_roundtrip_with_context!(Position::parse, "right center", "right center");
    assert_roundtrip_with_context!(Position::parse, "center top", "center top");
    assert_roundtrip_with_context!(Position::parse, "center bottom", "center bottom");
    assert_roundtrip_with_context!(Position::parse, "center 10px", "center 10px");
    assert_roundtrip_with_context!(Position::parse, "center 10%", "center 10%");
    assert_roundtrip_with_context!(Position::parse, "right 10%", "right 10%");

    // Only keywords can be reordered
    assert!(parse_entirely(Position::parse, "top 40%").is_err());
    assert!(parse_entirely(Position::parse, "40% left").is_err());

    // 3 and 4 value serialization
    assert_roundtrip_with_context!(Position::parse, "left 10px top 15px", "left 10px top 15px");
    assert_roundtrip_with_context!(Position::parse, "top 15px left 10px", "left 10px top 15px");
    assert_roundtrip_with_context!(Position::parse, "left 10% top 15px", "left 10% top 15px");
    assert_roundtrip_with_context!(Position::parse, "top 15px left 10%", "left 10% top 15px");
    assert_roundtrip_with_context!(Position::parse, "left top 15px", "left top 15px");
    assert_roundtrip_with_context!(Position::parse, "top 15px left", "left top 15px");
    assert_roundtrip_with_context!(Position::parse, "left 10px top", "left 10px top");
    assert_roundtrip_with_context!(Position::parse, "top left 10px", "left 10px top");
    assert_roundtrip_with_context!(Position::parse, "right 10px bottom", "right 10px bottom");
    assert_roundtrip_with_context!(Position::parse, "bottom right 10px", "right 10px bottom");
    assert_roundtrip_with_context!(Position::parse, "center right 10px", "right 10px center");
    assert_roundtrip_with_context!(Position::parse, "center bottom 10px", "center bottom 10px");

    // Invalid 3 value positions
    assert!(parse_entirely(Position::parse, "20px 30px 20px").is_err());
    assert!(parse_entirely(Position::parse, "top 30px 20px").is_err());
    assert!(parse_entirely(Position::parse, "50% bottom 20%").is_err());

    // Only horizontal and vertical keywords can have positions
    assert!(parse_entirely(Position::parse, "center 10px left 15px").is_err());
    assert!(parse_entirely(Position::parse, "center 10px 15px").is_err());
    assert!(parse_entirely(Position::parse, "center 10px bottom").is_err());

    // "Horizontal Horizontal" or "Vertical Vertical" positions cause error
    assert!(parse_entirely(Position::parse, "left right").is_err());
    assert!(parse_entirely(Position::parse, "left 10px right").is_err());
    assert!(parse_entirely(Position::parse, "left 10px right 15%").is_err());
    assert!(parse_entirely(Position::parse, "top bottom").is_err());
    assert!(parse_entirely(Position::parse, "top 10px bottom").is_err());
    assert!(parse_entirely(Position::parse, "top 10px bottom 15%").is_err());

    // Logical keywords are not supported in Position yet.
    assert!(parse(Position::parse, "x-start").is_err());
    assert!(parse(Position::parse, "y-end").is_err());
    assert!(parse(Position::parse, "x-start y-end").is_err());
    assert!(parse(Position::parse, "x-end 10px").is_err());
    assert!(parse(Position::parse, "y-start 20px").is_err());
    assert!(parse(Position::parse, "x-start bottom 10%").is_err());
    assert!(parse_entirely(Position::parse, "left y-start 10%").is_err());
    assert!(parse(Position::parse, "x-start 20px y-end 10%").is_err());
}

#[test]
fn test_horizontal_position() {
    // One value serializations.
    assert_roundtrip_with_context!(HorizontalPosition::parse, "20px", "20px");
    assert_roundtrip_with_context!(HorizontalPosition::parse, "25%", "25%");
    assert_roundtrip_with_context!(HorizontalPosition::parse, "center", "center");
    assert_roundtrip_with_context!(HorizontalPosition::parse, "left", "left");
    assert_roundtrip_with_context!(HorizontalPosition::parse, "right", "right");

    // Two value serializations.
    assert_roundtrip_with_context!(HorizontalPosition::parse, "right 10px", "right 10px");

    // Invalid horizontal positions.
    assert!(parse(HorizontalPosition::parse, "top").is_err());
    assert!(parse(HorizontalPosition::parse, "bottom").is_err());
    assert!(parse(HorizontalPosition::parse, "y-start").is_err());
    assert!(parse(HorizontalPosition::parse, "y-end").is_err());
    assert!(parse(HorizontalPosition::parse, "y-end 20px ").is_err());
    assert!(parse(HorizontalPosition::parse, "bottom 20px").is_err());
    assert!(parse(HorizontalPosition::parse, "bottom top").is_err());
    assert!(parse_entirely(HorizontalPosition::parse, "20px y-end").is_err());
    assert!(parse_entirely(HorizontalPosition::parse, "20px top").is_err());
    assert!(parse_entirely(HorizontalPosition::parse, "left center").is_err());
    assert!(parse_entirely(HorizontalPosition::parse, "left top").is_err());
    assert!(parse_entirely(HorizontalPosition::parse, "left right").is_err());
    assert!(parse_entirely(HorizontalPosition::parse, "20px 30px").is_err());
    assert!(parse_entirely(HorizontalPosition::parse, "10px left").is_err());
    assert!(parse_entirely(HorizontalPosition::parse, "x-end 20%").is_err());
    assert!(parse_entirely(HorizontalPosition::parse, "20px x-start").is_err());

    // Logical keywords are not supported in Position yet.
    assert!(parse(HorizontalPosition::parse, "x-start").is_err());
    assert!(parse(HorizontalPosition::parse, "x-end").is_err());
}

#[test]
fn test_vertical_position() {
    // One value serializations.
    assert_roundtrip_with_context!(VerticalPosition::parse, "20px", "20px");
    assert_roundtrip_with_context!(VerticalPosition::parse, "25%", "25%");
    assert_roundtrip_with_context!(VerticalPosition::parse, "center", "center");
    assert_roundtrip_with_context!(VerticalPosition::parse, "top", "top");
    assert_roundtrip_with_context!(VerticalPosition::parse, "bottom", "bottom");

    // Two value serializations.
    assert_roundtrip_with_context!(VerticalPosition::parse, "bottom 10px", "bottom 10px");

    // Invalid vertical positions.
    assert!(parse(VerticalPosition::parse, "left").is_err());
    assert!(parse(VerticalPosition::parse, "right").is_err());
    assert!(parse(VerticalPosition::parse, "x-start").is_err());
    assert!(parse(VerticalPosition::parse, "x-end").is_err());
    assert!(parse(VerticalPosition::parse, "x-end 20px").is_err());
    assert!(parse(VerticalPosition::parse, "left 20px").is_err());
    assert!(parse(VerticalPosition::parse, "left center").is_err());
    assert!(parse(VerticalPosition::parse, "left top").is_err());
    assert!(parse(VerticalPosition::parse, "left right").is_err());
    assert!(parse_entirely(VerticalPosition::parse, "20px x-end").is_err());
    assert!(parse_entirely(VerticalPosition::parse, "20px right").is_err());
    assert!(parse_entirely(VerticalPosition::parse, "bottom top").is_err());
    assert!(parse_entirely(VerticalPosition::parse, "20px 30px").is_err());
    assert!(parse_entirely(VerticalPosition::parse, "10px top").is_err());
    assert!(parse_entirely(VerticalPosition::parse, "y-end 20%").is_err());
    assert!(parse_entirely(VerticalPosition::parse, "20px y-start").is_err());

    // Logical keywords are not supported in Position yet.
    assert!(parse(VerticalPosition::parse, "y-start").is_err());
    assert!(parse(VerticalPosition::parse, "y-end").is_err());
}