/
mask.mako.rs
313 lines (275 loc) · 12.8 KB
/
mask.mako.rs
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
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
/* 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 http://mozilla.org/MPL/2.0/. */
<%namespace name="helpers" file="/helpers.mako.rs" />
<%helpers:shorthand name="mask" products="gecko" extra_prefixes="webkit"
sub_properties="mask-mode mask-repeat mask-clip mask-origin mask-composite mask-position-x
mask-position-y mask-size mask-image"
spec="https://drafts.fxtf.org/css-masking/#propdef-mask">
use properties::longhands::{mask_mode, mask_repeat, mask_clip, mask_origin, mask_composite, mask_position_x,
mask_position_y};
use properties::longhands::{mask_size, mask_image};
use values::specified::position::Position;
use parser::Parse;
impl From<mask_origin::single_value::SpecifiedValue> for mask_clip::single_value::SpecifiedValue {
fn from(origin: mask_origin::single_value::SpecifiedValue) -> mask_clip::single_value::SpecifiedValue {
match origin {
mask_origin::single_value::SpecifiedValue::content_box =>
mask_clip::single_value::SpecifiedValue::content_box,
mask_origin::single_value::SpecifiedValue::padding_box =>
mask_clip::single_value::SpecifiedValue::padding_box,
mask_origin::single_value::SpecifiedValue::border_box =>
mask_clip::single_value::SpecifiedValue::border_box,
% if product == "gecko":
mask_origin::single_value::SpecifiedValue::fill_box =>
mask_clip::single_value::SpecifiedValue::fill_box,
mask_origin::single_value::SpecifiedValue::stroke_box =>
mask_clip::single_value::SpecifiedValue::stroke_box,
mask_origin::single_value::SpecifiedValue::view_box =>
mask_clip::single_value::SpecifiedValue::view_box,
% endif
}
}
}
pub fn parse_value(context: &ParserContext, input: &mut Parser) -> Result<Longhands, ()> {
% for name in "image mode position_x position_y size repeat origin clip composite".split():
let mut mask_${name} = mask_${name}::SpecifiedValue(Vec::new());
% endfor
try!(input.parse_comma_separated(|input| {
% for name in "image mode position_x position_y size repeat origin clip composite".split():
let mut ${name} = None;
% endfor
loop {
if image.is_none() {
if let Ok(value) = input.try(|input| mask_image::single_value
::parse(context, input)) {
image = Some(value);
continue
}
}
if position_x.is_none() && position_y.is_none() {
if let Ok(value) = input.try(|input| Position::parse(context, input)) {
position_x = Some(value.horizontal);
position_y = Some(value.vertical);
// Parse mask size, if applicable.
size = input.try(|input| {
try!(input.expect_delim('/'));
mask_size::single_value::parse(context, input)
}).ok();
continue
}
}
% for name in "repeat origin clip composite mode".split():
if ${name}.is_none() {
if let Ok(value) = input.try(|input| mask_${name}::single_value
::parse(context, input)) {
${name} = Some(value);
continue
}
}
% endfor
break
}
if clip.is_none() {
if let Some(origin) = origin {
clip = Some(mask_clip::single_value::SpecifiedValue::from(origin));
}
}
let mut any = false;
% for name in "image mode position_x position_y size repeat origin clip composite".split():
any = any || ${name}.is_some();
% endfor
if any {
if position_x.is_some() || position_y.is_some() {
% for name in "position_x position_y".split():
if let Some(bg_${name}) = ${name} {
mask_${name}.0.push(bg_${name});
}
% endfor
} else {
% for name in "position_x position_y".split():
mask_${name}.0.push(mask_${name}::single_value
::get_initial_position_value());
% endfor
}
% for name in "image mode size repeat origin clip composite".split():
if let Some(m_${name}) = ${name} {
mask_${name}.0.push(m_${name});
} else {
mask_${name}.0.push(mask_${name}::single_value
::get_initial_specified_value());
}
% endfor
Ok(())
} else {
Err(())
}
}));
Ok(Longhands {
% for name in "image mode position_x position_y size repeat origin clip composite".split():
mask_${name}: Some(mask_${name}),
% endfor
})
}
impl<'a> LonghandsToSerialize<'a> {
fn to_css_declared<W>(&self, dest: &mut W) -> fmt::Result where W: fmt::Write {
// mako doesn't like ampersands following `<`
fn extract_value<T>(x: &DeclaredValue<T>) -> Option< &T> {
match *x {
DeclaredValue::Value(ref val) => Some(val),
_ => None,
}
}
use std::cmp;
let mut len = 0;
% for name in "image mode position_x position_y size repeat origin clip composite".split():
len = cmp::max(len, extract_value(self.mask_${name}).map(|i| i.0.len())
.unwrap_or(0));
% endfor
// There should be at least one declared value
if len == 0 {
return dest.write_str("")
}
for i in 0..len {
% for name in "image mode position_x position_y size repeat origin clip composite".split():
let ${name} = if let DeclaredValue::Value(ref arr) = *self.mask_${name} {
arr.0.get(i % arr.0.len())
} else {
None
};
% endfor
if let Some(image) = image {
try!(image.to_css(dest));
} else {
try!(write!(dest, "none"));
}
try!(write!(dest, " "));
if let Some(mode) = mode {
try!(mode.to_css(dest));
} else {
try!(write!(dest, "match-source"));
}
try!(write!(dest, " "));
try!(position_x.unwrap_or(&mask_position_x::single_value
::get_initial_position_value())
.to_css(dest));
try!(write!(dest, " "));
try!(position_y.unwrap_or(&mask_position_y::single_value
::get_initial_position_value())
.to_css(dest));
if let Some(size) = size {
try!(write!(dest, " / "));
try!(size.to_css(dest));
}
try!(write!(dest, " "));
if let Some(repeat) = repeat {
try!(repeat.to_css(dest));
} else {
try!(write!(dest, "repeat"));
}
match (origin, clip) {
(Some(origin), Some(clip)) => {
use properties::longhands::mask_origin::single_value::computed_value::T as Origin;
use properties::longhands::mask_clip::single_value::computed_value::T as Clip;
try!(write!(dest, " "));
match (origin, clip) {
(&Origin::padding_box, &Clip::padding_box) => {
try!(origin.to_css(dest));
},
(&Origin::border_box, &Clip::border_box) => {
try!(origin.to_css(dest));
},
(&Origin::content_box, &Clip::content_box) => {
try!(origin.to_css(dest));
},
_ => {
try!(origin.to_css(dest));
try!(write!(dest, " "));
try!(clip.to_css(dest));
}
}
},
_ => {}
};
try!(write!(dest, " "));
if let Some(composite) = composite {
try!(composite.to_css(dest));
} else {
try!(write!(dest, "add"));
}
}
Ok(())
}
}
</%helpers:shorthand>
<%helpers:shorthand name="mask-position" products="gecko" extra_prefixes="webkit"
sub_properties="mask-position-x mask-position-y"
spec="https://drafts.csswg.org/css-masks-4/#the-mask-position">
use properties::longhands::{mask_position_x,mask_position_y};
use values::specified::position::Position;
use parser::Parse;
pub fn parse_value(context: &ParserContext, input: &mut Parser) -> Result<Longhands, ()> {
let mut position_x = mask_position_x::SpecifiedValue(Vec::new());
let mut position_y = mask_position_y::SpecifiedValue(Vec::new());
let mut any = false;
try!(input.parse_comma_separated(|input| {
loop {
if let Ok(value) = input.try(|input| Position::parse(context, input)) {
position_x.0.push(value.horizontal);
position_y.0.push(value.vertical);
any = true;
continue
}
break
}
Ok(())
}));
if any == false {
return Err(());
}
Ok(Longhands {
mask_position_x: Some(position_x),
mask_position_y: Some(position_y),
})
}
impl<'a> LonghandsToSerialize<'a> {
fn to_css_declared<W>(&self, dest: &mut W) -> fmt::Result where W: fmt::Write {
// mako doesn't like ampersands following `<`
fn extract_value<T>(x: &DeclaredValue<T>) -> Option< &T> {
match *x {
DeclaredValue::Value(ref val) => Some(val),
_ => None,
}
}
use std::cmp;
let mut len = 0;
% for name in "x y".split():
len = cmp::max(len, extract_value(self.mask_position_${name})
.map(|i| i.0.len())
.unwrap_or(0));
% endfor
// There should be at least one declared value
if len == 0 {
return dest.write_str("")
}
for i in 0..len {
% for name in "x y".split():
let position_${name} = if let DeclaredValue::Value(ref arr) =
*self.mask_position_${name} {
arr.0.get(i % arr.0.len())
} else {
None
};
% endfor
try!(position_x.unwrap_or(&mask_position_x::single_value
::get_initial_position_value())
.to_css(dest));
try!(write!(dest, " "));
try!(position_y.unwrap_or(&mask_position_y::single_value
::get_initial_position_value())
.to_css(dest));
}
Ok(())
}
}
</%helpers:shorthand>