forked from servo/rust-css
-
Notifications
You must be signed in to change notification settings - Fork 0
/
complete.rs
256 lines (202 loc) · 7.65 KB
/
complete.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
/* 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 std::cast;
use color::Color;
use select::SelectResults;
use computed::ComputedStyle;
use n::h::CssHintLength;
use n::u::float_to_css_fixed;
use values::*;
use n;
pub struct CompleteSelectResults {
inner: SelectResults
}
impl<'self> CompleteSelectResults {
pub fn new_root(root: SelectResults) -> CompleteSelectResults {
CompleteSelectResults {
inner: root
}
}
pub fn new_from_parent(parent: &CompleteSelectResults,
child: SelectResults) -> CompleteSelectResults {
// New lifetime
{
let parent_computed = parent.computed_style();
let child_computed = child.computed_style();
//let net_parent_computed = &parent_computed.inner.inner;
let net_child_computed = &/*mut*/ child_computed.inner;
// FIXME: Need to get real font sizes
let cb: n::c::ComputeFontSizeCb =
|parent: &Option<n::h::CssHint>, child: &n::h::CssHint| -> n::h::CssHint {
match *child {
// Handle relative units
CssHintLength(n::t::CssUnitEm(child_em)) => {
match *parent {
Some(CssHintLength(parent_unit)) => {
// CSS3 Values 5.1.1: Multiply parent unit by child unit.
let mut new_value =
n::u::css_fixed_to_float(parent_unit.to_css_fixed());
new_value *= n::u::css_fixed_to_float(child_em);
let unit = parent_unit.modify(n::u::float_to_css_fixed(
new_value));
CssHintLength(unit)
}
_ => n::h::CssHintLength(n::t::CssUnitPx(float_to_css_fixed(16.0))),
}
}
CssHintLength(n::t::CssUnitPct(child_pct)) => {
match *parent {
Some(CssHintLength(parent_unit)) => {
// CSS3 Values 5.1.1: Multiply parent unit by child unit.
let mut new_value =
n::u::css_fixed_to_float(parent_unit.to_css_fixed());
new_value *= n::u::css_fixed_to_float(child_pct) / 100.0;
let unit = parent_unit.modify(n::u::float_to_css_fixed(
new_value));
CssHintLength(unit)
}
_ => n::h::CssHintLength(n::t::CssUnitPx(float_to_css_fixed(16.0))),
}
}
// Pass through absolute units
CssHintLength(unit) => CssHintLength(unit),
_ => {
n::h::CssHintLength(n::t::CssUnitPx(float_to_css_fixed(16.0)))
}
}
};
// XXX: Need an aliasable &mut here
let net_result_computed: &mut n::c::CssComputedStyle = unsafe { cast::transmute(net_child_computed) };
let net_child_computed: &mut n::c::CssComputedStyle = unsafe { cast::transmute(&child_computed.inner) };
let net_parent_computed = &parent_computed.inner.inner;
n::c::compose(net_parent_computed, net_child_computed, cb, net_result_computed);
}
CompleteSelectResults {
inner: child
}
}
pub fn computed_style(&'self self) -> CompleteStyle<'self> {
CompleteStyle {
inner: self.inner.computed_style()
}
}
}
pub struct CompleteStyle<'self> {
inner: ComputedStyle<'self>
}
impl<'self> CompleteStyle<'self> {
// CSS 2.1, Section 8 - Box model
pub fn margin_top(&self) -> CSSMargin {
strip(self.inner.margin_top())
}
pub fn margin_right(&self) -> CSSMargin {
strip(self.inner.margin_right())
}
pub fn margin_bottom(&self) -> CSSMargin {
strip(self.inner.margin_bottom())
}
pub fn margin_left(&self) -> CSSMargin {
strip(self.inner.margin_left())
}
pub fn padding_top(&self) -> CSSPadding {
strip(self.inner.padding_top())
}
pub fn padding_right(&self) -> CSSPadding {
strip(self.inner.padding_right())
}
pub fn padding_bottom(&self) -> CSSPadding {
strip(self.inner.padding_bottom())
}
pub fn padding_left(&self) -> CSSPadding {
strip(self.inner.padding_left())
}
pub fn border_top_width(&self) -> CSSBorderWidth {
strip(self.inner.border_top_width())
}
pub fn border_right_width(&self) -> CSSBorderWidth {
strip(self.inner.border_right_width())
}
pub fn border_bottom_width(&self) -> CSSBorderWidth {
strip(self.inner.border_bottom_width())
}
pub fn border_left_width(&self) -> CSSBorderWidth {
strip(self.inner.border_left_width())
}
pub fn border_top_color(&self) -> Color {
strip(self.inner.border_top_color())
}
pub fn border_right_color(&self) -> Color {
strip(self.inner.border_right_color())
}
pub fn border_bottom_color(&self) -> Color {
strip(self.inner.border_bottom_color())
}
pub fn border_left_color(&self) -> Color {
strip(self.inner.border_left_color())
}
// CSS 2.1, Section 9 - Visual formatting model
pub fn display(&self, root: bool) -> CSSDisplay {
strip(self.inner.display(root))
}
pub fn position(&self) -> CSSPosition {
strip(self.inner.position())
}
pub fn float(&self) -> CSSFloat {
strip(self.inner.float())
}
pub fn clear(&self) -> CSSClear {
strip(self.inner.clear())
}
// CSS 2.1, Section 10 - Visual formatting model details
pub fn width(&self) -> CSSWidth {
strip(self.inner.width())
}
pub fn height(&self) -> CSSHeight {
strip(self.inner.height())
}
pub fn line_height(&self) -> CSSLineHeight {
strip(self.inner.line_height())
}
pub fn vertical_align(&self) -> CSSVerticalAlign {
strip(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) -> Color {
strip(self.inner.background_color())
}
pub fn color(&self) -> Color {
strip(self.inner.color())
}
// CSS 2.1, Section 15 - Fonts
pub fn font_family(&self) -> ~[CSSFontFamily] {
strip(self.inner.font_family())
}
pub fn font_style(&self) -> CSSFontStyle {
strip(self.inner.font_style())
}
pub fn font_weight(&self) -> CSSFontWeight {
strip(self.inner.font_weight())
}
pub fn font_size(&self) -> CSSFontSize {
strip(self.inner.font_size())
}
pub fn text_decoration(&self) -> CSSTextDecoration{
strip(self.inner.text_decoration())
}
// CSS 2.1, Section 16 - Text
pub fn text_align(&self) -> CSSTextAlign {
strip(self.inner.text_align())
}
// CSS 2.1, Section 17 - Tables
// CSS 2.1, Section 18 - User interface
}
fn strip<T>(value: CSSValue<T>) -> T {
match value {
Inherit => fail!(~"unexpected 'inherit' value in complete style"),
Specified(v) => v
}
}