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
use context::QuirksMode;
use cssparser::{Parser, SourceLocation, UnicodeRange};
use error_reporting::{ParseErrorReporter, ContextualParseError};
use style_traits::{OneOrMoreSeparated, ParseError, ParsingMode, Separator};
use stylesheets::{CssRuleType, Origin, UrlExtraData, Namespaces};
#[cfg(feature = "gecko")]
#[inline]
pub fn assert_parsing_mode_match() {
use gecko_bindings::structs;
macro_rules! check_parsing_modes {
( $( $a:ident => $b:path ),*, ) => {
if cfg!(debug_assertions) {
let mut modes = ParsingMode::all();
$(
assert_eq!(structs::$a as usize, $b.bits() as usize, stringify!($b));
modes.remove($b);
)*
assert_eq!(modes, ParsingMode::empty(), "all ParsingMode bits should have an assertion");
}
}
}
check_parsing_modes! {
ParsingMode_Default => ParsingMode::DEFAULT,
ParsingMode_AllowUnitlessLength => ParsingMode::ALLOW_UNITLESS_LENGTH,
ParsingMode_AllowAllNumericValues => ParsingMode::ALLOW_ALL_NUMERIC_VALUES,
}
}
pub struct ParserErrorContext<'a, R: 'a> {
pub error_reporter: &'a R,
}
pub struct ParserContext<'a> {
pub stylesheet_origin: Origin,
pub url_data: &'a UrlExtraData,
pub rule_type: Option<CssRuleType>,
pub parsing_mode: ParsingMode,
pub quirks_mode: QuirksMode,
pub namespaces: Option<&'a Namespaces>,
}
impl<'a> ParserContext<'a> {
#[inline]
pub fn new(
stylesheet_origin: Origin,
url_data: &'a UrlExtraData,
rule_type: Option<CssRuleType>,
parsing_mode: ParsingMode,
quirks_mode: QuirksMode,
) -> Self {
ParserContext {
stylesheet_origin,
url_data,
rule_type,
parsing_mode,
quirks_mode,
namespaces: None,
}
}
#[inline]
pub fn new_for_cssom(
url_data: &'a UrlExtraData,
rule_type: Option<CssRuleType>,
parsing_mode: ParsingMode,
quirks_mode: QuirksMode,
) -> Self {
Self::new(
Origin::Author,
url_data,
rule_type,
parsing_mode,
quirks_mode,
)
}
#[inline]
pub fn new_with_rule_type(
context: &'a ParserContext,
rule_type: CssRuleType,
namespaces: &'a Namespaces,
) -> ParserContext<'a> {
ParserContext {
stylesheet_origin: context.stylesheet_origin,
url_data: context.url_data,
rule_type: Some(rule_type),
parsing_mode: context.parsing_mode,
quirks_mode: context.quirks_mode,
namespaces: Some(namespaces),
}
}
pub fn rule_type(&self) -> CssRuleType {
self.rule_type.expect("Rule type expected, but none was found.")
}
pub fn log_css_error<R>(
&self,
context: &ParserErrorContext<R>,
location: SourceLocation,
error: ContextualParseError,
)
where
R: ParseErrorReporter,
{
let location = SourceLocation {
line: location.line,
column: location.column,
};
context.error_reporter.report_error(self.url_data, location, error)
}
pub fn chrome_rules_enabled(&self) -> bool {
self.url_data.is_chrome() || self.stylesheet_origin == Origin::User
}
}
pub trait Parse : Sized {
fn parse<'i, 't>(
context: &ParserContext,
input: &mut Parser<'i, 't>,
) -> Result<Self, ParseError<'i>>;
}
impl<T> Parse for Vec<T>
where
T: Parse + OneOrMoreSeparated,
<T as OneOrMoreSeparated>::S: Separator,
{
fn parse<'i, 't>(context: &ParserContext, input: &mut Parser<'i, 't>)
-> Result<Self, ParseError<'i>> {
<T as OneOrMoreSeparated>::S::parse(input, |i| T::parse(context, i))
}
}
impl Parse for UnicodeRange {
fn parse<'i, 't>(_context: &ParserContext, input: &mut Parser<'i, 't>)
-> Result<Self, ParseError<'i>> {
UnicodeRange::parse(input).map_err(|e| e.into())
}
}