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