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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
|
/* 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 std::cell::RefCell;
use cssparser::SourceLocation;
use servo_arc::Arc;
use style::context::QuirksMode;
use style::error_reporting::{ContextualParseError, ParseErrorReporter};
use style::media_queries::MediaList;
use style::shared_lock::SharedRwLock;
use style::stylesheets::{AllowImportRules, Origin, Stylesheet, UrlExtraData};
use url::Url;
#[derive(Debug)]
struct CSSError {
pub url: Arc<Url>,
pub line: u32,
pub column: u32,
pub message: String,
}
struct TestingErrorReporter {
errors: RefCell<Vec<CSSError>>,
}
impl TestingErrorReporter {
pub fn new() -> Self {
TestingErrorReporter {
errors: RefCell::new(Vec::new()),
}
}
fn assert_messages_contain(&self, expected_errors: &[(u32, u32, &str)]) {
let errors = self.errors.borrow();
for (i, (error, &(line, column, message))) in errors.iter().zip(expected_errors).enumerate()
{
assert_eq!(
(error.line, error.column),
(line, column),
"line/column numbers of the {}th error: {:?}",
i + 1,
error.message
);
assert!(
error.message.contains(message),
"{:?} does not contain {:?}",
error.message,
message
);
}
if errors.len() < expected_errors.len() {
panic!("Missing errors: {:#?}", &expected_errors[errors.len()..]);
}
if errors.len() > expected_errors.len() {
panic!("Extra errors: {:#?}", &errors[expected_errors.len()..]);
}
}
}
impl ParseErrorReporter for TestingErrorReporter {
fn report_error(
&self,
url: &UrlExtraData,
location: SourceLocation,
error: ContextualParseError,
) {
self.errors.borrow_mut().push(CSSError {
url: url.0.clone(),
line: location.line,
column: location.column,
message: error.to_string(),
})
}
}
#[test]
fn test_report_error_stylesheet() {
let css = r"
div {
background-color: red;
display: invalid;
background-image: linear-gradient(0deg, black, invalid, transparent);
invalid: true;
}
@media (min-width: 10px invalid 1000px) {}
@font-face { src: url(), invalid, url(); }
@counter-style foo { symbols: a 0invalid b }
@font-feature-values Sans Sans { @foo {} @swash { foo: 1 invalid 2 } }
@invalid;
@media screen { @invalid; }
@supports (color: green) and invalid and (margin: 0) {}
@keyframes foo { from invalid {} to { margin: 0 invalid 0; } }
";
let url = Url::parse("about::test").unwrap();
let error_reporter = TestingErrorReporter::new();
let lock = SharedRwLock::new();
let media = Arc::new(lock.wrap(MediaList::empty()));
Stylesheet::from_str(
css,
url.clone().into(),
Origin::UserAgent,
media,
lock,
None,
Some(&error_reporter),
QuirksMode::NoQuirks,
AllowImportRules::Yes,
);
error_reporter.assert_messages_contain(&[
(
3,
18,
"Unsupported property declaration: 'display: invalid;'",
),
(
4,
43,
"Unsupported property declaration: 'background-image:",
), // FIXME: column should be around 56
(5, 17, "Unsupported property declaration: 'invalid: true;'"),
(7, 28, "Invalid media rule"),
// When @counter-style is supported, this should be replaced with two errors
(9, 19, "Invalid rule: '@counter-style "),
// When @font-feature-values is supported, this should be replaced with two errors
(10, 25, "Invalid rule: '@font-feature-values "),
(11, 13, "Invalid rule: '@invalid'"),
(12, 29, "Invalid rule: '@invalid'"),
(13, 34, "Invalid rule: '@supports "),
(14, 26, "Invalid keyframe rule: 'from invalid '"),
(
14,
52,
"Unsupported property declaration: 'margin: 0 invalid 0;'",
),
]);
assert_eq!(*error_reporter.errors.borrow()[0].url, url);
}
#[test]
fn test_no_report_unrecognized_vendor_properties() {
let css = r"
div {
-o-background-color: red;
_background-color: red;
-moz-background-color: red;
}
";
let url = Url::parse("about::test").unwrap();
let error_reporter = TestingErrorReporter::new();
let lock = SharedRwLock::new();
let media = Arc::new(lock.wrap(MediaList::empty()));
Stylesheet::from_str(
css,
url.into(),
Origin::UserAgent,
media,
lock,
None,
Some(&error_reporter),
QuirksMode::NoQuirks,
AllowImportRules::Yes,
);
error_reporter.assert_messages_contain(&[(
4,
31,
"Unsupported property declaration: '-moz-background-color: red;'",
)]);
}
#[test]
fn test_source_map_url() {
let tests = vec![
("", None),
(
"/*# sourceMappingURL=something */",
Some("something".to_string()),
),
];
for test in tests {
let url = Url::parse("about::test").unwrap();
let lock = SharedRwLock::new();
let media = Arc::new(lock.wrap(MediaList::empty()));
let stylesheet = Stylesheet::from_str(
test.0,
url.into(),
Origin::UserAgent,
media,
lock,
None,
None,
QuirksMode::NoQuirks,
AllowImportRules::Yes,
);
let url_opt = stylesheet.contents.source_map_url.read();
assert_eq!(*url_opt, test.1);
}
}
#[test]
fn test_source_url() {
let tests = vec![
("", None),
("/*# sourceURL=something */", Some("something".to_string())),
];
for test in tests {
let url = Url::parse("about::test").unwrap();
let lock = SharedRwLock::new();
let media = Arc::new(lock.wrap(MediaList::empty()));
let stylesheet = Stylesheet::from_str(
test.0,
url.into(),
Origin::UserAgent,
media,
lock,
None,
None,
QuirksMode::NoQuirks,
AllowImportRules::Yes,
);
let url_opt = stylesheet.contents.source_url.read();
assert_eq!(*url_opt, test.1);
}
}
|