⎇ 66bcb10bff823bfde7828eeef133cb2fa7de7dba
src/compute/block.rs
Ñawpa willay
Pi imata llamkarqan
ch'uya
Fixed clippy lint errors (needless_pass_by_ref_mut) (#517)
1//! Computes the CSS block layout algorithm in the case that the block container being laid out contains only block-level boxes
2use crate::compute::LayoutAlgorithm;
3use crate::geometry::{Line, Point, Rect, Size};
4use crate::style::{AvailableSpace, Display, LengthPercentageAuto, Overflow, Position};
5use crate::style_helpers::TaffyMaxContent;
6use crate::tree::{CollapsibleMarginSet, Layout, RunMode, SizeBaselinesAndMargins, SizingMode};
7use crate::tree::{LayoutTree, NodeId};
8use crate::util::sys::f32_max;
9use crate::util::sys::Vec;
10use crate::util::MaybeMath;
11use crate::util::{MaybeResolve, ResolveOrZero};
12
13#[cfg(feature = "debug")]
14use crate::util::debug::NODE_LOGGER;
15
16/// The public interface to Taffy's Block algorithm implementation
17pub struct BlockAlgorithm;
18impl LayoutAlgorithm for BlockAlgorithm {
19 const NAME: &'static str = "BLOCK";
20
21 fn perform_layout(
22 tree: &mut impl LayoutTree,
23 node: NodeId,
24 known_dimensions: Size<Option<f32>>,
25 parent_size: Size<Option<f32>>,
26 available_space: Size<AvailableSpace>,
27 _sizing_mode: SizingMode,
28 vertical_margins_are_collapsible: Line<bool>,
29 ) -> SizeBaselinesAndMargins {
30 compute(
31 tree,
32 node,
33 known_dimensions,
34 parent_size,
35 available_space,
36 RunMode::PerformLayout,
37 vertical_margins_are_collapsible,
38 )
39 }
40
41 fn measure_size(
42 tree: &mut impl LayoutTree,
43 node: NodeId,
44 known_dimensions: Size<Option<f32>>,
45 parent_size: Size<Option<f32>>,
46 available_space: Size<AvailableSpace>,
47 _sizing_mode: SizingMode,
48 vertical_margins_are_collapsible: Line<bool>,
49 ) -> Size<f32> {
50 compute(
51 tree,
52 node,
53 known_dimensions,
54 parent_size,
55 available_space,
56 RunMode::ComputeSize,
57 vertical_margins_are_collapsible,
58 )
59 .size
60 }
61}
62
63/// Per-child data that is accumulated and modified over the course of the layout algorithm
64struct BlockItem {
65 /// The identifier for the associated node
66 node_id: NodeId,
67
68 /// The "source order" of the item. This is the index of the item within the children iterator,
69 /// and controls the order in which the nodes are placed
70 order: u32,
71
72 /// The base size of this item
73 size: Size<Option<f32>>,
74 /// The minimum allowable size of this item
75 min_size: Size<Option<f32>>,
76 /// The maximum allowable size of this item
77 max_size: Size<Option<f32>>,
78
79 /// The position style of the item
80 position: Position,
81 /// The final offset of this item
82 inset: Rect<LengthPercentageAuto>,
83 /// The margin of this item
84 margin: Rect<LengthPercentageAuto>,
85 /// The sum of padding and border for this item
86 padding_border_sum: Size<f32>,
87
88 /// The computed border box size of this item
89 computed_size: Size<f32>,
90 /// The computed "static position" of this item. The static position is the position
91 /// taking into account padding, border, margins, and scrollbar_gutters but not inset
92 static_position: Point<f32>,
93 /// Whether margins can be collapsed through this item
94 can_be_collapsed_through: bool,
95}
96
97/// Computes the layout of [`LayoutTree`] according to the block layout algorithm
98pub fn compute(
99 tree: &mut impl LayoutTree,
100 node_id: NodeId,
101 known_dimensions: Size<Option<f32>>,
102 parent_size: Size<Option<f32>>,
103 available_space: Size<AvailableSpace>,
104 run_mode: RunMode,
105 vertical_margins_are_collapsible: Line<bool>,
106) -> SizeBaselinesAndMargins {
107 let style = tree.style(node_id);
108
109 // Pull these out earlier to avoid borrowing issues
110 let aspect_ratio = style.aspect_ratio;
111 let margin = style.margin.resolve_or_zero(parent_size.width);
112 let min_size = style.min_size.maybe_resolve(parent_size).maybe_apply_aspect_ratio(aspect_ratio);
113 let max_size = style.max_size.maybe_resolve(parent_size).maybe_apply_aspect_ratio(aspect_ratio);
114 let padding = style.padding.resolve_or_zero(parent_size.width);
115 let border = style.border.resolve_or_zero(parent_size.width);
116 let padding_border_size = (padding + border).sum_axes();
117 let clamped_style_size =
118 style.size.maybe_resolve(parent_size).maybe_apply_aspect_ratio(aspect_ratio).maybe_clamp(min_size, max_size);
119
120 // If both min and max in a given axis are set and max <= min then this determines the size in that axis
121 let min_max_definite_size = min_size.zip_map(max_size, |min, max| match (min, max) {
122 (Some(min), Some(max)) if max <= min => Some(min),
123 _ => None,
124 });
125
126 // Block nodes automatically stretch fit their width to fit available space if available space is definite
127 let available_space_based_size =
128 Size { width: available_space.width.into_option().maybe_sub(margin.horizontal_axis_sum()), height: None };
129
130 let styled_based_known_dimensions = known_dimensions
131 .or(min_max_definite_size)
132 .or(clamped_style_size)
133 .or(available_space_based_size)
134 .maybe_max(padding_border_size);
135
136 // Short-circuit layout if the container's size is fully determined by the container's size and the run mode
137 // is ComputeSize (and thus the container's size is all that we're interested in)
138 if run_mode == RunMode::ComputeSize {
139 if let Size { width: Some(width), height: Some(height) } = styled_based_known_dimensions {
140 return Size { width, height }.into();
141 }
142 }
143
144 #[cfg(feature = "debug")]
145 NODE_LOGGER.log("BLOCK");
146 compute_inner(
147 tree,
148 node_id,
149 styled_based_known_dimensions,
150 parent_size,
151 available_space,
152 run_mode,
153 vertical_margins_are_collapsible,
154 )
155}
156
157/// Computes the layout of [`LayoutTree`] according to the block layout algorithm
158fn compute_inner(
159 tree: &mut impl LayoutTree,
160 node_id: NodeId,
161 known_dimensions: Size<Option<f32>>,
162 parent_size: Size<Option<f32>>,
163 available_space: Size<AvailableSpace>,
164 run_mode: RunMode,
165 vertical_margins_are_collapsible: Line<bool>,
166) -> SizeBaselinesAndMargins {
167 let style = tree.style(node_id);
168 let raw_padding = style.padding;
169 let raw_border = style.border;
170 let raw_margin = style.margin;
171 let aspect_ratio = style.aspect_ratio;
172 let size = style.size.maybe_resolve(parent_size).maybe_apply_aspect_ratio(aspect_ratio);
173 let min_size = style.min_size.maybe_resolve(parent_size).maybe_apply_aspect_ratio(aspect_ratio);
174 let max_size = style.max_size.maybe_resolve(parent_size).maybe_apply_aspect_ratio(aspect_ratio);
175 let padding = style.padding.resolve_or_zero(parent_size.width);
176 let border = style.border.resolve_or_zero(parent_size.width);
177
178 // Scrollbar gutters are reserved when the `overflow` property is set to `Overflow::Scroll`.
179 // However, the axis are switched (transposed) because a node that scrolls vertically needs
180 // *horizontal* space to be reserved for a scrollbar
181 let scrollbar_gutter = {
182 let offsets = style.overflow.transpose().map(|overflow| match overflow {
183 Overflow::Scroll => style.scrollbar_width,
184 _ => 0.0,
185 });
186 // TODO: make side configurable based on the `direction` property
187 Rect { top: 0.0, left: 0.0, right: offsets.x, bottom: offsets.y }
188 };
189 let padding_border = padding + border;
190 let padding_border_size = padding_border.sum_axes();
191 let content_box_inset = padding_border + scrollbar_gutter;
192 let container_content_box_size = known_dimensions.maybe_sub(content_box_inset.sum_axes());
193
194 // Determine margin collapsing behaviour
195 let own_margins_collapse_with_children = Line {
196 start: vertical_margins_are_collapsible.start
197 && !style.overflow.x.is_scroll_container()
198 && !style.overflow.y.is_scroll_container()
199 && style.position == Position::Relative
200 && padding.top == 0.0
201 && border.top == 0.0,
202 end: vertical_margins_are_collapsible.end
203 && !style.overflow.x.is_scroll_container()
204 && !style.overflow.y.is_scroll_container()
205 && style.position == Position::Relative
206 && padding.bottom == 0.0
207 && border.bottom == 0.0
208 && size.height.is_none(),
209 };
210 let has_styles_preventing_being_collapsed_through = style.display != Display::Block
211 || style.overflow.x.is_scroll_container()
212 || style.overflow.y.is_scroll_container()
213 || style.position == Position::Absolute
214 || padding.top > 0.0
215 || padding.bottom > 0.0
216 || border.top > 0.0
217 || border.bottom > 0.0;
218
219 // 1. Generate items
220 let mut items = generate_item_list(tree, node_id, container_content_box_size);
221
222 // 2. Compute container width
223 let container_outer_width = known_dimensions.width.unwrap_or_else(|| {
224 let available_width = available_space.width.maybe_sub(content_box_inset.horizontal_axis_sum());
225 let intrinsic_width = determine_content_based_container_width(tree, &items, available_width)
226 + content_box_inset.horizontal_axis_sum();
227 intrinsic_width.maybe_clamp(min_size.width, max_size.width).maybe_max(Some(padding_border_size.width))
228 });
229
230 // Short-circuit if computing size and both dimensions known
231 if let (RunMode::ComputeSize, Some(container_outer_height)) = (run_mode, known_dimensions.height) {
232 return Size { width: container_outer_width, height: container_outer_height }.into();
233 }
234
235 // 3. Perform final item layout and return content height
236 let resolved_padding = raw_padding.resolve_or_zero(Some(container_outer_width));
237 let resolved_border = raw_border.resolve_or_zero(Some(container_outer_width));
238 let resolved_content_box_inset = resolved_padding + resolved_border + scrollbar_gutter;
239 let (intrinsic_outer_height, first_child_top_margin_set, last_child_bottom_margin_set) =
240 perform_final_layout_on_in_flow_children(
241 tree,
242 &mut items,
243 container_outer_width,
244 content_box_inset,
245 resolved_content_box_inset,
246 own_margins_collapse_with_children,
247 );
248 let container_outer_height = known_dimensions
249 .height
250 .unwrap_or(intrinsic_outer_height.maybe_clamp(min_size.height, max_size.height))
251 .maybe_max(Some(padding_border_size.height));
252 let final_outer_size = Size { width: container_outer_width, height: container_outer_height };
253
254 // Short-circuit if computing size
255 if run_mode == RunMode::ComputeSize {
256 return final_outer_size.into();
257 }
258
259 // 4. Layout absolutely positioned children
260 let absolute_position_inset = resolved_border + scrollbar_gutter;
261 let absolute_position_area = final_outer_size - absolute_position_inset.sum_axes();
262 let absolute_position_offset = Point { x: absolute_position_inset.left, y: absolute_position_inset.top };
263 perform_absolute_layout_on_absolute_children(tree, &items, absolute_position_area, absolute_position_offset);
264
265 // 5. Perform hidden layout on hidden children
266 let len = tree.child_count(node_id);
267 for order in 0..len {
268 let child = tree.child(node_id, order);
269 if tree.style(child).display == Display::None {
270 *tree.layout_mut(child) = Layout::with_order(order as u32);
271 tree.perform_child_layout(
272 child,
273 Size::NONE,
274 Size::NONE,
275 Size::MAX_CONTENT,
276 SizingMode::InherentSize,
277 Line::FALSE,
278 );
279 }
280 }
281
282 // 7. Determine whether this node can be collapsed through
283 let all_in_flow_children_can_be_collapsed_through =
284 items.iter().all(|item| item.position == Position::Absolute || item.can_be_collapsed_through);
285 let can_be_collapsed_through =
286 !has_styles_preventing_being_collapsed_through && all_in_flow_children_can_be_collapsed_through;
287
288 SizeBaselinesAndMargins {
289 size: final_outer_size,
290 first_baselines: Point::NONE,
291 top_margin: if own_margins_collapse_with_children.start {
292 first_child_top_margin_set
293 } else {
294 let margin_top = raw_margin.top.resolve_or_zero(parent_size.width);
295 CollapsibleMarginSet::from_margin(margin_top)
296 },
297 bottom_margin: if own_margins_collapse_with_children.end {
298 last_child_bottom_margin_set
299 } else {
300 let margin_bottom = raw_margin.bottom.resolve_or_zero(parent_size.width);
301 CollapsibleMarginSet::from_margin(margin_bottom)
302 },
303 margins_can_collapse_through: can_be_collapsed_through,
304 }
305}
306
307/// Create a `Vec` of `BlockItem` structs where each item in the `Vec` represents a child of the current node
308#[inline]
309fn generate_item_list(tree: &impl LayoutTree, node: NodeId, node_inner_size: Size<Option<f32>>) -> Vec<BlockItem> {
310 tree.children(node)
311 .map(|child_node_id| (child_node_id, tree.style(child_node_id)))
312 .filter(|(_, style)| style.display != Display::None)
313 .enumerate()
314 .map(|(order, (child_node_id, child_style))| {
315 let aspect_ratio = child_style.aspect_ratio;
316 let padding = child_style.padding.resolve_or_zero(node_inner_size);
317 let border = child_style.border.resolve_or_zero(node_inner_size);
318 BlockItem {
319 node_id: child_node_id,
320 order: order as u32,
321
322 size: child_style.size.maybe_resolve(node_inner_size).maybe_apply_aspect_ratio(aspect_ratio),
323 min_size: child_style.min_size.maybe_resolve(node_inner_size).maybe_apply_aspect_ratio(aspect_ratio),
324 max_size: child_style.max_size.maybe_resolve(node_inner_size).maybe_apply_aspect_ratio(aspect_ratio),
325 position: child_style.position,
326 inset: child_style.inset,
327 margin: child_style.margin,
328 padding_border_sum: (padding + border).sum_axes(),
329
330 // Fields to be computed later (for now we initialise with dummy values)
331 computed_size: Size::zero(),
332 static_position: Point::zero(),
333 can_be_collapsed_through: false,
334 }
335 })
336 .collect()
337}
338
339/// Compute the content-based width in the case that the width of the container is not known
340#[inline]
341fn determine_content_based_container_width(
342 tree: &mut impl LayoutTree,
343 items: &[BlockItem],
344 available_width: AvailableSpace,
345) -> f32 {
346 let available_space = Size { width: available_width, height: AvailableSpace::MinContent };
347
348 let mut max_child_width = 0.0;
349 for item in items.iter().filter(|item| item.position != Position::Absolute) {
350 let known_dimensions = item.size.maybe_clamp(item.min_size, item.max_size);
351
352 let width = known_dimensions.width.unwrap_or_else(|| {
353 let item_x_margin_sum =
354 item.margin.resolve_or_zero(available_space.width.into_option()).horizontal_axis_sum();
355 let size_and_baselines = tree.perform_child_layout(
356 item.node_id,
357 known_dimensions,
358 Size::NONE,
359 available_space.map_width(|w| w.maybe_sub(item_x_margin_sum)),
360 SizingMode::InherentSize,
361 Line::TRUE,
362 );
363
364 size_and_baselines.size.width + item_x_margin_sum
365 });
366 let width = f32_max(width, item.padding_border_sum.width);
367
368 max_child_width = f32_max(max_child_width, width);
369 }
370
371 max_child_width
372}
373
374/// Compute each child's final size and position
375#[inline]
376fn perform_final_layout_on_in_flow_children(
377 tree: &mut impl LayoutTree,
378 items: &mut [BlockItem],
379 container_outer_width: f32,
380 content_box_inset: Rect<f32>,
381 resolved_content_box_inset: Rect<f32>,
382 own_margins_collapse_with_children: Line<bool>,
383) -> (f32, CollapsibleMarginSet, CollapsibleMarginSet) {
384 // Resolve container_inner_width for sizing child nodes using intial content_box_inset
385 let container_inner_width = container_outer_width - content_box_inset.horizontal_axis_sum();
386 let parent_size = Size { width: Some(container_outer_width), height: None };
387 let available_space =
388 Size { width: AvailableSpace::Definite(container_inner_width), height: AvailableSpace::MinContent };
389
390 let mut committed_y_offset = resolved_content_box_inset.top;
391 let mut first_child_top_margin_set = CollapsibleMarginSet::ZERO;
392 let mut active_collapsible_margin_set = CollapsibleMarginSet::ZERO;
393 let mut is_collapsing_with_first_margin_set = true;
394 for item in items.iter_mut() {
395 if item.position == Position::Absolute {
396 item.static_position.y = committed_y_offset;
397 } else {
398 let item_margin = item.margin.map(|margin| margin.resolve_to_option(container_outer_width));
399 let item_non_auto_margin = item_margin.map(|m| m.unwrap_or(0.0));
400 let item_non_auto_x_margin_sum = item_non_auto_margin.horizontal_axis_sum();
401 let known_dimensions = item
402 .size
403 .map_width(|width| Some(width.unwrap_or(container_inner_width - item_non_auto_x_margin_sum)))
404 .maybe_clamp(item.min_size, item.max_size);
405
406 let item_layout = tree.perform_child_layout(
407 item.node_id,
408 known_dimensions,
409 parent_size,
410 available_space.map_width(|w| w.maybe_sub(item_non_auto_x_margin_sum)),
411 SizingMode::InherentSize,
412 Line::TRUE,
413 );
414 let final_size = item_layout.size;
415
416 let top_margin_set = item_layout.top_margin.collapse_with_margin(item_margin.top.unwrap_or(0.0));
417 let bottom_margin_set = item_layout.bottom_margin.collapse_with_margin(item_margin.bottom.unwrap_or(0.0));
418
419 // Expand auto margins to fill available space
420 // Note: Vertical auto-margins for relatively positioned block items simply resolve to 0.
421 // See: https://www.w3.org/TR/CSS21/visudet.html#abs-non-replaced-width
422 let free_x_space = f32_max(0.0, container_inner_width - final_size.width - item_non_auto_x_margin_sum);
423 let x_axis_auto_margin_size = {
424 let auto_margin_count = item_margin.left.is_none() as u8 + item_margin.right.is_none() as u8;
425 if auto_margin_count == 2 && item.size.width.is_none() {
426 0.0
427 } else if auto_margin_count > 0 {
428 free_x_space / auto_margin_count as f32
429 } else {
430 0.0
431 }
432 };
433 let resolved_margin = Rect {
434 left: item_margin.left.unwrap_or(x_axis_auto_margin_size),
435 right: item_margin.right.unwrap_or(x_axis_auto_margin_size),
436 top: top_margin_set.resolve(),
437 bottom: bottom_margin_set.resolve(),
438 };
439
440 // Resolve item inset
441 let inset =
442 item.inset.zip_size(Size { width: container_inner_width, height: 0.0 }, |p, s| p.maybe_resolve(s));
443 let inset_offset = Point {
444 x: inset.left.or(inset.right.map(|x| -x)).unwrap_or(0.0),
445 y: inset.top.or(inset.bottom.map(|x| -x)).unwrap_or(0.0),
446 };
447
448 let y_margin_offset = if is_collapsing_with_first_margin_set && own_margins_collapse_with_children.start {
449 0.0
450 } else {
451 active_collapsible_margin_set.collapse_with_margin(resolved_margin.top).resolve()
452 };
453
454 item.computed_size = item_layout.size;
455 item.can_be_collapsed_through = item_layout.margins_can_collapse_through;
456 item.static_position = Point {
457 x: resolved_content_box_inset.left,
458 y: committed_y_offset + active_collapsible_margin_set.resolve(),
459 };
460
461 *tree.layout_mut(item.node_id) = Layout {
462 order: item.order,
463 size: item_layout.size,
464 location: Point {
465 x: resolved_content_box_inset.left + inset_offset.x + resolved_margin.left,
466 y: committed_y_offset + inset_offset.y + y_margin_offset,
467 },
468 };
469
470 // Update first_child_top_margin_set
471 if is_collapsing_with_first_margin_set {
472 if item.can_be_collapsed_through {
473 first_child_top_margin_set = first_child_top_margin_set
474 .collapse_with_set(top_margin_set)
475 .collapse_with_set(bottom_margin_set);
476 } else {
477 first_child_top_margin_set = first_child_top_margin_set.collapse_with_set(top_margin_set);
478 is_collapsing_with_first_margin_set = false;
479 }
480 }
481
482 // Update active_collapsible_margin_set
483 if item.can_be_collapsed_through {
484 active_collapsible_margin_set = active_collapsible_margin_set
485 .collapse_with_set(top_margin_set)
486 .collapse_with_set(bottom_margin_set);
487 } else {
488 committed_y_offset += item_layout.size.height + y_margin_offset;
489 active_collapsible_margin_set = bottom_margin_set;
490 }
491 }
492 }
493
494 let last_child_bottom_margin_set = active_collapsible_margin_set;
495 let bottom_y_margin_offset =
496 if own_margins_collapse_with_children.end { 0.0 } else { last_child_bottom_margin_set.resolve() };
497
498 committed_y_offset += resolved_content_box_inset.bottom + bottom_y_margin_offset;
499 let content_height = f32_max(0.0, committed_y_offset);
500 (content_height, first_child_top_margin_set, last_child_bottom_margin_set)
501}
502
503/// Perform absolute layout on all absolutely positioned children.
504#[inline]
505fn perform_absolute_layout_on_absolute_children(
506 tree: &mut impl LayoutTree,
507 items: &[BlockItem],
508 area_size: Size<f32>,
509 area_offset: Point<f32>,
510) {
511 let area_width = area_size.width;
512 let area_height = area_size.height;
513
514 for item in items.iter().filter(|item| item.position == Position::Absolute) {
515 let child_style = tree.style(item.node_id);
516
517 // Skip items that are display:none or are not position:absolute
518 if child_style.display == Display::None || child_style.position != Position::Absolute {
519 continue;
520 }
521
522 let aspect_ratio = child_style.aspect_ratio;
523 let margin = child_style.margin.map(|margin| margin.resolve_to_option(area_width));
524 let padding = child_style.padding.resolve_or_zero(Some(area_width));
525 let border = child_style.border.resolve_or_zero(Some(area_width));
526 let padding_border_sum = (padding + border).sum_axes();
527
528 // Resolve inset
529 let left = child_style.inset.left.maybe_resolve(area_width);
530 let right = child_style.inset.right.maybe_resolve(area_width);
531 let top = child_style.inset.top.maybe_resolve(area_height);
532 let bottom = child_style.inset.bottom.maybe_resolve(area_height);
533
534 // Compute known dimensions from min/max/inherent size styles
535 let style_size = child_style.size.maybe_resolve(area_size).maybe_apply_aspect_ratio(aspect_ratio);
536 let min_size = child_style
537 .min_size
538 .maybe_resolve(area_size)
539 .maybe_apply_aspect_ratio(aspect_ratio)
540 .or(padding_border_sum.map(Some))
541 .maybe_max(padding_border_sum);
542 let max_size = child_style.max_size.maybe_resolve(area_size).maybe_apply_aspect_ratio(aspect_ratio);
543 let mut known_dimensions = style_size.maybe_clamp(min_size, max_size);
544
545 // Fill in width from left/right and reapply aspect ratio if:
546 // - Width is not already known
547 // - Item has both left and right inset properties set
548 if let (None, Some(left), Some(right)) = (known_dimensions.width, left, right) {
549 let new_width_raw = area_width.maybe_sub(margin.left).maybe_sub(margin.right) - left - right;
550 known_dimensions.width = Some(f32_max(new_width_raw, 0.0));
551 known_dimensions = known_dimensions.maybe_apply_aspect_ratio(aspect_ratio).maybe_clamp(min_size, max_size);
552 }
553
554 // Fill in height from top/bottom and reapply aspect ratio if:
555 // - Height is not already known
556 // - Item has both top and bottom inset properties set
557 if let (None, Some(top), Some(bottom)) = (known_dimensions.height, top, bottom) {
558 let new_height_raw = area_height.maybe_sub(margin.top).maybe_sub(margin.bottom) - top - bottom;
559 known_dimensions.height = Some(f32_max(new_height_raw, 0.0));
560 known_dimensions = known_dimensions.maybe_apply_aspect_ratio(aspect_ratio).maybe_clamp(min_size, max_size);
561 }
562
563 let measured_size_and_baselines = tree.perform_child_layout(
564 item.node_id,
565 known_dimensions,
566 area_size.map(Some),
567 Size {
568 width: AvailableSpace::Definite(area_width.maybe_clamp(min_size.width, max_size.width)),
569 height: AvailableSpace::Definite(area_height.maybe_clamp(min_size.height, max_size.height)),
570 },
571 SizingMode::ContentSize,
572 Line::FALSE,
573 );
574 let measured_size = measured_size_and_baselines.size;
575 let final_size = known_dimensions.unwrap_or(measured_size).maybe_clamp(min_size, max_size);
576
577 let non_auto_margin = Rect {
578 left: if left.is_some() { margin.left.unwrap_or(0.0) } else { 0.0 },
579 right: if right.is_some() { margin.right.unwrap_or(0.0) } else { 0.0 },
580 top: if top.is_some() { margin.top.unwrap_or(0.0) } else { 0.0 },
581 bottom: if bottom.is_some() { margin.left.unwrap_or(0.0) } else { 0.0 },
582 };
583
584 // Expand auto margins to fill available space
585 // https://www.w3.org/TR/CSS21/visudet.html#abs-non-replaced-width
586 let auto_margin = {
587 // Auto margins for absolutely positioned elements in block containers only resolve
588 // if inset is set. Otherwise they resolve to 0.
589 let absolute_auto_margin_space = Point {
590 x: right.map(|right| area_size.width - right - left.unwrap_or(0.0)).unwrap_or(final_size.width),
591 y: bottom.map(|bottom| area_size.height - bottom - top.unwrap_or(0.0)).unwrap_or(final_size.height),
592 };
593 let free_space = Size {
594 width: absolute_auto_margin_space.x - final_size.width - non_auto_margin.horizontal_axis_sum(),
595 height: absolute_auto_margin_space.y - final_size.height - non_auto_margin.vertical_axis_sum(),
596 };
597
598 let auto_margin_size = Size {
599 // If all three of 'left', 'width', and 'right' are 'auto': First set any 'auto' values for 'margin-left' and 'margin-right' to 0.
600 // Then, if the 'direction' property of the element establishing the static-position containing block is 'ltr' set 'left' to the
601 // static position and apply rule number three below; otherwise, set 'right' to the static position and apply rule number one below.
602 //
603 // If none of the three is 'auto': If both 'margin-left' and 'margin-right' are 'auto', solve the equation under the extra constraint
604 // that the two margins get equal values, unless this would make them negative, in which case when direction of the containing block is
605 // 'ltr' ('rtl'), set 'margin-left' ('margin-right') to zero and solve for 'margin-right' ('margin-left'). If one of 'margin-left' or
606 // 'margin-right' is 'auto', solve the equation for that value. If the values are over-constrained, ignore the value for 'left' (in case
607 // the 'direction' property of the containing block is 'rtl') or 'right' (in case 'direction' is 'ltr') and solve for that value.
608 width: {
609 let auto_margin_count = margin.left.is_none() as u8 + margin.right.is_none() as u8;
610 if auto_margin_count == 2
611 && (style_size.width.is_none() || style_size.width.unwrap() >= free_space.width)
612 {
613 0.0
614 } else if auto_margin_count > 0 {
615 free_space.width / auto_margin_count as f32
616 } else {
617 0.0
618 }
619 },
620 height: {
621 let auto_margin_count = margin.top.is_none() as u8 + margin.bottom.is_none() as u8;
622 if auto_margin_count == 2
623 && (style_size.height.is_none() || style_size.height.unwrap() >= free_space.height)
624 {
625 0.0
626 } else if auto_margin_count > 0 {
627 free_space.height / auto_margin_count as f32
628 } else {
629 0.0
630 }
631 },
632 };
633
634 Rect {
635 left: margin.left.map(|_| 0.0).unwrap_or(auto_margin_size.width),
636 right: margin.right.map(|_| 0.0).unwrap_or(auto_margin_size.width),
637 top: margin.top.map(|_| 0.0).unwrap_or(auto_margin_size.height),
638 bottom: margin.bottom.map(|_| 0.0).unwrap_or(auto_margin_size.height),
639 }
640 };
641
642 let resolved_margin = Rect {
643 left: margin.left.unwrap_or(auto_margin.left),
644 right: margin.right.unwrap_or(auto_margin.right),
645 top: margin.top.unwrap_or(auto_margin.top),
646 bottom: margin.bottom.unwrap_or(auto_margin.bottom),
647 };
648
649 let item_offset = Point {
650 x: left
651 .map(|left| left + resolved_margin.left)
652 .or(right.map(|right| area_size.width - final_size.width - right - resolved_margin.right))
653 .unwrap_or(resolved_margin.left),
654 y: top
655 .map(|top| top + resolved_margin.top)
656 .or(bottom.map(|bottom| area_size.height - final_size.height - bottom - resolved_margin.bottom))
657 .unwrap_or(item.static_position.y + resolved_margin.top),
658 };
659
660 *tree.layout_mut(item.node_id) =
661 Layout { order: item.order, size: final_size, location: area_offset + item_offset };
662 }
663}