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
238
use crate::parser::{Parse, ParserContext};
use crate::stylesheets::CorsMode;
use crate::values::computed::{Context, ToComputedValue};
use cssparser::Parser;
use servo_arc::Arc;
use servo_url::ServoUrl;
use std::fmt::{self, Write};
use style_traits::{CssWriter, ParseError, ToCss};
#[derive(Clone, Debug, Deserialize, MallocSizeOf, Serialize, SpecifiedValueInfo, ToShmem)]
pub struct CssUrl {
#[ignore_malloc_size_of = "Arc"]
original: Option<Arc<String>>,
resolved: Option<ServoUrl>,
}
impl CssUrl {
pub fn parse_from_string(url: String, context: &ParserContext, _: CorsMode) -> Self {
let serialization = Arc::new(url);
let resolved = context.url_data.join(&serialization).ok();
CssUrl {
original: Some(serialization),
resolved: resolved,
}
}
pub fn is_invalid(&self) -> bool {
self.resolved.is_none()
}
pub fn is_fragment(&self) -> bool {
error!("Can't determine whether the url is a fragment.");
false
}
pub fn url(&self) -> Option<&ServoUrl> {
self.resolved.as_ref()
}
pub fn as_str(&self) -> &str {
match self.resolved {
Some(ref url) => url.as_str(),
None => "",
}
}
pub fn for_cascade(url: ServoUrl) -> Self {
CssUrl {
original: None,
resolved: Some(url),
}
}
pub fn new_for_testing(url: &str) -> Self {
CssUrl {
original: Some(Arc::new(url.into())),
resolved: ServoUrl::parse(url).ok(),
}
}
pub fn parse_with_cors_mode<'i, 't>(
context: &ParserContext,
input: &mut Parser<'i, 't>,
cors_mode: CorsMode,
) -> Result<Self, ParseError<'i>> {
let url = input.expect_url()?;
Ok(Self::parse_from_string(
url.as_ref().to_owned(),
context,
cors_mode,
))
}
}
impl Parse for CssUrl {
fn parse<'i, 't>(
context: &ParserContext,
input: &mut Parser<'i, 't>,
) -> Result<Self, ParseError<'i>> {
Self::parse_with_cors_mode(context, input, CorsMode::None)
}
}
impl PartialEq for CssUrl {
fn eq(&self, other: &Self) -> bool {
self.resolved == other.resolved
}
}
impl Eq for CssUrl {}
impl ToCss for CssUrl {
fn to_css<W>(&self, dest: &mut CssWriter<W>) -> fmt::Result
where
W: Write,
{
let string = match self.original {
Some(ref original) => &**original,
None => match self.resolved {
Some(ref url) => url.as_str(),
None => "about:invalid",
},
};
dest.write_str("url(")?;
string.to_css(dest)?;
dest.write_str(")")
}
}
pub type SpecifiedUrl = CssUrl;
impl ToComputedValue for SpecifiedUrl {
type ComputedValue = ComputedUrl;
fn to_computed_value(&self, _: &Context) -> Self::ComputedValue {
match self.resolved {
Some(ref url) => ComputedUrl::Valid(url.clone()),
None => match self.original {
Some(ref url) => ComputedUrl::Invalid(url.clone()),
None => {
unreachable!("Found specified url with neither resolved or original URI!");
},
},
}
}
fn from_computed_value(computed: &ComputedUrl) -> Self {
match *computed {
ComputedUrl::Valid(ref url) => SpecifiedUrl {
original: None,
resolved: Some(url.clone()),
},
ComputedUrl::Invalid(ref url) => SpecifiedUrl {
original: Some(url.clone()),
resolved: None,
},
}
}
}
pub type SpecifiedImageUrl = CssUrl;
#[derive(Clone, Debug, Deserialize, MallocSizeOf, PartialEq, Serialize)]
pub enum ComputedUrl {
Invalid(#[ignore_malloc_size_of = "Arc"] Arc<String>),
Valid(ServoUrl),
}
impl ComputedUrl {
pub fn url(&self) -> Option<&ServoUrl> {
match *self {
ComputedUrl::Valid(ref url) => Some(url),
_ => None,
}
}
}
impl ToCss for ComputedUrl {
fn to_css<W>(&self, dest: &mut CssWriter<W>) -> fmt::Result
where
W: Write,
{
let string = match *self {
ComputedUrl::Valid(ref url) => url.as_str(),
ComputedUrl::Invalid(ref invalid_string) => invalid_string,
};
dest.write_str("url(")?;
string.to_css(dest)?;
dest.write_str(")")
}
}
pub type ComputedImageUrl = ComputedUrl;