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
233
234
235
236
237
#![deny(missing_docs)]
use crate::element_state::ElementState;
use crate::stylesheets::{Namespaces, Origin, UrlExtraData};
use crate::values::serialize_atom_identifier;
use crate::Atom;
use cssparser::{Parser as CssParser, ParserInput};
use selectors::parser::SelectorList;
use std::fmt::{self, Debug, Write};
use style_traits::{CssWriter, ParseError, ToCss};
pub type AttrValue = <SelectorImpl as ::selectors::SelectorImpl>::AttrValue;
#[cfg(feature = "servo")]
pub use crate::servo::selector_parser::*;
#[cfg(feature = "gecko")]
pub use crate::gecko::selector_parser::*;
#[cfg(feature = "servo")]
pub use crate::servo::selector_parser::ServoElementSnapshot as Snapshot;
#[cfg(feature = "gecko")]
pub use crate::gecko::snapshot::GeckoElementSnapshot as Snapshot;
#[cfg(feature = "servo")]
pub use crate::servo::restyle_damage::ServoRestyleDamage as RestyleDamage;
#[cfg(feature = "gecko")]
pub use crate::gecko::restyle_damage::GeckoRestyleDamage as RestyleDamage;
#[cfg_attr(feature = "servo", derive(MallocSizeOf))]
pub struct SelectorParser<'a> {
pub stylesheet_origin: Origin,
pub namespaces: &'a Namespaces,
pub url_data: &'a UrlExtraData,
}
impl<'a> SelectorParser<'a> {
pub fn parse_author_origin_no_namespace<'i>(
input: &'i str,
url_data: &UrlExtraData,
) -> Result<SelectorList<SelectorImpl>, ParseError<'i>> {
let namespaces = Namespaces::default();
let parser = SelectorParser {
stylesheet_origin: Origin::Author,
namespaces: &namespaces,
url_data,
};
let mut input = ParserInput::new(input);
SelectorList::parse(&parser, &mut CssParser::new(&mut input))
}
pub fn in_user_agent_stylesheet(&self) -> bool {
matches!(self.stylesheet_origin, Origin::UserAgent)
}
pub fn chrome_rules_enabled(&self) -> bool {
self.url_data.chrome_rules_enabled() || self.stylesheet_origin == Origin::User
}
}
#[derive(Clone, Debug, Eq, PartialEq)]
pub enum PseudoElementCascadeType {
Eager,
Lazy,
Precomputed,
}
#[derive(Clone, MallocSizeOf)]
pub struct PerPseudoElementMap<T> {
entries: [Option<T>; PSEUDO_COUNT],
}
impl<T> Default for PerPseudoElementMap<T> {
fn default() -> Self {
Self {
entries: PseudoElement::pseudo_none_array(),
}
}
}
impl<T> Debug for PerPseudoElementMap<T>
where
T: Debug,
{
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
f.write_str("[")?;
let mut first = true;
for entry in self.entries.iter() {
if !first {
f.write_str(", ")?;
}
first = false;
entry.fmt(f)?;
}
f.write_str("]")
}
}
impl<T> PerPseudoElementMap<T> {
pub fn get(&self, pseudo: &PseudoElement) -> Option<&T> {
self.entries[pseudo.index()].as_ref()
}
pub fn clear(&mut self) {
*self = Self::default();
}
pub fn set(&mut self, pseudo: &PseudoElement, value: T) {
self.entries[pseudo.index()] = Some(value);
}
pub fn get_or_insert_with<F>(&mut self, pseudo: &PseudoElement, f: F) -> &mut T
where
F: FnOnce() -> T,
{
let index = pseudo.index();
if self.entries[index].is_none() {
self.entries[index] = Some(f());
}
self.entries[index].as_mut().unwrap()
}
pub fn iter(&self) -> std::slice::Iter<Option<T>> {
self.entries.iter()
}
pub fn iter_mut(&mut self) -> std::slice::IterMut<Option<T>> {
self.entries.iter_mut()
}
}
#[derive(Clone, Debug, Eq, MallocSizeOf, PartialEq, ToShmem)]
pub struct Direction(pub Atom);
#[derive(Clone, Debug, Eq, PartialEq)]
pub enum HorizontalDirection {
Ltr,
Rtl,
}
impl Direction {
pub fn parse<'i, 't>(parser: &mut CssParser<'i, 't>) -> Result<Self, ParseError<'i>> {
let ident = parser.expect_ident()?;
Ok(Direction(match_ignore_ascii_case! { &ident,
"rtl" => atom!("rtl"),
"ltr" => atom!("ltr"),
_ => Atom::from(ident.as_ref()),
}))
}
pub fn as_horizontal_direction(&self) -> Option<HorizontalDirection> {
if self.0 == atom!("ltr") {
Some(HorizontalDirection::Ltr)
} else if self.0 == atom!("rtl") {
Some(HorizontalDirection::Rtl)
} else {
None
}
}
pub fn element_state(&self) -> ElementState {
match self.as_horizontal_direction() {
Some(HorizontalDirection::Ltr) => ElementState::IN_LTR_STATE,
Some(HorizontalDirection::Rtl) => ElementState::IN_RTL_STATE,
None => ElementState::empty(),
}
}
}
impl ToCss for Direction {
fn to_css<W>(&self, dest: &mut CssWriter<W>) -> fmt::Result
where
W: Write,
{
serialize_atom_identifier(&self.0, dest)
}
}