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