forked from servo/rust-css
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcomputed.rs
445 lines (374 loc) · 17.1 KB
/
computed.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
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
/* 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/. */
use color::{Color, rgba};
use units::{Length, Px, Em};
use netsurfcss::util::css_fixed_to_float;
use std::either::{Either, Left, Right};
use n;
use values::*;
pub struct ComputedStyle<'self> {
inner: n::c::CssComputedStyle<'self>
}
impl<'self> ComputedStyle<'self> {
// CSS 2.1, Section 8 - Box model
pub fn margin_top(&self) -> CSSValue<CSSMargin> {
convert_net_margin(self.inner.margin_top())
}
pub fn margin_right(&self) -> CSSValue<CSSMargin> {
convert_net_margin(self.inner.margin_right())
}
pub fn margin_bottom(&self) -> CSSValue<CSSMargin> {
convert_net_margin(self.inner.margin_bottom())
}
pub fn margin_left(&self) -> CSSValue<CSSMargin> {
convert_net_margin(self.inner.margin_left())
}
pub fn padding_top(&self) -> CSSValue<CSSPadding> {
convert_net_padding(self.inner.padding_top())
}
pub fn padding_right(&self) -> CSSValue<CSSPadding> {
convert_net_padding(self.inner.padding_right())
}
pub fn padding_bottom(&self) -> CSSValue<CSSPadding> {
convert_net_padding(self.inner.padding_bottom())
}
pub fn padding_left(&self) -> CSSValue<CSSPadding> {
convert_net_padding(self.inner.padding_left())
}
pub fn border_top_width(&self) -> CSSValue<CSSBorderWidth> {
convert_net_border_width(self.inner.border_top_width())
}
pub fn border_right_width(&self) -> CSSValue<CSSBorderWidth> {
convert_net_border_width(self.inner.border_right_width())
}
pub fn border_bottom_width(&self) -> CSSValue<CSSBorderWidth> {
convert_net_border_width(self.inner.border_bottom_width())
}
pub fn border_left_width(&self) -> CSSValue<CSSBorderWidth> {
convert_net_border_width(self.inner.border_left_width())
}
pub fn border_top_color(&self) -> CSSValue<Color> {
convert_net_color_value(self.inner.border_top_color())
}
pub fn border_right_color(&self) -> CSSValue<Color> {
convert_net_color_value(self.inner.border_right_color())
}
pub fn border_bottom_color(&self) -> CSSValue<Color> {
convert_net_color_value(self.inner.border_bottom_color())
}
pub fn border_left_color(&self) -> CSSValue<Color> {
convert_net_color_value(self.inner.border_left_color())
}
// CSS 2.1, Section 9 - Visual formatting model
pub fn display(&self, root: bool) -> CSSValue<CSSDisplay> {
convert_net_display_value(self.inner.display(root))
}
pub fn position(&self) -> CSSValue<CSSPosition> {
convert_net_position_value(self.inner.position())
}
pub fn float(&self) -> CSSValue<CSSFloat> {
convert_net_float_value(self.inner.float())
}
pub fn clear(&self) -> CSSValue<CSSClear> {
convert_net_clear_value(self.inner.clear())
}
// CSS 2.1, Section 10 - Visual formatting model details
pub fn width(&self) -> CSSValue<CSSWidth> {
convert_net_width_value(self.inner.width())
}
pub fn height(&self) -> CSSValue<CSSHeight> {
convert_net_height_value(self.inner.height())
}
pub fn line_height(&self) -> CSSValue<CSSLineHeight> {
convert_net_line_height_value(self.inner.line_height())
}
pub fn vertical_align(&self) -> CSSValue<CSSVerticalAlign> {
convert_net_vertical_align_value(self.inner.vertical_align())
}
// CSS 2.1, Section 11 - Visual effects
// CSS 2.1, Section 12 - Generated content, automatic numbering, and lists
// CSS 2.1, Section 13 - Paged media
// CSS 2.1, Section 14 - Colors and Backgrounds
pub fn background_color(&self) -> CSSValue<Color> {
convert_net_color_value(self.inner.background_color())
}
pub fn color(&self) -> CSSValue<Color> {
convert_net_color_value(self.inner.color())
}
// CSS 2.1, Section 15 - Fonts
pub fn font_family(&self) -> CSSValue<~[CSSFontFamily]> {
convert_net_font_family_value(self.inner.font_family())
}
pub fn font_style(&self) -> CSSValue<CSSFontStyle> {
convert_net_font_style_value(self.inner.font_style())
}
pub fn font_weight(&self) -> CSSValue<CSSFontWeight> {
convert_net_font_weight_value(self.inner.font_weight())
}
pub fn font_size(&self) -> CSSValue<CSSFontSize> {
convert_net_font_size_value(self.inner.font_size())
}
// CSS 2.1, Section 16 - Text
pub fn text_align(&self) -> CSSValue<CSSTextAlign> {
convert_net_text_align_value(self.inner.text_align())
}
pub fn text_decoration(&self) -> CSSValue<CSSTextDecoration> {
convert_net_text_decoration_value(self.inner.text_decoration())
}
// CSS 2.1, Section 17 - Tables
// CSS 2.1, Section 18 - User interface
}
fn convert_net_color(color: n::t::CssColor) -> Color {
rgba(color.r, color.g, color.b, (color.a as float) / 255.0)
}
fn convert_net_color_value(color: n::v::CssColorValue) -> CSSValue<Color> {
match color {
n::v::CssColorInherit => Inherit,
n::v::CssColorColor(v) => Specified(convert_net_color(v))
}
}
fn convert_net_border_width(width: n::v::CssBorderWidthValue) -> CSSValue<CSSBorderWidth> {
match width {
n::v::CssBorderWidthInherit => Inherit,
n::v::CssBorderWidthThin => Specified(CSSBorderWidthThin),
n::v::CssBorderWidthMedium => Specified(CSSBorderWidthMedium),
n::v::CssBorderWidthThick => Specified(CSSBorderWidthThick),
n::v::CssBorderWidthWidth(width) => Specified(CSSBorderWidthLength(convert_net_unit_to_length(width))),
}
}
fn convert_net_margin(margin: n::v::CssMarginValue) -> CSSValue<CSSMargin> {
match margin {
n::v::CssMarginInherit => Inherit,
n::v::CssMarginSet(value) => {
let length = convert_net_unit_to_length_or_percent(value);
match length {
Left(abs) => Specified(CSSMarginLength(abs)),
Right(percent) => Specified(CSSMarginPercentage(percent))
}
}
n::v::CssMarginAuto => Specified(CSSMarginAuto)
}
}
fn convert_net_padding(padding: n::v::CssPaddingValue) -> CSSValue<CSSPadding> {
match padding {
n::v::CssPaddingInherit => Inherit,
n::v::CssPaddingSet(value) => {
let length = convert_net_unit_to_length_or_percent(value);
match length {
Left(abs) => Specified(CSSPaddingLength(abs)),
Right(percent) => Specified(CSSPaddingPercentage(percent))
}
}
}
}
fn convert_net_width_value(value: n::v::CssWidthValue) -> CSSValue<CSSWidth> {
match value {
n::v::CssWidthInherit => Inherit,
n::v::CssWidthSet(value) => {
let length = convert_net_unit_to_length_or_percent(value);
match length {
Left(abs) => Specified(CSSWidthLength(abs)),
Right(percent) => Specified(CSSWidthPercentage(percent))
}
}
n::v::CssWidthAuto => Specified(CSSWidthAuto)
}
}
fn convert_net_height_value(value: n::v::CssHeightValue) -> CSSValue<CSSHeight> {
match value {
n::v::CssHeightInherit => Inherit,
n::v::CssHeightSet(value) => {
let length = convert_net_unit_to_length_or_percent(value);
match length {
Left(abs) => Specified(CSSHeightLength(abs)),
Right(percent) => Specified(CSSHeightPercentage(percent))
}
}
n::v::CssHeightAuto => Specified(CSSHeightAuto)
}
}
fn convert_net_display_value(value: n::v::CssDisplayValue) -> CSSValue<CSSDisplay> {
match value {
n::v::CssDisplayInherit => Inherit,
n::v::CssDisplayInline => Specified(CSSDisplayInline),
n::v::CssDisplayBlock => Specified(CSSDisplayBlock),
n::v::CssDisplayListItem => Specified(CSSDisplayListItem),
n::v::CssDisplayRunIn => unimpl("display: run-in"), // FIXME: Not in CSS 2.1
n::v::CssDisplayInlineBlock => Specified(CSSDisplayInlineBlock),
n::v::CssDisplayTable => Specified(CSSDisplayTable),
n::v::CssDisplayInlineTable => Specified(CSSDisplayInlineTable),
n::v::CssDisplayTableRowGroup => Specified(CSSDisplayTableRowGroup),
n::v::CssDisplayTableHeaderGroup => Specified(CSSDisplayTableHeaderGroup),
n::v::CssDisplayTableFooterGroup => Specified(CSSDisplayTableFooterGroup),
n::v::CssDisplayTableRow => Specified(CSSDisplayTableRow),
n::v::CssDisplayTableColumnGroup => Specified(CSSDisplayTableColumnGroup),
n::v::CssDisplayTableColumn => Specified(CSSDisplayTableColumn),
n::v::CssDisplayTableCell => Specified(CSSDisplayTableCell),
n::v::CssDisplayTableCaption => Specified(CSSDisplayTableCaption),
n::v::CssDisplayNone => Specified(CSSDisplayNone)
}
}
fn convert_net_float_value(value: n::v::CssFloatValue) -> CSSValue<CSSFloat> {
match value {
n::v::CssFloatInherit => Inherit,
n::v::CssFloatLeft => Specified(CSSFloatLeft),
n::v::CssFloatRight => Specified(CSSFloatRight),
n::v::CssFloatNone => Specified(CSSFloatNone)
}
}
fn convert_net_clear_value(value: n::v::CssClearValue) -> CSSValue<CSSClear> {
match value {
n::v::CssClearInherit => Inherit,
n::v::CssClearNone => Specified(CSSClearNone),
n::v::CssClearLeft => Specified(CSSClearLeft),
n::v::CssClearRight => Specified(CSSClearRight),
n::v::CssClearBoth => Specified(CSSClearBoth)
}
}
fn convert_net_position_value(value: n::v::CssPositionValue) -> CSSValue<CSSPosition> {
match value {
n::v::CssPositionInherit => Inherit,
n::v::CssPositionStatic => Specified(CSSPositionStatic),
n::v::CssPositionRelative => Specified(CSSPositionRelative),
n::v::CssPositionAbsolute => Specified(CSSPositionAbsolute),
n::v::CssPositionFixed => Specified(CSSPositionFixed)
}
}
fn convert_net_font_family_value(value: n::v::CssFontFamilyValue) -> CSSValue<~[CSSFontFamily]> {
use units::{Serif, SansSerif, Cursive, Fantasy, Monospace};
match value {
n::v::CssFontFamilyInherit => Inherit,
n::v::CssFontFamilySerif => Specified(~[CSSFontFamilyGenericFamily(Serif)]),
n::v::CssFontFamilySansSerif => Specified(~[CSSFontFamilyGenericFamily(SansSerif)]),
n::v::CssFontFamilyCursive => Specified(~[CSSFontFamilyGenericFamily(Cursive)]),
n::v::CssFontFamilyFantasy => Specified(~[CSSFontFamilyGenericFamily(Fantasy)]),
n::v::CssFontFamilyMonospace => Specified(~[CSSFontFamilyGenericFamily(Monospace)]),
n::v::CssFontFamilyValue(names) => Specified(names.map(|n| CSSFontFamilyFamilyName(n.to_str()) ))
}
}
fn convert_net_font_size_value(value: n::v::CssFontSizeValue) -> CSSValue<CSSFontSize> {
use units::*;
match value {
n::v::CssFontSizeInherit => Inherit,
n::v::CssFontSizeXXSmall => Specified(CSSFontSizeAbsoluteSize(XXSmall)),
n::v::CssFontSizeXSmall => Specified(CSSFontSizeAbsoluteSize(XSmall)),
n::v::CssFontSizeSmall => Specified(CSSFontSizeAbsoluteSize(Small)),
n::v::CssFontSizeMedium => Specified(CSSFontSizeAbsoluteSize(Medium)),
n::v::CssFontSizeLarge => Specified(CSSFontSizeAbsoluteSize(Large)),
n::v::CssFontSizeXLarge => Specified(CSSFontSizeAbsoluteSize(XLarge)),
n::v::CssFontSizeXXLarge => Specified(CSSFontSizeAbsoluteSize(XXLarge)),
n::v::CssFontSizeLarger => Specified(CSSFontSizeRelativeSize(Larger)),
n::v::CssFontSizeSmaller => Specified(CSSFontSizeRelativeSize(Smaller)),
n::v::CssFontSizeDimension(size) => {
match convert_net_unit_to_length_or_percent(size) {
Left(val) => Specified(CSSFontSizeLength(val)),
Right(val) => Specified(CSSFontSizePercentage(val))
}
}
}
}
fn convert_net_font_style_value(value: n::v::CssFontStyleValue) -> CSSValue<CSSFontStyle> {
match value {
n::v::CssFontStyleInherit => Inherit,
n::v::CssFontStyleNormal => Specified(CSSFontStyleNormal),
n::v::CssFontStyleItalic => Specified(CSSFontStyleItalic),
n::v::CssFontStyleOblique => Specified(CSSFontStyleOblique)
}
}
fn convert_net_font_weight_value(value: n::v::CssFontWeightValue) -> CSSValue<CSSFontWeight> {
match value {
n::v::CssFontWeightInherit => Inherit,
n::v::CssFontWeightNormal => Specified(CSSFontWeightNormal),
n::v::CssFontWeightBold => Specified(CSSFontWeightBold),
n::v::CssFontWeightBolder => Specified(CSSFontWeightBolder),
n::v::CssFontWeightLighter => Specified(CSSFontWeightLighter),
n::v::CssFontWeight100 => Specified(CSSFontWeight100),
n::v::CssFontWeight200 => Specified(CSSFontWeight200),
n::v::CssFontWeight300 => Specified(CSSFontWeight300),
n::v::CssFontWeight400 => Specified(CSSFontWeight400),
n::v::CssFontWeight500 => Specified(CSSFontWeight500),
n::v::CssFontWeight600 => Specified(CSSFontWeight600),
n::v::CssFontWeight700 => Specified(CSSFontWeight700),
n::v::CssFontWeight800 => Specified(CSSFontWeight800),
n::v::CssFontWeight900 => Specified(CSSFontWeight900),
}
}
fn convert_net_text_align_value(value: n::v::CssTextAlignValue) -> CSSValue<CSSTextAlign> {
match value {
n::v::CssTextAlignInherit => Inherit,
n::v::CssTextAlignInheritIfNonMagic => unimpl("inherit if non-magic? wtf?"),
n::v::CssTextAlignLeft => Specified(CSSTextAlignLeft),
n::v::CssTextAlignRight => Specified(CSSTextAlignRight),
n::v::CssTextAlignCenter => Specified(CSSTextAlignCenter),
n::v::CssTextAlignJustify => Specified(CSSTextAlignJustify),
n::v::CssTextAlignDefault => Specified(CSSTextAlignLeft),
n::v::CssTextAlignLibcssLeft => unimpl("text-align libcss left"),
n::v::CssTextAlignLibcssCenter => unimpl("text-align libcss center"),
n::v::CssTextAlignLibcssRight => unimpl("text-align libcss right"),
}
}
fn convert_net_text_decoration_value(value: n::v::CssTextDecorationValue) -> CSSValue<CSSTextDecoration> {
match value {
n::v::CssTextDecorationInherit => Inherit,
n::v::CssTextDecorationNone => Specified(CSSTextDecorationNone),
n::v::CssTextDecorationBlink => Specified(CSSTextDecorationBlink),
n::v::CssTextDecorationLineThrough => Specified(CSSTextDecorationLineThrough),
n::v::CssTextDecorationOverline => Specified(CSSTextDecorationOverline),
n::v::CssTextDecorationUnderline => Specified(CSSTextDecorationUnderline),
}
}
fn convert_net_line_height_value(value: n::v::CssLineHeightValue) -> CSSValue<CSSLineHeight> {
match value {
n::v::CssLineHeightInherit => Inherit,
n::v::CssLineHeightNumber(n) => Specified(CSSLineHeightNumber(css_fixed_to_float(n))),
n::v::CssLineHeightDimension(v) => {
match convert_net_unit_to_length_or_percent(v) {
Left(val) => Specified(CSSLineHeightLength(val)),
Right(val) => Specified(CSSLineHeightPercentage(val))
}
},
n::v::CssLineHeightNormal => Specified(CSSLineHeightNormal)
}
}
fn convert_net_vertical_align_value(value: n::v::CssVerticalAlignValue) -> CSSValue<CSSVerticalAlign> {
match value {
n::v::CssVerticalAlignInherit => Inherit,
n::v::CssVerticalAlignBaseline => Specified(CSSVerticalAlignBaseline),
n::v::CssVerticalAlignSub => Specified(CSSVerticalAlignSub),
n::v::CssVerticalAlignSuper => Specified(CSSVerticalAlignSuper),
n::v::CssVerticalAlignTop => Specified(CSSVerticalAlignTop),
n::v::CssVerticalAlignTextTop => Specified(CSSVerticalAlignTextTop),
n::v::CssVerticalAlignMiddle => Specified(CSSVerticalAlignMiddle),
n::v::CssVerticalAlignBottom => Specified(CSSVerticalAlignBottom),
n::v::CssVerticalAlignTextBottom => Specified(CSSVerticalAlignTextBottom),
n::v::CssVerticalAlignDimension(v) => {
match convert_net_unit_to_length_or_percent(v) {
Left(val) => Specified(CSSVerticalAlignLength(val)),
Right(val) => Specified(CSSVerticalAlignPercentage(val))
}
}
}
}
fn convert_net_unit_to_length(unit: n::t::CssUnit) -> Length {
match convert_net_unit_to_length_or_percent(unit) {
Left(v) => v,
Right(*) => fail!(~"unexpected percentage unit"),
}
}
fn convert_net_unit_to_length_or_percent(unit: n::t::CssUnit) -> Either<Length, float> {
match unit {
n::t::CssUnitPx(l) => Left(Px(css_fixed_to_float(l))),
n::t::CssUnitEm(l) => Left(Em(css_fixed_to_float(l))),
n::t::CssUnitPt(l) => Left(Px(css_fixed_to_float(l) / 72f * 96f)),
n::t::CssUnitCm(l) => Left(Px(css_fixed_to_float(l) / 2.54f * 96f)),
n::t::CssUnitMm(l) => Left(Px(css_fixed_to_float(l) / 25.4f * 96f)),
n::t::CssUnitIn(l) => Left(Px(css_fixed_to_float(l) / 1f * 96f)),
n::t::CssUnitPc(l) => Left(Px(css_fixed_to_float(l) / 6f * 96f)),
n::t::CssUnitPct(p) => Right(css_fixed_to_float(p)),
_ => unimpl("unit")
}
}
fn unimpl(what: &str) -> ! {
fail!(fmt!("css unimplemented %?", what))
}