Skip to main content

strata_layout/
positioning.rs

1//! CSS Positioning Scheme Engine (Relative, Absolute, Fixed, Sticky).
2//!
3//! Implements W3C CSS 2.1 Section 10.1 (Containing Block), Section 10.3.7 & 10.6.4
4//! (Absolute Positioning Equations, Double-Anchored Inset Stretching, Auto-Margin Centering),
5//! and CSS Positioned Layout Level 3 Section 6 (Sticky Constraints).
6
7use crate::box_model::{
8    clamp_dimension, clamp_dimension_with_node, resolve_dimension, resolve_dimension_with_node,
9};
10use crate::geometry::{LayoutGeometry, Rect};
11use crate::tree::LayoutNode;
12use strata_style::types::{
13    AlignItems, AlignSelf, Dimension, Display, FlexDirection, JustifyContent, Position,
14};
15
16/// Active containing block context propagated down the layout hierarchy.
17#[derive(Debug, Clone, Copy, PartialEq)]
18pub struct ContainingBlock {
19    /// The padding box of the containing block in global coordinates.
20    pub padding_box: Rect,
21    /// The viewport bounds (for `position: fixed`, overridden by `transform`/`filter` ancestors).
22    pub viewport: Rect,
23    /// The root initial containing block viewport (for Top-Layer `<Portal>` elements).
24    pub initial_viewport: Rect,
25    /// Scroll offset of the viewport or scroll container (scroll_x, scroll_y).
26    pub scroll_offset: (f32, f32),
27}
28
29impl ContainingBlock {
30    /// Creates a containing block from a padding box and viewport.
31    pub const fn new(padding_box: Rect, viewport: Rect) -> Self {
32        Self {
33            padding_box,
34            viewport,
35            initial_viewport: viewport,
36            scroll_offset: (0.0, 0.0),
37        }
38    }
39
40    /// Creates an initial containing block matching the viewport.
41    pub const fn from_viewport(viewport_width: f32, viewport_height: f32) -> Self {
42        let vp = Rect::new(0.0, 0.0, viewport_width, viewport_height);
43        Self {
44            padding_box: vp,
45            viewport: vp,
46            initial_viewport: vp,
47            scroll_offset: (0.0, 0.0),
48        }
49    }
50
51    /// Creates a containing block with an explicit scroll offset.
52    pub const fn with_scroll(
53        padding_box: Rect,
54        viewport: Rect,
55        scroll_x: f32,
56        scroll_y: f32,
57    ) -> Self {
58        Self {
59            padding_box,
60            viewport,
61            initial_viewport: viewport,
62            scroll_offset: (scroll_x, scroll_y),
63        }
64    }
65}
66
67/// Resolves out-of-flow and relative positioning offsets for a container's children
68/// and recursively propagates containing blocks down through all descendants.
69pub fn solve_positioning(container: &mut LayoutNode, viewport_width: f32, viewport_height: f32) {
70    solve_positioning_guarded(
71        container,
72        viewport_width,
73        viewport_height,
74        crate::tree::LayoutDepthGuard::root(),
75    );
76}
77
78/// Resolves positioning with an active depth guard to prevent call-stack exhaustion.
79pub fn solve_positioning_guarded(
80    container: &mut LayoutNode,
81    viewport_width: f32,
82    viewport_height: f32,
83    guard: crate::tree::LayoutDepthGuard,
84) {
85    let padding_box = if container.geometry.padding_box.width > 0.0
86        || container.geometry.padding_box.height > 0.0
87    {
88        container.geometry.padding_box
89    } else {
90        Rect::new(0.0, 0.0, viewport_width, viewport_height)
91    };
92
93    let initial_cb = ContainingBlock::new(
94        padding_box,
95        Rect::new(0.0, 0.0, viewport_width, viewport_height),
96    );
97
98    solve_positioning_children_guarded(container, initial_cb, guard);
99}
100
101/// Repositions absolutely positioned flex children that rely on the static
102/// position on one or both axes.
103fn apply_flex_static_positions(container: &mut LayoutNode) {
104    let content = container.geometry.content_box;
105    if content.width <= 0.0 && content.height <= 0.0 {
106        return;
107    }
108    let is_row = matches!(
109        container.style.flex_direction,
110        FlexDirection::Row | FlexDirection::RowReverse
111    );
112
113    for child in &mut container.children {
114        if child.style.position != Position::Absolute {
115            continue;
116        }
117        let left_auto = matches!(child.style.insets.left, Dimension::Auto);
118        let right_auto = matches!(child.style.insets.right, Dimension::Auto);
119        let top_auto = matches!(child.style.insets.top, Dimension::Auto);
120        let bottom_auto = matches!(child.style.insets.bottom, Dimension::Auto);
121        let horiz_static = left_auto && right_auto;
122        let vert_static = top_auto && bottom_auto;
123        if !(horiz_static || vert_static) {
124            continue;
125        }
126
127        let item_w =
128            child.geometry.border_box.width + child.style.margin.left + child.style.margin.right;
129        let item_h =
130            child.geometry.border_box.height + child.style.margin.top + child.style.margin.bottom;
131
132        let main_offset = if is_row {
133            justify_offset(container.style.justify_content, content.width, item_w)
134        } else {
135            justify_offset(container.style.justify_content, content.height, item_h)
136        };
137
138        let effective_align = match child.style.align_self {
139            AlignSelf::Auto => container.style.align_items,
140            AlignSelf::Stretch => AlignItems::Stretch,
141            AlignSelf::FlexStart => AlignItems::FlexStart,
142            AlignSelf::FlexEnd => AlignItems::FlexEnd,
143            AlignSelf::Center => AlignItems::Center,
144            AlignSelf::Baseline => AlignItems::Baseline,
145        };
146        let cross_offset = if is_row {
147            align_offset(effective_align, content.height, item_h)
148        } else {
149            align_offset(effective_align, content.width, item_w)
150        };
151
152        let static_x = if is_row {
153            content.x + main_offset + child.style.margin.left
154        } else {
155            content.x + cross_offset + child.style.margin.left
156        };
157        let static_y = if is_row {
158            content.y + cross_offset + child.style.margin.top
159        } else {
160            content.y + main_offset + child.style.margin.top
161        };
162
163        // W3C CSS Flexbox Level 1 §4.1: static position on an axis applies only
164        // when BOTH insets on that axis are `auto`. Any specified inset (`left`
165        // OR `right`, `top` OR `bottom`) anchors to the containing block.
166        let dx = if horiz_static {
167            static_x - child.geometry.border_box.x
168        } else {
169            0.0
170        };
171        let dy = if vert_static {
172            static_y - child.geometry.border_box.y
173        } else {
174            0.0
175        };
176        if dx != 0.0 || dy != 0.0 {
177            child.translate(dx, dy);
178        }
179    }
180}
181
182/// Resolves a `justify-content` offset for a single flex item.
183fn justify_offset(justify: JustifyContent, available: f32, item: f32) -> f32 {
184    let free = (available - item).max(0.0);
185    match justify {
186        JustifyContent::FlexStart => 0.0,
187        JustifyContent::FlexEnd => free,
188        JustifyContent::Center => free / 2.0,
189        JustifyContent::SpaceBetween => 0.0,
190        JustifyContent::SpaceAround | JustifyContent::SpaceEvenly => free / 2.0,
191    }
192}
193
194/// Resolves a cross-axis `align-items` offset for a single flex item.
195fn align_offset(align: AlignItems, available: f32, item: f32) -> f32 {
196    let free = (available - item).max(0.0);
197    match align {
198        AlignItems::FlexStart | AlignItems::Stretch | AlignItems::Baseline => 0.0,
199        AlignItems::FlexEnd => free,
200        AlignItems::Center => free / 2.0,
201    }
202}
203
204/// Recursively traverses child nodes, resolving positioning and propagating the containing block.
205pub fn solve_positioning_children(container: &mut LayoutNode, cb: ContainingBlock) {
206    solve_positioning_children_guarded(container, cb, crate::tree::LayoutDepthGuard::root());
207}
208
209/// Recursively traverses child nodes with an active depth guard to prevent stack overflow.
210pub fn solve_positioning_children_guarded(
211    container: &mut LayoutNode,
212    cb: ContainingBlock,
213    guard: crate::tree::LayoutDepthGuard,
214) {
215    if guard.is_exhausted() {
216        return;
217    }
218    let child_guard = match guard.enter() {
219        Some(g) => g,
220        None => return,
221    };
222    for child in &mut container.children {
223        match child.style.position {
224            Position::Static => {
225                // Static elements do not establish a containing block unless they have a
226                // transform, filter, or backdrop-filter (CSS Transforms §2 / Filter Effects §5).
227                if !child.children.is_empty() {
228                    let child_cb = if child.style.establishes_fixed_containing_block() {
229                        ContainingBlock {
230                            padding_box: child.geometry.padding_box,
231                            viewport: child.geometry.padding_box,
232                            initial_viewport: cb.initial_viewport,
233                            scroll_offset: cb.scroll_offset,
234                        }
235                    } else {
236                        cb
237                    };
238                    solve_positioning_children_guarded(child, child_cb, child_guard);
239                }
240            }
241            Position::Relative => {
242                // 1. In-flow offset relative to normal position
243                let mut dx = 0.0;
244                let mut dy = 0.0;
245
246                if let Some(top_px) =
247                    resolve_dimension(child.style.insets.top, cb.padding_box.height)
248                {
249                    dy += top_px;
250                } else if let Some(bot_px) =
251                    resolve_dimension(child.style.insets.bottom, cb.padding_box.height)
252                {
253                    dy -= bot_px;
254                }
255
256                if let Some(left_px) =
257                    resolve_dimension(child.style.insets.left, cb.padding_box.width)
258                {
259                    dx += left_px;
260                } else if let Some(right_px) =
261                    resolve_dimension(child.style.insets.right, cb.padding_box.width)
262                {
263                    dx -= right_px;
264                }
265
266                let delta_x = dx - child.applied_relative_offset.0;
267                let delta_y = dy - child.applied_relative_offset.1;
268                if delta_x != 0.0 || delta_y != 0.0 {
269                    child.translate(delta_x, delta_y);
270                }
271                child.applied_relative_offset = (dx, dy);
272
273                // 2. Relative elements establish a containing block for absolute descendants
274                let child_vp = if child.style.establishes_fixed_containing_block() {
275                    child.geometry.padding_box
276                } else {
277                    cb.viewport
278                };
279                let child_cb = ContainingBlock {
280                    padding_box: child.geometry.padding_box,
281                    viewport: child_vp,
282                    initial_viewport: cb.initial_viewport,
283                    scroll_offset: cb.scroll_offset,
284                };
285
286                if !child.children.is_empty() {
287                    solve_positioning_children_guarded(child, child_cb, child_guard);
288                }
289            }
290            Position::Absolute => {
291                // Out-of-flow positioning relative to nearest non-static ancestor (cb.padding_box)
292                solve_out_of_flow_box_guarded(
293                    child,
294                    cb.padding_box,
295                    cb.viewport,
296                    false,
297                    child_guard,
298                );
299
300                // Absolute elements establish a containing block for descendants
301                let child_vp = if child.style.establishes_fixed_containing_block() {
302                    child.geometry.padding_box
303                } else {
304                    cb.viewport
305                };
306                let child_cb = ContainingBlock {
307                    padding_box: child.geometry.padding_box,
308                    viewport: child_vp,
309                    initial_viewport: cb.initial_viewport,
310                    scroll_offset: cb.scroll_offset,
311                };
312
313                if !child.children.is_empty() {
314                    solve_positioning_children_guarded(child, child_cb, child_guard);
315                }
316            }
317            Position::Fixed => {
318                // Top-layer `<Portal>` elements always resolve against the root initial viewport,
319                // escaping even `transform` / `filter` / `backdrop-filter` ancestors.
320                let fixed_vp = if child.is_top_layer {
321                    cb.initial_viewport
322                } else {
323                    cb.viewport
324                };
325                solve_out_of_flow_box_guarded(child, fixed_vp, fixed_vp, true, child_guard);
326
327                // Fixed elements establish a containing block for descendants
328                let child_vp = if child.style.establishes_fixed_containing_block() {
329                    child.geometry.padding_box
330                } else {
331                    fixed_vp
332                };
333                let child_cb = ContainingBlock {
334                    padding_box: child.geometry.padding_box,
335                    viewport: child_vp,
336                    initial_viewport: cb.initial_viewport,
337                    scroll_offset: (0.0, 0.0),
338                };
339
340                if !child.children.is_empty() {
341                    solve_positioning_children_guarded(child, child_cb, child_guard);
342                }
343            }
344            Position::Sticky => {
345                // In CSS Positioned Layout Level 3 §6, the containing block for a sticky element
346                // is established by its nearest block container ancestor (`container`), not the
347                // nearest positioned ancestor.
348                let parent_box = if container.geometry.padding_box.width > 0.0
349                    || container.geometry.padding_box.height > 0.0
350                {
351                    container.geometry.padding_box
352                } else {
353                    cb.padding_box
354                };
355                let sticky_cb = ContainingBlock {
356                    padding_box: parent_box,
357                    viewport: cb.viewport,
358                    initial_viewport: cb.initial_viewport,
359                    scroll_offset: cb.scroll_offset,
360                };
361                solve_sticky_box(child, sticky_cb);
362
363                let child_vp = if child.style.establishes_fixed_containing_block() {
364                    child.geometry.padding_box
365                } else {
366                    cb.viewport
367                };
368                let child_cb = ContainingBlock {
369                    padding_box: child.geometry.padding_box,
370                    viewport: child_vp,
371                    initial_viewport: cb.initial_viewport,
372                    scroll_offset: cb.scroll_offset,
373                };
374
375                if !child.children.is_empty() {
376                    solve_positioning_children_guarded(child, child_cb, child_guard);
377                }
378            }
379        }
380    }
381
382    // W3C CSS Flexbox Level 1 §4.1: apply flex static positions to direct
383    // absolutely positioned children whose horizontal or vertical inset pair is `auto`.
384    if matches!(container.style.display, Display::Flex | Display::InlineFlex) {
385        apply_flex_static_positions(container);
386    }
387}
388
389/// Solves out-of-flow geometry for an absolute or fixed element according to W3C CSS 2.1 §10.3.7 & §10.6.4.
390pub fn solve_out_of_flow_box(
391    child: &mut LayoutNode,
392    cb_rect: Rect,
393    viewport: Rect,
394    is_fixed: bool,
395) {
396    solve_out_of_flow_box_guarded(
397        child,
398        cb_rect,
399        viewport,
400        is_fixed,
401        crate::tree::LayoutDepthGuard::root(),
402    );
403}
404
405/// Solves out-of-flow geometry with an active depth guard.
406pub fn solve_out_of_flow_box_guarded(
407    child: &mut LayoutNode,
408    cb_rect: Rect,
409    _viewport: Rect,
410    _is_fixed: bool,
411    guard: crate::tree::LayoutDepthGuard,
412) {
413    let left = resolve_dimension(child.style.insets.left, cb_rect.width);
414    let right = resolve_dimension(child.style.insets.right, cb_rect.width);
415    let top = resolve_dimension(child.style.insets.top, cb_rect.height);
416    let bottom = resolve_dimension(child.style.insets.bottom, cb_rect.height);
417
418    let prev_x = child.geometry.border_box.x;
419    let prev_y = child.geometry.border_box.y;
420    let pre_existing_w = child.geometry.border_box.width;
421    let pre_existing_h = child.geometry.border_box.height;
422
423    if prev_x != 0.0 || prev_y != 0.0 {
424        child.translate(-prev_x, -prev_y);
425    }
426
427    // 1. Horizontal Sizing & Intrinsic Shrink-to-Fit Resolution (W3C CSS 2.1 §10.3.7)
428    let (tentative_w, width_is_auto) = match (left, right, child.style.width) {
429        (Some(l), Some(r), Dimension::Auto) => {
430            let available =
431                (cb_rect.width - l - r - child.style.margin.left - child.style.margin.right)
432                    .max(0.0);
433            (
434                clamp_dimension_with_node(
435                    available,
436                    child.style.min_width,
437                    child.style.max_width,
438                    cb_rect.width,
439                    child,
440                ),
441                false,
442            )
443        }
444        (_, _, Dimension::Auto) => {
445            let inset_sum_x = left.unwrap_or(0.0)
446                + right.unwrap_or(0.0)
447                + child.style.margin.left
448                + child.style.margin.right;
449            let avail_for_stf = (cb_rect.width - inset_sum_x).max(0.0);
450            let stf_w = crate::intrinsic::compute_min_max_sizes(child, Some(cb_rect.width))
451                .shrink_to_fit(avail_for_stf);
452            let w = if stf_w > 0.0 { stf_w } else { pre_existing_w };
453            (
454                clamp_dimension_with_node(
455                    w,
456                    child.style.min_width,
457                    child.style.max_width,
458                    cb_rect.width,
459                    child,
460                ),
461                true,
462            )
463        }
464        _ => {
465            let w = resolve_dimension_with_node(child.style.width, cb_rect.width, child)
466                .unwrap_or(pre_existing_w);
467            (
468                clamp_dimension_with_node(
469                    w,
470                    child.style.min_width,
471                    child.style.max_width,
472                    cb_rect.width,
473                    child,
474                ),
475                false,
476            )
477        }
478    };
479
480    // 2. Vertical Definite vs Auto Height Pre-Resolution (W3C CSS 2.1 §10.6.4)
481    let definite_h = match (top, bottom, child.style.height) {
482        (Some(t), Some(b), Dimension::Auto) => {
483            let available =
484                (cb_rect.height - t - b - child.style.margin.top - child.style.margin.bottom)
485                    .max(0.0);
486            Some(clamp_dimension(
487                available,
488                child.style.min_height,
489                child.style.max_height,
490                cb_rect.height,
491            ))
492        }
493        (_, _, Dimension::Auto) => None,
494        _ => {
495            let h = resolve_dimension(child.style.height, cb_rect.height).unwrap_or(pre_existing_h);
496            Some(clamp_dimension(
497                h,
498                child.style.min_height,
499                child.style.max_height,
500                cb_rect.height,
501            ))
502        }
503    };
504
505    // 3. Format child subtree at origin (0, 0) BEFORE computing right/bottom inset coordinates
506    // so auto width and auto height reflect the laid-out content dimensions.
507    child.geometry = LayoutGeometry::from_border_box(
508        Rect::new(0.0, 0.0, tentative_w, definite_h.unwrap_or(0.0)),
509        child.style.padding,
510        child.style.border_width,
511        child.style.margin,
512    );
513
514    if !child.children.is_empty() || !child.inline_items.is_empty() {
515        child.layout_children_inner_guarded(guard, false);
516    } else if let Some(ref text_str) = child.text {
517        let content_w = child.geometry.content_box.width.max(0.0);
518        let wrap_w = if matches!(
519            child.style.white_space,
520            strata_style::types::WhiteSpace::NoWrap | strata_style::types::WhiteSpace::Pre
521        ) {
522            None
523        } else {
524            Some(content_w)
525        };
526        let (_, text_h) = crate::text::measure_text_bounds_styled_with_family(
527            text_str,
528            child.style.font_size,
529            child.style.line_height,
530            wrap_w,
531            crate::text::is_monospace_family(child.style.font_family),
532            child.style.font_family,
533            child.style.font_weight,
534            child.style.font_style,
535        );
536        if definite_h.is_none() {
537            child.geometry.border_box.height = text_h
538                + child.style.padding.top
539                + child.style.padding.bottom
540                + child.style.border_width.top
541                + child.style.border_width.bottom;
542        }
543    } else if let Some(ref img) = child.image {
544        if definite_h.is_none() && img.height > 0 {
545            let content_w = child.geometry.content_box.width.max(0.0);
546            let img_h = if img.width > 0 && content_w > 0.0 {
547                content_w * (img.height as f32 / img.width as f32)
548            } else {
549                img.height as f32
550            };
551            child.geometry.border_box.height = img_h
552                + child.style.padding.top
553                + child.style.padding.bottom
554                + child.style.border_width.top
555                + child.style.border_width.bottom;
556        }
557    }
558
559    let raw_w = if width_is_auto {
560        child.geometry.border_box.width.max(tentative_w)
561    } else {
562        tentative_w
563    };
564    let item_w = clamp_dimension_with_node(
565        raw_w,
566        child.style.min_width,
567        child.style.max_width,
568        cb_rect.width,
569        child,
570    );
571
572    let item_h = match definite_h {
573        Some(h) => h,
574        None => {
575            let auto_h = if child.geometry.border_box.height > 0.0 {
576                child.geometry.border_box.height
577            } else {
578                pre_existing_h
579            };
580            clamp_dimension(
581                auto_h,
582                child.style.min_height,
583                child.style.max_height,
584                cb_rect.height,
585            )
586        }
587    };
588
589    // 4. Horizontal Position & Auto-Margin Centering (mx-auto / m-auto)
590    let item_x = match (left, right) {
591        (Some(l), Some(r)) if child.style.margin_auto_left && child.style.margin_auto_right => {
592            let free_space = (cb_rect.width - l - r) - item_w;
593            let auto_m = (free_space / 2.0).max(0.0);
594            cb_rect.x + l + auto_m
595        }
596        (Some(l), _) if child.style.margin_auto_left => {
597            let free_space = (cb_rect.width - l) - item_w;
598            cb_rect.x + l + free_space.max(0.0)
599        }
600        (Some(l), _) => cb_rect.x + l + child.style.margin.left,
601        (None, Some(r)) => cb_rect.x + cb_rect.width - r - child.style.margin.right - item_w,
602        (None, None) => cb_rect.x + child.style.margin.left,
603    };
604
605    // 5. Vertical Position & Auto-Margin Centering (my-auto / m-auto)
606    let item_y = match (top, bottom) {
607        (Some(t), Some(b)) if child.style.margin_auto_top && child.style.margin_auto_bottom => {
608            let free_space = (cb_rect.height - t - b) - item_h;
609            let auto_m = (free_space / 2.0).max(0.0);
610            cb_rect.y + t + auto_m
611        }
612        (Some(t), _) if child.style.margin_auto_top => {
613            let free_space = (cb_rect.height - t) - item_h;
614            cb_rect.y + t + free_space.max(0.0)
615        }
616        (Some(t), _) => cb_rect.y + t + child.style.margin.top,
617        (None, Some(b)) => cb_rect.y + cb_rect.height - b - child.style.margin.bottom - item_h,
618        (None, None) => cb_rect.y + child.style.margin.top,
619    };
620
621    // 6. Finalize child box at origin and translate entire subtree to (item_x, item_y)
622    child.geometry = LayoutGeometry::from_border_box(
623        Rect::new(0.0, 0.0, item_w, item_h),
624        child.style.padding,
625        child.style.border_width,
626        child.style.margin,
627    );
628    if item_x != 0.0 || item_y != 0.0 {
629        child.translate(item_x, item_y);
630    }
631    child.geometry.snap_to_pixel();
632}
633
634/// Solves sticky positioning by evaluating insets against scroll offset and clamping to parent bounds.
635fn solve_sticky_box(child: &mut LayoutNode, cb: ContainingBlock) {
636    let normal_x = child.geometry.border_box.x;
637    let normal_y = child.geometry.border_box.y;
638
639    let mut dx = 0.0;
640    let mut dy = 0.0;
641
642    // Vertical sticky evaluation
643    if let Some(top_px) = resolve_dimension(child.style.insets.top, cb.viewport.height) {
644        let target_y = cb.scroll_offset.1 + top_px;
645        let sticky_y = normal_y.max(target_y);
646        let max_sticky_y = cb.padding_box.y + cb.padding_box.height
647            - child.geometry.border_box.height
648            - child.style.margin.bottom;
649        let clamped_y = sticky_y.min(max_sticky_y.max(normal_y));
650        dy = clamped_y - normal_y;
651    } else if let Some(bot_px) = resolve_dimension(child.style.insets.bottom, cb.viewport.height) {
652        let target_y =
653            cb.scroll_offset.1 + cb.viewport.height - bot_px - child.geometry.border_box.height;
654        let sticky_y = normal_y.min(target_y);
655        let min_sticky_y = cb.padding_box.y + child.style.margin.top;
656        let clamped_y = sticky_y.max(min_sticky_y.min(normal_y));
657        dy = clamped_y - normal_y;
658    }
659
660    // Horizontal sticky evaluation
661    if let Some(left_px) = resolve_dimension(child.style.insets.left, cb.viewport.width) {
662        let target_x = cb.scroll_offset.0 + left_px;
663        let sticky_x = normal_x.max(target_x);
664        let max_sticky_x = cb.padding_box.x + cb.padding_box.width
665            - child.geometry.border_box.width
666            - child.style.margin.right;
667        let clamped_x = sticky_x.min(max_sticky_x.max(normal_x));
668        dx = clamped_x - normal_x;
669    } else if let Some(right_px) = resolve_dimension(child.style.insets.right, cb.viewport.width) {
670        let target_x =
671            cb.scroll_offset.0 + cb.viewport.width - right_px - child.geometry.border_box.width;
672        let sticky_x = normal_x.min(target_x);
673        let min_sticky_x = cb.padding_box.x + child.style.margin.left;
674        let clamped_x = sticky_x.max(min_sticky_x.min(normal_x));
675        dx = clamped_x - normal_x;
676    }
677
678    if dx != 0.0 || dy != 0.0 {
679        child.translate(dx, dy);
680    }
681}