/
block.rs
524 lines (451 loc) · 21.9 KB
/
block.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
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
/* 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/. */
//! CSS block layout.
use layout::box::{RenderBox, RenderBoxUtils};
use layout::context::LayoutContext;
use layout::display_list_builder::{DisplayListBuilder, ExtraDisplayListData};
use layout::flow::{BlockFlowClass, FlowClass, FlowContext, FlowData, ImmutableFlowUtils};
use layout::flow;
use layout::model::{MaybeAuto, Specified, Auto, specified_or_none, specified};
use layout::float_context::{FloatContext, Invalid};
use std::cell::Cell;
use geom::point::Point2D;
use geom::size::Size2D;
use geom::rect::Rect;
use gfx::display_list::DisplayList;
use servo_util::geometry::{Au, to_frac_px};
use servo_util::geometry;
pub struct BlockFlow {
/// Data common to all flows.
base: FlowData,
/// The associated render box.
box: Option<@RenderBox>,
/// Whether this block flow is the root flow.
is_root: bool
}
impl BlockFlow {
pub fn new(base: FlowData) -> BlockFlow {
BlockFlow {
base: base,
box: None,
is_root: false
}
}
pub fn new_root(base: FlowData) -> BlockFlow {
BlockFlow {
base: base,
box: None,
is_root: true
}
}
pub fn teardown(&mut self) {
for box in self.box.iter() {
box.teardown();
}
self.box = None;
}
/// Computes left and right margins and width based on CSS 2.1 section 10.3.3.
/// Requires borders and padding to already be computed.
fn compute_horiz(&self,
width: MaybeAuto,
left_margin: MaybeAuto,
right_margin: MaybeAuto,
available_width: Au)
-> (Au, Au, Au) {
// If width is not 'auto', and width + margins > available_width, all 'auto' margins are
// treated as 0.
let (left_margin, right_margin) = match width {
Auto => (left_margin, right_margin),
Specified(width) => {
let left = left_margin.specified_or_zero();
let right = right_margin.specified_or_zero();
if((left + right + width) > available_width) {
(Specified(left), Specified(right))
} else {
(left_margin, right_margin)
}
}
};
//Invariant: left_margin_Au + width_Au + right_margin_Au == available_width
let (left_margin_Au, width_Au, right_margin_Au) = match (left_margin, width, right_margin) {
//If all have a computed value other than 'auto', the system is over-constrained and we need to discard a margin.
//if direction is ltr, ignore the specified right margin and solve for it. If it is rtl, ignore the specified
//left margin. FIXME(eatkinson): this assumes the direction is ltr
(Specified(margin_l), Specified(width), Specified(_margin_r)) => (margin_l, width, available_width - (margin_l + width )),
//If exactly one value is 'auto', solve for it
(Auto, Specified(width), Specified(margin_r)) => (available_width - (width + margin_r), width, margin_r),
(Specified(margin_l), Auto, Specified(margin_r)) => (margin_l, available_width - (margin_l + margin_r), margin_r),
(Specified(margin_l), Specified(width), Auto) => (margin_l, width, available_width - (margin_l + width)),
//If width is set to 'auto', any other 'auto' value becomes '0', and width is solved for
(Auto, Auto, Specified(margin_r)) => (Au::new(0), available_width - margin_r, margin_r),
(Specified(margin_l), Auto, Auto) => (margin_l, available_width - margin_l, Au::new(0)),
(Auto, Auto, Auto) => (Au::new(0), available_width, Au::new(0)),
//If left and right margins are auto, they become equal
(Auto, Specified(width), Auto) => {
let margin = (available_width - width).scale_by(0.5);
(margin, width, margin)
}
};
//return values in same order as params
(width_Au, left_margin_Au, right_margin_Au)
}
// inline(always) because this is only ever called by in-order or non-in-order top-level
// methods
#[inline(always)]
fn assign_height_block_base(&mut self, ctx: &mut LayoutContext, inorder: bool) {
let mut cur_y = Au::new(0);
let mut clearance = Au::new(0);
let mut top_offset = Au::new(0);
let mut bottom_offset = Au::new(0);
let mut left_offset = Au::new(0);
let mut float_ctx = Invalid;
for &box in self.box.iter() {
let base = box.base();
clearance = match base.clear() {
None => Au::new(0),
Some(clear) => {
self.base.floats_in.clearance(clear)
}
};
let mut model_ref = base.model.mutate();
let model = &mut model_ref.ptr;
top_offset = clearance + model.margin.top + model.border.top + model.padding.top;
cur_y = cur_y + top_offset;
bottom_offset = model.margin.bottom + model.border.bottom + model.padding.bottom;
left_offset = model.offset();
}
if inorder {
// Floats for blocks work like this:
// self.floats_in -> child[0].floats_in
// visit child[0]
// child[i-1].floats_out -> child[i].floats_in
// visit child[i]
// repeat until all children are visited.
// last_child.floats_out -> self.floats_out (done at the end of this method)
float_ctx = self.base.floats_in.translate(Point2D(-left_offset, -top_offset));
for kid in self.base.child_iter() {
flow::mut_base(*kid).floats_in = float_ctx.clone();
kid.assign_height_inorder(ctx);
float_ctx = flow::mut_base(*kid).floats_out.clone();
}
}
let mut collapsible = Au::new(0);
let mut collapsing = Au::new(0);
let mut margin_top = Au::new(0);
let mut margin_bottom = Au::new(0);
let mut top_margin_collapsible = false;
let mut bottom_margin_collapsible = false;
let mut first_in_flow = true;
for &box in self.box.iter() {
let base = box.base();
let mut model_ref = base.model.mutate();
let model = &mut model_ref.ptr;
if !self.is_root && model.border.top == Au::new(0) && model.padding.top == Au::new(0) {
collapsible = model.margin.top;
top_margin_collapsible = true;
}
if !self.is_root && model.border.bottom == Au::new(0) && model.padding.bottom == Au::new(0) {
bottom_margin_collapsible = true;
}
margin_top = model.margin.top;
margin_bottom = model.margin.bottom;
}
for kid in self.base.child_iter() {
kid.collapse_margins(top_margin_collapsible,
&mut first_in_flow,
&mut margin_top,
&mut top_offset,
&mut collapsing,
&mut collapsible);
let child_node = flow::mut_base(*kid);
cur_y = cur_y - collapsing;
child_node.position.origin.y = cur_y;
cur_y = cur_y + child_node.position.size.height;
}
// The bottom margin collapses with its last in-flow block-level child's bottom margin
// if the parent has no bottom boder, no bottom padding.
collapsing = if bottom_margin_collapsible {
if margin_bottom < collapsible {
margin_bottom = collapsible;
}
collapsible
} else {
Au::new(0)
};
// TODO: A box's own margins collapse if the 'min-height' property is zero, and it has neither
// top or bottom borders nor top or bottom padding, and it has a 'height' of either 0 or 'auto',
// and it does not contain a line box, and all of its in-flow children's margins (if any) collapse.
let mut height = if self.is_root {
Au::max(ctx.screen_size.size.height, cur_y)
} else {
cur_y - top_offset - collapsing
};
for &box in self.box.iter() {
let base = box.base();
let style = base.style();
height = match MaybeAuto::from_style(style.Box.height, Au::new(0)) {
Auto => height,
Specified(value) => value
};
}
let mut noncontent_height = Au::new(0);
for box in self.box.iter() {
let base = box.base();
let mut model_ref = base.model.mutate();
let mut position_ref = base.position.mutate();
let (model, position) = (&mut model_ref.ptr, &mut position_ref.ptr);
// The associated box is the border box of this flow.
model.margin.top = margin_top;
model.margin.bottom = margin_bottom;
position.origin.y = clearance + model.margin.top;
noncontent_height = model.padding.top + model.padding.bottom + model.border.top +
model.border.bottom;
position.size.height = height + noncontent_height;
noncontent_height = noncontent_height + clearance + model.margin.top +
model.margin.bottom;
}
self.base.position.size.height = height + noncontent_height;
if inorder {
let extra_height = height - (cur_y - top_offset) + bottom_offset;
self.base.floats_out = float_ctx.translate(Point2D(left_offset, -extra_height));
} else {
self.base.floats_out = self.base.floats_in.clone();
}
}
pub fn build_display_list_block<E:ExtraDisplayListData>(
&mut self,
builder: &DisplayListBuilder,
dirty: &Rect<Au>,
list: &Cell<DisplayList<E>>)
-> bool {
if self.base.node.is_iframe_element() {
let x = self.base.abs_position.x + do self.box.map_default(Au::new(0)) |box| {
let model = box.base().model.get();
model.margin.left + model.border.left + model.padding.left
};
let y = self.base.abs_position.y + do self.box.map_default(Au::new(0)) |box| {
let model = box.base().model.get();
model.margin.top + model.border.top + model.padding.top
};
let w = self.base.position.size.width - do self.box.map_default(Au::new(0)) |box| {
box.base().model.get().noncontent_width()
};
let h = self.base.position.size.height - do self.box.map_default(Au::new(0)) |box| {
box.base().model.get().noncontent_height()
};
do self.base.node.with_mut_iframe_element |iframe_element| {
iframe_element.size.get_mut_ref().set_rect(Rect(Point2D(to_frac_px(x) as f32,
to_frac_px(y) as f32),
Size2D(to_frac_px(w) as f32,
to_frac_px(h) as f32)));
}
}
let abs_rect = Rect(self.base.abs_position, self.base.position.size);
if !abs_rect.intersects(dirty) {
return true;
}
debug!("build_display_list_block: adding display element");
// add box that starts block context
for box in self.box.iter() {
box.build_display_list(builder, dirty, &self.base.abs_position, list)
}
// TODO: handle any out-of-flow elements
let this_position = self.base.abs_position;
for child in self.base.child_iter() {
let child_base = flow::mut_base(*child);
child_base.abs_position = this_position + child_base.position.origin;
}
false
}
}
impl FlowContext for BlockFlow {
fn class(&self) -> FlowClass {
BlockFlowClass
}
fn as_block<'a>(&'a mut self) -> &'a mut BlockFlow {
self
}
/* Recursively (bottom-up) determine the context's preferred and
minimum widths. When called on this context, all child contexts
have had their min/pref widths set. This function must decide
min/pref widths based on child context widths and dimensions of
any boxes it is responsible for flowing. */
/* TODO: floats */
/* TODO: absolute contexts */
/* TODO: inline-blocks */
fn bubble_widths(&mut self, _: &mut LayoutContext) {
let mut min_width = Au::new(0);
let mut pref_width = Au::new(0);
let mut num_floats = 0;
/* find max width from child block contexts */
for child_ctx in self.base.child_iter() {
assert!(child_ctx.starts_block_flow() || child_ctx.starts_inline_flow());
let child_base = flow::mut_base(*child_ctx);
min_width = geometry::max(min_width, child_base.min_width);
pref_width = geometry::max(pref_width, child_base.pref_width);
num_floats = num_floats + child_base.num_floats;
}
/* if not an anonymous block context, add in block box's widths.
these widths will not include child elements, just padding etc. */
for box in self.box.iter() {
{
// Can compute border width here since it doesn't depend on anything.
let base = box.base();
base.model.mutate().ptr.compute_borders(base.style())
}
let (this_minimum_width, this_preferred_width) = box.minimum_and_preferred_widths();
min_width = min_width + this_minimum_width;
pref_width = pref_width + this_preferred_width;
}
self.base.min_width = min_width;
self.base.pref_width = pref_width;
self.base.num_floats = num_floats;
}
/// Recursively (top-down) determines the actual width of child contexts and boxes. When called
/// on this context, the context has had its width set by the parent context.
///
/// Dual boxes consume some width first, and the remainder is assigned to all child (block)
/// contexts.
fn assign_widths(&mut self, ctx: &mut LayoutContext) {
debug!("assign_widths_block: assigning width for flow {}", self.base.id);
if self.is_root {
debug!("Setting root position");
self.base.position.origin = Au::zero_point();
self.base.position.size.width = ctx.screen_size.size.width;
self.base.floats_in = FloatContext::new(self.base.num_floats);
self.base.is_inorder = false;
}
//position was set to the containing block by the flow's parent
let mut remaining_width = self.base.position.size.width;
let mut x_offset = Au::new(0);
for &box in self.box.iter() {
let base = box.base();
let style = base.style();
let mut model_ref = base.model.mutate();
let model = &mut model_ref.ptr;
// Can compute padding here since we know containing block width.
model.compute_padding(style, remaining_width);
// Margins are 0 right now so model.noncontent_width() is just borders + padding.
let available_width = remaining_width - model.noncontent_width();
// Top and bottom margins for blocks are 0 if auto.
let margin_top = MaybeAuto::from_style(style.Margin.margin_top,
remaining_width).specified_or_zero();
let margin_bottom = MaybeAuto::from_style(style.Margin.margin_bottom,
remaining_width).specified_or_zero();
let (width, maybe_margin_left, maybe_margin_right) =
(MaybeAuto::from_style(style.Box.width, remaining_width),
MaybeAuto::from_style(style.Margin.margin_left, remaining_width),
MaybeAuto::from_style(style.Margin.margin_right, remaining_width));
let (width, margin_left, margin_right) = self.compute_horiz(width,
maybe_margin_left,
maybe_margin_right,
available_width);
// If the tentative used width is greater than 'max-width', width should be recalculated,
// but this time using the computed value of 'max-width' as the computed value for 'width'.
let (width, margin_left, margin_right) = {
match specified_or_none(style.Box.max_width, remaining_width) {
Some(value) if value < width => self.compute_horiz(Specified(value),
maybe_margin_left,
maybe_margin_right,
available_width),
_ => (width, margin_left, margin_right)
}
};
// If the resulting width is smaller than 'min-width', width should be recalculated,
// but this time using the value of 'min-width' as the computed value for 'width'.
let (width, margin_left, margin_right) = {
let computed_min_width = specified(style.Box.min_width, remaining_width);
if computed_min_width > width {
self.compute_horiz(Specified(computed_min_width),
maybe_margin_left,
maybe_margin_right,
available_width)
} else {
(width, margin_left, margin_right)
}
};
model.margin.top = margin_top;
model.margin.right = margin_right;
model.margin.bottom = margin_bottom;
model.margin.left = margin_left;
x_offset = model.offset();
remaining_width = width;
//The associated box is the border box of this flow
let position_ref = base.position.mutate();
position_ref.ptr.origin.x = model.margin.left;
let padding_and_borders = model.padding.left + model.padding.right +
model.border.left + model.border.right;
position_ref.ptr.size.width = remaining_width + padding_and_borders;
}
let has_inorder_children = self.base.is_inorder || self.base.num_floats > 0;
for kid in self.base.child_iter() {
assert!(kid.starts_block_flow() || kid.starts_inline_flow());
let child_base = flow::mut_base(*kid);
child_base.position.origin.x = x_offset;
child_base.position.size.width = remaining_width;
child_base.is_inorder = has_inorder_children;
if !child_base.is_inorder {
child_base.floats_in = FloatContext::new(0);
}
}
}
fn assign_height_inorder(&mut self, ctx: &mut LayoutContext) {
debug!("assign_height_inorder: assigning height for block {}", self.base.id);
self.assign_height_block_base(ctx, true);
}
fn assign_height(&mut self, ctx: &mut LayoutContext) {
debug!("assign_height: assigning height for block {}", self.base.id);
// This is the only case in which a block flow can start an inorder
// subtraversal.
if self.is_root && self.base.num_floats > 0 {
self.assign_height_inorder(ctx);
return;
}
self.assign_height_block_base(ctx, false);
}
fn collapse_margins(&mut self,
top_margin_collapsible: bool,
first_in_flow: &mut bool,
margin_top: &mut Au,
top_offset: &mut Au,
collapsing: &mut Au,
collapsible: &mut Au) {
for &box in self.box.iter() {
let base = box.base();
let mut model_ref = base.model.mutate();
let model = &mut model_ref.ptr;
// The top margin collapses with its first in-flow block-level child's
// top margin if the parent has no top border, no top padding.
if *first_in_flow && top_margin_collapsible {
// If top-margin of parent is less than top-margin of its first child,
// the parent box goes down until its top is aligned with the child.
if *margin_top < model.margin.top {
// TODO: The position of child floats should be updated and this
// would influence clearance as well. See #725
let extra_margin = model.margin.top - *margin_top;
*top_offset = *top_offset + extra_margin;
*margin_top = model.margin.top;
}
}
// The bottom margin of an in-flow block-level element collapses
// with the top margin of its next in-flow block-level sibling.
*collapsing = geometry::min(model.margin.top, *collapsible);
*collapsible = model.margin.bottom;
}
*first_in_flow = false;
}
fn debug_str(&self) -> ~str {
if self.is_root {
~"BlockFlow(root)"
} else {
let txt = ~"BlockFlow: ";
txt.append(match self.box {
Some(rb) => {
rb.debug_str()
}
None => { ~"" }
})
}
}
}