1
//! Formatting context layout (block, inline, table, and flex/grid via Taffy)
2

            
3
use std::{
4
    collections::{BTreeMap, HashMap},
5
    sync::Arc,
6
};
7

            
8
use azul_core::{
9
    dom::{FormattingContext, NodeId, NodeType},
10
    geom::{LogicalPosition, LogicalRect, LogicalSize},
11
    resources::RendererResources,
12
    styled_dom::{StyledDom, StyledNodeState},
13
};
14
use azul_css::{
15
    css::CssPropertyValue,
16
    props::{
17
        basic::{
18
            font::{StyleFontStyle, StyleFontWeight},
19
            pixel::{DEFAULT_FONT_SIZE, PT_TO_PX},
20
            ColorU, PhysicalSize, PropertyContext, ResolutionContext, SizeMetric,
21
        },
22
        layout::{
23
            ColumnCount, ColumnWidth, LayoutBorderSpacing, LayoutClear, LayoutDisplay, LayoutFloat,
24
            LayoutHeight, LayoutJustifyContent, LayoutOverflow, LayoutPosition, LayoutTableLayout,
25
            LayoutTextJustify, LayoutWidth, LayoutWritingMode, ShapeInside, ShapeOutside,
26
            StyleBorderCollapse, StyleCaptionSide, StyleEmptyCells,
27
        },
28
        property::CssProperty,
29
        style::{
30
            BorderStyle, StyleDirection, StyleHyphens, StyleLineBreak, StyleListStylePosition,
31
            StyleListStyleType, StyleOverflowWrap, StyleTextAlign, StyleTextAlignLast,
32
            StyleTextBoxTrim, StyleTextCombineUpright, StyleTextOrientation, StyleUnicodeBidi,
33
            StyleVerticalAlign, StyleVisibility, StyleWhiteSpace, StyleWordBreak,
34
        },
35
    },
36
};
37
use rust_fontconfig::FcWeight;
38
use taffy::{AvailableSpace, LayoutInput, Line, Size as TaffySize};
39

            
40
#[cfg(feature = "text_layout")]
41
use crate::text3;
42
use crate::{
43
    debug_ifc_layout, debug_info, debug_log, debug_table_layout, debug_warning,
44
    font_traits::{
45
        ContentIndex, FontLoaderTrait, ImageSource, InlineContent, InlineImage, InlineShape,
46
        LayoutFragment, ObjectFit, ParsedFontTrait, SegmentAlignment, ShapeBoundary,
47
        ShapeDefinition, ShapedItem, Size, StyleProperties, StyledRun, TextLayoutCache,
48
        UnifiedConstraints,
49
    },
50
    solver3::{
51
        geometry::{BoxProps, EdgeSizes, IntrinsicSizes},
52
        getters::{
53
            get_css_border_bottom_width, get_css_border_top_width,
54
            get_css_height, get_css_padding_bottom, get_css_padding_top,
55
            get_css_width, get_direction_property, get_unicode_bidi_property,
56
            get_display_property, get_element_font_size, get_float, get_clear,
57
            get_list_style_position, get_list_style_type, get_overflow_x, get_overflow_y,
58
            get_parent_font_size, get_root_font_size, get_style_properties,
59
            get_text_align, get_text_box_trim_property, get_text_orientation_property,
60
            get_vertical_align_property, get_visibility, get_white_space_property,
61
            get_writing_mode, MultiValue,
62
        },
63
        layout_tree::{
64
            AnonymousBoxType, CachedInlineLayout, LayoutNode, LayoutNodeHot, LayoutNodeWarm, LayoutNodeCold, LayoutTree, PseudoElement,
65
        },
66
        positioning::get_position_type,
67
        scrollbar::ScrollbarRequirements,
68
        sizing::extract_text_from_node,
69
        taffy_bridge, LayoutContext, LayoutDebugMessage, LayoutError, Result,
70
    },
71
    text3::cache::{AvailableSpace as Text3AvailableSpace, TextAlign as Text3TextAlign},
72
};
73

            
74
/// Default scrollbar width in pixels (CSS `scrollbar-width: auto`).
75
/// This is only used as a fallback when per-node CSS cannot be queried.
76
/// Prefer `getters::get_layout_scrollbar_width_px()` for per-node resolution.
77
pub const DEFAULT_SCROLLBAR_WIDTH_PX: f32 = 16.0;
78

            
79
// Note: DEFAULT_FONT_SIZE and PT_TO_PX are imported from pixel
80

            
81
/// Result of BFC layout with margin escape information
82
#[derive(Debug, Clone)]
83
pub(crate) struct BfcLayoutResult {
84
    /// Standard layout output (positions, overflow size, baseline)
85
    pub output: LayoutOutput,
86
    /// Top margin that escaped the BFC (for parent-child collapse)
87
    /// If Some, this margin should be used by parent instead of positioning this BFC
88
    pub escaped_top_margin: Option<f32>,
89
    /// Bottom margin that escaped the BFC (for parent-child collapse)
90
    /// If Some, this margin should collapse with next sibling
91
    pub escaped_bottom_margin: Option<f32>,
92
}
93

            
94
impl BfcLayoutResult {
95
21245
    pub fn from_output(output: LayoutOutput) -> Self {
96
21245
        Self {
97
21245
            output,
98
21245
            escaped_top_margin: None,
99
21245
            escaped_bottom_margin: None,
100
21245
        }
101
21245
    }
102
}
103

            
104
/// The CSS `overflow` property behavior.
105
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
106
pub enum OverflowBehavior {
107
    Visible,
108
    Hidden,
109
    Clip,
110
    Scroll,
111
    Auto,
112
}
113

            
114
impl OverflowBehavior {
115
5
    pub fn is_clipped(&self) -> bool {
116
5
        matches!(self, Self::Hidden | Self::Clip | Self::Scroll | Self::Auto)
117
5
    }
118

            
119
5
    pub fn is_scroll(&self) -> bool {
120
5
        matches!(self, Self::Scroll | Self::Auto)
121
5
    }
122
}
123

            
124
/// Input constraints for a layout function.
125
#[derive(Debug)]
126
pub struct LayoutConstraints<'a> {
127
    /// The available space for the content, excluding padding and borders.
128
    pub available_size: LogicalSize,
129
    /// The CSS writing-mode of the context.
130
    pub writing_mode: LayoutWritingMode,
131
    /// Full writing mode context (writing-mode + direction + text-orientation).
132
    /// Used by writing-mode-aware layout code to correctly map inline/block
133
    /// dimensions to physical x/y coordinates.
134
    pub writing_mode_ctx: super::geometry::WritingModeContext,
135
    /// The state of the parent Block Formatting Context, if applicable.
136
    /// This is how state (like floats) is passed down.
137
    pub bfc_state: Option<&'a mut BfcState>,
138
    // Other properties like text-align would go here.
139
    pub text_align: TextAlign,
140
    /// The size of the containing block (parent's content box).
141
    /// This is used for resolving percentage-based sizes and as parent_size for Taffy.
142
    pub containing_block_size: LogicalSize,
143
    /// The semantic type of the available width constraint.
144
    ///
145
    /// This field is crucial for correct inline layout caching:
146
    /// - `Definite(w)`: Normal layout with a specific available width
147
    /// - `MinContent`: Intrinsic minimum width measurement (maximum wrapping)
148
    /// - `MaxContent`: Intrinsic maximum width measurement (no wrapping)
149
    ///
150
    /// When caching inline layouts, we must track which constraint type was used
151
    /// to compute the cached result. A layout computed with `MinContent` (width=0)
152
    /// must not be reused when the actual available width is known.
153
    pub available_width_type: Text3AvailableSpace,
154
}
155

            
156
/// Manages all layout state for a single Block Formatting Context.
157
/// This struct is created by the BFC root and lives for the duration of its layout.
158
#[derive(Debug, Clone)]
159
pub struct BfcState {
160
    /// The current position for the next in-flow block element.
161
    pub pen: LogicalPosition,
162
    /// The state of all floated elements within this BFC.
163
    pub floats: FloatingContext,
164
    /// The state of margin collapsing within this BFC.
165
    pub margins: MarginCollapseContext,
166
}
167

            
168
impl BfcState {
169
    pub fn new() -> Self {
170
        Self {
171
            pen: LogicalPosition::zero(),
172
            floats: FloatingContext::default(),
173
            margins: MarginCollapseContext::default(),
174
        }
175
    }
176
}
177

            
178
/// Manages vertical margin collapsing within a BFC.
179
#[derive(Debug, Default, Clone)]
180
pub struct MarginCollapseContext {
181
    /// The bottom margin of the last in-flow, block-level element.
182
    /// Can be positive or negative.
183
    pub last_in_flow_margin_bottom: f32,
184
}
185

            
186
/// The result of laying out a formatting context.
187
#[derive(Debug, Default, Clone)]
188
pub struct LayoutOutput {
189
    /// The final positions of child nodes, relative to the container's content-box origin.
190
    pub positions: BTreeMap<usize, LogicalPosition>,
191
    /// The total size occupied by the content, which may exceed `available_size`.
192
    pub overflow_size: LogicalSize,
193
    // +spec:inline-formatting-context:f7eebb - baseline along inline axis for glyph alignment
194
    /// The baseline of the context, if applicable, measured from the top of its content box.
195
    pub baseline: Option<f32>,
196
}
197

            
198
/// Text alignment options
199
#[derive(Debug, Clone, Copy, Default)]
200
pub enum TextAlign {
201
    #[default]
202
    Start,
203
    End,
204
    Center,
205
    Justify,
206
}
207

            
208
/// Represents a single floated element within a BFC.
209
#[derive(Debug, Clone, Copy)]
210
struct FloatBox {
211
    /// The type of float (Left or Right).
212
    kind: LayoutFloat,
213
    /// The rectangle of the float's content box (origin includes top/left margin offset).
214
    rect: LogicalRect,
215
    /// The margin sizes (needed to calculate true margin-box bounds).
216
    margin: EdgeSizes,
217
}
218

            
219
/// Manages the state of all floated elements within a Block Formatting Context.
220
// +spec:block-formatting-context:a4e6f9 - float rules reference only elements in the same BFC (scoped via BfcState)
221
// +spec:floats:2fa329 - Float positioning (left/right shift), content flow along sides, and clear property
222
/// +spec:floats:970b4c - Implements CSS2§9.5 float positioning and flow interaction
223
#[derive(Debug, Default, Clone)]
224
pub struct FloatingContext {
225
    /// All currently positioned floats within the BFC.
226
    pub floats: Vec<FloatBox>,
227
}
228

            
229
impl FloatingContext {
230
    /// Add a newly positioned float to the context
231
1960
    pub fn add_float(&mut self, kind: LayoutFloat, rect: LogicalRect, margin: EdgeSizes) {
232
1960
        self.floats.push(FloatBox { kind, rect, margin });
233
1960
    }
234

            
235
    // +spec:box-model:0c9b13 - line boxes next to floats are shortened to make room
236
    // +spec:floats:148fcd - floating boxes reduce available line box width between containing block edges
237
    // +spec:floats:49a491 - Line boxes stacked with no separation except float clearance, never overlap
238
    // +spec:floats:8974e6 - text flows into vacated space by narrowing line boxes around floats
239
    // +spec:floats:af94f2 - content displaced by float: line boxes shrink to avoid float margin boxes
240
    // +spec:floats:e5961b - remaining text flows into vacated space via available_line_box_space
241
    // +spec:inline-formatting-context:7cbe58 - shortened line boxes due to floats; shift down if too small
242
    /// Finds the available space on the cross-axis for a line box at a given main-axis range.
243
    // +spec:containing-block:4b0c44 - line boxes shortened by floats resume containing block width after float
244
    ///
245
    /// Returns a tuple of (`cross_start_offset`, `cross_end_offset`) relative to the
246
    /// BFC content box, defining the available space for an in-flow element.
247
    // +spec:inline-formatting-context:e70328 - line box width reduced by floats between containing block edges
248
1680
    pub fn available_line_box_space(
249
1680
        &self,
250
1680
        main_start: f32,
251
1680
        main_end: f32,
252
1680
        bfc_cross_size: f32,
253
1680
        wm: LayoutWritingMode,
254
1680
    ) -> (f32, f32) {
255
1680
        let mut available_cross_start = 0.0_f32;
256
1680
        let mut available_cross_end = bfc_cross_size;
257

            
258
2590
        for float in &self.floats {
259
            // Get the logical main-axis span of the existing float's MARGIN BOX.
260
910
            let float_main_start = float.rect.origin.main(wm) - float.margin.main_start(wm);
261
910
            let float_main_end = float_main_start + float.rect.size.main(wm)
262
910
                + float.margin.main_start(wm) + float.margin.main_end(wm);
263

            
264
            // Check for overlap on the main axis.
265
910
            if main_end > float_main_start && main_start < float_main_end {
266
                // CSS 2.2 § 9.5: border box must not overlap MARGIN BOX of floats,
267
                // so we include the float's margins in the cross-axis bounds.
268
805
                let float_cross_start = float.rect.origin.cross(wm) - float.margin.cross_start(wm);
269
805
                let float_cross_end = float_cross_start + float.rect.size.cross(wm)
270
805
                    + float.margin.cross_start(wm) + float.margin.cross_end(wm);
271

            
272
                // +spec:floats:17a63f - float left/right map to line-left/line-right via logical coords
273
                // +spec:writing-modes:e55820 - line-relative mappings: left/right interpreted as line-left/line-right per writing mode
274
805
                if float.kind == LayoutFloat::Left {
275
630
                    // "line-left", i.e., cross-start
276
630
                    available_cross_start = available_cross_start.max(float_cross_end);
277
630
                } else {
278
175
                    // Float::Right, i.e., cross-end
279
175
                    available_cross_end = available_cross_end.min(float_cross_start);
280
175
                }
281
105
            }
282
        }
283
1680
        (available_cross_start, available_cross_end)
284
1680
    }
285

            
286
    // +spec:block-formatting-context:d06e6e - clearance computation for clear property on blocks and floats (CSS 2.2 § 9.5.2)
287
    // +spec:floats:31a3d5 - Clearance computation: places border edge even with bottom outer edge of lowest float to be cleared
288
    // +spec:floats:f9bef1 - clear property moves element below preceding floats
289
    /// Returns the main-axis offset needed to be clear of floats of the given type.
290
    // +spec:block-formatting-context:7f6bde - CSS 2.2 § 9.5.2 clear property: clearance places border edge below bottom outer edge of cleared floats
291
    // +spec:block-formatting-context:ef493f - clearance computation: places border edge even with bottom outer edge of lowest float to be cleared; inhibits margin collapsing
292
    // +spec:box-model:b118fe - top border edge must be below bottom outer edge of earlier floats
293
    // +spec:floats:415066 - Clear property: top border edge below bottom outer edge of cleared floats
294
    // +spec:floats:7e4ad6 - clear property: element box may not be adjacent to earlier floats; only considers floats in same BFC
295
    // +spec:floats:32e45d - clear:right causes sibling to flow below right floats
296
    // +spec:floats:7f417a - clear property prevents content from flowing next to floats
297
    // +spec:floats:d06304 - clear property moves element below floats, leaving blank space
298
    // +spec:overflow:1a7aff - clearance calculation (incl. negative clearance) and clear on floats (constraint #10)
299
    // +spec:positioning:1c2508 - clearance calculation: places border edge even with bottom outer edge of lowest cleared float (CSS 2.2 § 9.5.2)
300
    // +spec:positioning:fe0912 - clearance computation: places border edge below bottom outer edge of cleared floats
301
    // (clearance = amount to place border edge even with bottom outer edge of lowest
302
    // float to be cleared); clearance can be negative per spec example 2
303
    // +spec:floats:054a1e - Clearance computation: positions border edge below bottom outer edge of cleared floats
304
    // +spec:floats:cb984c - Clearance can be negative per spec example 2; inhibits margin collapsing
305
525
    pub fn clearance_offset(
306
525
        &self,
307
525
        clear: LayoutClear,
308
525
        current_main_offset: f32,
309
525
        wm: LayoutWritingMode,
310
525
    ) -> f32 {
311
525
        let mut max_end_offset = 0.0_f32;
312

            
313
525
        let check_left = clear == LayoutClear::Left || clear == LayoutClear::Both;
314
525
        let check_right = clear == LayoutClear::Right || clear == LayoutClear::Both;
315

            
316
1155
        for float in &self.floats {
317
630
            let should_clear_this_float = (check_left && float.kind == LayoutFloat::Left)
318
315
                || (check_right && float.kind == LayoutFloat::Right);
319

            
320
630
            if should_clear_this_float {
321
490
                // CSS 2.2 § 9.5.2: "the top border edge of the box be below the bottom outer edge"
322
490
                // Outer edge = margin-box boundary (content + padding + border + margin)
323
490
                let float_margin_box_end = float.rect.origin.main(wm)
324
490
                    + float.rect.size.main(wm)
325
490
                    + float.margin.main_end(wm);
326
490
                max_end_offset = max_end_offset.max(float_margin_box_end);
327
490
            }
328
        }
329

            
330
525
        if max_end_offset > current_main_offset {
331
315
            max_end_offset
332
        } else {
333
210
            current_main_offset
334
        }
335
525
    }
336
}
337

            
338
/// Encapsulates all state needed to lay out a single Block Formatting Context.
339
struct BfcLayoutState {
340
    /// The current position for the next in-flow block element.
341
    pen: LogicalPosition,
342
    floats: FloatingContext,
343
    margins: MarginCollapseContext,
344
    /// The writing mode of the BFC root.
345
    writing_mode: LayoutWritingMode,
346
}
347

            
348
/// Result of a formatting context layout operation
349
#[derive(Debug, Default)]
350
pub struct LayoutResult {
351
    pub positions: Vec<(usize, LogicalPosition)>,
352
    pub overflow_size: Option<LogicalSize>,
353
    pub baseline_offset: f32,
354
}
355

            
356
// Entry Point & Dispatcher
357

            
358
/// Main dispatcher for formatting context layout.
359
///
360
/// Routes layout to the appropriate formatting context handler based on the node's
361
/// `formatting_context` property. This is the main entry point for all layout operations.
362
///
363
/// # CSS Spec References
364
/// - CSS 2.2 § 9.4: Formatting contexts
365
/// - CSS Flexbox § 3: Flex formatting contexts
366
/// - CSS Grid § 5: Grid formatting contexts
367
// +spec:block-formatting-context:b04653 - dispatches layout by formatting context type (BFC, IFC, Table, Flex, Grid)
368
// +spec:block-formatting-context:e46499 - inner display type determines formatting context (BFC, IFC, table, flex, grid)
369
29791
pub fn layout_formatting_context<T: ParsedFontTrait>(
370
29791
    ctx: &mut LayoutContext<'_, T>,
371
29791
    tree: &mut LayoutTree,
372
29791
    text_cache: &mut crate::font_traits::TextLayoutCache,
373
29791
    node_index: usize,
374
29791
    constraints: &LayoutConstraints,
375
29791
    float_cache: &mut HashMap<usize, FloatingContext>,
376
29791
) -> Result<BfcLayoutResult> {
377
29791
    let node = tree.get(node_index).ok_or(LayoutError::InvalidTree)?;
378

            
379
29791
    debug_info!(
380
29021
        ctx,
381
29021
        "[layout_formatting_context] node_index={}, fc={:?}, available_size={:?}",
382
        node_index,
383
        node.formatting_context,
384
        constraints.available_size
385
    );
386

            
387
    // +spec:block-formatting-context:06a24f - CSS 2.2 § 9.4: block-level boxes → BFC, inline-level → IFC
388
    // +spec:block-formatting-context:9428cf - block container can establish both BFC and IFC simultaneously
389
    // +spec:inline-formatting-context:8bfe73 - display:flow generates inline box (Inline) or block container (Block) based on outer display type
390
29791
    match node.formatting_context {
391
        FormattingContext::Block { .. } => {
392
3044
            layout_bfc(ctx, tree, text_cache, node_index, constraints, float_cache)
393
        }
394
        // +spec:inline-formatting-context:a180ed - IFC establishment: inline-level boxes fragmented into line boxes with baseline alignment
395
17260
        FormattingContext::Inline => layout_ifc(ctx, text_cache, tree, node_index, constraints)
396
17260
            .map(BfcLayoutResult::from_output),
397
        FormattingContext::InlineBlock => {
398
            // +spec:display-property:1f5ddf - inline-level boxes with non-flow inner display establish new formatting context
399
            // +spec:inline-formatting-context:1ad004 - atomic inline (inline-block) establishes new formatting context
400
            // CSS 2.2 § 9.4.1: "inline-blocks... establish new block formatting contexts"
401
            // +spec:inline-block:8d21f6 - inline-block generates inline-level block container (BFC inside, atomic inline outside)
402
            // InlineBlock ALWAYS establishes a BFC for its contents.
403
            // The element itself participates as an atomic inline in its parent's IFC,
404
            // but its children are laid out in a BFC, not an IFC.
405
140
            let mut temp_float_cache = HashMap::new();
406
140
            layout_bfc(ctx, tree, text_cache, node_index, constraints, &mut temp_float_cache)
407
        }
408
        // +spec:table-layout:753687 - CSS 2.2 §17.2 table model: display values map to FormattingContext variants and dispatch table layout
409
1155
        FormattingContext::Table => layout_table_fc(ctx, tree, text_cache, node_index, constraints)
410
1155
            .map(BfcLayoutResult::from_output),
411
        // Table-internal flex items are blockified during tree construction
412
        // (blockify_flex_item_if_table_internal in layout_tree.rs), so they arrive
413
        // here as Block, not TableCell etc.
414
        FormattingContext::Flex | FormattingContext::Grid => {
415
212
            layout_flex_grid(ctx, tree, text_cache, node_index, constraints)
416
        }
417
        // that are not block boxes, so they establish new BFCs for their contents
418
        FormattingContext::TableCell | FormattingContext::TableCaption => {
419
7980
            let mut temp_float_cache = HashMap::new();
420
7980
            layout_bfc(ctx, tree, text_cache, node_index, constraints, &mut temp_float_cache)
421
        }
422
        _ => {
423
            // Unknown formatting context - fall back to BFC
424
            let mut temp_float_cache = HashMap::new();
425
            layout_bfc(
426
                ctx,
427
                tree,
428
                text_cache,
429
                node_index,
430
                constraints,
431
                &mut temp_float_cache,
432
            )
433
        }
434
    }
435
29791
}
436

            
437
// Flex / grid layout (taffy Bridge)
438
// containing block determined by grid-placement properties; Taffy handles this internally
439
// (grid auto-placement §8.5 and abspos grid items use grid-area CB, not just padding box)
440

            
441
/// Lays out a Flex or Grid formatting context using the Taffy layout engine.
442
///
443
/// # CSS Spec References
444
///
445
/// - CSS Flexbox § 9: Flex Layout Algorithm
446
/// - CSS Grid § 12: Grid Layout Algorithm
447
// gutters on either side of collapsed tracks collapse including distributed alignment space,
448
// minimum contribution = outer size from min-width/min-height if specified size is auto else
449
// min-content contribution) — all handled by Taffy grid implementation
450
///
451
/// # Implementation Notes
452
///
453
/// - Resolves explicit CSS dimensions to pixel values for `known_dimensions`
454
/// - Uses `InherentSize` mode when explicit dimensions are set
455
/// - Uses `ContentSize` mode for auto-sizing (shrink-to-fit)
456
212
fn layout_flex_grid<T: ParsedFontTrait>(
457
212
    ctx: &mut LayoutContext<'_, T>,
458
212
    tree: &mut LayoutTree,
459
212
    text_cache: &mut crate::font_traits::TextLayoutCache,
460
212
    node_index: usize,
461
212
    constraints: &LayoutConstraints,
462
212
) -> Result<BfcLayoutResult> {
463
    // Available space comes directly from constraints - margins are handled by Taffy
464
212
    let available_space = TaffySize {
465
212
        width: AvailableSpace::Definite(constraints.available_size.width),
466
212
        height: AvailableSpace::Definite(constraints.available_size.height),
467
212
    };
468

            
469
212
    let node = tree.get(node_index).ok_or(LayoutError::InvalidTree)?;
470

            
471
    // from flex line's cross size (clamped by min/max) when align-self:stretch, cross-size:auto,
472
    // and neither cross-axis margin is auto. Otherwise uses hypothetical cross size.
473
    // NOTE: visibility:collapse strut size for flex items is handled internally by Taffy.
474
    //
475
    // Resolve explicit CSS dimensions to pixel values.
476
    // This is CRITICAL for align-items: stretch to work correctly!
477
    // Taffy uses known_dimensions to calculate cross_axis_available_space for children.
478
212
    let (explicit_width, has_explicit_width) =
479
212
        resolve_explicit_dimension_width(ctx, node, constraints);
480
212
    let (explicit_height, has_explicit_height) =
481
212
        resolve_explicit_dimension_height(ctx, node, constraints);
482

            
483
    // FIX: For root nodes or nodes where the parent provides a definite size,
484
    // use the available_size as known_dimensions if no explicit CSS width/height is set.
485
    // This is critical for `align-self: stretch` to work - Taffy needs to know the
486
    // cross-axis size of the container to stretch children to fill it.
487
212
    let is_root = node.parent.is_none();
488

            
489
212
    let bp = node.box_props.unpack();
490
212
    let width_adjustment = bp.border.left
491
212
        + bp.border.right
492
212
        + bp.padding.left
493
212
        + bp.padding.right;
494
212
    let height_adjustment = bp.border.top
495
212
        + bp.border.bottom
496
212
        + bp.padding.top
497
212
        + bp.padding.bottom;
498

            
499
    // `constraints.available_size` is the root's CONTENT-BOX (produced by
500
    // `prepare_layout_context::inner_size(final_used_size)`), not the viewport
501
    // border-box. Previously, the code used it as if it were border-box,
502
    // causing taffy to subtract padding a second time and shrink the content
503
    // area by 2x padding. For the root, pull the actual border-box from
504
    // `node.used_size` (set by `calculate_used_size_for_node` before this call).
505
212
    let root_border_box = node.used_size;
506

            
507
212
    let effective_width = if has_explicit_width {
508
70
        explicit_width
509
142
    } else if is_root {
510
140
        root_border_box.as_ref().map(|s| s.width).or_else(|| {
511
            if constraints.available_size.width.is_finite() {
512
                // Fallback: convert content-box to border-box.
513
                Some(constraints.available_size.width + width_adjustment)
514
            } else {
515
                None
516
            }
517
        })
518
    } else {
519
2
        None
520
    };
521
212
    let effective_height = if has_explicit_height {
522
210
        explicit_height
523
2
    } else if is_root {
524
        root_border_box.as_ref().map(|s| s.height).or_else(|| {
525
            if constraints.available_size.height.is_finite() {
526
                Some(constraints.available_size.height + height_adjustment)
527
            } else {
528
                None
529
            }
530
        })
531
    } else {
532
2
        None
533
    };
534
212
    let has_effective_width = effective_width.is_some();
535
212
    let has_effective_height = effective_height.is_some();
536

            
537
    // Taffy interprets known_dimensions as border-box. CSS width/height default
538
    // to content-box, so explicit values need +padding+border added. For the
539
    // ROOT element, however, we auto-apply box-sizing: border-box — the common
540
    // CSS reset pattern — so `height:100%` + padding fits the viewport instead
541
    // of overflowing by padding (which the default content-box interpretation
542
    // would produce, since 100% of ICB is viewport-sized content, with padding
543
    // added outside pushing border-box past the viewport).
544
212
    let adjusted_width = if has_explicit_width && !is_root {
545
        explicit_width.map(|w| w + width_adjustment)
546
212
    } else if has_explicit_width && is_root {
547
70
        explicit_width
548
    } else {
549
142
        effective_width
550
    };
551
212
    let adjusted_height = if has_explicit_height && !is_root {
552
        explicit_height.map(|h| h + height_adjustment)
553
212
    } else if has_explicit_height && is_root {
554
210
        explicit_height
555
    } else {
556
2
        effective_height
557
    };
558

            
559
    // CSS Flexbox § 9.2: Use InherentSize when explicit dimensions are set,
560
    // ContentSize for auto-sizing (shrink-to-fit behavior).
561
212
    let sizing_mode = if has_effective_width || has_effective_height {
562
210
        taffy::SizingMode::InherentSize
563
    } else {
564
2
        taffy::SizingMode::ContentSize
565
    };
566

            
567
212
    let known_dimensions = TaffySize {
568
212
        width: adjusted_width,
569
212
        height: adjusted_height,
570
212
    };
571

            
572
    // parent_size tells Taffy the size of the container's parent.
573
    // For root nodes, the "parent" is the viewport, but since margins are already
574
    // handled by calculate_used_size_for_node(), we use containing_block_size directly.
575
    // For non-root nodes, containing_block_size is already the parent's content-box.
576
212
    let parent_size = translate_taffy_size(constraints.containing_block_size);
577

            
578
212
    let taffy_inputs = LayoutInput {
579
212
        known_dimensions,
580
212
        parent_size,
581
212
        available_space,
582
212
        run_mode: taffy::RunMode::PerformLayout,
583
212
        sizing_mode,
584
212
        axis: taffy::RequestedAxis::Both,
585
212
        // Flex and Grid containers establish a new BFC, preventing margin collapse.
586
212
        vertical_margins_are_collapsible: Line::FALSE,
587
212
    };
588

            
589
212
    debug_info!(
590
142
        ctx,
591
142
        "CALLING LAYOUT_TAFFY FOR FLEX/GRID FC node_index={:?}",
592
        node_index
593
    );
594

            
595
    // For the root with auto-applied border-box: sync node.used_size so
596
    // display-list rendering matches the border-box we handed taffy.
597
    // Without this, the root's background/border would paint at the
598
    // inflated size from calculate_used_size_for_node while taffy placed
599
    // children inside a smaller content-box.
600
212
    if is_root {
601
210
        if let (Some(aw), Some(ah)) = (adjusted_width, adjusted_height) {
602
210
            if let Some(node_mut) = tree.get_mut(node_index) {
603
210
                node_mut.used_size = Some(LogicalSize::new(aw, ah));
604
210
            }
605
        }
606
2
    }
607

            
608
    // Cache border values before the mutable borrow in layout_taffy_subtree
609
212
    let border_left = bp.border.left;
610
212
    let border_top = bp.border.top;
611

            
612
212
    let taffy_output =
613
212
        taffy_bridge::layout_taffy_subtree(ctx, tree, text_cache, node_index, taffy_inputs);
614

            
615
    // Collect child positions from the tree (Taffy stores results directly on nodes).
616
212
    let mut output = LayoutOutput::default();
617
    // Use content_size for overflow detection, not container size.
618
    // content_size represents the actual size of all children, which may exceed the container.
619
    //
620
    // Taffy's content_size is measured from (0,0) of the border-box, so it includes
621
    // border.top/left as a leading offset.  The scrollbar geometry and scroll clamp
622
    // both measure inside the padding-box (border stripped).  Subtract the start
623
    // border so that overflow_size is in the same coordinate space as the viewport
624
    // (padding-box), preventing extra scroll range equal to the border width.
625
212
    let raw = translate_taffy_size_back(taffy_output.content_size);
626
212
    output.overflow_size = LogicalSize::new(
627
212
        (raw.width - border_left).max(0.0),
628
212
        (raw.height - border_top).max(0.0),
629
212
    );
630

            
631
212
    let children: Vec<usize> = tree.children(node_index).to_vec();
632
460
    for &child_idx in &children {
633
248
        if let Some(warm_node) = tree.warm(child_idx) {
634
248
            if let Some(pos) = warm_node.relative_position {
635
248
                output.positions.insert(child_idx, pos);
636
248
            }
637
        }
638
    }
639

            
640
212
    Ok(BfcLayoutResult::from_output(output))
641
212
}
642

            
643
/// Resolves explicit CSS width to pixel value for Taffy layout.
644
212
fn resolve_explicit_dimension_width<T: ParsedFontTrait>(
645
212
    ctx: &LayoutContext<'_, T>,
646
212
    node: &LayoutNodeHot,
647
212
    constraints: &LayoutConstraints,
648
212
) -> (Option<f32>, bool) {
649
212
    node.dom_node_id
650
212
        .map(|id| {
651
212
            let width = get_css_width(
652
212
                ctx.styled_dom,
653
212
                id,
654
212
                &ctx.styled_dom.styled_nodes.as_container()[id].styled_node_state,
655
            );
656
212
            match width.unwrap_or_default() {
657
142
                LayoutWidth::Auto => (None, false),
658
70
                LayoutWidth::Px(px) => {
659
70
                    let pixels = resolve_size_metric(
660
70
                        px.metric,
661
70
                        px.number.get(),
662
70
                        constraints.available_size.width,
663
70
                        ctx.viewport_size,
664
                    );
665
70
                    (Some(pixels), true)
666
                }
667
                LayoutWidth::MinContent | LayoutWidth::MaxContent | LayoutWidth::FitContent(_) => (None, false),
668
                LayoutWidth::Calc(items) => {
669
                    let node_state = &ctx.styled_dom.styled_nodes.as_container()[id].styled_node_state;
670
                    let em = get_element_font_size(ctx.styled_dom, id, node_state);
671
                    let calc_ctx = super::calc::CalcResolveContext {
672
                        items, em_size: em, rem_size: DEFAULT_FONT_SIZE,
673
                    };
674
                    let px = super::calc::evaluate_calc(&calc_ctx, constraints.available_size.width);
675
                    (Some(px), true)
676
                }
677
            }
678
212
        })
679
212
        .unwrap_or((None, false))
680
212
}
681

            
682
/// Resolves explicit CSS height to pixel value for Taffy layout.
683
212
fn resolve_explicit_dimension_height<T: ParsedFontTrait>(
684
212
    ctx: &LayoutContext<'_, T>,
685
212
    node: &LayoutNodeHot,
686
212
    constraints: &LayoutConstraints,
687
212
) -> (Option<f32>, bool) {
688
212
    node.dom_node_id
689
212
        .map(|id| {
690
212
            let height = get_css_height(
691
212
                ctx.styled_dom,
692
212
                id,
693
212
                &ctx.styled_dom.styled_nodes.as_container()[id].styled_node_state,
694
            );
695
212
            match height.unwrap_or_default() {
696
2
                LayoutHeight::Auto => (None, false),
697
210
                LayoutHeight::Px(px) => {
698
210
                    let pixels = resolve_size_metric(
699
210
                        px.metric,
700
210
                        px.number.get(),
701
210
                        constraints.available_size.height,
702
210
                        ctx.viewport_size,
703
                    );
704
210
                    (Some(pixels), true)
705
                }
706
                LayoutHeight::MinContent | LayoutHeight::MaxContent | LayoutHeight::FitContent(_) => (None, false),
707
                LayoutHeight::Calc(items) => {
708
                    let node_state = &ctx.styled_dom.styled_nodes.as_container()[id].styled_node_state;
709
                    let em = get_element_font_size(ctx.styled_dom, id, node_state);
710
                    let calc_ctx = super::calc::CalcResolveContext {
711
                        items, em_size: em, rem_size: DEFAULT_FONT_SIZE,
712
                    };
713
                    let px = super::calc::evaluate_calc(&calc_ctx, constraints.available_size.height);
714
                    (Some(px), true)
715
                }
716
            }
717
212
        })
718
212
        .unwrap_or((None, false))
719
212
}
720

            
721
// +spec:floats:167a2c - Float positioning rules (CSS 2.2 § 9.5.1): left/right/none, precise placement constraints
722
// +spec:floats:6a1769 - Float shortens line boxes, margins never collapse, stacking order
723
// +spec:floats:15bfd9 - float:right positions element at line-right edge within BFC
724
// +spec:floats:afc8e2 - Float positioning rules (CSS 2.2 § 9.5 rules 1-8): left/right edge containment, earlier-float stacking, outer-top constraints, and "move down" when insufficient space
725
/// Position a float within a BFC, considering existing floats.
726
/// Returns the LogicalRect (margin box) for the float.
727
// +spec:box-model:db0f02 - Float positioning: line boxes shortened by floats, floats shift down if no space, BFC elements must not overlap float margin boxes
728
// +spec:containing-block:136e45 - Float shifted left/right until outer edge touches containing block edge or another float
729
// +spec:containing-block:3ebb4e - Content moves below floats when containing block too narrow
730
// +spec:floats:45fce7 - Float positioning: pulled out of flow, line boxes shortened around float
731
// +spec:floats:f6c218 - float pulled out of flow, line boxes shorten around it
732
// +spec:height-calculation:86142a - CSS 2.2 §9.5 float positioning, clearance, and margin non-collapsing
733
// +spec:width-calculation:761677 - float positioning: content flows around floats, line boxes shortened by float presence
734
910
fn position_float(
735
910
    float_ctx: &FloatingContext,
736
910
    float_type: LayoutFloat,
737
910
    size: LogicalSize,
738
910
    margin: &EdgeSizes,
739
910
    current_main_offset: f32,
740
910
    bfc_cross_size: f32,
741
910
    wm: LayoutWritingMode,
742
910
) -> LogicalRect {
743
    // Start at the current main-axis position (Y in horizontal-tb)
744
910
    let mut main_start = current_main_offset;
745

            
746
    // Calculate total size including margins
747
910
    let total_main = size.main(wm) + margin.main_start(wm) + margin.main_end(wm);
748
910
    let total_cross = size.cross(wm) + margin.cross_start(wm) + margin.cross_end(wm);
749

            
750
    // +spec:floats:3d89d8 - shift float downward when not enough horizontal room
751
    // Find a position where the float fits
752
910
    let cross_start = loop {
753
945
        let (avail_start, avail_end) = float_ctx.available_line_box_space(
754
945
            main_start,
755
945
            main_start + total_main,
756
945
            bfc_cross_size,
757
945
            wm,
758
945
        );
759

            
760
945
        let available_width = avail_end - avail_start;
761

            
762
945
        if available_width >= total_cross {
763
            // +spec:floats:449158 - left float positioned at line-left, content flows on right
764
            // Found space that fits
765
910
            if float_type == LayoutFloat::Left {
766
                // +spec:writing-modes:84bcba - floats positioned at line-left / line-right
767
                // Position at line-left (avail_start)
768
630
                break avail_start + margin.cross_start(wm);
769
            } else {
770
                // Position at line-right (avail_end - size)
771
280
                break avail_end - total_cross + margin.cross_start(wm);
772
            }
773
35
        }
774

            
775
        // top is moved lower than earlier float's bottom (outer edge / margin box bottom)
776
        // Not enough space at this Y, move down past the lowest overlapping float's margin box bottom
777
35
        let next_main = float_ctx
778
35
            .floats
779
35
            .iter()
780
35
            .filter(|f| {
781
35
                let f_main_start = f.rect.origin.main(wm) - f.margin.main_start(wm);
782
35
                let f_main_end = f_main_start + f.rect.size.main(wm)
783
35
                    + f.margin.main_start(wm) + f.margin.main_end(wm);
784
35
                f_main_end > main_start && f_main_start < main_start + total_main
785
35
            })
786
35
            .map(|f| f.rect.origin.main(wm) + f.rect.size.main(wm) + f.margin.main_end(wm))
787
35
            .max_by(|a, b| a.partial_cmp(b).unwrap_or(std::cmp::Ordering::Equal));
788

            
789
35
        if let Some(next) = next_main {
790
35
            main_start = next;
791
35
        } else {
792
            // No overlapping floats found, use current position anyway
793
            if float_type == LayoutFloat::Left {
794
                break avail_start + margin.cross_start(wm);
795
            } else {
796
                break avail_end - total_cross + margin.cross_start(wm);
797
            }
798
        }
799
    };
800

            
801
910
    LogicalRect {
802
910
        origin: LogicalPosition::from_main_cross(
803
910
            main_start + margin.main_start(wm),
804
910
            cross_start,
805
910
            wm,
806
910
        ),
807
910
        size,
808
910
    }
809
910
}
810

            
811
// Block Formatting Context (CSS 2.2 § 9.4.1)
812

            
813
/// Lays out a Block Formatting Context (BFC).
814
///
815
/// This is the corrected, architecturally-sound implementation. It solves the
816
/// "chicken-and-egg" problem by performing its own two-pass layout:
817
///
818
/// 1. **Sizing Pass:** It first iterates through its children and triggers their layout recursively
819
///    by calling `calculate_layout_for_subtree`. This ensures that the `used_size` property of each
820
///    child is correctly populated.
821
///
822
/// 2. **Positioning Pass:** It then iterates through the children again. Now that each child has a
823
///    valid size, it can apply the standard block-flow logic: stacking them vertically and
824
///    advancing a "pen" by each child's outer height.
825
///
826
/// # Margin Collapsing Architecture
827
///
828
/// CSS 2.1 Section 8.3.1 compliant margin collapsing:
829
///
830
/// ```text
831
/// layout_bfc()
832
///   ├─ Check parent border/padding blockers
833
///   ├─ For each child:
834
///   │   ├─ Check child border/padding blockers
835
///   │   ├─ is_first_child?
836
///   │   │   └─ Check parent-child top collapse
837
///   │   ├─ Sibling collapse?
838
///   │   │   └─ advance_pen_with_margin_collapse()
839
///   │   │       └─ collapse_margins(prev_bottom, curr_top)
840
///   │   ├─ Position child
841
///   │   ├─ is_empty_block()?
842
///   │   │   └─ Collapse own top+bottom margins (collapse through)
843
///   │   └─ Save bottom margin for next sibling
844
///   └─ Check parent-child bottom collapse
845
/// ```
846
///
847
/// **Collapsing Rules:**
848
///
849
/// - Sibling margins: Adjacent vertical margins collapse to max (or sum if mixed signs)
850
/// - Parent-child: First child's top margin can escape parent (if no border/padding)
851
/// - Parent-child: Last child's bottom margin can escape parent (if no border/padding/height)
852
/// - Empty blocks: Top+bottom margins collapse with each other, then with siblings
853
/// - Blockers: Border, padding, inline content, or new BFC prevents collapsing
854
///
855
/// This approach is compliant with the CSS visual formatting model and works within
856
/// the constraints of the existing layout engine architecture.
857
// +spec:display-property:f38f52 - BFC handles normal flow, relative positioning offsets, and float extraction (CSS 2.2 § 9.8)
858
11164
fn layout_bfc<T: ParsedFontTrait>(
859
11164
    ctx: &mut LayoutContext<'_, T>,
860
11164
    tree: &mut LayoutTree,
861
11164
    text_cache: &mut crate::font_traits::TextLayoutCache,
862
11164
    node_index: usize,
863
11164
    constraints: &LayoutConstraints,
864
11164
    float_cache: &mut HashMap<usize, FloatingContext>,
865
11164
) -> Result<BfcLayoutResult> {
866
11164
    let node = tree
867
11164
        .get(node_index)
868
11164
        .ok_or(LayoutError::InvalidTree)?
869
11164
        .clone();
870
    // +spec:block-formatting-context:4f4ff6 - writing-mode determines block flow direction (main axis) for ordering block-level boxes in BFC
871
11164
    let writing_mode = constraints.writing_mode;
872
11164
    let mut output = LayoutOutput::default();
873

            
874
11164
    debug_info!(
875
10464
        ctx,
876
10464
        "\n[layout_bfc] ENTERED for node_index={}, children.len()={}, incoming_bfc_state={}",
877
        node_index,
878
10464
        tree.children(node_index).len(),
879
10464
        constraints.bfc_state.is_some()
880
    );
881

            
882
    // Initialize FloatingContext for this BFC
883
    //
884
    // We always recalculate float positions in this pass, but we'll store them in the cache
885
    // so that subsequent layout passes (for auto-sizing) have access to the positioned floats
886
11164
    let mut float_context = FloatingContext::default();
887

            
888
    // +spec:containing-block:42b75f - Block element establishes containing block for inline content (IFC)
889
    // Calculate this node's content-box size for use as containing block for children
890
    // CSS 2.2 § 10.1: The containing block for in-flow children is formed by the
891
    // content edge of the parent's content box.
892
    //
893
    // We use constraints.available_size directly as this already represents the
894
    // content-box available to this node (set by parent). For nodes with explicit
895
    // sizes, used_size contains the border-box which we convert to content-box.
896
    //
897
    // NOTE(writing-modes): The containing block size uses physical width/height.
898
    // In vertical writing modes, the block progression direction is horizontal,
899
    // so the "available width" for children maps to the physical height of
900
    // the containing block. The main_pen variable below tracks block progression
901
    // using logical main-axis coordinates; the WritingModeContext in constraints
902
    // determines how main/cross map to physical x/y via from_main_cross().
903
    // +spec:inline-block:17944a - orthogonal flow roots get infinite available inline space here (not yet detected)
904
    // +spec:inline-block:a60e22 - other layout models pass through infinite inline space to contained block containers
905
11164
    let mut children_containing_block_size = if let Some(used_size) = node.used_size {
906
        // Node has used_size (border-box) - convert to content-box.
907
        // For auto-height containers, the pre-layout `used_size.height` is a
908
        // placeholder (calculate_used_size_for_node returns 0 for block-level
909
        // auto-height; apply_content_based_height resolves it after children lay out).
910
        // In that window, `constraints.available_size.height` holds the containing
911
        // block's height — the value children should use as their own containing
912
        // block for percentage-height resolution and indefinite-height semantics.
913
7873
        let inner = node.box_props.inner_size(used_size, writing_mode);
914
7873
        let height_is_auto = tree
915
7873
            .warm(node_index)
916
7873
            .map(|w| w.computed_style.height.is_none())
917
7873
            .unwrap_or(true);
918
7873
        if height_is_auto {
919
5656
            LogicalSize::new(inner.width, constraints.available_size.height)
920
        } else {
921
2217
            inner
922
        }
923
    } else {
924
        // No used_size yet - use available_size directly (this is already content-box
925
        // when coming from parent's layout constraints)
926
3291
        constraints.available_size
927
    };
928

            
929
    // +spec:overflow:ffe6f7 - scrollbar space subtracted from containing block per spec §11.1.1
930
    // Reserve space for vertical scrollbar when appropriate.
931
    //
932
    // - overflow: scroll  → ALWAYS reserve (CSS spec: scrollbar always shown)
933
    // - overflow: auto    → Reserve ONLY when a previous pass / the anti-jitter
934
    //   merge (`merge_scrollbar_info`) already determined a scrollbar is needed.
935
    //   On the very first pass the node has no scrollbar_info yet, so no space
936
    //   is reserved.  After `compute_scrollbar_info` detects overflow it sets
937
    //   `reflow_needed_for_scrollbars = true`, triggering a second pass where
938
    //   `node.scrollbar_info.needs_vertical == true` and space IS reserved.
939
    //   The merge uses `||` (keep once detected), preventing cross-frame jitter.
940
11164
    let scrollbar_reservation = node
941
11164
        .dom_node_id
942
11164
        .map(|dom_id| {
943
11160
            let styled_node_state = ctx
944
11160
                .styled_dom
945
11160
                .styled_nodes
946
11160
                .as_container()
947
11160
                .get(dom_id)
948
11160
                .map(|s| s.styled_node_state.clone())
949
11160
                .unwrap_or_default();
950
11160
            let overflow_y =
951
11160
                crate::solver3::getters::get_overflow_y(ctx.styled_dom, dom_id, &styled_node_state);
952
            use azul_css::props::layout::LayoutOverflow;
953
11160
            match overflow_y.unwrap_or_default() {
954
                LayoutOverflow::Scroll => {
955
                    crate::solver3::getters::get_layout_scrollbar_width_px(ctx, dom_id, &styled_node_state)
956
                }
957
                LayoutOverflow::Auto => {
958
                    let already_needs = tree.warm(node_index)
959
                        .and_then(|w| w.scrollbar_info.as_ref())
960
                        .map(|s| s.needs_vertical)
961
                        .unwrap_or(false);
962
                    if already_needs {
963
                        crate::solver3::getters::get_layout_scrollbar_width_px(ctx, dom_id, &styled_node_state)
964
                    } else {
965
                        0.0
966
                    }
967
                }
968
11160
                _ => 0.0,
969
            }
970
11160
        })
971
11164
        .unwrap_or(0.0);
972

            
973
11164
    if scrollbar_reservation > 0.0 {
974
        children_containing_block_size.width =
975
            (children_containing_block_size.width - scrollbar_reservation).max(0.0);
976
11164
    }
977

            
978
    // === Pass 1: Pre-compute child sizes (restored two-pass BFC) ===
979
    //
980
    // Inspired by Taffy's two-pass approach: first measure, then position.
981
    //
982
    // This was removed in commit 1a3e5850 and replaced with a single-pass approach
983
    // that computed sizes just-in-time during positioning. The single-pass approach
984
    // caused regression 8e092a2e because positioning decisions (margin collapsing,
985
    // float clearance, available width after floats) depend on knowing ALL sibling
986
    // sizes upfront, not just the ones visited so far.
987
    //
988
    // With the per-node cache (§9.1-§9.2), the re-added Pass 1 is efficient:
989
    // - Each child subtree is computed once and stored in NodeCache
990
    // - Pass 2 positioning reads sizes from tree nodes (used_size set by Pass 1)
991
    // - When calculate_layout_for_subtree recurses into children after layout_bfc
992
    //   returns, it hits the per-node cache (same available_size) — O(1) per child.
993
    //
994
    // Performance: O(n) for the tree. No double-computation thanks to caching.
995
    {
996
11164
        let mut temp_positions: super::PositionVec = Vec::new();
997
11164
        let mut temp_scrollbar_reflow = false;
998

            
999
11164
        let bfc_children = tree.children(node_index).to_vec();
20799
        for &child_index in &bfc_children {
9635
            let child_node = tree.get(child_index).ok_or(LayoutError::InvalidTree)?;
9635
            let child_dom_id = child_node.dom_node_id;
            // +spec:positioning:447b06 - Absolute positioning pulls element out of flow, skip from normal layout
            // +spec:positioning:77a2d2 - Absolutely positioned children are ignored for auto height
            // +spec:positioning:b47ac2 - Only normal flow children taken into account for auto height
            // Skip absolutely/fixed positioned children — they're laid out separately
            // +spec:positioning:c7e5c5 - out-of-flow elements ignored for word boundary / hyphenation
            // +spec:positioning:7dd6d1 - Absolutely positioned boxes are taken out of the normal flow (no impact on later siblings, no margin collapsing)
9635
            let position_type = get_position_type(ctx.styled_dom, child_dom_id);
9635
            if position_type == LayoutPosition::Absolute || position_type == LayoutPosition::Fixed {
35
                continue;
9600
            }
            // Compute the child's full subtree layout with temporary positions.
            // Position (0,0) is intentionally wrong — Pass 1 only cares about sizing.
            // The correct positions are determined in Pass 2 below.
9600
            crate::solver3::cache::calculate_layout_for_subtree(
9600
                ctx,
9600
                tree,
9600
                text_cache,
9600
                child_index,
9600
                LogicalPosition::zero(),
9600
                children_containing_block_size,
9600
                &mut temp_positions,
9600
                &mut temp_scrollbar_reflow,
9600
                float_cache,
9600
                crate::solver3::cache::ComputeMode::ComputeSize,
            )?;
        }
    }
    // +spec:block-formatting-context:98b633 - CSS 2.2 § 9.4.1: boxes laid out vertically, margins collapse
    // === Pass 2: Position children using known sizes ===
    //
    // All children now have used_size set from Pass 1. This pass handles:
    // - Margin collapsing (parent-child + sibling-sibling)
    // - Float positioning and clearance
    // - Normal flow block positioning
11164
    let mut main_pen = 0.0f32;
11164
    let mut max_cross_size = 0.0f32;
    // Track escaped margins separately from content-box height
    // CSS 2.2 § 8.3.1: Escaped margins don't contribute to parent's content-box height,
    // but DO affect sibling positioning within the parent
11164
    let mut total_escaped_top_margin = 0.0f32;
    // Track all inter-sibling margins (collapsed) - these are also not part of content height
11164
    let mut total_sibling_margins = 0.0f32;
    // Margin collapsing state
11164
    let mut last_margin_bottom = 0.0f32;
11164
    let mut is_first_child = true;
11164
    let mut first_child_index: Option<usize> = None;
11164
    let mut last_child_index: Option<usize> = None;
    // Parent's own margins (for escape calculation)
11164
    let node_bp = node.box_props.unpack();
11164
    let parent_margin_top = node_bp.margin.main_start(writing_mode);
11164
    let parent_margin_bottom = node_bp.margin.main_end(writing_mode);
    // margins do not collapse across formatting context boundaries: an independent
    // BFC (float, overflow != visible, display: flex/grid, etc.) isolates its
    // children's margins. The DOM root is NOT a BFC boundary for this purpose —
    // its first child's margin still collapses through it (then gets absorbed at
    // the root, since there's no grandparent to escape to).
11164
    let establishes_own_bfc = establishes_new_bfc(ctx, &node, tree.cold(node_index));
11164
    let is_bfc_root = node.parent.is_none() || establishes_own_bfc;
    // parent_has_*_blocker inhibits parent-child margin collapse per CSS 2.2 §8.3.1.
    // An explicit border/padding blocks, and an independent BFC blocks, but the
    // root on its own does not.
11164
    let parent_has_top_blocker = establishes_own_bfc
2983
        || has_margin_collapse_blocker(&node_bp, writing_mode, true);
11164
    let parent_has_bottom_blocker = establishes_own_bfc
2983
        || has_margin_collapse_blocker(&node_bp, writing_mode, false);
    // Track accumulated top margin for first-child escape
11164
    let mut accumulated_top_margin = 0.0f32;
11164
    let mut top_margin_resolved = false;
    // Track if first child's margin escaped (for return value)
11164
    let mut top_margin_escaped = false;
    // Track if we have any actual content (non-empty blocks)
11164
    let mut has_content = false;
    // +spec:display-property:9f6e18 - BFC dispatches normal flow, floats, and relative positioning (CSS 2.2 §9.8)
11164
    let pos_children = tree.children(node_index).to_vec();
20799
    for &child_index in &pos_children {
9635
        let child_node = tree.get(child_index).ok_or(LayoutError::InvalidTree)?;
9635
        let child_dom_id = child_node.dom_node_id;
        // +spec:floats:2cec1b - 'position' and 'float' determine the positioning algorithm
        // +spec:positioning:dccad6 - floats only apply to non-absolutely-positioned boxes
9635
        let position_type = get_position_type(ctx.styled_dom, child_dom_id);
9635
        if position_type == LayoutPosition::Absolute || position_type == LayoutPosition::Fixed {
35
            continue;
9600
        }
        // +spec:floats:2cec1b - float property determines positioning algorithm (float path)
        // +spec:floats:f6c0b2 - floats only processed in BFC; other formatting contexts (flex/grid) inhibit floating
        // Check if this child is a float - if so, position it at current main_pen
9600
        let is_float = if let Some(node_id) = child_dom_id {
9561
            let float_type = get_float_property(ctx.styled_dom, Some(node_id));
9561
            if float_type != LayoutFloat::None {
                // Calculate float size just-in-time if not already computed
26
                let float_size = match child_node.used_size {
26
                    Some(size) => size,
                    None => {
                        let intrinsic = tree.warm(child_index).and_then(|w| w.intrinsic_sizes).unwrap_or_default();
                        let child_bp = child_node.box_props.unpack();
                        let computed_size = crate::solver3::sizing::calculate_used_size_for_node(
                            ctx.styled_dom,
                            child_dom_id,
                            &children_containing_block_size,
                            intrinsic,
                            &child_bp,
                            &ctx.viewport_size,
                        )?;
                        if let Some(node_mut) = tree.get_mut(child_index) {
                            node_mut.used_size = Some(computed_size);
                        }
                        computed_size
                    }
                };
                // Re-borrow after potential mutation
26
                let child_node = tree.get(child_index).ok_or(LayoutError::InvalidTree)?;
26
                let child_bp2 = child_node.box_props.unpack();
26
                let float_margin = &child_bp2.margin;
                // +spec:floats:d0d163 - clear on floats adds constraint #10: float top below cleared floats' bottom
                // +spec:floats:7adb9d - Clear on floats: constraint #10, top outer edge must be below earlier cleared floats
26
                let float_clear = get_clear_property(ctx.styled_dom, Some(node_id));
26
                let float_y = if float_clear != LayoutClear::None {
                    float_context.clearance_offset(float_clear, main_pen + last_margin_bottom, writing_mode)
                } else {
                    // +spec:floats:ef96cb - Float margins never collapse with adjacent margins
                    // CSS 2.2 § 9.5: Float margins don't collapse with any other margins.
26
                    main_pen + last_margin_bottom
                };
26
                debug_info!(
26
                    ctx,
26
                    "[layout_bfc] Positioning float: index={}, type={:?}, size={:?}, at Y={} \
26
                     (main_pen={} + last_margin={})",
                    child_index,
                    float_type,
                    float_size,
                    float_y,
                    main_pen,
                    last_margin_bottom
                );
                // Position the float at the CURRENT main_pen + last margin (respects DOM order!)
26
                let float_rect = position_float(
26
                    &float_context,
26
                    float_type,
26
                    float_size,
26
                    float_margin,
                    // Include last_margin_bottom since float margins don't collapse!
26
                    float_y,
26
                    constraints.available_size.cross(writing_mode),
26
                    writing_mode,
                );
26
                debug_info!(ctx, "[layout_bfc] Float positioned at: {:?}", float_rect);
                // Add to float context BEFORE positioning next element
26
                float_context.add_float(float_type, float_rect, *float_margin);
                // Store position in output
26
                output.positions.insert(child_index, float_rect.origin);
26
                debug_info!(
26
                    ctx,
26
                    "[layout_bfc] *** FLOAT POSITIONED: child={}, main_pen={} (unchanged - floats \
26
                     don't advance pen)",
                    child_index,
                    main_pen
                );
                // Floats are taken out of normal flow - DON'T advance main_pen
                // Continue to next child
26
                continue;
9535
            }
9535
            false
        } else {
39
            false
        };
        // Early exit for floats (already handled above)
9574
        if is_float {
            continue;
9574
        }
        // From here: normal flow (non-float) children only
        // Track first and last in-flow children for parent-child collapse
9574
        if first_child_index.is_none() {
9271
            first_child_index = Some(child_index);
9271
        }
9574
        last_child_index = Some(child_index);
        // Calculate child's used_size just-in-time if not already computed
        // This replaces the old "Pass 1" that recursively laid out grandchildren with wrong positions
9574
        let child_size = match child_node.used_size {
9574
            Some(size) => size,
            None => {
                // Calculate size without recursive layout
                let intrinsic = tree.warm(child_index).and_then(|w| w.intrinsic_sizes).unwrap_or_default();
                let child_used_size = crate::solver3::sizing::calculate_used_size_for_node(
                    ctx.styled_dom,
                    child_dom_id,
                    &children_containing_block_size,
                    intrinsic,
                    &child_node.box_props.unpack(),
                    &ctx.viewport_size,
                )?;
                // Update the node with computed size (we need to re-borrow mutably)
                if let Some(node_mut) = tree.get_mut(child_index) {
                    node_mut.used_size = Some(child_used_size);
                }
                child_used_size
            }
        };
        // Re-borrow child_node after potential mutation
9574
        let child_node = tree.get(child_index).ok_or(LayoutError::InvalidTree)?;
9574
        let child_bp = child_node.box_props.unpack();
9574
        let child_margin = &child_bp.margin;
9574
        debug_info!(
9574
            ctx,
9574
            "[layout_bfc] Child {} margin from box_props: top={}, right={}, bottom={}, left={}",
            child_index,
            child_margin.top,
            child_margin.right,
            child_margin.bottom,
            child_margin.left
        );
        // +spec:block-formatting-context:0f802c - margins use containing block's writing mode for collapsing/auto expansion in orthogonal flows
9574
        let child_own_margin_top = child_margin.main_start(writing_mode);
9574
        let child_own_margin_bottom = child_margin.main_end(writing_mode);
        // CSS 2.2 § 8.3.1: If a child has no top blocker (no padding/border) and its
        // own BFC layout produced an escaped_top_margin, that margin represents the
        // collapsed value of (child's margin, child's first child's margin, ...).
        // Use it for sibling collapse instead of the child's own margin.
9574
        let child_escaped_top = if !has_margin_collapse_blocker(&child_bp, writing_mode, true) {
9220
            tree.warm(child_index).and_then(|w| w.escaped_top_margin)
354
        } else { None };
9574
        let child_escaped_bottom = if !has_margin_collapse_blocker(&child_bp, writing_mode, false) {
9223
            tree.warm(child_index).and_then(|w| w.escaped_bottom_margin)
351
        } else { None };
9574
        let child_margin_top = child_escaped_top.unwrap_or(child_own_margin_top);
9574
        let child_margin_bottom = child_escaped_bottom.unwrap_or(child_own_margin_bottom);
9574
        debug_info!(
9574
            ctx,
9574
            "[layout_bfc] Child {} final margins: margin_top={}, margin_bottom={}",
            child_index,
            child_margin_top,
            child_margin_bottom
        );
        // Check if this child has border/padding that prevents margin collapsing
9574
        let child_has_top_blocker =
9574
            has_margin_collapse_blocker(&child_bp, writing_mode, true);
9574
        let child_has_bottom_blocker =
9574
            has_margin_collapse_blocker(&child_bp, writing_mode, false);
        // +spec:floats:dc195a - Clear property only applies to block-level elements (CSS 2.2 § 9.5.2)
        // Check for clear property FIRST - clearance affects whether element is considered empty
        // CSS 2.2 § 9.5.2: "Clearance inhibits margin collapsing"
        // An element with clearance is NOT empty even if it has no content
9574
        let child_clear = if let Some(node_id) = child_dom_id {
9535
            get_clear_property(ctx.styled_dom, Some(node_id))
        } else {
39
            LayoutClear::None
        };
9574
        debug_info!(
9574
            ctx,
9574
            "[layout_bfc] Child {} clear property: {:?}",
            child_index,
            child_clear
        );
        // PHASE 1: Empty Block Detection & Self-Collapse
9574
        let is_empty = is_empty_block(tree, child_index);
        // Handle empty blocks FIRST (they collapse through and don't participate in layout)
        // EXCEPTION: Elements with clear property are NOT skipped even if empty!
        // CSS 2.2 § 9.5.2: Clear property affects positioning even for empty elements
9574
        if is_empty
2
            && !child_has_top_blocker
2
            && !child_has_bottom_blocker
2
            && child_clear == LayoutClear::None
        {
            // Empty block: collapse its own top and bottom margins FIRST
2
            let self_collapsed = collapse_margins(child_margin_top, child_margin_bottom);
            // Then collapse with previous margin (sibling or parent)
2
            if is_first_child {
1
                is_first_child = false;
                // Empty first child: its collapsed margin can escape with parent's
1
                if !parent_has_top_blocker {
1
                    accumulated_top_margin = collapse_margins(parent_margin_top, self_collapsed);
1
                } else {
                    // Parent has blocker: add margins
                    if accumulated_top_margin == 0.0 {
                        accumulated_top_margin = parent_margin_top;
                    }
                    main_pen += accumulated_top_margin + self_collapsed;
                    top_margin_resolved = true;
                    accumulated_top_margin = 0.0;
                }
1
                last_margin_bottom = self_collapsed;
1
            } else {
1
                // Empty sibling: collapse with previous sibling's bottom margin
1
                last_margin_bottom = collapse_margins(last_margin_bottom, self_collapsed);
1
            }
            // Skip positioning and pen advance (empty has no visual presence)
2
            continue;
9572
        }
        // From here on: non-empty blocks only (or empty blocks with clear property)
        // Apply clearance if needed
        // +spec:floats:148ee6 - clear:left pushes element below float; clearance added above top margin
        // CSS 2.2 § 9.5.2: Clearance inhibits margin collapsing.
        //
        // Per CSS 2.2 § 9.5.2, the clearance computation works as follows:
        // 1. Compute the "hypothetical position" — where the border edge would be
        //    with normal margin collapsing (as if clear:none).
        // 2. If the hypothetical position is NOT past the relevant floats,
        //    clearance is introduced and the border edge is placed at float bottom.
        // 3. The final border edge = max(float_bottom, hypothetical_position).
        //
        // This means child_margin_top is already accounted for in the hypothetical
        // position and must NOT be added again after clearance positions main_pen.
9572
        let clearance_applied = if child_clear != LayoutClear::None {
5
            let hypothetical = main_pen + collapse_margins(last_margin_bottom, child_margin_top);
5
            let cleared_position =
5
                float_context.clearance_offset(child_clear, hypothetical, writing_mode);
5
            debug_info!(
5
                ctx,
5
                "[layout_bfc] Child {} clearance check: cleared_position={}, hypothetical={} (main_pen={} + collapse({}, {}))",
                child_index,
                cleared_position,
                hypothetical,
                main_pen,
                last_margin_bottom,
                child_margin_top
            );
5
            if cleared_position > hypothetical {
4
                debug_info!(
4
                    ctx,
4
                    "[layout_bfc] Applying clearance: child={}, clear={:?}, old_pen={}, new_pen={}",
                    child_index,
                    child_clear,
                    main_pen,
                    cleared_position
                );
4
                main_pen = cleared_position;
4
                true // Signal that clearance was applied
            } else {
1
                false
            }
        } else {
9567
            false
        };
        // PHASE 2: Parent-Child Top Margin Escape (First Child)
        //
        // CSS 2.2 § 8.3.1: "The top margin of a box is adjacent to the top margin of its first
        // in-flow child if the box has no top border, no top padding, and the child has no
        // clearance." CSS 2.2 § 9.5.2: "Clearance inhibits margin collapsing"
9572
        if is_first_child {
9270
            is_first_child = false;
            // Clearance prevents collapse (acts as invisible blocker)
9270
            if clearance_applied {
                // Clearance inhibits all margin collapsing for this element
                // The clearance has already positioned main_pen at the correct
                // border-edge position (= max(float_bottom, hypothetical)).
                // The hypothetical already includes child_margin_top via
                // collapse_margins, so we must NOT add it again here.
3
                debug_info!(
3
                    ctx,
3
                    "[layout_bfc] First child {} with CLEARANCE: no collapse, child_margin={}, \
3
                     main_pen={}",
                    child_index,
                    child_margin_top,
                    main_pen
                );
9267
            } else if !parent_has_top_blocker {
                // Margin Escape Case
                //
                // CSS 2.2 § 8.3.1: "The top margin of an in-flow block element collapses with
                // its first in-flow block-level child's top margin if the element has no top
                // border, no top padding, and the child has no clearance."
                //
                // When margins collapse, they "escape" upward through the parent to be resolved
                // in the grandparent's coordinate space. This is critical for understanding the
                // coordinate system separation:
                //
                // Example:
                // <body padding=20>
                //  <div margin=0>
                //      <div margin=30></div>
                //  </div>
                // </body>
                //
                //   - Middle div (our parent) has no padding → margins can escape
                //   - Inner div's 30px margin collapses with middle div's 0px margin = 30px
                //   - This 30px margin "escapes" to be handled by body's BFC
                //   - Body positions middle div at Y=30 (relative to body's content-box)
                //   - Middle div's content-box height does NOT include the escaped 30px
                //   - Inner div is positioned at Y=0 in middle div's content-box
                //
                // **NOTE**: This is a subtle but critical distinction in coordinate systems:
                //
                //   - Parent's margin belongs to grandparent's coordinate space
                //   - Child's margin (when escaped) also belongs to grandparent's coordinate space
                //   - They collapse BEFORE entering this BFC's coordinate space
                //   - We return the collapsed margin so grandparent can position parent correctly
                //
                // **NOTE**: Child's own blocker status (padding/border) is IRRELEVANT for
                // parent-child  collapse. The child may have padding that prevents
                // collapse with ITS OWN  children, but this doesn't prevent its
                // margin from escaping  through its parent.
                //
                // **NOTE**: Previously, we incorrectly added parent_margin_top to main_pen in
                //  the blocked case, which double-counted the margin by mixing
                //  coordinate systems. The parent's margin is NEVER in our (the
                //  parent's content-box) coordinate system!
                //
                // We collapse the parent's margin with the child's margin.
                // This combined margin is what "escapes" to the grandparent.
                // The grandparent uses this to position the parent.
                //
                // Effectively, we are saying "The parent starts here, but its effective
                // top margin is now max(parent_margin, child_margin)".
1038
                accumulated_top_margin = collapse_margins(parent_margin_top, child_margin_top);
1038
                top_margin_resolved = true;
1038
                top_margin_escaped = true;
                // Track escaped margin so it gets subtracted from content-box height
                // The escaped margin is NOT part of our content-box - it belongs to our
                // parent's parent
1038
                total_escaped_top_margin = accumulated_top_margin;
                // Position child at pen (no margin applied - it escaped!)
1038
                debug_info!(
1038
                    ctx,
1038
                    "[layout_bfc] First child {} margin ESCAPES: parent_margin={}, \
1038
                     child_margin={}, collapsed={}, total_escaped={}",
                    child_index,
                    parent_margin_top,
                    child_margin_top,
                    accumulated_top_margin,
                    total_escaped_top_margin
                );
            } else {
                // Margin Blocked Case
                //
                // CSS 2.2 § 8.3.1: "no top padding and no top border" required for collapse.
                // When padding or border exists, margins do NOT collapse and exist in different
                // coordinate spaces.
                //
                // CRITICAL COORDINATE SYSTEM SEPARATION:
                //
                //   This is where the architecture becomes subtle. When layout_bfc() is called:
                //   1. We are INSIDE the parent's content-box coordinate space (main_pen starts at
                //      0)
                //   2. The parent's own margin was ALREADY RESOLVED by the grandparent's BFC
                //   3. The parent's margin is in the grandparent's coordinate space, not ours
                //   4. We NEVER reference the parent's margin in this BFC - it's outside our scope
                //
                // Example:
                //
                // <body padding=20>
                //   <div margin=30 padding=20>
                //      <div margin=30></div>
                //   </div>
                // </body>
                //
                //   - Middle div has padding=20 → blocker exists, margins don't collapse
                //   - Body's BFC positions middle div at Y=30 (middle div's margin, in body's
                //     space)
                //   - Middle div's BFC starts at its content-box (after the padding)
                //   - main_pen=0 at the top of middle div's content-box
                //   - Inner div has margin=30 → we add 30 to main_pen (in OUR coordinate space)
                //   - Inner div positioned at Y=30 (relative to middle div's content-box)
                //   - Absolute position: 20 (body padding) + 30 (middle margin) + 20 (middle
                //     padding) + 30 (inner margin) = 100px
                //
                // **NOTE**: Previous code incorrectly added parent_margin_top to main_pen here:
                //
                //     - main_pen += parent_margin_top;  // WRONG! Mixes coordinate systems
                //     - main_pen += child_margin_top;
                //
                //   This caused the "double margin" bug where margins were applied twice:
                //
                //   - Once by grandparent positioning parent (correct)
                //   - Again inside parent's BFC (INCORRECT - wrong coordinate system)
                //
                //   The parent's margin belongs to GRANDPARENT's coordinate space and was already
                //   used to position the parent. Adding it again here is like adding feet to
                //   meters.
                //
                //   We ONLY add the child's margin in our (parent's content-box) coordinate space.
                //   The parent's margin is irrelevant to us - it's outside our scope.
8229
                main_pen += child_margin_top;
8229
                debug_info!(
8229
                    ctx,
8229
                    "[layout_bfc] First child {} BLOCKED: parent_has_blocker={}, advanced by \
8229
                     child_margin={}, main_pen={}",
                    child_index,
                    parent_has_top_blocker,
                    child_margin_top,
                    main_pen
                );
            }
        } else {
            // Not first child: handle sibling collapse
            // CSS 2.2 § 8.3.1 Rule 1: "Vertical margins of adjacent block boxes in the normal flow
            // collapse" CSS 2.2 § 9.5.2: "Clearance inhibits margin collapsing"
            // Resolve accumulated top margin if not yet done (for parent's first in-flow child)
302
            if !top_margin_resolved {
105
                main_pen += accumulated_top_margin;
105
                top_margin_resolved = true;
105
                debug_info!(
105
                    ctx,
105
                    "[layout_bfc] RESOLVED top margin for node {} at sibling {}: accumulated={}, \
105
                     main_pen={}",
                    node_index,
                    child_index,
                    accumulated_top_margin,
                    main_pen
                );
197
            }
302
            if clearance_applied {
                // Clearance has already positioned main_pen at the correct
                // border-edge = max(float_bottom, hypothetical). The hypothetical
                // already includes collapse_margins(last_margin_bottom, child_margin_top),
                // so we must NOT add child_margin_top again here.
1
                debug_info!(
1
                    ctx,
1
                    "[layout_bfc] Child {} with CLEARANCE: no collapse with sibling, \
1
                     child_margin_top={}, main_pen={}",
                    child_index,
                    child_margin_top,
                    main_pen
                );
            } else {
                // Sibling Margin Collapse
                //
                // CSS 2.2 § 8.3.1: "Vertical margins of adjacent block boxes in the normal
                // flow collapse." The collapsed margin is the maximum of the two margins.
                //
                // IMPORTANT: Sibling margins ARE part of the parent's content-box height!
                //
                // Unlike escaped margins (which belong to grandparent's space), sibling margins
                // are the space BETWEEN children within our content-box.
                //
                // Example:
                //
                // <div>
                //  <div margin-bottom=30></div>
                //  <div margin-top=40></div>
                // </div>
                //
                //   - First child ends at Y=100 (including its content + margins)
                //   - Collapsed margin = max(30, 40) = 40px
                //   - Second child starts at Y=140 (100 + 40)
                //   - Parent's content-box height includes this 40px gap
                //
                // We track total_sibling_margins for debugging, but NOTE: we do **not**
                // subtract these from content-box height! They are part of the layout space.
                //
                // Previously we subtracted total_sibling_margins from content-box height:
                //
                //   content_box_height = main_pen - total_escaped_top_margin -
                // total_sibling_margins;
                //
                // This was wrong because sibling margins are between boxes (part of content),
                // not outside boxes (like escaped margins).
301
                let collapsed = collapse_margins(last_margin_bottom, child_margin_top);
301
                main_pen += collapsed;
301
                total_sibling_margins += collapsed;
301
                debug_info!(
301
                    ctx,
301
                    "[layout_bfc] Sibling collapse for child {}: last_margin_bottom={}, \
301
                     child_margin_top={}, collapsed={}, main_pen={}, total_sibling_margins={}",
                    child_index,
                    last_margin_bottom,
                    child_margin_top,
                    collapsed,
                    main_pen,
                    total_sibling_margins
                );
            }
        }
        // Position child (non-empty blocks only reach here)
        //
        // +spec:block-formatting-context:1dada5 - Normal flow boxes in BFC touch containing block edge
        // +spec:block-formatting-context:9f56cb - each box's left outer edge touches containing block left edge; new BFC may shrink due to floats
        // CSS 2.2 § 9.4.1: "In a block formatting context, each box's left outer edge touches
        // the left edge of the containing block (for right-to-left formatting, right edges touch).
        // This is true even in the presence of floats (although a box's line boxes may shrink
        // due to the floats), unless the box establishes a new block formatting context
        // (in which case the box itself may become narrower due to the floats)."
        //
        // +spec:block-formatting-context:3d2811 - Float overlap with normal flow element borders
        // +spec:display-property:796059 - BFC/replaced/table border box must not overlap float margin boxes; line boxes shorten around floats
        // +spec:floats:5214a6 - BFC/replaced/table border box must not overlap float margin boxes; shrink or clear below
        // CSS 2.2 § 9.5: "The border box of a table, a block-level replaced element, or an element
        // in the normal flow that establishes a new block formatting context (such as an element
        // with 'overflow' other than 'visible') must not overlap any floats in the same block
        // formatting context as the element itself."
        // +spec:floats:a29f70 - BFC roots, tables, and block-level replaced elements must not overlap float margin boxes
9572
        let child_node = tree.get(child_index).ok_or(LayoutError::InvalidTree)?;
9572
        let avoids_floats = establishes_new_bfc(ctx, child_node, tree.cold(child_index))
9393
            || is_block_level_replaced(ctx, child_node);
        // Query available space considering floats ONLY if child avoids floats
9572
        let (cross_start, cross_end, available_cross) = if avoids_floats {
            // New BFC / replaced / table: Must shrink or move down to avoid overlapping floats
179
            let child_cross_needed = child_size.cross(writing_mode);
179
            let bfc_cross = constraints.available_size.cross(writing_mode);
179
            let (mut start, mut end) = float_context.available_line_box_space(
179
                main_pen,
179
                main_pen + child_size.main(writing_mode),
179
                bfc_cross,
179
                writing_mode,
179
            );
179
            let mut available = end - start;
            // CSS 2.2 § 9.5: "If necessary, implementations should clear the said element
            // by placing it below any preceding floats, but may place it adjacent to such
            // floats if there is sufficient space."
179
            if available < child_cross_needed && !float_context.floats.is_empty() {
1
                let clear_to = float_context.floats.iter()
1
                    .filter(|f| {
1
                        let f_main_start = f.rect.origin.main(writing_mode) - f.margin.main_start(writing_mode);
1
                        let f_main_end = f_main_start + f.rect.size.main(writing_mode)
1
                            + f.margin.main_start(writing_mode) + f.margin.main_end(writing_mode);
1
                        f_main_end > main_pen && f_main_start < main_pen + child_size.main(writing_mode)
1
                    })
1
                    .map(|f| {
1
                        f.rect.origin.main(writing_mode) + f.rect.size.main(writing_mode)
1
                            + f.margin.main_end(writing_mode)
1
                    })
1
                    .fold(main_pen, f32::max);
1
                if clear_to > main_pen {
1
                    main_pen = clear_to;
1
                    let (s, e) = float_context.available_line_box_space(
1
                        main_pen,
1
                        main_pen + child_size.main(writing_mode),
1
                        bfc_cross,
1
                        writing_mode,
1
                    );
1
                    start = s;
1
                    end = e;
1
                    available = end - start;
1
                }
178
            }
179
            debug_info!(
179
                ctx,
179
                "[layout_bfc] Child {} avoids floats: shrinking to avoid floats, \
179
                 cross_range={}..{}, available_cross={}",
                child_index,
                start,
                end,
                available
            );
179
            (start, end, available)
        } else {
            // Normal flow: Overlaps floats, positioned at full width
            // Only the child's INLINE CONTENT (if any) wraps around floats
9393
            let start = 0.0;
9393
            let end = constraints.available_size.cross(writing_mode);
9393
            let available = end - start;
9393
            debug_info!(
9393
                ctx,
9393
                "[layout_bfc] Child {} is normal flow: overlapping floats at full width, \
9393
                 available_cross={}",
                child_index,
                available
            );
9393
            (start, end, available)
        };
        // Get child's margin, margin_auto, size, and formatting context
9572
        let (child_margin_cloned, child_margin_auto, child_used_size, is_inline_fc, child_dom_id_for_debug) = {
9572
            let child_node = tree.get(child_index).ok_or(LayoutError::InvalidTree)?;
9572
            let cbp = child_node.box_props.unpack();
9572
            (
9572
                cbp.margin.clone(),
9572
                cbp.margin_auto,
9572
                child_node.used_size.unwrap_or_default(),
9572
                child_node.formatting_context == FormattingContext::Inline,
9572
                child_node.dom_node_id,
9572
            )
        };
9572
        let child_margin = &child_margin_cloned;
9572
        debug_info!(
9572
            ctx,
9572
            "[layout_bfc] Child {} margin_auto: left={}, right={}, top={}, bottom={}",
            child_index,
            child_margin_auto.left,
            child_margin_auto.right,
            child_margin_auto.top,
            child_margin_auto.bottom
        );
9572
        debug_info!(
9572
            ctx,
9572
            "[layout_bfc] Child {} used_size: width={}, height={}",
            child_index,
            child_used_size.width,
            child_used_size.height
        );
        // Position child
        // For normal flow blocks (including IFCs): position at full width (cross_start = 0)
        // For BFC-establishing blocks: position in available space between floats
        //
        // CSS 2.2 § 10.3.3: If margin-left and margin-right are both auto,
        // their used values are equal, centering the element horizontally.
9572
        let (child_cross_pos, mut child_main_pos) = if avoids_floats {
            // BFC: Position in float-free space, but also check margin:auto centering.
            // A flex container or overflow:hidden box establishes a BFC (must avoid floats)
            // but can still be centered via margin:auto — these are independent concepts.
179
            let cross_pos = if child_margin_auto.left && child_margin_auto.right {
                let remaining = (available_cross - child_used_size.cross(writing_mode)).max(0.0);
                debug_info!(
                    ctx,
                    "[layout_bfc] Child {} BFC + margin:auto centering: available={}, size={}, offset={}",
                    child_index, available_cross, child_used_size.cross(writing_mode), remaining / 2.0
                );
                cross_start + remaining / 2.0
179
            } else if child_margin_auto.left {
                let remaining = (available_cross - child_used_size.cross(writing_mode) - child_margin.right).max(0.0);
                cross_start + remaining
            } else {
179
                cross_start + child_margin.cross_start(writing_mode)
            };
179
            (cross_pos, main_pen)
        } else {
            // Normal flow: Check for margin: auto centering
9393
            let available_cross = constraints.available_size.cross(writing_mode);
9393
            let child_cross_size = child_used_size.cross(writing_mode);
9393
            debug_info!(
9393
                ctx,
9393
                "[layout_bfc] Child {} centering check: available_cross={}, child_cross_size={}, margin_auto.left={}, margin_auto.right={}",
                child_index,
                available_cross,
                child_cross_size,
                child_margin_auto.left,
                child_margin_auto.right
            );
            // +spec:block-formatting-context:d52ce5 - auto margins resolved per containing block's writing mode for centering
            // +spec:width-calculation:0c5044 - auto margins center element on cross axis (respects writing mode)
            // +spec:width-calculation:25c2fc - §10.3.3: block-level margin auto centering and over-constrained resolution
            // +spec:width-calculation:ba691f - auto margins treated as zero when element overflows containing block (via .max(0.0) on remaining_space)
            // +spec:width-calculation:324e7e - both margin-left and margin-right auto => equal used values (centering)
            // CSS 2.2 § 10.3.3: If both margin-left and margin-right are auto,
            // center the element within the available space
9393
            let cross_pos = if child_margin_auto.left && child_margin_auto.right {
                // Center: (available - child_width) / 2
1
                let remaining_space = (available_cross - child_cross_size).max(0.0);
1
                debug_info!(
1
                    ctx,
1
                    "[layout_bfc] Child {} CENTERING: remaining_space={}, cross_pos={}",
                    child_index,
                    remaining_space,
1
                    remaining_space / 2.0
                );
1
                remaining_space / 2.0
9392
            } else if child_margin_auto.left {
                // Only left is auto: push element to the right
                let remaining_space = (available_cross - child_cross_size - child_margin.right).max(0.0);
                debug_info!(
                    ctx,
                    "[layout_bfc] Child {} margin-left:auto only, pushing right: remaining_space={}",
                    child_index,
                    remaining_space
                );
                remaining_space
9392
            } else if child_margin_auto.right {
                // Only right is auto: element stays at left with its margin
                debug_info!(
                    ctx,
                    "[layout_bfc] Child {} margin-right:auto only, using left margin={}",
                    child_index,
                    child_margin.cross_start(writing_mode)
                );
                child_margin.cross_start(writing_mode)
            } else {
                // +spec:box-model:218643 - over-constrained: drop end margin per containing block writing mode
                // +spec:width-calculation:d172a4 - over-constrained: LTR ignores margin-right, RTL ignores margin-left
                // in LTR, margin-right is ignored (element positioned at margin-left);
                // in RTL, margin-left is ignored (element positioned from right edge)
9392
                let is_rtl = tree.get(node_index)
9392
                    .and_then(|n| n.dom_node_id)
9392
                    .map_or(false, |cb_dom_id| {
9388
                        let node_state = ctx.styled_dom.styled_nodes.as_container()
9388
                            .get(cb_dom_id)
9388
                            .map(|s| s.styled_node_state.clone())
9388
                            .unwrap_or_default();
9388
                        matches!(
9388
                            get_direction_property(ctx.styled_dom, cb_dom_id, &node_state),
                            MultiValue::Exact(StyleDirection::Rtl)
                        )
9388
                    });
9392
                let cross_pos = if is_rtl {
                    // RTL: ignore margin-left, position from right edge
                    available_cross - child_cross_size - child_margin.cross_end(writing_mode)
                } else {
                    // LTR (default): ignore margin-right, position at margin-left
9392
                    child_margin.cross_start(writing_mode)
                };
9392
                debug_info!(
9392
                    ctx,
9392
                    "[layout_bfc] Child {} NO auto margins (over-constrained), is_rtl={}, cross_pos={}",
                    child_index,
                    is_rtl,
                    cross_pos
                );
9392
                cross_pos
            };
9393
            (cross_pos, main_pen)
        };
        // NOTE: We do NOT adjust child_main_pos based on child's escaped_top_margin here!
        // The escaped_top_margin represents margins that escaped FROM the child's own children.
        // The child's position in THIS BFC is determined by main_pen and the child's own margin
        // (which was already handled in the margin collapse logic above).
        //
        // Previously, this code incorrectly added child_escaped_margin to child_main_pos,
        // which caused double-application of margins because:
        // 1. The child's margin was used to calculate its position in THIS BFC
        // 2. Then its escaped_top_margin (which included its own margin) was added again
        //
        // The correct behavior per CSS 2.2 § 8.3.1 is:
        // - The child's escaped_top_margin is used by THIS node's parent to position THIS node
        // - It does NOT affect how we position the child within our content-box
        // final_pos is [CoordinateSpace::Parent] - relative to this BFC's content-box
9572
        let final_pos =
9572
            LogicalPosition::from_main_cross(child_main_pos, child_cross_pos, writing_mode);
9572
        debug_info!(
9572
            ctx,
9572
            "[layout_bfc] *** NORMAL FLOW BLOCK POSITIONED: child={}, final_pos={:?}, \
9572
             main_pen={}, avoids_floats={}",
            child_index,
            final_pos,
            main_pen,
            avoids_floats
        );
        // Re-layout IFC children with float context for correct text wrapping
        // Normal flow blocks WITH inline content need float context propagated
9572
        if is_inline_fc && !avoids_floats {
            // Use cached floats if available (from previous layout passes),
            // otherwise use the floats positioned in this pass
8536
            let floats_for_ifc = float_cache.get(&node_index).unwrap_or(&float_context);
8536
            debug_info!(
8536
                ctx,
8536
                "[layout_bfc] Re-layouting IFC child {} (normal flow) with parent's float context \
8536
                 at Y={}, child_cross_pos={}",
                child_index,
                main_pen,
                child_cross_pos
            );
8536
            debug_info!(
8536
                ctx,
8536
                "[layout_bfc]   Using {} floats (from cache: {})",
8536
                floats_for_ifc.floats.len(),
8536
                float_cache.contains_key(&node_index)
            );
            // Translate float coordinates from BFC-relative to IFC-relative
            // The IFC child is positioned at (child_cross_pos, main_pen) in BFC coordinates
            // Floats need to be relative to the IFC's CONTENT-BOX origin (inside padding/border)
8536
            let child_node = tree.get(child_index).ok_or(LayoutError::InvalidTree)?;
8536
            let cbp = child_node.box_props.unpack();
8536
            let padding_border_cross = cbp.padding.cross_start(writing_mode)
8536
                + cbp.border.cross_start(writing_mode);
8536
            let padding_border_main = cbp.padding.main_start(writing_mode)
8536
                + cbp.border.main_start(writing_mode);
            // Content-box origin in BFC coordinates
8536
            let content_box_cross = child_cross_pos + padding_border_cross;
8536
            let content_box_main = main_pen + padding_border_main;
8536
            debug_info!(
8536
                ctx,
8536
                "[layout_bfc]   Border-box at ({}, {}), Content-box at ({}, {}), \
8536
                 padding+border=({}, {})",
                child_cross_pos,
                main_pen,
                content_box_cross,
                content_box_main,
                padding_border_cross,
                padding_border_main
            );
8536
            let mut ifc_floats = FloatingContext::default();
8542
            for float_box in &floats_for_ifc.floats {
                // Convert float position from BFC coords to IFC CONTENT-BOX relative coords
6
                let float_rel_to_ifc = LogicalRect {
6
                    origin: LogicalPosition {
6
                        x: float_box.rect.origin.x - content_box_cross,
6
                        y: float_box.rect.origin.y - content_box_main,
6
                    },
6
                    size: float_box.rect.size,
6
                };
6
                debug_info!(
6
                    ctx,
6
                    "[layout_bfc] Float {:?}: BFC coords = {:?}, IFC-content-relative = {:?}",
                    float_box.kind,
                    float_box.rect,
                    float_rel_to_ifc
                );
6
                ifc_floats.add_float(float_box.kind, float_rel_to_ifc, float_box.margin);
            }
            // Create a BfcState with IFC-relative float coordinates
8536
            let mut bfc_state = BfcState {
8536
                pen: LogicalPosition::zero(), // IFC starts at its own origin
8536
                floats: ifc_floats.clone(),
8536
                margins: MarginCollapseContext::default(),
8536
            };
8536
            debug_info!(
8536
                ctx,
8536
                "[layout_bfc]   Created IFC-relative FloatingContext with {} floats",
8536
                ifc_floats.floats.len()
            );
            // Get the IFC child's content-box size (after padding/border)
8536
            let child_node = tree.get(child_index).ok_or(LayoutError::InvalidTree)?;
8536
            let child_dom_id = child_node.dom_node_id;
            // +spec:containing-block:a8ada9 - line box width determined by containing block and floats
            // For inline elements (display: inline), use containing block width as available
            // width. Inline elements flow within the containing block and wrap at its width.
            // CSS 2.2 § 10.3.1: For inline elements, available width = containing block width.
8536
            let display = get_display_property(ctx.styled_dom, child_dom_id).unwrap_or_default();
8536
            let child_content_size = if display == LayoutDisplay::Inline {
                // Inline elements use the containing block's content-box width
7634
                LogicalSize::new(
7634
                    children_containing_block_size.width,
7634
                    children_containing_block_size.height,
                )
            } else {
                // Block-level elements use their own content-box
902
                child_node.box_props.inner_size(child_size, writing_mode)
            };
8536
            debug_info!(
8536
                ctx,
8536
                "[layout_bfc]   IFC child size: border-box={:?}, content-box={:?}",
                child_size,
                child_content_size
            );
            // Create new constraints with float context
            // IMPORTANT: Use the child's CONTENT-BOX width, not the BFC width!
8536
            let ifc_constraints = LayoutConstraints {
8536
                available_size: child_content_size,
8536
                bfc_state: Some(&mut bfc_state),
8536
                writing_mode,
8536
                writing_mode_ctx: constraints.writing_mode_ctx,
8536
                text_align: constraints.text_align,
8536
                containing_block_size: constraints.containing_block_size,
8536
                available_width_type: Text3AvailableSpace::Definite(child_content_size.width),
8536
            };
            // Re-layout the IFC with float awareness
            // This will pass floats as exclusion zones to text3 for line wrapping
8536
            let ifc_result = layout_formatting_context(
8536
                ctx,
8536
                tree,
8536
                text_cache,
8536
                child_index,
8536
                &ifc_constraints,
8536
                float_cache,
            )?;
            // DON'T update used_size - the box keeps its full width!
            // Only the text layout inside changes to wrap around floats
8536
            debug_info!(
8536
                ctx,
8536
                "[layout_bfc] IFC child {} re-layouted with float context (text will wrap, box \
8536
                 stays full width)",
                child_index
            );
            // NOTE: We do NOT merge inline-block positions from the IFC's output.positions here!
            // The IFC's inline-block children will be correctly positioned when 
            // calculate_layout_for_subtree recursively processes the IFC node (child_index).
            // At that point, layout_ifc will be called again, and the inline-block positions
            // will be relative to the IFC's content-box, which is what we want.
            //
            // Merging them here would cause them to be processed by process_inflow_child
            // with the BFC's content-box position (self_content_box_pos of the BFC), 
            // resulting in incorrect absolute positions.
1036
        }
9572
        output.positions.insert(child_index, final_pos);
        // CSS margin collapse: escaped margins are handled via accumulated_top_margin
        // at the START of layout, not by adjusting positions after layout.
        // We simply advance by the child's actual size.
9572
        main_pen += child_size.main(writing_mode);
9572
        has_content = true;
        // Update last margin for next sibling
        // CSS 2.2 § 8.3.1: The bottom margin of this box will collapse with the top margin
        // of the next sibling (if no clearance or blockers intervene)
        // element (between prev sibling's bottom and this element's top margin). The cleared
        // element's bottom margin is still available for normal collapsing with the next sibling.
        // CSS 2.2 § 9.5.2: "Clearance inhibits margin collapsing and acts as spacing above
        // the margin-top of an element."
9572
        last_margin_bottom = child_margin_bottom;
9572
        debug_info!(
9572
            ctx,
9572
            "[layout_bfc] Child {} positioned at final_pos={:?}, size={:?}, advanced main_pen to \
9572
             {}, last_margin_bottom={}, clearance_applied={}",
            child_index,
            final_pos,
            child_size,
            main_pen,
            last_margin_bottom,
            clearance_applied
        );
        // Track the maximum cross-axis size to determine the BFC's overflow size.
9572
        let child_cross_extent =
9572
            child_cross_pos + child_size.cross(writing_mode) + child_margin.cross_end(writing_mode);
9572
        max_cross_size = max_cross_size.max(child_cross_extent);
    }
    // Store the float context in cache for future layout passes
    // This happens after ALL children (floats and normal) have been positioned
11164
    debug_info!(
10464
        ctx,
10464
        "[layout_bfc] Storing {} floats in cache for node {}",
10464
        float_context.floats.len(),
        node_index
    );
11164
    float_cache.insert(node_index, float_context.clone());
    // PHASE 3: Parent-Child Bottom Margin Escape
11164
    let mut escaped_top_margin = None;
11164
    let mut escaped_bottom_margin = None;
    // Handle top margin escape
11164
    if top_margin_escaped {
        // First child's margin escaped through parent
1038
        escaped_top_margin = Some(accumulated_top_margin);
1038
        debug_info!(
1038
            ctx,
1038
            "[layout_bfc] Returning escaped top margin: accumulated={}, node={}",
            accumulated_top_margin,
            node_index
        );
10126
    } else if !top_margin_resolved && accumulated_top_margin > 0.0 {
        // No content was positioned, all margins accumulated (empty blocks)
1
        escaped_top_margin = Some(accumulated_top_margin);
1
        debug_info!(
1
            ctx,
1
            "[layout_bfc] Escaping top margin (no content): accumulated={}, node={}",
            accumulated_top_margin,
            node_index
        );
    } else {
        // Don't set escaped_top_margin = Some(0) — that would override the child's
        // own margin (e.g., 30px) with 0 during sibling collapse.
10125
        debug_info!(
9425
            ctx,
9425
            "[layout_bfc] NOT escaping top margin: top_margin_resolved={}, escaped={}, \
9425
             accumulated={}, node={}",
            top_margin_resolved,
            top_margin_escaped,
            accumulated_top_margin,
            node_index
        );
    }
    // Handle bottom margin escape
11164
    if let Some(last_idx) = last_child_index {
9271
        let last_child = tree.get(last_idx).ok_or(LayoutError::InvalidTree)?;
9271
        let last_child_bp = last_child.box_props.unpack();
9271
        let last_has_bottom_blocker =
9271
            has_margin_collapse_blocker(&last_child_bp, writing_mode, false);
9271
        debug_info!(
9271
            ctx,
9271
            "[layout_bfc] Bottom margin for node {}: parent_has_bottom_blocker={}, \
9271
             last_has_bottom_blocker={}, last_margin_bottom={}, main_pen_before={}",
            node_index,
            parent_has_bottom_blocker,
            last_has_bottom_blocker,
            last_margin_bottom,
            main_pen
        );
9271
        if !parent_has_bottom_blocker && !last_has_bottom_blocker && has_content {
            // Last child's bottom margin can escape
727
            let collapsed_bottom = collapse_margins(parent_margin_bottom, last_margin_bottom);
727
            escaped_bottom_margin = Some(collapsed_bottom);
727
            debug_info!(
727
                ctx,
727
                "[layout_bfc] Bottom margin ESCAPED for node {}: collapsed={}",
                node_index,
                collapsed_bottom
            );
            // Don't add last_margin_bottom to pen (it escaped)
        } else {
            // Can't escape: add to pen
8544
            main_pen += last_margin_bottom;
            // NOTE: We do NOT add parent_margin_bottom to main_pen here!
            // parent_margin_bottom is added OUTSIDE the content-box (in the margin-box)
            // The content-box height should only include children's content and margins
8544
            debug_info!(
8544
                ctx,
8544
                "[layout_bfc] Bottom margin BLOCKED for node {}: added last_margin_bottom={}, \
8544
                 main_pen_after={}",
                node_index,
                last_margin_bottom,
                main_pen
            );
        }
    } else {
        // No children: just use parent's margins
1893
        if !top_margin_resolved {
1893
            main_pen += parent_margin_top;
1893
        }
1893
        main_pen += parent_margin_bottom;
    }
    // CRITICAL: If this is a root node (no parent), apply escaped margins directly
    // instead of propagating them upward (since there's no parent to receive them)
11164
    let is_root_node = node.parent.is_none();
11164
    if is_root_node {
1420
        if let Some(top) = escaped_top_margin {
            // Adjust all child positions downward by the escaped top margin
1000
            for (_, pos) in output.positions.iter_mut() {
1000
                let current_main = pos.main(writing_mode);
1000
                *pos = LogicalPosition::from_main_cross(
1000
                    current_main + top,
1000
                    pos.cross(writing_mode),
1000
                    writing_mode,
1000
                );
1000
            }
895
            main_pen += top;
525
        }
1420
        if let Some(bottom) = escaped_bottom_margin {
579
            main_pen += bottom;
866
        }
        // For root nodes, don't propagate margins further
1420
        escaped_top_margin = None;
1420
        escaped_bottom_margin = None;
9744
    }
    // CSS 2.2 § 9.5: Floats don't contribute to container height with overflow:visible
    //
    // However, browsers DO expand containers to contain floats in specific cases:
    //
    // 1. If there's NO in-flow content (main_pen == 0), floats determine height
    // 2. If container establishes a BFC (overflow != visible)
    //
    // In this case, we have in-flow content (main_pen > 0) and overflow:visible,
    // so floats should NOT expand the container. Their margins can "bleed" beyond
    // the container boundaries into the parent.
    //
    // This matches Chrome/Firefox behavior where float margins escape through
    // the container's padding when there's existing in-flow content.
    // +spec:block-formatting-context:7954a2 - 10.6.3: auto height for block-level non-replaced elements in normal flow
    // Content-box Height Calculation
    //
    // CSS 2.2 § 8.3.1: "The top border edge of the box is defined to coincide with
    // the top border edge of the [first] child" when margins collapse/escape.
    //
    // This means escaped margins do NOT contribute to the parent's content-box height.
    //
    // Calculation:
    //
    //   main_pen = total vertical space used by all children and margins
    //
    //   Components of main_pen:
    //
    //   1. Children's border-boxes (always included)
    //   2. Sibling collapsed margins (space BETWEEN children - part of content)
    //   3. First child's position (0 if margin escaped, margin_top if blocked)
    //
    //   What to subtract:
    //
    //   - total_escaped_top_margin: First child's margin that went to grandparent's space This
    //     margin is OUTSIDE our content-box, so we must subtract it.
    //
    //   What NOT to subtract:
    //
    //   - total_sibling_margins: These are the gaps BETWEEN children, which are
    //    legitimately part of our content area's layout space.
    //
    // Example with escaped margin:
    //   <div class="parent" padding=0>              <!-- Node 2 -->
    //     <div class="child1" margin=30></div>      <!-- Node 3, margin escapes -->
    //     <div class="child2" margin=40></div>      <!-- Node 5 -->
    //   </div>
    //
    //   Layout process:
    //
    //   - Node 3 positioned at main_pen=0 (margin escaped)
    //   - Node 3 size=140px → main_pen advances to 140
    //   - Sibling collapse: max(30 child1 bottom, 40 child2 top) = 40px
    //   - main_pen advances to 180
    //   - Node 5 size=130px → main_pen advances to 310
    //   - total_escaped_top_margin = 30
    //   - total_sibling_margins = 40 (tracked but NOT subtracted)
    //   - content_box_height = 310 - 30 = 280px ✓
    //
    // Previously, we calculated:
    //
    //   content_box_height = main_pen - total_escaped_top_margin - total_sibling_margins
    //
    // This incorrectly subtracted sibling margins, making parent too small.
    // Sibling margins are *between* boxes (part of layout), not *outside* boxes
    // (like escaped margins).
    // +spec:box-model:4eebed - auto height for BFC = top margin-edge of topmost child to bottom margin-edge of bottommost child
    // +spec:box-model:4eebed - auto height = top margin-edge of topmost child to bottom margin-edge of bottommost child
    // +spec:height-calculation:d65226 - §10.6.7 auto heights for BFC roots: block children use
    // margin-edge of topmost/bottommost, floats extend height if below content edge
    // +spec:positioning:1a05bb - 10.6.7 auto height for BFC roots: block children use margin edges,
    // abspos ignored (skipped in Pass 1/2), relative considered without offset (applied after layout),
    // floats whose bottom margin edge exceeds content edge expand height (below)
    // +spec:positioning:e6712c - Auto height for BFC: distance between top/bottom margin-edges of
    // block children (minus escaped margins), ignoring absolutely positioned children (skipped at
    // line ~966), considering relatively positioned boxes without offset (applied after layout),
    // and extending to include floats whose bottom margin edge exceeds content edge
    // +spec:positioning:f94d22 - 10.6.3: block-level non-replaced auto height = distance from top content edge to last in-flow child bottom margin edge (or zero)
    // CSS 2.2 §8.3.1: escaped margins (both top and bottom) don't contribute to parent height
11164
    let mut content_box_height = main_pen - total_escaped_top_margin
11164
        - escaped_bottom_margin.unwrap_or(0.0);
    // +spec:block-formatting-context:f73d3e - BFC root grows to fully contain its floats; floats from outside cannot protrude in
    // whose bottom margin edge exceeds bottom content edge; only floats participating
    // in this BFC are counted (not floats inside abspos descendants or nested BFCs)
    // +spec:box-model:1d4798 - auto height includes floats whose bottom margin edge exceeds content edge
    // only floats participating in this BFC are counted (not floats inside abspos descendants or nested BFCs)
11164
    if is_bfc_root {
9601
        for float_box in &float_context.floats {
            let float_bottom_margin_edge = float_box.rect.origin.main(writing_mode)
                + float_box.rect.size.main(writing_mode)
                + float_box.margin.main_end(writing_mode);
            if float_bottom_margin_edge > content_box_height {
                content_box_height = float_bottom_margin_edge;
            }
        }
1563
    }
    // +spec:display-contents:f6de1a - content height overflow tracked via overflow_size
    // +spec:overflow:043182 - overflow computed from box bounds + children overflow
11164
    output.overflow_size =
11164
        LogicalSize::from_main_cross(content_box_height, max_cross_size, writing_mode);
11164
    debug_info!(
10464
        ctx,
10464
        "[layout_bfc] FINAL for node {}: main_pen={}, total_escaped_top={}, \
10464
         total_sibling_margins={}, content_box_height={}",
        node_index,
        main_pen,
        total_escaped_top_margin,
        total_sibling_margins,
        content_box_height
    );
    // +spec:inline-formatting-context:2227a4 - atomic inline baseline for inline-block/inline-table
    // Baseline calculation would happen here in a full implementation.
    // CSS2 §10.8.1: For inline-block, baseline is the baseline of the last
    // line box in normal flow, or the bottom margin edge if no line boxes.
11164
    output.baseline = None;
    // Store escaped margins in the LayoutNode for use by parent
11164
    if let Some(warm_mut) = tree.warm_mut(node_index) {
11164
        warm_mut.escaped_top_margin = escaped_top_margin;
11164
        warm_mut.escaped_bottom_margin = escaped_bottom_margin;
11164
    }
11164
    if let Some(warm_mut) = tree.warm_mut(node_index) {
11164
        warm_mut.baseline = output.baseline;
11164
    }
11164
    Ok(BfcLayoutResult {
11164
        output,
11164
        escaped_top_margin,
11164
        escaped_bottom_margin,
11164
    })
11164
}
// Inline Formatting Context (CSS 2.2 § 9.4.2)
// +spec:display-property:ede6f4 - inline layout: mixed stream of text and inline-level boxes
/// Lays out an Inline Formatting Context (IFC) by delegating to the `text3` engine.
///
/// This function acts as a bridge between the box-tree world of `solver3` and the
/// rich text layout world of `text3`. Its responsibilities are:
///
/// 1. **Collect Content**: Traverse the direct children of the IFC root and convert them into a
///    `Vec<InlineContent>`, the input format for `text3`. This involves:
///
///     - Recursively laying out `inline-block` children to determine their final size and baseline,
///       which are then passed to `text3` as opaque objects.
///     - Extracting raw text runs from inline text nodes.
///
/// 2. **Translate Constraints**: Convert the `LayoutConstraints` (available space, floats) from
///    `solver3` into the more detailed `UnifiedConstraints` that `text3` requires.
///
/// 3. **Invoke Text Layout**: Call the `text3` cache's `layout_flow` method to perform the complex
///    tasks of BIDI analysis, shaping, line breaking, justification, and vertical alignment.
/// +spec:display-property:e96c82 - inline formatting context: flow of elements/text wrapped into lines
///
/// 4. **Integrate Results**: Process the `UnifiedLayout` returned by `text3`:
///
///     - Store the rich layout result on the IFC root `LayoutNode` for the display list generation
///       pass.
///     - Update the `positions` map for all `inline-block` children based on the positions
///       calculated by `text3`.
///     - Extract the final overflow size and baseline for the IFC root itself
// NOTE(writing-modes): The IFC currently assumes inline direction = horizontal
// and block direction = vertical. In vertical writing modes, line boxes would
// stack horizontally and inline content would flow vertically. The writing mode
// is now available via constraints.writing_mode_ctx for agents to use when
// implementing vertical text layout in the text3 engine.
// +spec:display-property:574e7b - text-box-trim for inline boxes trims block-end to content edge (TODO: implement trimming per text-box-edge metric)
// +spec:display-property:da284a - IFC: flow inline-level boxes into line boxes, size/position each fragment
// +spec:inline-formatting-context:275f64 - IFC: boxes laid out horizontally into line boxes, respecting margins/borders/padding
18205
fn layout_ifc<T: ParsedFontTrait>(
18205
    ctx: &mut LayoutContext<'_, T>,
18205
    text_cache: &mut crate::font_traits::TextLayoutCache,
18205
    tree: &mut LayoutTree,
18205
    node_index: usize,
18205
    constraints: &LayoutConstraints,
18205
) -> Result<LayoutOutput> {
18205
    let ifc_start = (ctx.get_system_time_fn.cb)();
18205
    let float_count = constraints
18205
        .bfc_state
18205
        .as_ref()
18205
        .map(|s| s.floats.floats.len())
18205
        .unwrap_or(0);
18205
    debug_info!(
18205
        ctx,
18205
        "[layout_ifc] ENTRY: node_index={}, has_bfc_state={}, float_count={}",
        node_index,
18205
        constraints.bfc_state.is_some(),
        float_count
    );
18205
    debug_ifc_layout!(ctx, "CALLED for node_index={}", node_index);
    // +spec:display-property:7f3c1d - Anonymous inline boxes: text directly in block containers treated as anonymous inline elements in IFC
    // +spec:display-property:5a795c - root inline box: block container generates anonymous inline box holding all inline-level contents, inheriting from parent
    // For anonymous boxes, we need to find the DOM ID from a parent or child
    // CSS 2.2 § 9.2.1.1: Anonymous boxes inherit properties from their enclosing box
18205
    let node = tree.get(node_index).ok_or(LayoutError::InvalidTree)?;
18205
    let ifc_root_dom_id = match node.dom_node_id {
18135
        Some(id) => id,
        None => {
            // Anonymous box - get DOM ID from parent or first child with DOM ID
70
            let parent_dom_id = node
70
                .parent
70
                .and_then(|p| tree.get(p))
70
                .and_then(|n| n.dom_node_id);
70
            if let Some(id) = parent_dom_id {
70
                id
            } else {
                // Try to find DOM ID from first child
                tree.children(node_index)
                    .iter()
                    .filter_map(|&child_idx| tree.get(child_idx))
                    .filter_map(|n| n.dom_node_id)
                    .next()
                    .ok_or(LayoutError::InvalidTree)?
            }
        }
    };
18205
    debug_ifc_layout!(ctx, "ifc_root_dom_id={:?}", ifc_root_dom_id);
    // +spec:display-property:a469a6 - line boxes created as needed for inline-level content in IFC
    // +spec:display-property:f3c875 - calculate layout bounds (size contributions) of each inline-level box
    // Phase 1: Collect and measure all inline-level children.
18205
    let phase1_start = (ctx.get_system_time_fn.cb)();
18205
    let (inline_content, child_map) =
18205
        collect_and_measure_inline_content(ctx, text_cache, tree, node_index, constraints)?;
18205
    let _phase1_time = (ctx.get_system_time_fn.cb)().duration_since(&phase1_start);
18205
    debug_info!(
18205
        ctx,
18205
        "[layout_ifc] Collected {} inline content items for node {}",
18205
        inline_content.len(),
        node_index
    );
18205
    if inline_content.len() > 10 {
        let _text_count = inline_content.iter().filter(|i| matches!(i, InlineContent::Text(_))).count();
        let _shape_count = inline_content.iter().filter(|i| matches!(i, InlineContent::Shape(_))).count();
18205
    }
18243
    for (i, item) in inline_content.iter().enumerate() {
18243
        match item {
18083
            InlineContent::Text(run) => debug_info!(ctx, "  [{}] Text: '{}'", i, run.text),
            InlineContent::Marker {
                run,
                position_outside,
            } => debug_info!(
                ctx,
                "  [{}] Marker: '{}' (outside={})",
                i,
                run.text,
                position_outside
            ),
140
            InlineContent::Shape(_) => debug_info!(ctx, "  [{}] Shape", i),
            InlineContent::Image(_) => debug_info!(ctx, "  [{}] Image", i),
20
            _ => debug_info!(ctx, "  [{}] Other", i),
        }
    }
18205
    debug_ifc_layout!(
18205
        ctx,
18205
        "Collected {} inline content items",
18205
        inline_content.len()
    );
18205
    if inline_content.is_empty() {
        debug_warning!(ctx, "inline_content is empty, returning default output!");
        // The node has no inline-level content this pass (e.g. its only
        // inline child — a text run or an inline image — was removed by a
        // relayout). Any `inline_layout_result` left over from a previous
        // frame is now stale: the display-list generator paints inline
        // objects (images, inline-block shapes) straight out of this cached
        // layout (see display_list.rs `paint_inline_*`), so a leftover entry
        // would re-emit the removed content AND index `styled_nodes` with a
        // `source_node_id` that no longer exists in the new DOM (OOB panic).
        // Clear it so the empty IFC renders nothing.
        if let Some(warm_node) = tree.warm_mut(node_index) {
            warm_node.inline_layout_result = None;
        }
        return Ok(LayoutOutput::default());
18205
    }
    // === Phase 2d: IFC incremental relayout decision tree ===
    //
    // Check if a cached layout exists with matching constraints. If so,
    // try incremental relayout (GlyphSwap or LineShift) before falling
    // back to full layout_flow().
    {
18205
        let cached_ifc = tree
18205
            .warm(node_index)
18205
            .and_then(|n| n.inline_layout_result.as_ref());
18205
        if let Some(cached) = cached_ifc {
13483
            if let Some(ref line_breaks) = cached.line_breaks {
                // Collect per-item advance widths from cached metrics
13483
                let old_advances: Vec<f32> = cached.item_metrics.iter()
13483
                    .map(|m| m.advance_width)
13483
                    .collect();
                // Cache-reuse fast path. Real incremental relayout for text
                // edits lives in LayoutWindow::try_incremental_text_relayout
                // (window.rs) — it has the newly-shaped items and the edited
                // node id, so it can compute real dirty_item_indices and
                // take the GlyphSwap / LineShift branches. Here we only
                // know the IFC is being re-entered (e.g. viewport resize on
                // a static IFC); with nothing re-shaped yet, the best we can
                // do is "no items changed at this level" → trivial GlyphSwap
                // to return the cached layout unchanged.
13483
                let result = crate::text3::cache::try_incremental_relayout(
13483
                    &[], // empty = no dirty items detected at this level
13483
                    &old_advances,
13483
                    &old_advances, // same advances since we haven't reshaped yet
13483
                    line_breaks,
                );
13483
                match result {
                    crate::text3::cache::IncrementalRelayoutResult::GlyphSwap => {
                        // No items changed — return cached layout directly
13483
                        debug_info!(ctx, "[layout_ifc] Phase 2d: GlyphSwap — reusing cached layout");
13483
                        let main_frag = &cached.layout;
13483
                        let frag_bounds = main_frag.bounds();
13483
                        let mut output = LayoutOutput::default();
13483
                        output.overflow_size = LogicalSize::new(frag_bounds.width, frag_bounds.height);
13483
                        output.baseline = main_frag.last_baseline();
                        // Re-position inline-block children from cached layout
128968
                        for positioned_item in &main_frag.items {
128968
                            if let ShapedItem::Object { source, .. } = &positioned_item.item {
70
                                if let Some(&child_node_index) = child_map.get(source) {
70
                                    output.positions.insert(child_node_index, LogicalPosition {
70
                                        x: positioned_item.position.x,
70
                                        y: positioned_item.position.y,
70
                                    });
70
                                }
128898
                            }
                        }
13483
                        return Ok(output);
                    }
                    _ => {
                        // Fall through to full layout_flow
                    }
                }
            }
4722
        }
    }
    // Phase 2: Translate constraints and define a single layout fragment for text3.
4722
    let text3_constraints =
4722
        translate_to_text3_constraints(ctx, constraints, ctx.styled_dom, ifc_root_dom_id);
    // Clone constraints for caching (before they're moved into fragments)
4722
    let cached_constraints = text3_constraints.clone();
4722
    debug_info!(
4722
        ctx,
4722
        "[layout_ifc] CALLING text_cache.layout_flow for node {} with {} exclusions",
        node_index,
4722
        text3_constraints.shape_exclusions.len()
    );
4722
    let fragments = vec![LayoutFragment {
4722
        id: "main".to_string(),
4722
        constraints: text3_constraints,
4722
    }];
    // Phase 3: Invoke the text layout engine.
    // Get pre-loaded fonts from font manager (fonts should be loaded before layout)
4722
    let phase3_start = (ctx.get_system_time_fn.cb)();
4722
    let loaded_fonts = ctx.font_manager.get_loaded_fonts();
4722
    let text_layout_result = match text_cache.layout_flow(
4722
        &inline_content,
4722
        &[],
4722
        &fragments,
4722
        &ctx.font_manager.font_chain_cache,
4722
        &ctx.font_manager.fc_cache,
4722
        &loaded_fonts,
4722
        ctx.debug_messages,
4722
    ) {
4722
        Ok(result) => result,
        Err(e) => {
            // Font errors should not stop layout of other elements.
            // Log the error and return a zero-sized layout.
            debug_warning!(ctx, "Text layout failed: {:?}", e);
            debug_warning!(
                ctx,
                "Continuing with zero-sized layout for node {}",
                node_index
            );
            let mut output = LayoutOutput::default();
            output.overflow_size = LogicalSize::new(0.0, 0.0);
            return Ok(output);
        }
    };
4722
    let _phase3_time = (ctx.get_system_time_fn.cb)().duration_since(&phase3_start);
4722
    let _total_ifc_time = (ctx.get_system_time_fn.cb)().duration_since(&ifc_start);
    // Phase 4: Integrate results back into the solver3 layout tree.
4722
    let mut output = LayoutOutput::default();
4722
    debug_ifc_layout!(
4722
        ctx,
4722
        "text_layout_result has {} fragment_layouts",
4722
        text_layout_result.fragment_layouts.len()
    );
4722
    if let Some(main_frag) = text_layout_result.fragment_layouts.get("main") {
4722
        let frag_bounds = main_frag.bounds();
4722
        debug_ifc_layout!(
4722
            ctx,
4722
            "Found 'main' fragment with {} items, bounds={}x{}",
4722
            main_frag.items.len(),
            frag_bounds.width,
            frag_bounds.height
        );
4722
        debug_ifc_layout!(ctx, "Storing inline_layout_result on node {}", node_index);
        // Determine if we should store this layout result using the new
        // CachedInlineLayout system. The key insight is that inline layouts
        // depend on available width:
        //
        // - Min-content measurement uses width ≈ 0 (maximum line wrapping)
        // - Max-content measurement uses width = ∞ (no line wrapping)
        // - Final layout uses the actual column/container width
        //
        // We must track which constraint type was used, otherwise a min-content
        // measurement would incorrectly be reused for final rendering.
4722
        let has_floats = constraints
4722
            .bfc_state
4722
            .as_ref()
4722
            .map(|s| !s.floats.floats.is_empty())
4722
            .unwrap_or(false);
4722
        let current_width_type = constraints.available_width_type;
4722
        let warm_node = tree.warm_mut(node_index).ok_or(LayoutError::InvalidTree)?;
4722
        let should_store = match &warm_node.inline_layout_result {
            None => {
                // No cached result - always store
4722
                debug_info!(
4722
                    ctx,
4722
                    "[layout_ifc] Storing NEW inline_layout_result for node {} (width_type={:?}, \
4722
                     has_floats={})",
                    node_index,
                    current_width_type,
                    has_floats
                );
4722
                true
            }
            Some(cached) => {
                // Check if the new result should replace the cached one
                if cached.should_replace_with(current_width_type, has_floats) {
                    debug_info!(
                        ctx,
                        "[layout_ifc] REPLACING inline_layout_result for node {} (old: \
                         width={:?}, floats={}) with (new: width={:?}, floats={})",
                        node_index,
                        cached.available_width,
                        cached.has_floats,
                        current_width_type,
                        has_floats
                    );
                    true
                } else {
                    debug_info!(
                        ctx,
                        "[layout_ifc] KEEPING cached inline_layout_result for node {} (cached: \
                         width={:?}, floats={}, new: width={:?}, floats={})",
                        node_index,
                        cached.available_width,
                        cached.has_floats,
                        current_width_type,
                        has_floats
                    );
                    false
                }
            }
        };
4722
        if should_store {
4722
            warm_node.inline_layout_result = Some(CachedInlineLayout::new_with_constraints(
4722
                main_frag.clone(),
4722
                current_width_type,
4722
                has_floats,
4722
                cached_constraints.clone(),
4722
            ));
4722
        }
        // Extract the overall size and baseline for the IFC root.
        // +spec:display-property:a0d0ab - IFC height = top of topmost line box to bottom of bottommost line box
        // +spec:display-property:a63b8f - baseline-source defaults to auto (last baseline for inline-block/IFC)
4722
        output.overflow_size = LogicalSize::new(frag_bounds.width, frag_bounds.height);
4722
        output.baseline = main_frag.last_baseline();
4722
        warm_node.baseline = output.baseline;
        // +spec:box-model:929f42 - text-box-trim: trim half-leading from first/last formatted line
        // +spec:box-model:02e0f9 - text-box-trim: trim-end and trim-both, no effect with non-zero padding/border
        //
        // CSS Inline 3 § 6.2: For block containers, trim the block-start/block-end side
        // of the first/last formatted line. If there is intervening non-zero padding or
        // borders, there is no effect. Does not apply to flex, grid, or table contexts.
4722
        let ifc_node_state = &ctx.styled_dom.styled_nodes.as_container()[ifc_root_dom_id].styled_node_state;
        // Fast path: if no node in the DOM declared text-box-trim, the cascade
        // walk would always return None → skip it.
4722
        let text_box_trim = {
4722
            let skip = ctx.styled_dom
4722
                .css_property_cache
4722
                .ptr
4722
                .compact_cache
4722
                .as_ref()
4722
                .map(|cc| cc.dom_declared_flags & azul_css::compact_cache::DOM_HAS_TEXT_BOX_TRIM == 0)
4722
                .unwrap_or(false);
4722
            if skip {
4722
                StyleTextBoxTrim::None
            } else {
                get_text_box_trim_property(ctx.styled_dom, ifc_root_dom_id, ifc_node_state)
                    .unwrap_or(StyleTextBoxTrim::None)
            }
        };
4722
        if text_box_trim != StyleTextBoxTrim::None && !main_frag.items.is_empty() {
            // Half-leading = (line-height - (ascent + descent)) / 2
            let half_leading = (cached_constraints.resolved_line_height()
                - (cached_constraints.strut_ascent + cached_constraints.strut_descent))
                / 2.0;
            let half_leading = half_leading.max(0.0);
            // Check for intervening non-zero padding/border on block-start (top)
            let has_pad_or_border_top = match get_css_padding_top(ctx.styled_dom, ifc_root_dom_id, ifc_node_state) {
                MultiValue::Exact(pv) => pv.number.get() != 0.0,
                _ => false,
            } || match get_css_border_top_width(ctx.styled_dom, ifc_root_dom_id, ifc_node_state) {
                MultiValue::Exact(pv) => pv.number.get() != 0.0,
                _ => false,
            };
            // Check for intervening non-zero padding/border on block-end (bottom)
            let has_pad_or_border_bottom = match get_css_padding_bottom(ctx.styled_dom, ifc_root_dom_id, ifc_node_state) {
                MultiValue::Exact(pv) => pv.number.get() != 0.0,
                _ => false,
            } || match get_css_border_bottom_width(ctx.styled_dom, ifc_root_dom_id, ifc_node_state) {
                MultiValue::Exact(pv) => pv.number.get() != 0.0,
                _ => false,
            };
            let trim_start = matches!(text_box_trim, StyleTextBoxTrim::TrimStart | StyleTextBoxTrim::TrimBoth)
                && !has_pad_or_border_top;
            let trim_end = matches!(text_box_trim, StyleTextBoxTrim::TrimEnd | StyleTextBoxTrim::TrimBoth)
                && !has_pad_or_border_bottom;
            let mut height_reduction = 0.0;
            if trim_start && half_leading > 0.0 {
                height_reduction += half_leading;
            }
            if trim_end && half_leading > 0.0 {
                height_reduction += half_leading;
            }
            if height_reduction > 0.0 {
                output.overflow_size.height = (output.overflow_size.height - height_reduction).max(0.0);
            }
4722
        }
        // Position all the inline-block children based on text3's calculations.
        // [CoordinateSpace::Parent] - positions are relative to IFC's content-box (0,0)
38419
        for positioned_item in &main_frag.items {
38419
            if let ShapedItem::Object { source, content, .. } = &positioned_item.item {
70
                if let Some(&child_node_index) = child_map.get(source) {
70
                    // new_relative_pos is [CoordinateSpace::Parent] - relative to this IFC's content-box
70
                    let new_relative_pos = LogicalPosition {
70
                        x: positioned_item.position.x,
70
                        y: positioned_item.position.y,
70
                    };
70
                    output.positions.insert(child_node_index, new_relative_pos);
70
                }
38349
            }
        }
    }
4722
    Ok(output)
18205
}
280
fn translate_taffy_size(size: LogicalSize) -> TaffySize<Option<f32>> {
280
    TaffySize {
280
        width: Some(size.width),
280
        height: Some(size.height),
280
    }
280
}
/// Helper: Convert StyleFontStyle to text3::cache::FontStyle
36505
pub fn convert_font_style(style: StyleFontStyle) -> crate::font_traits::FontStyle {
36505
    match style {
36505
        StyleFontStyle::Normal => crate::font_traits::FontStyle::Normal,
        StyleFontStyle::Italic => crate::font_traits::FontStyle::Italic,
        StyleFontStyle::Oblique => crate::font_traits::FontStyle::Oblique,
    }
36505
}
/// Helper: Convert StyleFontWeight to FcWeight
36505
pub fn convert_font_weight(weight: StyleFontWeight) -> FcWeight {
36505
    match weight {
        StyleFontWeight::W100 => FcWeight::Thin,
        StyleFontWeight::W200 => FcWeight::ExtraLight,
        StyleFontWeight::W300 | StyleFontWeight::Lighter => FcWeight::Light,
36330
        StyleFontWeight::Normal => FcWeight::Normal,
        StyleFontWeight::W500 => FcWeight::Medium,
        StyleFontWeight::W600 => FcWeight::SemiBold,
175
        StyleFontWeight::Bold => FcWeight::Bold,
        StyleFontWeight::W800 => FcWeight::ExtraBold,
        StyleFontWeight::W900 | StyleFontWeight::Bolder => FcWeight::Black,
    }
36505
}
/// Resolves a CSS size metric to pixels.
///
/// - `metric`: The CSS unit (px, pt, em, vw, etc.)
/// - `value`: The numeric value
/// - `containing_block_size`: Size of containing block (for percentage)
/// - `viewport_size`: Viewport dimensions (for vw, vh, vmin, vmax)
#[inline]
280
fn resolve_size_metric(
280
    metric: SizeMetric,
280
    value: f32,
280
    containing_block_size: f32,
280
    viewport_size: LogicalSize,
280
) -> f32 {
280
    match metric {
175
        SizeMetric::Px => value,
        SizeMetric::Pt => value * PT_TO_PX,
105
        SizeMetric::Percent => value / 100.0 * containing_block_size,
        SizeMetric::Em | SizeMetric::Rem => value * DEFAULT_FONT_SIZE,
        SizeMetric::Vw => value / 100.0 * viewport_size.width,
        SizeMetric::Vh => value / 100.0 * viewport_size.height,
        SizeMetric::Vmin => value / 100.0 * viewport_size.width.min(viewport_size.height),
        SizeMetric::Vmax => value / 100.0 * viewport_size.width.max(viewport_size.height),
        // In, Cm, Mm: convert to pixels using standard DPI (96)
        SizeMetric::In => value * 96.0,
        SizeMetric::Cm => value * 96.0 / 2.54,
        SizeMetric::Mm => value * 96.0 / 25.4,
    }
280
}
2100
pub fn translate_taffy_size_back(size: TaffySize<f32>) -> LogicalSize {
2100
    LogicalSize {
2100
        width: size.width,
2100
        height: size.height,
2100
    }
2100
}
770
pub fn translate_taffy_point_back(point: taffy::Point<f32>) -> LogicalPosition {
770
    LogicalPosition {
770
        x: point.x,
770
        y: point.y,
770
    }
770
}
// +spec:block-formatting-context:40e03e - BFC root: block container establishing new BFC (contains floats, excludes external floats, suppresses margin collapsing)
/// Checks if a node establishes a new Block Formatting Context (BFC).
///
/// Per CSS 2.2 § 9.4.1, a BFC is established by:
/// - Floats (elements with float other than 'none')
/// - Absolutely positioned elements (position: absolute or fixed)
/// - Block containers that are not block boxes (e.g., inline-blocks, table-cells)
/// - Block boxes with 'overflow' other than 'visible' and 'clip'
/// - Elements with 'display: flow-root'
/// - Table cells, table captions, and inline-blocks
///
/// Normal flow block-level boxes do NOT establish a new BFC.
///
/// This is critical for correct float interaction: normal blocks should overlap floats
/// (not shrink around them), while their inline content wraps around floats.
// +spec:block-formatting-context:241d22 - block container establishes new BFC or continues parent's, based on overflow/position/float/display
// +spec:block-formatting-context:9fe441 - BFC establishment based on position, float, overflow, and display properties
// +spec:display-property:3c7369 - block boxes establishing independent FC create new BFC; flex containers already do; non-replaced inlines cannot
// +spec:positioning:1e94f6 - floats, abspos, inline-blocks/table-cells/table-captions, overflow!=visible establish new BFC
20736
fn establishes_new_bfc<T: ParsedFontTrait>(ctx: &LayoutContext<'_, T>, node: &LayoutNodeHot, cold: Option<&LayoutNodeCold>) -> bool {
    // +spec:block-formatting-context:f39cd3 - table wrapper box establishes a BFC (CSS 2.2 §17.4)
    // Anonymous table wrapper boxes have no dom_node_id but must still establish BFC
    // +spec:height-calculation:e20498 - table wrapper box establishes BFC (CSS 2.2 §17.4)
    // +spec:positioning:b780d3 - Table wrapper box establishes BFC (CSS 2.2 § 17.4)
20736
    if cold.and_then(|c| c.anonymous_type) == Some(AnonymousBoxType::TableWrapper) {
        return true;
20736
    }
20736
    let Some(dom_id) = node.dom_node_id else {
43
        return false;
    };
20693
    let node_state = &ctx.styled_dom.styled_nodes.as_container()[dom_id].styled_node_state;
    // 1. Floats establish BFC
20693
    let float_val = get_float(ctx.styled_dom, dom_id, node_state);
20668
    if matches!(
20693
        float_val,
        MultiValue::Exact(LayoutFloat::Left | LayoutFloat::Right)
    ) {
25
        return true;
20668
    }
    // +spec:positioning:69468c - absolute/fixed forces independent formatting context
20668
    let position = crate::solver3::positioning::get_position_type(ctx.styled_dom, Some(dom_id));
20668
    if matches!(position, LayoutPosition::Absolute | LayoutPosition::Fixed) {
35
        return true;
20633
    }
    // 3. Inline-blocks, table-cells, table-captions establish BFC
20633
    let display = get_display_property(ctx.styled_dom, Some(dom_id));
12513
    if matches!(
20633
        display,
        MultiValue::Exact(
            LayoutDisplay::InlineBlock | LayoutDisplay::TableCell | LayoutDisplay::TableCaption
        )
    ) {
8120
        return true;
12513
    }
    // 4. display: flow-root establishes BFC
    // +spec:display-property:14bae6 - flow-root establishes a formatting context that contains/excludes floats
12513
    if matches!(display, MultiValue::Exact(LayoutDisplay::FlowRoot)) {
        return true;
12513
    }
    // +spec:overflow:0a944d - clip does NOT establish BFC; hidden/scroll/auto do establish BFC
    // +spec:overflow:631a4c - scroll containers establish independent formatting context (BFC)
    // +spec:overflow:f6a186 - overflow:clip does NOT establish BFC; use display:flow-root for that
    // +spec:overflow:717de1 - overflow != visible/clip establishes BFC per CSS 2.2 §9.4.1
    // +spec:positioning:6feb32 - overflow:clip does NOT establish new formatting context; hidden/scroll/auto do
    // 5. Block boxes with overflow other than 'visible' or 'clip' establish BFC
    // +spec:overflow:b34aef - Block boxes with overflow other than 'visible' or 'clip' establish BFC
    // Note: 'clip' does NOT establish BFC per CSS Overflow Module Level 3
12513
    let overflow_x = get_overflow_x(ctx.styled_dom, dom_id, node_state);
12513
    let overflow_y = get_overflow_y(ctx.styled_dom, dom_id, node_state);
24988
    let creates_bfc_via_overflow = |ov: &MultiValue<LayoutOverflow>| {
24950
        matches!(
24988
            ov,
            &MultiValue::Exact(
                LayoutOverflow::Hidden | LayoutOverflow::Scroll | LayoutOverflow::Auto
            )
        )
24988
    };
12513
    if creates_bfc_via_overflow(&overflow_x) || creates_bfc_via_overflow(&overflow_y) {
38
        return true;
12475
    }
    // 6. Table, Flex, and Grid containers establish BFC (via FormattingContext)
    // +spec:block-formatting-context:f15b87 - display:table participates in a BFC
12333
    if matches!(
12475
        node.formatting_context,
        FormattingContext::Table | FormattingContext::Flex | FormattingContext::Grid
    ) {
142
        return true;
12333
    }
    // +spec:block-formatting-context:33e6cd - block container with different writing-mode than parent establishes independent BFC
    // CSS Writing Modes 4 § 3.2: if a block container has a different writing-mode
    // than its parent, its inner display type computes to flow-root (i.e., it establishes BFC).
    {
12333
        let hierarchy = ctx.styled_dom.node_hierarchy.as_container();
12333
        if let Some(parent_dom_id) = hierarchy[dom_id].parent_id() {
10913
            let parent_state = &ctx.styled_dom.styled_nodes.as_container()[parent_dom_id].styled_node_state;
10913
            let child_wm = get_writing_mode(ctx.styled_dom, dom_id, node_state).unwrap_or_default();
10913
            let parent_wm = get_writing_mode(ctx.styled_dom, parent_dom_id, parent_state).unwrap_or_default();
10913
            if child_wm != parent_wm {
                return true;
10913
            }
1420
        }
    }
    // Normal flow block boxes do NOT establish BFC
    // NOTE: align-content != normal should also establish BFC per CSS-DISPLAY-3, but align-content is not yet implemented for block containers
12333
    false
20736
}
// +spec:display-property:5e5420 - replaced element identification (glossary: replaced elements have natural dimensions, establish independent formatting context)
/// CSS 2.2 § 9.5: "The border box of a table, a block-level replaced element, or an element
/// in the normal flow that establishes a new block formatting context [...] must not overlap
/// the margin box of any floats in the same block formatting context as the element itself."
9393
fn is_block_level_replaced<T: ParsedFontTrait>(ctx: &LayoutContext<'_, T>, node: &LayoutNodeHot) -> bool {
9393
    let Some(dom_id) = node.dom_node_id else {
39
        return false;
    };
    // Check display is block-level
9354
    let display = get_display_property(ctx.styled_dom, Some(dom_id));
9354
    let is_block_level = matches!(
9354
        display,
        MultiValue::Exact(LayoutDisplay::Block | LayoutDisplay::ListItem | LayoutDisplay::FlowRoot)
    );
9354
    if !is_block_level {
7599
        return false;
1755
    }
    // Check if the element is a replaced element (image, video, etc.)
1755
    let node_data = &ctx.styled_dom.node_data.as_container()[dom_id];
1755
    matches!(
1755
        node_data.get_node_type(),
        NodeType::Image(_)
    )
9393
}
/// Translates solver3 layout constraints into the text3 engine's unified constraints.
4722
fn translate_to_text3_constraints<'a, T: ParsedFontTrait>(
4722
    ctx: &mut LayoutContext<'_, T>,
4722
    constraints: &'a LayoutConstraints<'a>,
4722
    styled_dom: &StyledDom,
4722
    dom_id: NodeId,
4722
) -> UnifiedConstraints {
    // DOM-level declared flags: if a bit is clear, no node in this DOM
    // declared the corresponding property → cascade walks always return
    // None, and we use the default value directly. All flags default to
    // "set" when there is no compact cache (paranoid fallback).
    use azul_css::compact_cache::{
        DOM_HAS_SHAPE_INSIDE, DOM_HAS_SHAPE_OUTSIDE, DOM_HAS_TEXT_JUSTIFY,
        DOM_HAS_TEXT_INDENT, DOM_HAS_COLUMN_COUNT, DOM_HAS_COLUMN_GAP,
        DOM_HAS_COLUMN_WIDTH,
        DOM_HAS_INITIAL_LETTER, DOM_HAS_INITIAL_LETTER_ALIGN,
        DOM_HAS_LINE_CLAMP, DOM_HAS_HANGING_PUNCTUATION,
        DOM_HAS_TEXT_COMBINE_UPRIGHT, DOM_HAS_EXCLUSION_MARGIN,
        DOM_HAS_SHAPE_MARGIN,
        DOM_HAS_HYPHENATION_LANGUAGE, DOM_HAS_UNICODE_BIDI,
        DOM_HAS_HYPHENS, DOM_HAS_WORD_BREAK, DOM_HAS_OVERFLOW_WRAP,
        DOM_HAS_LINE_BREAK, DOM_HAS_TEXT_ALIGN_LAST, DOM_HAS_LINE_HEIGHT,
    };
4722
    let dom_declared = styled_dom
4722
        .css_property_cache
4722
        .ptr
4722
        .compact_cache
4722
        .as_ref()
4722
        .map(|cc| cc.dom_declared_flags)
4722
        .unwrap_or(!0u32);
    // Convert floats into exclusion zones for text3 to flow around.
4722
    let mut shape_exclusions = if let Some(ref bfc_state) = constraints.bfc_state {
        debug_info!(
            ctx,
            "[translate_to_text3] dom_id={:?}, converting {} floats to exclusions",
            dom_id,
            bfc_state.floats.floats.len()
        );
        bfc_state
            .floats
            .floats
            .iter()
            .enumerate()
            .map(|(i, float_box)| {
                let rect = crate::text3::cache::Rect {
                    x: float_box.rect.origin.x,
                    y: float_box.rect.origin.y,
                    width: float_box.rect.size.width,
                    height: float_box.rect.size.height,
                };
                debug_info!(
                    ctx,
                    "[translate_to_text3]   Exclusion #{}: {:?} at ({}, {}) size {}x{}",
                    i,
                    float_box.kind,
                    rect.x,
                    rect.y,
                    rect.width,
                    rect.height
                );
                ShapeBoundary::Rectangle(rect)
            })
            .collect()
    } else {
4722
        debug_info!(
4722
            ctx,
4722
            "[translate_to_text3] dom_id={:?}, NO bfc_state - no float exclusions",
            dom_id
        );
4722
        Vec::new()
    };
4722
    debug_info!(
4722
        ctx,
4722
        "[translate_to_text3] dom_id={:?}, available_size={}x{}, shape_exclusions.len()={}",
        dom_id,
        constraints.available_size.width,
        constraints.available_size.height,
4722
        shape_exclusions.len()
    );
    // Map text-align and justify-content from CSS to text3 enums.
4722
    let id = dom_id;
4722
    let node_data = &styled_dom.node_data.as_container()[id];
4722
    let node_state = &styled_dom.styled_nodes.as_container()[id].styled_node_state;
    // Read CSS Shapes properties
    // For reference box, use the element's CSS height if available, otherwise available_size
    // This is important because available_size.height might be infinite during auto height
    // calculation
4722
    let ref_box_height = if constraints.available_size.height.is_finite() {
801
        constraints.available_size.height
    } else {
        // Try to get explicit CSS height
        // NOTE: If height is infinite, we can't properly resolve % heights
        // This is a limitation - shape-inside with % heights requires finite containing block
3921
        styled_dom
3921
            .css_property_cache
3921
            .ptr
3921
            .get_height(node_data, &id, node_state)
3921
            .and_then(|v| v.get_property())
3921
            .and_then(|h| match h {
                LayoutHeight::Px(v) => {
                    // Only accept absolute units (px, pt, in, cm, mm) - no %, em, rem
                    // since we can't resolve relative units without proper context
                    match v.metric {
                        SizeMetric::Px => Some(v.number.get()),
                        SizeMetric::Pt => Some(v.number.get() * PT_TO_PX),
                        SizeMetric::In => Some(v.number.get() * 96.0),
                        SizeMetric::Cm => Some(v.number.get() * 96.0 / 2.54),
                        SizeMetric::Mm => Some(v.number.get() * 96.0 / 25.4),
                        _ => None, // Ignore %, em, rem
                    }
                }
                _ => None,
            })
3921
            .unwrap_or(constraints.available_size.width) // Fallback: use width as height (square)
    };
4722
    let reference_box = crate::text3::cache::Rect {
4722
        x: 0.0,
4722
        y: 0.0,
4722
        width: constraints.available_size.width,
4722
        height: ref_box_height,
4722
    };
    // shape-inside: Text flows within the shape boundary
4722
    debug_info!(ctx, "Checking shape-inside for node {:?}", id);
4722
    debug_info!(
4722
        ctx,
4722
        "Reference box: {:?} (available_size height was: {})",
        reference_box,
        constraints.available_size.height
    );
4722
    let shape_boundaries = if dom_declared & DOM_HAS_SHAPE_INSIDE != 0 {
        styled_dom
            .css_property_cache
            .ptr
            .get_shape_inside(node_data, &id, node_state)
            .and_then(|v| {
                debug_info!(ctx, "Got shape-inside value: {:?}", v);
                v.get_property()
            })
            .and_then(|shape_inside| {
                debug_info!(ctx, "shape-inside property: {:?}", shape_inside);
                if let ShapeInside::Shape(css_shape) = shape_inside {
                    debug_info!(
                        ctx,
                        "Converting CSS shape to ShapeBoundary: {:?}",
                        css_shape
                    );
                    let boundary =
                        ShapeBoundary::from_css_shape(css_shape, reference_box, ctx.debug_messages);
                    debug_info!(ctx, "Created ShapeBoundary: {:?}", boundary);
                    Some(vec![boundary])
                } else {
                    debug_info!(ctx, "shape-inside is None");
                    None
                }
            })
            .unwrap_or_default()
    } else {
4722
        Vec::new()
    };
4722
    debug_info!(
4722
        ctx,
4722
        "Final shape_boundaries count: {}",
4722
        shape_boundaries.len()
    );
    // shape-outside: Text wraps around the shape (adds to exclusions)
4722
    debug_info!(ctx, "Checking shape-outside for node {:?}", id);
4722
    if dom_declared & DOM_HAS_SHAPE_OUTSIDE != 0 {
        if let Some(shape_outside_value) = styled_dom
            .css_property_cache
            .ptr
            .get_shape_outside(node_data, &id, node_state)
        {
            debug_info!(ctx, "Got shape-outside value: {:?}", shape_outside_value);
            if let Some(shape_outside) = shape_outside_value.get_property() {
                debug_info!(ctx, "shape-outside property: {:?}", shape_outside);
                if let ShapeOutside::Shape(css_shape) = shape_outside {
                    debug_info!(
                        ctx,
                        "Converting CSS shape-outside to ShapeBoundary: {:?}",
                        css_shape
                    );
                    let boundary =
                        ShapeBoundary::from_css_shape(css_shape, reference_box, ctx.debug_messages);
                    debug_info!(ctx, "Created ShapeBoundary (exclusion): {:?}", boundary);
                    shape_exclusions.push(boundary);
                }
            }
        } else {
            debug_info!(ctx, "No shape-outside value found");
        }
4722
    }
    // TODO: clip-path will be used for rendering clipping (not text layout)
4722
    let writing_mode = get_writing_mode(styled_dom, id, node_state).unwrap_or_default();
4722
    let text_align = get_text_align(styled_dom, id, node_state).unwrap_or_default();
4722
    let text_justify = if dom_declared & DOM_HAS_TEXT_JUSTIFY != 0 {
        styled_dom
            .css_property_cache
            .ptr
            .get_text_justify(node_data, &id, node_state)
            .and_then(|s| s.get_property().copied())
            .unwrap_or_default()
    } else {
4722
        Default::default()
    };
    // Get font-size for resolving line-height
    // Use helper function which checks dependency chain first
4722
    let font_size = get_element_font_size(styled_dom, id, node_state);
4722
    let line_height_value = if dom_declared & DOM_HAS_LINE_HEIGHT != 0 {
        styled_dom
            .css_property_cache
            .ptr
            .get_line_height(node_data, &id, node_state)
            .and_then(|s| s.get_property().cloned())
            .unwrap_or_default()
    } else {
4722
        Default::default()
    };
4722
    let hyphenation = if dom_declared & DOM_HAS_HYPHENS != 0 {
        styled_dom
            .css_property_cache
            .ptr
            .get_hyphens(node_data, &id, node_state)
            .and_then(|s| s.get_property().copied())
            .unwrap_or_default()
    } else {
4722
        Default::default()
    };
4722
    let word_break_css = if dom_declared & DOM_HAS_WORD_BREAK != 0 {
        styled_dom
            .css_property_cache
            .ptr
            .get_word_break(node_data, &id, node_state)
            .and_then(|s| s.get_property().copied())
            .unwrap_or_default()
    } else {
4722
        Default::default()
    };
4722
    let overflow_wrap_css = if dom_declared & DOM_HAS_OVERFLOW_WRAP != 0 {
35
        styled_dom
35
            .css_property_cache
35
            .ptr
35
            .get_overflow_wrap(node_data, &id, node_state)
35
            .and_then(|s| s.get_property().copied())
35
            .unwrap_or_default()
    } else {
4687
        Default::default()
    };
4722
    let line_break_css = if dom_declared & DOM_HAS_LINE_BREAK != 0 {
        styled_dom
            .css_property_cache
            .ptr
            .get_line_break(node_data, &id, node_state)
            .and_then(|s| s.get_property().copied())
            .unwrap_or_default()
    } else {
4722
        Default::default()
    };
4722
    let text_align_last_css = if dom_declared & DOM_HAS_TEXT_ALIGN_LAST != 0 {
        styled_dom
            .css_property_cache
            .ptr
            .get_text_align_last(node_data, &id, node_state)
            .and_then(|s| s.get_property().copied())
            .unwrap_or_default()
    } else {
4722
        Default::default()
    };
4722
    let overflow_behaviour = get_overflow_x(styled_dom, id, node_state).unwrap_or_default();
    // +spec:display-property:21f728 - vertical-align shorthand resolves inline-level box alignment
    // +spec:display-property:98fa8e - alignment-baseline values for inline-level boxes in IFC (implemented via vertical-align shorthand)
    // +spec:display-property:1f71ad - baseline-shift + alignment-baseline longhands mapped through vertical-align
    // +spec:display-property:89dd7b - line-relative shift values (top/center/bottom) and aligned subtree alignment
    // +spec:inline-formatting-context:21da06 - vertical-align uses line-over/line-under sides via writing_mode logical mapping
    // +spec:inline-formatting-context:295603 - baseline alignment: vertical-align determines how inline boxes align (baseline, super, sub, etc.)
    // +spec:inline-formatting-context:7351bf - default alignment baseline is alphabetic in horizontal typographic mode
    // +spec:inline-formatting-context:85de3d - vertical-align shorthand: alignment within line box
    // +spec:inline-formatting-context:aa8af0 - alignment baseline chosen by vertical-align, defaults to parent's dominant baseline
    // +spec:inline-formatting-context:e475d2 - baseline and vertical-align control transverse alignment of inline content on line boxes
    // +spec:overflow:d44eac - vertical-align inline box alignment (CSS 2.2 model covers baseline/top/middle/bottom/sub/super/text-top/text-bottom)
    // +spec:writing-modes:313575 - alignment-baseline: inline-level boxes align baselines within parent inline box's alignment context along inline axis
    // +spec:writing-modes:60ad67 - inline layout aligns boxes in block axis via baselines
    // +spec:writing-modes:0127e5 - line-relative directions: line-over/under map to vertical-align top/bottom
    // Get vertical-align from CSS property cache (defaults to Baseline per CSS spec)
    // +spec:inline-formatting-context:686f8b - vertical-align shorthand: alignment-baseline + baseline-shift for inline boxes
    // +spec:inline-formatting-context:e579b6 - vertical-align / baseline alignment in inline context
    // +spec:inline-formatting-context:a01a75 - dominant baseline alignment for atomic inlines
4722
    let vertical_align = match get_vertical_align_property(styled_dom, id, node_state) {
945
        MultiValue::Exact(v) => v,
3777
        _ => StyleVerticalAlign::default(),
    };
    // +spec:display-property:c03a6b - baseline-shift (sub/super/length/percentage) and line-relative (top/center/bottom) shifts handled via vertical-align
4722
    let vertical_align = match vertical_align {
3777
        StyleVerticalAlign::Baseline => text3::cache::VerticalAlign::Baseline,
        StyleVerticalAlign::Top => text3::cache::VerticalAlign::Top,
945
        StyleVerticalAlign::Middle => text3::cache::VerticalAlign::Middle,
        StyleVerticalAlign::Bottom => text3::cache::VerticalAlign::Bottom,
        StyleVerticalAlign::Sub => text3::cache::VerticalAlign::Sub,
        // +spec:inline-formatting-context:fe563c - vertical-align: super shifts inline to superscript position
        // +spec:inline-formatting-context:fe563c - vertical-align:super shifts child to superscript position
        StyleVerticalAlign::Superscript => text3::cache::VerticalAlign::Super,
        StyleVerticalAlign::TextTop => text3::cache::VerticalAlign::TextTop,
        StyleVerticalAlign::TextBottom => text3::cache::VerticalAlign::TextBottom,
        // §10.8.1: <percentage> refers to line-height of the element itself
        StyleVerticalAlign::Percentage(p) => {
            let lh_n = line_height_value.inner.normalized();
            let resolved_lh = if lh_n < 0.0 { -lh_n } else { lh_n * font_size };
            let offset = p.normalized() * resolved_lh;
            text3::cache::VerticalAlign::Offset(offset)
        }
        // §10.8.1: <length> is absolute offset from baseline
        StyleVerticalAlign::Length(l) => {
            let offset = super::calc::resolve_pixel_value(&l, 0.0, font_size, font_size);
            text3::cache::VerticalAlign::Offset(offset)
        }
    };
    // +spec:block-formatting-context:987746 - text-orientation property (mixed/upright/sideways) for vertical writing modes
    // +spec:inline-formatting-context:cbe738 - text-orientation (mixed/upright/sideways) bi-orientational transform for vertical text
    // +spec:writing-modes:09a1bb - vertical typesetting orientation (upright/sideways) for vertical-rl/vertical-lr
    // +spec:writing-modes:2eb1b2 - text-orientation (mixed/upright/sideways) applied to vertical text layout
4722
    let text_orientation = match get_text_orientation_property(styled_dom, id, node_state) {
        MultiValue::Exact(o) => match o {
            StyleTextOrientation::Mixed => text3::cache::TextOrientation::Mixed,
            StyleTextOrientation::Upright => text3::cache::TextOrientation::Upright,
            // +spec:block-formatting-context:a606e6 - sideways text typeset rotated 90° CW in vertical modes
            StyleTextOrientation::Sideways => text3::cache::TextOrientation::Sideways,
        },
4722
        _ => text3::cache::TextOrientation::default(),
    };
    // +spec:display-property:8364c0 - direction property (ltr/rtl) sets paragraph embedding level for bidi algorithm
    // +spec:text-alignment-spacing:97b93a - direction property affects text-align:justify last-line alignment
    // +spec:writing-modes:73aaff - block elements inherit base direction from parent via CSS direction property
    // +spec:writing-modes:8a888b - line box inline base direction from containing block's direction
    // Get the direction property from the CSS cache (defaults to LTR if not set)
    // +spec:display-property:da3b59 - direction property specifies inline base direction for ordering inline-level content
    // +spec:inline-formatting-context:97af40 - direction property sets inline base direction for bidi, text alignment, overflow
    // +spec:writing-modes:2deb38 - bidirectional reordering via CSS direction property
    // +spec:writing-modes:fbb332 - in vertical writing modes, text-orientation:upright forces used direction to ltr
4722
    let direction = match constraints.writing_mode {
        LayoutWritingMode::VerticalRl | LayoutWritingMode::VerticalLr
            if matches!(text_orientation, text3::cache::TextOrientation::Upright) =>
        {
            Some(text3::cache::BidiDirection::Ltr)
        }
4722
        _ => match get_direction_property(styled_dom, id, node_state) {
4722
            MultiValue::Exact(d) => Some(match d {
4722
                StyleDirection::Ltr => text3::cache::BidiDirection::Ltr,
                StyleDirection::Rtl => text3::cache::BidiDirection::Rtl,
            }),
            _ => None,
        },
    };
    // Get unicode-bidi property for bidi algorithm configuration
    // +spec:containing-block:0d4914 - unicode-bidi: plaintext causes P2/P3 heuristics instead of HL1 override
4722
    let unicode_bidi_val = if dom_declared & DOM_HAS_UNICODE_BIDI != 0 {
        match get_unicode_bidi_property(styled_dom, id, node_state) {
            MultiValue::Exact(u) => match u {
                StyleUnicodeBidi::Normal => text3::cache::UnicodeBidi::Normal,
                StyleUnicodeBidi::Embed => text3::cache::UnicodeBidi::Embed,
                StyleUnicodeBidi::Isolate => text3::cache::UnicodeBidi::Isolate,
                StyleUnicodeBidi::BidiOverride => text3::cache::UnicodeBidi::BidiOverride,
                StyleUnicodeBidi::IsolateOverride => text3::cache::UnicodeBidi::IsolateOverride,
                StyleUnicodeBidi::Plaintext => text3::cache::UnicodeBidi::Plaintext,
            },
            _ => text3::cache::UnicodeBidi::Normal,
        }
    } else {
4722
        text3::cache::UnicodeBidi::Normal
    };
4722
    debug_info!(
4722
        ctx,
4722
        "dom_id={:?}, available_size={}x{}, setting available_width={}",
        dom_id,
        constraints.available_size.width,
        constraints.available_size.height,
        constraints.available_size.width
    );
    // +spec:box-model:8113d7 - text-indent treated as margin on start edge of line box
    // +spec:display-contents:5f95ac - text-indent: percentage=0 for intrinsic sizing, each-line and hanging keywords
    // +spec:floats:17c74a - text-indent applied to first line (5em indentation with no floats)
    // +spec:positioning:1e32b1 - text-indent with hanging/each-line keywords resolved and passed to text layout
4722
    let text_indent_prop = if dom_declared & DOM_HAS_TEXT_INDENT != 0 {
        styled_dom
            .css_property_cache
            .ptr
            .get_text_indent(node_data, &id, node_state)
            .and_then(|s| s.get_property().cloned())
    } else {
4722
        None
    };
4722
    let is_intrinsic_sizing = matches!(
4722
        constraints.available_width_type,
        Text3AvailableSpace::MinContent | Text3AvailableSpace::MaxContent
    );
    // +spec:intrinsic-sizing:0e8625 - percentage text-indent treated as 0 for intrinsic size contributions
4722
    let text_indent = text_indent_prop
4722
        .map(|ti| {
            // CSS Text 3 §8.1: "Percentages must be treated as 0 for the purpose
            // of calculating intrinsic size contributions"
            if is_intrinsic_sizing && ti.inner.to_percent().is_some() {
                return 0.0;
            }
            let context = ResolutionContext {
                element_font_size: get_element_font_size(styled_dom, id, node_state),
                parent_font_size: get_parent_font_size(styled_dom, id, node_state),
                root_font_size: get_root_font_size(styled_dom, node_state),
                containing_block_size: PhysicalSize::new(constraints.available_size.width, 0.0),
                element_size: None,
                viewport_size: PhysicalSize::new(ctx.viewport_size.width, ctx.viewport_size.height),
            };
            ti.inner
                .resolve_with_context(&context, PropertyContext::Other)
        })
4722
        .unwrap_or(0.0);
4722
    let text_indent_each_line = text_indent_prop.map(|ti| ti.each_line).unwrap_or(false);
4722
    let text_indent_hanging = text_indent_prop.map(|ti| ti.hanging).unwrap_or(false);
    // ResolutionContext shared by column-gap and column-width (both resolve
    // lengths against the same font/viewport, with no containing-block size).
4722
    let column_resolve_ctx = ResolutionContext {
4722
        element_font_size: get_element_font_size(styled_dom, id, node_state),
4722
        parent_font_size: get_parent_font_size(styled_dom, id, node_state),
4722
        root_font_size: get_root_font_size(styled_dom, node_state),
4722
        containing_block_size: PhysicalSize::new(0.0, 0.0),
4722
        element_size: None,
4722
        viewport_size: PhysicalSize::new(ctx.viewport_size.width, ctx.viewport_size.height),
4722
    };
    // Read a declared CSS property from the cache, returning None when the
    // DOM-level declared bit is clear (no node sets the property).
    macro_rules! declared_prop {
        ($bit:expr, $getter:ident) => {
            if dom_declared & $bit != 0 {
                styled_dom
                    .css_property_cache
                    .ptr
                    .$getter(node_data, &id, node_state)
                    .and_then(|s| s.get_property())
            } else {
                None
            }
        };
    }
    // Get column-gap for multi-column layout (default: normal = 1em)
4722
    let column_gap = declared_prop!(DOM_HAS_COLUMN_GAP, get_column_gap)
4722
        .map(|cg| {
            cg.inner
                .resolve_with_context(&column_resolve_ctx, PropertyContext::Other)
        })
4722
        .unwrap_or_else(|| get_element_font_size(styled_dom, id, node_state));
    // Get column-width for multi-column layout (None = auto)
4722
    let column_width =
4722
        declared_prop!(DOM_HAS_COLUMN_WIDTH, get_column_width).and_then(|cw| match cw {
            ColumnWidth::Auto => None,
            ColumnWidth::Length(px) => {
                Some(px.resolve_with_context(&column_resolve_ctx, PropertyContext::Other))
            }
        });
    // Get column-count for multi-column layout (default: 1 = no columns)
4722
    let explicit_column_count =
4722
        declared_prop!(DOM_HAS_COLUMN_COUNT, get_column_count).copied();
    // CSS multi-column: derive column count from column-width when column-count is auto.
    // Per spec: N = max(1, floor((available-width + column-gap) / (column-width + column-gap)))
4722
    let columns = match (explicit_column_count, column_width) {
        (Some(ColumnCount::Integer(n)), _) => n,
        (_, Some(cw)) if cw > 0.0 => {
            let avail = constraints.available_size.width;
            ((avail + column_gap) / (cw + column_gap)).floor().max(1.0) as u32
        }
4722
        _ => 1,
    };
    // +spec:line-breaking:b4928e - white-space values mapped to wrap/whitespace processing rules
    // Map white-space CSS property to TextWrap
4722
    let resolved_ws = match get_white_space_property(styled_dom, id, node_state) {
4722
        MultiValue::Exact(ws) => ws,
        _ => StyleWhiteSpace::Normal,
    };
4722
    let text_wrap = match resolved_ws {
4676
        StyleWhiteSpace::Normal => text3::cache::TextWrap::Wrap,
36
        StyleWhiteSpace::Nowrap => text3::cache::TextWrap::NoWrap,
6
        StyleWhiteSpace::Pre => text3::cache::TextWrap::NoWrap,
2
        StyleWhiteSpace::PreWrap => text3::cache::TextWrap::Wrap,
2
        StyleWhiteSpace::PreLine => text3::cache::TextWrap::Wrap,
        StyleWhiteSpace::BreakSpaces => text3::cache::TextWrap::Wrap,
    };
4722
    let white_space_mode = match resolved_ws {
4676
        StyleWhiteSpace::Normal => text3::cache::WhiteSpaceMode::Normal,
36
        StyleWhiteSpace::Nowrap => text3::cache::WhiteSpaceMode::Nowrap,
6
        StyleWhiteSpace::Pre => text3::cache::WhiteSpaceMode::Pre,
2
        StyleWhiteSpace::PreWrap => text3::cache::WhiteSpaceMode::PreWrap,
2
        StyleWhiteSpace::PreLine => text3::cache::WhiteSpaceMode::PreLine,
        StyleWhiteSpace::BreakSpaces => text3::cache::WhiteSpaceMode::BreakSpaces,
    };
    // +spec:block-formatting-context:fd60a8 - initial letter box is in-flow in its BFC, originating line box
    // +spec:block-formatting-context:c5ba02 - initial letter inline flow layout (alignment, white space collapsing)
    // +spec:block-formatting-context:83f8a7 - initial letter wrapping modes (none, all, first)
    // +spec:block-formatting-context:fef28d - initial letter box is in-flow in its BFC, part of originating line box
    // +spec:box-model:c3ce58 - initial letter block-start margin edge must be below containing block content edge
    // +spec:display-contents:568fe2 - initial letter participates in same IFC as its line
    // +spec:display-property:a89adb - initial letter boxes from non-replaced inline boxes and atomic inlines
    // +spec:display-property:4b59ce - initial-letter applies to inline-level boxes at start of first line
    // +spec:display-property:756cad - initial-letter sizing: drop/raise/sunken initial computation
    // +spec:display-property:8b08f4 - initial-letter applied to first inline-level child of block container
    // +spec:display-property:8c1dce - initial-letter property: size/sink for drop caps on inline-level boxes
    // +spec:display-property:b453a3 - initial-letter applies to inline-level boxes in IFC
    // +spec:display-property:b5e149 - initial letters are in-flow inline-level content, not floats
    // +spec:display-property:fa044e - initial-letter applies to first-child inline-level boxes
    // +spec:line-height:306d87 - initial-letter sizing must use containing block's line-height, not spanned lines' heights
    // +spec:writing-modes:903310 - atomic initial letters use normal sizing; only positioning is special
    // Get initial-letter for drop caps
    // +spec:display-property:4c69bf - read initial-letter-align for alignment points
4722
    let initial_letter_align = if dom_declared & DOM_HAS_INITIAL_LETTER_ALIGN != 0 {
        styled_dom
            .css_property_cache
            .ptr
            .get_initial_letter_align(node_data, &id, node_state)
            .and_then(|s| s.get_property())
            .map(|a| match a {
                azul_css::props::style::text::StyleInitialLetterAlign::Auto => text3::cache::InitialLetterAlign::Auto,
                azul_css::props::style::text::StyleInitialLetterAlign::Alphabetic => text3::cache::InitialLetterAlign::Alphabetic,
                azul_css::props::style::text::StyleInitialLetterAlign::Hanging => text3::cache::InitialLetterAlign::Hanging,
                azul_css::props::style::text::StyleInitialLetterAlign::Ideographic => text3::cache::InitialLetterAlign::Ideographic,
            })
            .unwrap_or(text3::cache::InitialLetterAlign::Auto)
    } else {
4722
        text3::cache::InitialLetterAlign::Auto
    };
    // +spec:display-property:5af252 - initial-letter on inline-level box not at line start uses normal
    // +spec:text-alignment-spacing:a17609 - sunken initial letters suppress letter-spacing and justification (not word-spacing) with adjacent content
    // +spec:display-property:68ab22 - initial-letter only applies in IFC (inline-level);
    // float!=none or position!=static causes display to compute to block (BFC), so
    // initial-letter naturally does not apply to those elements
    // +spec:writing-modes:c89d19 - initial-letter block-axis positioning: sink determines block offset
    // +spec:display-property:b67500 - initial-letter size/sink: values other than normal make box an initial letter box (inline-level, in-flow)
    // +spec:display-property:416f27 - initial-letter sink defaults to "drop" (sink = size floored) when omitted
4722
    let initial_letter = if dom_declared & DOM_HAS_INITIAL_LETTER != 0 {
        styled_dom
            .css_property_cache
            .ptr
            .get_initial_letter(node_data, &id, node_state)
            .and_then(|s| s.get_property())
            .map(|il| {
                use std::num::NonZeroUsize;
                let sink = match il.sink {
                    azul_css::corety::OptionU32::Some(s) => s,
                    azul_css::corety::OptionU32::None => il.size, // "drop" assumed: sink = size
                };
                text3::cache::InitialLetter {
                    size: il.size as f32,
                    sink,
                    count: NonZeroUsize::new(1).unwrap(),
                    align: initial_letter_align,
                }
            })
    } else {
4722
        None
    };
    // If initial-letter is set, compute the drop cap exclusion area and add it
    // to the shape exclusions so that text wraps around the enlarged letter.
    // +spec:box-model:d4adf6 - ancestor inline boundaries excluded via geometric exclusion
    // +spec:floats:c5e23f - floats in subsequent lines adjacent to a sunk initial letter must clear it
4722
    if let Some(ref il) = initial_letter {
        let lh_n = line_height_value.inner.normalized();
        let computed_line_height = if lh_n < 0.0 { -lh_n } else { lh_n * font_size };
        let (letter_w, letter_h) = layout_initial_letter(
            il.size,
            il.sink,
            constraints.available_size.width,
            computed_line_height,
        );
        if letter_w > 0.0 && letter_h > 0.0 {
            // Place the exclusion at the inline-start (x=0, y=0 relative to the IFC).
            // This creates a rectangular exclusion that text flows around.
            shape_exclusions.push(ShapeBoundary::Rectangle(crate::text3::cache::Rect {
                x: 0.0,
                y: 0.0,
                width: letter_w,
                height: letter_h,
            }));
        }
4722
    }
    // Get line-clamp for limiting visible lines
4722
    let line_clamp = if dom_declared & DOM_HAS_LINE_CLAMP != 0 {
        styled_dom
            .css_property_cache
            .ptr
            .get_line_clamp(node_data, &id, node_state)
            .and_then(|s| s.get_property())
            .and_then(|lc| std::num::NonZeroUsize::new(lc.max_lines))
    } else {
4722
        None
    };
    // Get hanging-punctuation for hanging punctuation marks
4722
    let hanging_punctuation = if dom_declared & DOM_HAS_HANGING_PUNCTUATION != 0 {
        styled_dom
            .css_property_cache
            .ptr
            .get_hanging_punctuation(node_data, &id, node_state)
            .and_then(|s| s.get_property())
            .map(|hp| hp.is_enabled())
            .unwrap_or(false)
    } else {
4722
        false
    };
    // Get text-combine-upright for vertical text combination
    // +spec:line-breaking:9f150a - text-combine-upright:all composes glyphs horizontally, ignoring letter-spacing and forced line breaks
    // +spec:line-breaking:1b88cd - text-combine-upright:all layout: inline-block with 1em square, ignoring forced line breaks
    // +spec:inline-formatting-context:c8d8d9 - text-combine-upright compression passed to text shaping engine
    // +spec:inline-formatting-context:f4ef7d - text-combine-upright layout rules (1em square composition)
4722
    let text_combine_upright = if dom_declared & DOM_HAS_TEXT_COMBINE_UPRIGHT != 0 {
        styled_dom
            .css_property_cache
            .ptr
            .get_text_combine_upright(node_data, &id, node_state)
            .and_then(|s| s.get_property())
            // +spec:display-property:6f174d - text-combine-upright horizontal-in-vertical composition
            .map(|tcu| match tcu {
                StyleTextCombineUpright::None => text3::cache::TextCombineUpright::None,
                StyleTextCombineUpright::All => text3::cache::TextCombineUpright::All,
                StyleTextCombineUpright::Digits(n) => text3::cache::TextCombineUpright::Digits(*n),
            })
    } else {
4722
        None
    };
    // Get exclusion-margin (CSS Exclusions L1) and shape-margin (CSS Shapes L1)
    // for shape exclusions. We sum both into a single margin knob — strictly,
    // they apply to different sources (exclusion-margin → CSS Exclusions,
    // shape-margin → shape-outside), but the layout solver currently keeps
    // a single per-IFC margin value, so the two get added.
4722
    let exclusion_margin_base = if dom_declared & DOM_HAS_EXCLUSION_MARGIN != 0 {
        styled_dom
            .css_property_cache
            .ptr
            .get_exclusion_margin(node_data, &id, node_state)
            .and_then(|s| s.get_property())
            .map(|em| em.inner.get() as f32)
            .unwrap_or(0.0)
    } else {
4722
        0.0
    };
4722
    let shape_margin = if dom_declared & DOM_HAS_SHAPE_MARGIN != 0 {
        styled_dom
            .css_property_cache
            .ptr
            .get_shape_margin(node_data, &id, node_state)
            .and_then(|s| s.get_property())
            .map(|sm| sm.inner.number.get() as f32)
            .unwrap_or(0.0)
    } else {
4722
        0.0
    };
4722
    let exclusion_margin = exclusion_margin_base + shape_margin;
    // Get hyphenation-language for language-specific hyphenation
4722
    let hyphenation_language = if dom_declared & DOM_HAS_HYPHENATION_LANGUAGE != 0 {
        styled_dom
            .css_property_cache
            .ptr
            .get_hyphenation_language(node_data, &id, node_state)
            .and_then(|s| s.get_property())
            .and_then(|hl| {
                #[cfg(feature = "text_layout_hyphenation")]
                {
                    use hyphenation::{Language, Load};
                    // Parse BCP 47 language code to hyphenation::Language
                    match hl.inner.as_str() {
                        "en-US" | "en" => Some(Language::EnglishUS),
                        "de-DE" | "de" => Some(Language::German1996),
                        "fr-FR" | "fr" => Some(Language::French),
                        "es-ES" | "es" => Some(Language::Spanish),
                        "it-IT" | "it" => Some(Language::Italian),
                        "pt-PT" | "pt" => Some(Language::Portuguese),
                        "nl-NL" | "nl" => Some(Language::Dutch),
                        "pl-PL" | "pl" => Some(Language::Polish),
                        "ru-RU" | "ru" => Some(Language::Russian),
                        "zh-CN" | "zh" => Some(Language::Chinese),
                        _ => None, // Unsupported language
                    }
                }
                #[cfg(not(feature = "text_layout_hyphenation"))]
                {
                    None::<crate::text3::script::Language>
                }
            })
    } else {
4722
        None
    };
    UnifiedConstraints {
4722
        exclusion_margin,
4722
        hyphenation_language,
4722
        text_indent,
4722
        text_indent_each_line,
4722
        text_indent_hanging,
4722
        initial_letter,
4722
        line_clamp,
4722
        columns,
4722
        column_gap,
4722
        hanging_punctuation,
4722
        text_wrap,
4722
        white_space_mode,
4722
        text_combine_upright,
4722
        segment_alignment: SegmentAlignment::Total,
4722
        overflow: match overflow_behaviour {
4686
            LayoutOverflow::Visible => text3::cache::OverflowBehavior::Visible,
36
            LayoutOverflow::Hidden | LayoutOverflow::Clip => text3::cache::OverflowBehavior::Hidden,
            LayoutOverflow::Scroll => text3::cache::OverflowBehavior::Scroll,
            LayoutOverflow::Auto => text3::cache::OverflowBehavior::Auto,
        },
        // Use the semantic available_width_type directly instead of converting from float.
        // This preserves MinContent/MaxContent semantics for intrinsic sizing.
4722
        available_width: constraints.available_width_type,
        // For scrollable containers (overflow: scroll/auto), don't constrain height
        // so that the full content is laid out and content_size is calculated correctly.
4722
        available_height: match overflow_behaviour {
            LayoutOverflow::Scroll | LayoutOverflow::Auto => None,
4722
            _ => Some(constraints.available_size.height),
        },
4722
        shape_boundaries, // CSS shape-inside: text flows within shape
4722
        shape_exclusions, // CSS shape-outside + floats: text wraps around shapes
4722
        writing_mode: Some(match writing_mode {
4722
            LayoutWritingMode::HorizontalTb => text3::cache::WritingMode::HorizontalTb,
            LayoutWritingMode::VerticalRl => text3::cache::WritingMode::VerticalRl,
            LayoutWritingMode::VerticalLr => text3::cache::WritingMode::VerticalLr,
        }),
4722
        direction, // Use the CSS direction property (currently defaulting to LTR)
4722
        unicode_bidi: unicode_bidi_val,
        // +spec:overflow:7ff7d1 - hyphens property: none/manual/auto hyphenation control
4722
        hyphenation: match hyphenation {
            StyleHyphens::None => text3::cache::Hyphens::None,
4722
            StyleHyphens::Manual => text3::cache::Hyphens::Manual,
            StyleHyphens::Auto => text3::cache::Hyphens::Auto,
        },
4722
        text_orientation,
        // +spec:text-alignment-spacing:6cb965 - text-align shorthand sets text-align-all (mapped here from computed value)
        // +spec:text-alignment-spacing:838967 - map text-align values (start/end/left/right/center/justify) to inline alignment
        // +spec:text-alignment-spacing:d9ea45 - property index: text-align, text-justify, letter-spacing mapped to layout
        // +spec:text-alignment-spacing:600fda - text-align values (left/right/center/justify) mapped per CSS Text §6.1
4722
        text_align: match text_align {
            StyleTextAlign::Start => text3::cache::TextAlign::Start,
            StyleTextAlign::End => text3::cache::TextAlign::End,
4722
            StyleTextAlign::Left => text3::cache::TextAlign::Left,
            StyleTextAlign::Right => text3::cache::TextAlign::Right,
            StyleTextAlign::Center => text3::cache::TextAlign::Center,
            StyleTextAlign::Justify => text3::cache::TextAlign::Justify,
        },
        // +spec:text-alignment-spacing:0ea31d - text-justify inter-word/inter-character/distribute mapped per §6.4
        // +spec:text-alignment-spacing:01244f - text-justify: none disables justification, auto uses inter-word as universal default
4722
        text_justify: match text_justify {
            LayoutTextJustify::None => text3::cache::JustifyContent::None,
4722
            LayoutTextJustify::Auto => text3::cache::JustifyContent::InterWord,
            LayoutTextJustify::InterWord => text3::cache::JustifyContent::InterWord,
            LayoutTextJustify::InterCharacter => text3::cache::JustifyContent::InterCharacter,
            LayoutTextJustify::Distribute => text3::cache::JustifyContent::InterCharacter, // distribute computes to inter-character
        },
        // +spec:line-height:79f3aa - line-height resolved: normal defaults to 1.2, <number>/<percentage> × font-size
        // Negative normalized() = absolute px value (convention from parser for "50px" etc.)
        line_height: text3::cache::LineHeight::Px({
4722
            let n = line_height_value.inner.normalized();
4722
            if n < 0.0 { -n } else { n * font_size }
        }),
        // container's first available font. Approximated as 80%/20% of font_size (typical
        // for Latin fonts). TODO: resolve actual font and use its OS/2 metrics.
4722
        strut_ascent: font_size * 0.8,
4722
        strut_descent: font_size * 0.2,
4722
        strut_x_height: font_size * 0.5, // 0.5em fallback per CSS Inline 3 Appendix A
        // ch unit width: try to get actual space width from font, fall back to 0.5 * font_size
4722
        ch_width: font_size * 0.5, // TODO: resolve from ParsedFontTrait::get_space_width()
4722
        vertical_align,
        // +spec:inline-formatting-context:48ce44 - overflow-wrap property: break at otherwise disallowed points to prevent overflow
        // +spec:line-breaking:bbb5f7 - overflow-wrap: anywhere vs break-word distinction for min-content
4722
        overflow_wrap: if word_break_css == StyleWordBreak::BreakWord {
            // +spec:line-breaking:815882 - break-word forces overflow-wrap: anywhere
            text3::cache::OverflowWrap::Anywhere
        } else {
4722
            match overflow_wrap_css {
4687
                StyleOverflowWrap::Normal => text3::cache::OverflowWrap::Normal,
                StyleOverflowWrap::Anywhere => text3::cache::OverflowWrap::Anywhere,
35
                StyleOverflowWrap::BreakWord => text3::cache::OverflowWrap::BreakWord,
            }
        },
4722
        text_align_last: match text_align_last_css {
4722
            StyleTextAlignLast::Auto => text3::cache::TextAlign::default(),
            StyleTextAlignLast::Start => text3::cache::TextAlign::Start,
            StyleTextAlignLast::End => text3::cache::TextAlign::End,
            StyleTextAlignLast::Left => text3::cache::TextAlign::Left,
            StyleTextAlignLast::Right => text3::cache::TextAlign::Right,
            StyleTextAlignLast::Center => text3::cache::TextAlign::Center,
            StyleTextAlignLast::Justify => text3::cache::TextAlign::Justify,
        },
        // +spec:line-breaking:815882 - word-break: break-word => normal + overflow-wrap: anywhere
4722
        word_break: match word_break_css {
4722
            StyleWordBreak::Normal | StyleWordBreak::BreakWord => text3::cache::WordBreak::Normal,
            StyleWordBreak::BreakAll => text3::cache::WordBreak::BreakAll,
            StyleWordBreak::KeepAll => text3::cache::WordBreak::KeepAll,
        },
        // +spec:white-space-processing:bc5f7b - line-break with break-spaces allows breaking before first space
        // CSS Text Level 3 §5.3: The line-break property affects preserved white space behavior:
        // - normal/pre-line: preserved white space at end/start of line is discarded
        // - nowrap/pre: wrapping is forbidden altogether
        // - pre-wrap: preserved white space hangs
        // - break-spaces: allows breaking before first space of a sequence
        // break-spaces allows wrapping preserved spaces to next line; for other white-space values,
        // preserved spaces at line ends are either discarded (normal, pre-line), wrapping is
        // forbidden (nowrap, pre), or they hang (pre-wrap).
4722
        line_break: match line_break_css {
4722
            StyleLineBreak::Auto => text3::cache::LineBreakStrictness::Auto,
            StyleLineBreak::Loose => text3::cache::LineBreakStrictness::Loose,
            StyleLineBreak::Normal => text3::cache::LineBreakStrictness::Normal,
            StyleLineBreak::Strict => text3::cache::LineBreakStrictness::Strict,
            StyleLineBreak::Anywhere => text3::cache::LineBreakStrictness::Anywhere,
        },
    }
4722
}
// Table Formatting Context (CSS 2.2 § 17)
// +spec:display-property:d887c0 - Table wrapper box BFC, caption-side, table grid layout (§17.4-17.5)
// +spec:positioning:930891 - Table formatting context implementation (CSS 2.2 § 17 introduction)
// +spec:inline-formatting-context:9c272d - CSS table model: row-primary structure, display-to-table-element mapping, visual formatting as rectangular grid
/// Lays out a Table Formatting Context.
/// Table column information for layout calculations
#[derive(Debug, Clone)]
pub struct TableColumnInfo {
    /// Minimum width required for this column
    pub min_width: f32,
    /// Maximum width desired for this column
    pub max_width: f32,
    /// Computed final width for this column
    pub computed_width: Option<f32>,
}
/// Information about a table cell for layout
#[derive(Debug, Clone)]
pub struct TableCellInfo {
    /// Node index in the layout tree
    pub node_index: usize,
    /// Column index (0-based)
    pub column: usize,
    /// Number of columns this cell spans
    pub colspan: usize,
    /// Row index (0-based)
    pub row: usize,
    /// Number of rows this cell spans
    pub rowspan: usize,
}
/// Table layout context - holds all information needed for table layout
#[derive(Debug)]
struct TableLayoutContext {
    /// Information about each column
    columns: Vec<TableColumnInfo>,
    /// Information about each cell
    cells: Vec<TableCellInfo>,
    /// Number of rows in the table
    num_rows: usize,
    /// Whether to use fixed or auto layout algorithm
    use_fixed_layout: bool,
    /// Computed height for each row
    row_heights: Vec<f32>,
    /// Computed baseline offset for each row (distance from row top to row baseline)
    row_baselines: Vec<f32>,
    // +spec:inline-formatting-context:440ca9 - border-collapse/border-spacing/visibility:collapse table properties (CSS 2.2 §17.5-17.6)
    /// Border collapse mode
    border_collapse: StyleBorderCollapse,
    /// Border spacing (only used when border_collapse is Separate)
    border_spacing: LayoutBorderSpacing,
    /// CSS 2.2 Section 17.4: Index of table-caption child, if any
    caption_index: Option<usize>,
    //   from display without forcing table re-layout
    /// CSS 2.2 Section 17.6: Rows with visibility:collapse (dynamic effects)
    /// Set of row indices that have visibility:collapse
    collapsed_rows: std::collections::HashSet<usize>,
    /// CSS 2.2 Section 17.6: Columns with visibility:collapse (dynamic effects)
    /// Set of column indices that have visibility:collapse
    collapsed_columns: std::collections::HashSet<usize>,
    /// Rows that are hidden-empty (zero height, border-spacing on only one side)
    hidden_empty_rows: std::collections::HashSet<usize>,
    /// Layout tree indices for each row (row index → layout node index)
    row_node_indices: Vec<usize>,
}
impl TableLayoutContext {
1155
    fn new() -> Self {
1155
        Self {
1155
            columns: Vec::new(),
1155
            cells: Vec::new(),
1155
            num_rows: 0,
1155
            use_fixed_layout: false,
1155
            row_heights: Vec::new(),
1155
            row_baselines: Vec::new(),
1155
            border_collapse: StyleBorderCollapse::Separate,
1155
            border_spacing: LayoutBorderSpacing::default(),
1155
            caption_index: None,
1155
            collapsed_rows: std::collections::HashSet::new(),
1155
            collapsed_columns: std::collections::HashSet::new(),
1155
            hidden_empty_rows: std::collections::HashSet::new(),
1155
            row_node_indices: Vec::new(),
1155
        }
1155
    }
}
// +spec:table-layout:485791 - Six superimposed table layers: table, column-group, column, row-group, row, cell (bottom to top)
// +spec:table-layout:dcdf1b - Collapsing border model: border conflict resolution uses layer priority (cell > row > row-group > column > column-group > table)
/// Source of a border in the border conflict resolution algorithm
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord)]
pub enum BorderSource {
    Table = 0,
    ColumnGroup = 1,
    Column = 2,
    RowGroup = 3,
    Row = 4,
    Cell = 5,
}
/// Information about a border for conflict resolution
#[derive(Debug, Clone)]
pub struct BorderInfo {
    pub width: f32,
    pub style: BorderStyle,
    pub color: ColorU,
    pub source: BorderSource,
}
impl BorderInfo {
    pub fn new(width: f32, style: BorderStyle, color: ColorU, source: BorderSource) -> Self {
        Self {
            width,
            style,
            color,
            source,
        }
    }
    // +spec:block-formatting-context:f772ae - border style priority for table border conflict resolution
    /// Get the priority of a border style for conflict resolution
    /// Higher number = higher priority
1400
    pub fn style_priority(style: &BorderStyle) -> u8 {
1400
        match style {
            BorderStyle::Hidden => 255, // Highest - suppresses all borders
            BorderStyle::None => 0,     // Lowest - loses to everything
280
            BorderStyle::Double => 8,
665
            BorderStyle::Solid => 7,
105
            BorderStyle::Dashed => 6,
70
            BorderStyle::Dotted => 5,
70
            BorderStyle::Ridge => 4,
70
            BorderStyle::Outset => 3,
70
            BorderStyle::Groove => 2,
70
            BorderStyle::Inset => 1,
        }
1400
    }
    // +spec:box-model:2255c2 - Collapsing border conflict resolution (hidden wins, then none loses, then wider wins, then style priority)
    // +spec:box-model:b42c79 - border conflict resolution: hidden wins, then wider, then style priority, then source
    // +spec:box-model:503e9e - border conflict resolution: hidden wins, then wider, then style priority, then source priority
    // +spec:box-model:7eb217 - Border conflict resolution: hidden > none < wider > style priority > source priority > left/top
    // +spec:overflow:1fb482 - Border conflict resolution per CSS 2.2 §17.6.2.1 (hidden wins, then wider, then style priority, then source priority)
    // +spec:table-layout:882560 - Border conflict resolution (17.6.2.1): hidden wins, none loses, wider wins, style priority, source priority
    /// Compare two borders for conflict resolution per CSS 2.2 Section 17.6.2.1
    /// Returns the winning border
    // +spec:table-layout:21053b - border conflict resolution: hidden suppresses all, style priorities
    // +spec:table-layout:076617 - border conflict resolution algorithm and border style semantics in collapsing model
1015
    pub fn resolve_conflict(a: &BorderInfo, b: &BorderInfo) -> Option<BorderInfo> {
        // 1. 'hidden' wins and suppresses all borders
1015
        if a.style == BorderStyle::Hidden || b.style == BorderStyle::Hidden {
105
            return None;
910
        }
        // 2. Filter out 'none' - if both are none, no border
910
        let a_is_none = a.style == BorderStyle::None;
910
        let b_is_none = b.style == BorderStyle::None;
910
        if a_is_none && b_is_none {
            return None;
910
        }
910
        if a_is_none {
35
            return Some(b.clone());
875
        }
875
        if b_is_none {
            return Some(a.clone());
875
        }
        // 3. Wider border wins
875
        if a.width > b.width {
            return Some(a.clone());
875
        }
875
        if b.width > a.width {
175
            return Some(b.clone());
700
        }
        // 4. If same width, compare style priority
700
        let a_priority = Self::style_priority(&a.style);
700
        let b_priority = Self::style_priority(&b.style);
700
        if a_priority > b_priority {
385
            return Some(a.clone());
315
        }
315
        if b_priority > a_priority {
35
            return Some(b.clone());
280
        }
        // 5. If same style, source priority:
        // Cell > Row > RowGroup > Column > ColumnGroup > Table
280
        if a.source > b.source {
245
            return Some(a.clone());
35
        }
35
        if b.source > a.source {
            return Some(b.clone());
35
        }
        // 6. Same priority - prefer first one (left/top in LTR)
35
        Some(a.clone())
1015
    }
}
/// Get border information for a node
fn get_border_info<T: ParsedFontTrait>(
    ctx: &LayoutContext<'_, T>,
    node: &LayoutNodeHot,
    source: BorderSource,
) -> (BorderInfo, BorderInfo, BorderInfo, BorderInfo) {
    use azul_css::props::{
        basic::{
            pixel::{PhysicalSize, PropertyContext, ResolutionContext},
            ColorU,
        },
        style::BorderStyle,
    };
    use get_element_font_size;
    use get_parent_font_size;
    use get_root_font_size;
    let default_border = BorderInfo::new(
        0.0,
        BorderStyle::None,
        ColorU {
            r: 0,
            g: 0,
            b: 0,
            a: 0,
        },
        source,
    );
    let Some(dom_id) = node.dom_node_id else {
        return (
            default_border.clone(),
            default_border.clone(),
            default_border.clone(),
            default_border.clone(),
        );
    };
    let node_data = &ctx.styled_dom.node_data.as_container()[dom_id];
    let node_state = ctx.styled_dom.styled_nodes.as_container()[dom_id].styled_node_state.clone();
    let cache = &ctx.styled_dom.css_property_cache.ptr;
    // FAST PATH: compact cache for normal state
    if let Some(ref cc) = cache.compact_cache {
        let idx = dom_id.index();
        // Border styles from packed u16
        let bts = cc.get_border_top_style(idx);
        let brs = cc.get_border_right_style(idx);
        let bbs = cc.get_border_bottom_style(idx);
        let bls = cc.get_border_left_style(idx);
        // Border colors from u32 RGBA
        let make_color = |raw: u32| -> ColorU {
            if raw == 0 {
                ColorU { r: 0, g: 0, b: 0, a: 0 }
            } else {
                ColorU {
                    r: ((raw >> 24) & 0xFF) as u8,
                    g: ((raw >> 16) & 0xFF) as u8,
                    b: ((raw >> 8) & 0xFF) as u8,
                    a: (raw & 0xFF) as u8,
                }
            }
        };
        let btc = make_color(cc.get_border_top_color_raw(idx));
        let brc = make_color(cc.get_border_right_color_raw(idx));
        let bbc = make_color(cc.get_border_bottom_color_raw(idx));
        let blc = make_color(cc.get_border_left_color_raw(idx));
        // Border widths from i16 × 10
        let decode_width = |raw: i16| -> f32 {
            if raw >= azul_css::compact_cache::I16_SENTINEL_THRESHOLD {
                0.0 // sentinel → fall back to 0
            } else {
                raw as f32 / 10.0
            }
        };
        let btw = decode_width(cc.get_border_top_width_raw(idx));
        let brw = decode_width(cc.get_border_right_width_raw(idx));
        let bbw = decode_width(cc.get_border_bottom_width_raw(idx));
        let blw = decode_width(cc.get_border_left_width_raw(idx));
        let top = if bts == BorderStyle::None { default_border.clone() }
            else { BorderInfo::new(btw, bts, btc, source) };
        let right = if brs == BorderStyle::None { default_border.clone() }
            else { BorderInfo::new(brw, brs, brc, source) };
        let bottom = if bbs == BorderStyle::None { default_border.clone() }
            else { BorderInfo::new(bbw, bbs, bbc, source) };
        let left = if bls == BorderStyle::None { default_border.clone() }
            else { BorderInfo::new(blw, bls, blc, source) };
        return (top, right, bottom, left);
    }
    // SLOW PATH: full cascade resolution
    let cache = &ctx.styled_dom.css_property_cache.ptr;
    // Create resolution context for border-width (em/rem support, no % support)
    let element_font_size = get_element_font_size(ctx.styled_dom, dom_id, &node_state);
    let parent_font_size = get_parent_font_size(ctx.styled_dom, dom_id, &node_state);
    let root_font_size = get_root_font_size(ctx.styled_dom, &node_state);
    let resolution_context = ResolutionContext {
        element_font_size,
        parent_font_size,
        root_font_size,
        // Not used for border-width
        containing_block_size: PhysicalSize::new(0.0, 0.0),
        // Not used for border-width
        element_size: None,
        viewport_size: PhysicalSize::new(ctx.viewport_size.width, ctx.viewport_size.height),
    };
    // Top border
    let top = cache
        .get_border_top_style(node_data, &dom_id, &node_state)
        .and_then(|s| s.get_property())
        .map(|style_val| {
            let width = cache
                .get_border_top_width(node_data, &dom_id, &node_state)
                .and_then(|w| w.get_property())
                .map(|w| {
                    w.inner
                        .resolve_with_context(&resolution_context, PropertyContext::BorderWidth)
                })
                .unwrap_or(0.0);
            let color = cache
                .get_border_top_color(node_data, &dom_id, &node_state)
                .and_then(|c| c.get_property())
                .map(|c| c.inner)
                .unwrap_or(ColorU {
                    r: 0,
                    g: 0,
                    b: 0,
                    a: 255,
                });
            BorderInfo::new(width, style_val.inner, color, source)
        })
        .unwrap_or_else(|| default_border.clone());
    // Right border
    let right = cache
        .get_border_right_style(node_data, &dom_id, &node_state)
        .and_then(|s| s.get_property())
        .map(|style_val| {
            let width = cache
                .get_border_right_width(node_data, &dom_id, &node_state)
                .and_then(|w| w.get_property())
                .map(|w| {
                    w.inner
                        .resolve_with_context(&resolution_context, PropertyContext::BorderWidth)
                })
                .unwrap_or(0.0);
            let color = cache
                .get_border_right_color(node_data, &dom_id, &node_state)
                .and_then(|c| c.get_property())
                .map(|c| c.inner)
                .unwrap_or(ColorU {
                    r: 0,
                    g: 0,
                    b: 0,
                    a: 255,
                });
            BorderInfo::new(width, style_val.inner, color, source)
        })
        .unwrap_or_else(|| default_border.clone());
    // Bottom border
    let bottom = cache
        .get_border_bottom_style(node_data, &dom_id, &node_state)
        .and_then(|s| s.get_property())
        .map(|style_val| {
            let width = cache
                .get_border_bottom_width(node_data, &dom_id, &node_state)
                .and_then(|w| w.get_property())
                .map(|w| {
                    w.inner
                        .resolve_with_context(&resolution_context, PropertyContext::BorderWidth)
                })
                .unwrap_or(0.0);
            let color = cache
                .get_border_bottom_color(node_data, &dom_id, &node_state)
                .and_then(|c| c.get_property())
                .map(|c| c.inner)
                .unwrap_or(ColorU {
                    r: 0,
                    g: 0,
                    b: 0,
                    a: 255,
                });
            BorderInfo::new(width, style_val.inner, color, source)
        })
        .unwrap_or_else(|| default_border.clone());
    // Left border
    let left = cache
        .get_border_left_style(node_data, &dom_id, &node_state)
        .and_then(|s| s.get_property())
        .map(|style_val| {
            let width = cache
                .get_border_left_width(node_data, &dom_id, &node_state)
                .and_then(|w| w.get_property())
                .map(|w| {
                    w.inner
                        .resolve_with_context(&resolution_context, PropertyContext::BorderWidth)
                })
                .unwrap_or(0.0);
            let color = cache
                .get_border_left_color(node_data, &dom_id, &node_state)
                .and_then(|c| c.get_property())
                .map(|c| c.inner)
                .unwrap_or(ColorU {
                    r: 0,
                    g: 0,
                    b: 0,
                    a: 255,
                });
            BorderInfo::new(width, style_val.inner, color, source)
        })
        .unwrap_or_else(|| default_border.clone());
    (top, right, bottom, left)
}
// +spec:table-layout:c5e446 - table-layout property (auto|fixed) controls layout algorithm selection
/// Get the table-layout property for a table node
1155
fn get_table_layout_property<T: ParsedFontTrait>(
1155
    ctx: &LayoutContext<'_, T>,
1155
    node: &LayoutNodeHot,
1155
) -> LayoutTableLayout {
1155
    let Some(dom_id) = node.dom_node_id else {
        return LayoutTableLayout::Auto;
    };
1155
    let node_data = &ctx.styled_dom.node_data.as_container()[dom_id];
1155
    let node_state = ctx.styled_dom.styled_nodes.as_container()[dom_id].styled_node_state.clone();
1155
    ctx.styled_dom
1155
        .css_property_cache
1155
        .ptr
1155
        .get_table_layout(node_data, &dom_id, &node_state)
1155
        .and_then(|prop| prop.get_property().copied())
1155
        .unwrap_or(LayoutTableLayout::Auto)
1155
}
/// Get the border-collapse property for a table node
1155
fn get_border_collapse_property<T: ParsedFontTrait>(
1155
    ctx: &LayoutContext<'_, T>,
1155
    node: &LayoutNodeHot,
1155
) -> StyleBorderCollapse {
1155
    let Some(dom_id) = node.dom_node_id else {
        return StyleBorderCollapse::Separate;
    };
    // FAST PATH: compact cache
1155
    if let Some(ref cc) = ctx.styled_dom.css_property_cache.ptr.compact_cache {
1155
        return cc.get_border_collapse(dom_id.index());
    }
    let node_data = &ctx.styled_dom.node_data.as_container()[dom_id];
    let node_state = ctx.styled_dom.styled_nodes.as_container()[dom_id].styled_node_state.clone();
    ctx.styled_dom
        .css_property_cache
        .ptr
        .get_border_collapse(node_data, &dom_id, &node_state)
        .and_then(|prop| prop.get_property().copied())
        .unwrap_or(StyleBorderCollapse::Separate)
1155
}
/// Get the border-spacing property for a table node
1155
fn get_border_spacing_property<T: ParsedFontTrait>(
1155
    ctx: &LayoutContext<'_, T>,
1155
    node: &LayoutNodeHot,
1155
) -> LayoutBorderSpacing {
1155
    if let Some(dom_id) = node.dom_node_id {
        // FAST PATH: compact cache
1155
        if let Some(ref cc) = ctx.styled_dom.css_property_cache.ptr.compact_cache {
1155
            let idx = dom_id.index();
1155
            let h_raw = cc.get_border_spacing_h_raw(idx);
1155
            let v_raw = cc.get_border_spacing_v_raw(idx);
            // If both are non-sentinel, use compact values
1155
            if h_raw < azul_css::compact_cache::I16_SENTINEL_THRESHOLD
1155
                && v_raw < azul_css::compact_cache::I16_SENTINEL_THRESHOLD
            {
1155
                return LayoutBorderSpacing::new_separate(
1155
                    azul_css::props::basic::pixel::PixelValue::px(h_raw as f32 / 10.0),
1155
                    azul_css::props::basic::pixel::PixelValue::px(v_raw as f32 / 10.0),
                );
            }
            // sentinel → fall through to slow path
        }
        let node_data = &ctx.styled_dom.node_data.as_container()[dom_id];
        let node_state = ctx.styled_dom.styled_nodes.as_container()[dom_id].styled_node_state.clone();
        if let Some(prop) = ctx.styled_dom.css_property_cache.ptr.get_border_spacing(
            node_data,
            &dom_id,
            &node_state,
        ) {
            if let Some(value) = prop.get_property() {
                return *value;
            }
        }
    }
    LayoutBorderSpacing::default() // Default: 0
1155
}
/// Get the empty-cells property for a table-cell node.
/// Returns Show (default) or Hide.
1190
fn get_empty_cells_property<T: ParsedFontTrait>(
1190
    ctx: &LayoutContext<'_, T>,
1190
    node: &LayoutNodeHot,
1190
) -> StyleEmptyCells {
1190
    let Some(dom_id) = node.dom_node_id else {
        return StyleEmptyCells::Show;
    };
1190
    let node_data = &ctx.styled_dom.node_data.as_container()[dom_id];
1190
    let node_state = ctx.styled_dom.styled_nodes.as_container()[dom_id].styled_node_state.clone();
1190
    ctx.styled_dom
1190
        .css_property_cache
1190
        .ptr
1190
        .get_empty_cells(node_data, &dom_id, &node_state)
1190
        .and_then(|prop| prop.get_property().copied())
1190
        .unwrap_or(StyleEmptyCells::Show)
1190
}
/// CSS 2.2 Section 17.4 - Tables in the visual formatting model:
///
/// "The caption box is a block box that retains its own content, padding,
/// border, and margin areas. The caption-side property specifies the position
/// of the caption box with respect to the table box."
///
/// Get the caption-side property for a table node.
/// Returns Top (default) or Bottom.
1155
fn get_caption_side_property<T: ParsedFontTrait>(
1155
    ctx: &LayoutContext<'_, T>,
1155
    node: &LayoutNodeHot,
1155
) -> StyleCaptionSide {
1155
    if let Some(dom_id) = node.dom_node_id {
1155
        let node_data = &ctx.styled_dom.node_data.as_container()[dom_id];
1155
        let node_state = ctx.styled_dom.styled_nodes.as_container()[dom_id].styled_node_state.clone();
        if let Some(prop) =
1155
            ctx.styled_dom
1155
                .css_property_cache
1155
                .ptr
1155
                .get_caption_side(node_data, &dom_id, &node_state)
        {
            if let Some(value) = prop.get_property() {
                return *value;
            }
1155
        }
    }
1155
    StyleCaptionSide::Top // Default per CSS 2.2
1155
}
//   removes entire row or column from display; space made available for other content;
//   spanned content clipped; does not otherwise affect table layout
// +spec:inline-formatting-context:9f5f31 - visibility:collapse for table rows/columns, border-collapse and border-spacing
/// CSS 2.2 Section 17.6 - Dynamic row and column effects:
///
// +spec:box-model:547563 - visibility:collapse removes table rows/columns; elsewhere same as hidden
/// "The 'visibility' value 'collapse' removes a row or column from display,
/// but it has a different effect than 'visibility: hidden' on other elements.
/// When a row or column is collapsed, the space normally occupied by the row
/// or column is removed."
///
/// Check if a node has visibility:collapse set.
///
/// This is used for table rows and columns to optimize dynamic hiding.
/// // +spec:overflow:ebb1f9 - For non-table elements, collapse == hidden (no special handling needed)
1190
fn is_visibility_collapsed<T: ParsedFontTrait>(
1190
    ctx: &LayoutContext<'_, T>,
1190
    node: &LayoutNodeHot,
1190
) -> bool {
1190
    if let Some(dom_id) = node.dom_node_id {
1190
        let node_state = ctx.styled_dom.styled_nodes.as_container()[dom_id].styled_node_state.clone();
1190
        if let MultiValue::Exact(value) = get_visibility(ctx.styled_dom, dom_id, &node_state) {
1190
            return matches!(value, StyleVisibility::Collapse);
        }
    }
    false
1190
}
// +spec:overflow:af97a8 - empty-cells in separated borders model; collapsing border overflow
// +spec:table-layout:dcdf1b - empty-cells property controls rendering of borders/backgrounds around empty cells in separated borders model
/// CSS 2.2 Section 17.6.1.1 - Borders and Backgrounds around empty cells
///
/// In the separated borders model, the 'empty-cells' property controls the rendering of
/// borders and backgrounds around cells that have no visible content. Empty means it has no
/// children, or has children that are only collapsed whitespace."
///
/// Check if a table cell is empty (has no visible content).
///
/// This is used by the rendering pipeline to decide whether to paint borders/backgrounds
/// when empty-cells: hide is set in separated border model.
///
//   in-flow content (including empty elements) other than collapsed whitespace
/// A cell is considered empty if:
///
/// - It has no children, OR
/// - It has children but no inline_layout_result (no rendered content)
///
/// Note: Full whitespace detection would require checking text content during rendering.
/// This function provides a basic check suitable for layout phase.
fn is_cell_empty(tree: &LayoutTree, cell_index: usize) -> bool {
    if tree.get(cell_index).is_none() {
        return true; // Invalid cell is considered empty
    }
    // No children = empty
    if tree.children(cell_index).is_empty() {
        return true;
    }
    // If cell has an inline layout result, check if it's empty
    if let Some(warm_node) = tree.warm(cell_index) {
        if let Some(ref cached_layout) = warm_node.inline_layout_result {
            // Check if inline layout has any rendered content
            // Empty inline layouts have no items (glyphs/fragments)
            // Note: This is a heuristic - full detection requires text content analysis
            return cached_layout.layout.items.is_empty();
        }
    }
    // Check if all children have no content
    // A more thorough check would recursively examine all descendants
    //
    // For now, we use a simple heuristic: if there are children, assume not empty
    // unless proven otherwise by inline_layout_result
    // Cell with children but no inline layout = likely has block-level content = not empty
    false
}
/// Main function to layout a table formatting context
// +spec:table-layout:235e8e - CSS 2.2 §17.1-17.2 table model: fixed/auto algorithms, row/column/cell/caption structure
// +spec:table-layout:a6422d - CSS table model: table structure analysis, row/column/cell layout, caption, border-collapse
1155
pub fn layout_table_fc<T: ParsedFontTrait>(
1155
    ctx: &mut LayoutContext<'_, T>,
1155
    tree: &mut LayoutTree,
1155
    text_cache: &mut crate::font_traits::TextLayoutCache,
1155
    node_index: usize,
1155
    constraints: &LayoutConstraints,
1155
) -> Result<LayoutOutput> {
1155
    debug_log!(ctx, "Laying out table");
1155
    debug_table_layout!(
1155
        ctx,
1155
        "node_index={}, available_size={:?}, writing_mode={:?}",
        node_index,
        constraints.available_size,
        constraints.writing_mode
    );
    // Multi-pass table layout algorithm:
    //
    // 1. Analyze table structure - identify rows, cells, columns
    // 2. Determine table-layout property (fixed vs auto)
    // 3. Calculate column widths
    // 4. Layout cells and calculate row heights
    // 5. Position cells in final grid
    // Get the table node to read CSS properties
1155
    let table_node = tree
1155
        .get(node_index)
1155
        .ok_or(LayoutError::InvalidTree)?
1155
        .clone();
    // Calculate the table's border-box width for column distribution
    // This accounts for the table's own width property (e.g., width: 100%)
1155
    let table_border_box_width = if let Some(dom_id) = table_node.dom_node_id {
        // Use calculate_used_size_for_node to resolve table width (respects width:100%)
1155
        let intrinsic = tree.warm(node_index).and_then(|w| w.intrinsic_sizes).unwrap_or_default();
1155
        let containing_block_size = LogicalSize {
1155
            width: constraints.available_size.width,
1155
            height: constraints.available_size.height,
1155
        };
1155
        let table_bp = table_node.box_props.unpack();
1155
        let table_size = crate::solver3::sizing::calculate_used_size_for_node(
1155
            ctx.styled_dom,
1155
            Some(dom_id),
1155
            &containing_block_size,
1155
            intrinsic,
1155
            &table_bp,
1155
            &ctx.viewport_size,
        )?;
1155
        table_size.width
    } else {
        constraints.available_size.width
    };
    // Subtract padding and border to get content-box width for column distribution
1155
    let tbp = table_node.box_props.unpack();
1155
    let table_content_box_width = {
1155
        let padding_width = tbp.padding.left + tbp.padding.right;
1155
        let border_width = tbp.border.left + tbp.border.right;
1155
        (table_border_box_width - padding_width - border_width).max(0.0)
    };
1155
    debug_table_layout!(ctx, "Table Layout Debug");
1155
    debug_table_layout!(ctx, "Node index: {}", node_index);
1155
    debug_table_layout!(
1155
        ctx,
1155
        "Available size from parent: {:.2} x {:.2}",
        constraints.available_size.width,
        constraints.available_size.height
    );
1155
    debug_table_layout!(ctx, "Table border-box width: {:.2}", table_border_box_width);
1155
    debug_table_layout!(
1155
        ctx,
1155
        "Table content-box width: {:.2}",
        table_content_box_width
    );
1155
    debug_table_layout!(
1155
        ctx,
1155
        "Table padding: L={:.2} R={:.2}",
        tbp.padding.left,
        tbp.padding.right
    );
1155
    debug_table_layout!(
1155
        ctx,
1155
        "Table border: L={:.2} R={:.2}",
        tbp.border.left,
        tbp.border.right
    );
1155
    debug_table_layout!(ctx, "=");
    // Phase 1: Analyze table structure
1155
    let mut table_ctx = analyze_table_structure(tree, node_index, ctx)?;
    // +spec:table-layout:ff5671 - table-layout property (fixed vs auto) controls column width algorithm
    // +spec:width-calculation:7a5b23 - table-layout property determines fixed vs auto algorithm (CSS 2.2 §17.5.2)
    // Phase 2: Read CSS properties and determine layout algorithm
1155
    let table_layout = get_table_layout_property(ctx, &table_node);
1155
    table_ctx.use_fixed_layout = matches!(table_layout, LayoutTableLayout::Fixed);
    // +spec:containing-block:cc1453 - collapsing border model: border-collapse property drives table border handling
    // Read border properties
1155
    table_ctx.border_collapse = get_border_collapse_property(ctx, &table_node);
1155
    table_ctx.border_spacing = get_border_spacing_property(ctx, &table_node);
1155
    debug_log!(
1155
        ctx,
1155
        "Table layout: {:?}, border-collapse: {:?}, border-spacing: {:?}",
        table_layout,
        table_ctx.border_collapse,
        table_ctx.border_spacing
    );
    // +spec:width-calculation:431d60 - fixed vs auto table layout column width algorithms (CSS 2.2 §17.5.2.1, §17.5.2.2)
    // Phase 3: Calculate column widths
1155
    if table_ctx.use_fixed_layout {
        // DEBUG: Log available width passed into fixed column calculation
        debug_table_layout!(
            ctx,
            "FIXED layout: table_content_box_width={:.2}",
            table_content_box_width
        );
        calculate_column_widths_fixed(ctx, tree, &mut table_ctx, table_content_box_width);
    } else {
        // Pass table_content_box_width for column distribution in auto layout
1155
        calculate_column_widths_auto_with_width(
1155
            &mut table_ctx,
1155
            tree,
1155
            text_cache,
1155
            ctx,
1155
            constraints,
1155
            table_content_box_width,
        )?;
    }
1155
    debug_table_layout!(ctx, "After column width calculation:");
1155
    debug_table_layout!(ctx, "  Number of columns: {}", table_ctx.columns.len());
2940
    for (i, col) in table_ctx.columns.iter().enumerate() {
2940
        debug_table_layout!(
2940
            ctx,
2940
            "  Column {}: width={:.2}",
            i,
2940
            col.computed_width.unwrap_or(0.0)
        );
    }
1155
    let total_col_width: f32 = table_ctx
1155
        .columns
1155
        .iter()
1155
        .filter_map(|c| c.computed_width)
1155
        .sum();
1155
    debug_table_layout!(ctx, "  Total column width: {:.2}", total_col_width);
    // Phase 4: Calculate row heights based on cell content
1155
    calculate_row_heights(&mut table_ctx, tree, text_cache, ctx, constraints)?;
    // Phase 5: Position cells in final grid and collect positions
1155
    let mut cell_positions =
1155
        position_table_cells(&mut table_ctx, tree, ctx, node_index, constraints)?;
    // Calculate final table size including border-spacing
1155
    let mut table_width: f32 = table_ctx
1155
        .columns
1155
        .iter()
1155
        .filter_map(|col| col.computed_width)
1155
        .sum();
1155
    let mut table_height: f32 = table_ctx.row_heights.iter().sum();
1155
    debug_table_layout!(
1155
        ctx,
1155
        "After calculate_row_heights: table_height={:.2}, row_heights={:?}",
        table_height,
        table_ctx.row_heights
    );
    // +spec:box-model:494f6b - collapsing border model: row-width formula and table border width computation
    // +spec:box-model:e7d0a3 - Separated borders model: border-spacing, empty-cells, collapsing border width calculation
    // +spec:box-sizing:ee702c - separated borders model: border-spacing between adjoining cells
    // Add border-spacing to table size if border-collapse is separate
    // +spec:box-model:acb81f - separated borders model: border-spacing between adjoining cell borders
    // +spec:box-model:e480b1 - table width = left inner padding edge to right inner padding edge (including border-spacing)
1155
    if table_ctx.border_collapse == StyleBorderCollapse::Separate {
        use get_element_font_size;
        use get_parent_font_size;
        use get_root_font_size;
        use PhysicalSize;
        use PropertyContext;
        use ResolutionContext;
1155
        let styled_dom = ctx.styled_dom;
1155
        let table_id = tree.nodes[node_index].dom_node_id.unwrap();
1155
        let table_state = &styled_dom.styled_nodes.as_container()[table_id].styled_node_state;
1155
        let spacing_context = ResolutionContext {
1155
            element_font_size: get_element_font_size(styled_dom, table_id, table_state),
1155
            parent_font_size: get_parent_font_size(styled_dom, table_id, table_state),
1155
            root_font_size: get_root_font_size(styled_dom, table_state),
1155
            containing_block_size: PhysicalSize::new(0.0, 0.0),
1155
            element_size: None,
1155
            viewport_size: PhysicalSize::new(ctx.viewport_size.width, ctx.viewport_size.height),
1155
        };
1155
        let h_spacing = table_ctx
1155
            .border_spacing
1155
            .horizontal
1155
            .resolve_with_context(&spacing_context, PropertyContext::Other)
1155
            .max(0.0);
1155
        let v_spacing = table_ctx
1155
            .border_spacing
1155
            .vertical
1155
            .resolve_with_context(&spacing_context, PropertyContext::Other)
1155
            .max(0.0);
        // Add spacing: left + (n-1 between columns) + right = n+1 spacings
1155
        let num_cols = table_ctx.columns.len();
1155
        if num_cols > 0 {
1155
            table_width += h_spacing * (num_cols + 1) as f32;
1155
        }
        // Add spacing: top + (n-1 between rows) + bottom = n+1 spacings
1155
        if table_ctx.num_rows > 0 {
1155
            let full_spacings = (table_ctx.num_rows + 1) as f32;
1155
            // Each hidden-empty row loses one side of border-spacing
1155
            let hidden_empty_count = table_ctx.hidden_empty_rows.len() as f32;
1155
            table_height += v_spacing * (full_spacings - hidden_empty_count);
1155
        }
    }
    // +spec:table-layout:24dbf9 - §17.4 table wrapper box model: caption positioning, BFC establishment
    // +spec:width-calculation:600f98 - caption-side positions caption above/below table box (CSS 2.2 §17.4)
    // CSS 2.2 Section 17.4: Layout and position the caption if present
    //
    // "The caption box is a block box that retains its own content,
    // padding, border, and margin areas."
1155
    let caption_side = get_caption_side_property(ctx, &table_node);
1155
    let mut caption_height = 0.0;
1155
    let mut table_y_offset = 0.0;
1155
    if let Some(caption_idx) = table_ctx.caption_index {
        debug_log!(
            ctx,
            "Laying out caption with caption-side: {:?}",
            caption_side
        );
        // Layout caption as a block with the table's width as available width
        let caption_constraints = LayoutConstraints {
            available_size: LogicalSize {
                width: table_width,
                height: constraints.available_size.height,
            },
            writing_mode: constraints.writing_mode,
            writing_mode_ctx: constraints.writing_mode_ctx,
            bfc_state: None, // Caption creates its own BFC
            text_align: constraints.text_align,
            containing_block_size: constraints.containing_block_size,
            available_width_type: Text3AvailableSpace::Definite(table_width),
        };
        // Layout the caption node
        let mut empty_float_cache = HashMap::new();
        let caption_result = layout_formatting_context(
            ctx,
            tree,
            text_cache,
            caption_idx,
            &caption_constraints,
            &mut empty_float_cache,
        )?;
        caption_height = caption_result.output.overflow_size.height;
        let caption_position = match caption_side {
            StyleCaptionSide::Top => {
                // Caption on top: position at y=0, table starts below caption
                table_y_offset = caption_height;
                LogicalPosition { x: 0.0, y: 0.0 }
            }
            StyleCaptionSide::Bottom => {
                // Caption on bottom: table starts at y=0, caption below table
                LogicalPosition {
                    x: 0.0,
                    y: table_height,
                }
            }
        };
        // Add caption position to the positions map
        cell_positions.insert(caption_idx, caption_position);
        debug_log!(
            ctx,
            "Caption positioned at x={:.2}, y={:.2}, height={:.2}",
            caption_position.x,
            caption_position.y,
            caption_height
        );
1155
    }
    // Adjust all table cell positions if caption is on top
1155
    if table_y_offset > 0.0 {
        debug_log!(
            ctx,
            "Adjusting table cells by y offset: {:.2}",
            table_y_offset
        );
        // Adjust cell positions in the map
        for cell_info in &table_ctx.cells {
            if let Some(pos) = cell_positions.get_mut(&cell_info.node_index) {
                pos.y += table_y_offset;
            }
        }
1155
    }
1155
    let total_height = table_height + caption_height;
1155
    debug_table_layout!(ctx, "Final table dimensions:");
1155
    debug_table_layout!(ctx, "  Content width (columns): {:.2}", table_width);
1155
    debug_table_layout!(ctx, "  Content height (rows): {:.2}", table_height);
1155
    debug_table_layout!(ctx, "  Caption height: {:.2}", caption_height);
1155
    debug_table_layout!(ctx, "  Total height: {:.2}", total_height);
1155
    debug_table_layout!(ctx, "End Table Debug");
    // Create output with the table's final size and cell positions
    // +spec:box-model:52fcfe - overflow_size must include borders that spill into margin in collapsing border model
1155
    let output = LayoutOutput {
1155
        overflow_size: LogicalSize {
1155
            width: table_width,
1155
            height: total_height,
1155
        },
1155
        // Cell positions calculated in position_table_cells
1155
        positions: cell_positions,
1155
        // line box or first in-flow table-row; if none, bottom of content edge
1155
        // TODO: implement proper table baseline propagation
1155
        baseline: None,
1155
    };
1155
    Ok(output)
1155
}
// +spec:display-property:f47f8a - Table structure analysis: caption positioning, row/column/row-group traversal per CSS 2.2 §17.4-17.5
/// Analyze the table structure to identify rows, cells, and columns
1155
fn analyze_table_structure<T: ParsedFontTrait>(
1155
    tree: &LayoutTree,
1155
    table_index: usize,
1155
    ctx: &mut LayoutContext<'_, T>,
1155
) -> Result<TableLayoutContext> {
1155
    let mut table_ctx = TableLayoutContext::new();
1155
    let table_node = tree.get(table_index).ok_or(LayoutError::InvalidTree)?;
    // +spec:width-calculation:0a2766 - table internal elements form rectangular grid of rows/columns (CSS 2.2 §17.5)
    // CSS 2.2 Section 17.4: A table may have one table-caption child.
    // Traverse children to find caption, columns/colgroups, rows, and row groups
1190
    for &child_idx in tree.children(table_index) {
1190
        if let Some(child) = tree.get(child_idx) {
            // Check if this is a table caption
1190
            if matches!(child.formatting_context, FormattingContext::TableCaption) {
                debug_log!(ctx, "Found table caption at index {}", child_idx);
                table_ctx.caption_index = Some(child_idx);
                continue;
1190
            }
            // CSS 2.2 Section 17.2: Check for column groups
1190
            if matches!(
1190
                child.formatting_context,
                FormattingContext::TableColumnGroup
            ) {
                analyze_table_colgroup(tree, child_idx, &mut table_ctx, ctx)?;
                continue;
1190
            }
            // Check if this is a table row or row group
1190
            match child.formatting_context {
                FormattingContext::TableRow => {
1190
                    analyze_table_row(tree, child_idx, &mut table_ctx, ctx)?;
                }
                FormattingContext::TableRowGroup => {
                    // Process rows within the row group
                    for &row_idx in tree.children(child_idx) {
                        if let Some(row) = tree.get(row_idx) {
                            if matches!(row.formatting_context, FormattingContext::TableRow) {
                                analyze_table_row(tree, row_idx, &mut table_ctx, ctx)?;
                            }
                        }
                    }
                }
                _ => {}
            }
        }
    }
1155
    debug_log!(
1155
        ctx,
1155
        "Table structure: {} rows, {} columns, {} cells{}",
        table_ctx.num_rows,
1155
        table_ctx.columns.len(),
1155
        table_ctx.cells.len(),
1155
        if table_ctx.caption_index.is_some() {
            ", has caption"
        } else {
1155
            ""
        }
    );
1155
    Ok(table_ctx)
1155
}
/// Analyze a table column group to identify columns and track collapsed columns
///
/// - CSS 2.2 Section 17.2: Column groups contain columns
/// - CSS 2.2 Section 17.6: Columns can have visibility:collapse
fn analyze_table_colgroup<T: ParsedFontTrait>(
    tree: &LayoutTree,
    colgroup_index: usize,
    table_ctx: &mut TableLayoutContext,
    ctx: &mut LayoutContext<'_, T>,
) -> Result<()> {
    let colgroup_node = tree.get(colgroup_index).ok_or(LayoutError::InvalidTree)?;
    // Check if the colgroup itself has visibility:collapse
    if is_visibility_collapsed(ctx, colgroup_node) {
        // All columns in this group should be collapsed
        // TODO: For now, just mark the group (actual column indices will be determined later)
        debug_log!(
            ctx,
            "Column group at index {} has visibility:collapse",
            colgroup_index
        );
    }
    // Check for individual column elements within the group
    for &col_idx in tree.children(colgroup_index) {
        if let Some(col_node) = tree.get(col_idx) {
            // Note: Individual columns don't have a FormattingContext::TableColumn
            // They are represented as children of TableColumnGroup
            // Check visibility:collapse on each column
            if is_visibility_collapsed(ctx, col_node) {
                // We need to determine the actual column index this represents
                // For now, we'll track it during cell analysis
                debug_log!(ctx, "Column at index {} has visibility:collapse", col_idx);
            }
        }
    }
    Ok(())
}
// +spec:display-property:7f167c - Table grid cell placement: rows fill table top-to-bottom, cells placed left-to-right with colspan/rowspan
/// Analyze a table row to identify cells and update column count
1190
fn analyze_table_row<T: ParsedFontTrait>(
1190
    tree: &LayoutTree,
1190
    row_index: usize,
1190
    table_ctx: &mut TableLayoutContext,
1190
    ctx: &mut LayoutContext<'_, T>,
1190
) -> Result<()> {
    // +spec:inline-formatting-context:3f8091 - table visual layout: cells occupy grid cells, row/column spanning
1190
    let row_node = tree.get(row_index).ok_or(LayoutError::InvalidTree)?;
1190
    let row_num = table_ctx.num_rows;
1190
    table_ctx.num_rows += 1;
    // Track the layout tree index for this row (for positioning/painting)
1190
    if table_ctx.row_node_indices.len() <= row_num {
1190
        table_ctx.row_node_indices.resize(row_num + 1, 0);
1190
    }
1190
    table_ctx.row_node_indices[row_num] = row_index;
    // CSS 2.2 Section 17.6: Check if this row has visibility:collapse
1190
    if is_visibility_collapsed(ctx, row_node) {
        debug_log!(ctx, "Row {} has visibility:collapse", row_num);
        table_ctx.collapsed_rows.insert(row_num);
1190
    }
1190
    let mut col_index = 0;
2975
    for &cell_idx in tree.children(row_index) {
2975
        if let Some(cell) = tree.get(cell_idx) {
2975
            if matches!(cell.formatting_context, FormattingContext::TableCell) {
                // Get colspan and rowspan (TODO: from CSS properties)
2975
                let colspan = 1; // TODO: Get from CSS
2975
                let rowspan = 1; // TODO: Get from CSS
2975
                let cell_info = TableCellInfo {
2975
                    node_index: cell_idx,
2975
                    column: col_index,
2975
                    colspan,
2975
                    row: row_num,
2975
                    rowspan,
2975
                };
2975
                table_ctx.cells.push(cell_info);
                // Update column count
2975
                let max_col = col_index + colspan;
5915
                while table_ctx.columns.len() < max_col {
2940
                    table_ctx.columns.push(TableColumnInfo {
2940
                        min_width: 0.0,
2940
                        max_width: 0.0,
2940
                        computed_width: None,
2940
                    });
2940
                }
2975
                col_index += colspan;
            }
        }
    }
1190
    Ok(())
1190
}
// +spec:overflow:66f584 - Fixed table layout: cells use overflow property to clip overflowing content
// +spec:positioning:46070a - Fixed table layout (17.5.2.1) and auto table layout (17.5.2.2) column width algorithms
// +spec:table-layout:875401 - Fixed table layout algorithm (17.5.2.1): column widths from first-row cells, remaining columns divide space equally, table width = max(width property, sum of columns)
/// Calculate column widths using the fixed table layout algorithm
/// // +spec:overflow:de613c - Fixed table layout algorithm (CSS 2.2 Section 17.5.2.1)
// +spec:table-layout:8b72b3 - fixed table layout: column width from column elements/first-row cells, remaining columns equal division
///
/// CSS 2.2 Section 17.5.2.1: In fixed table layout, the horizontal layout
/// does not depend on cell contents. Column widths are determined by:
/// 1. Column elements with explicit (non-auto) width
/// 2. First-row cells with explicit (non-auto) width
/// 3. Remaining columns equally divide remaining horizontal space
///
/// CSS 2.2 Section 17.6: Columns with visibility:collapse are excluded
/// from width calculations
// +spec:table-layout:c5e446 - Fixed table layout algorithm: column widths from col elements or first-row cells, remaining columns divide equally
/// +spec:width-calculation:8c958a - Fixed table layout: column widths from col elements, first-row cells, then equal distribution (CSS 2.2 §17.5.2.1)
fn calculate_column_widths_fixed<T: ParsedFontTrait>(
    ctx: &mut LayoutContext<'_, T>,
    tree: &LayoutTree,
    table_ctx: &mut TableLayoutContext,
    available_width: f32,
) {
    debug_table_layout!(
        ctx,
        "calculate_column_widths_fixed: num_cols={}, available_width={:.2}",
        table_ctx.columns.len(),
        available_width
    );
    let num_cols = table_ctx.columns.len();
    if num_cols == 0 {
        return;
    }
    let num_visible_cols = num_cols - table_ctx.collapsed_columns.len();
    if num_visible_cols == 0 {
        for col in &mut table_ctx.columns {
            col.computed_width = Some(0.0);
        }
        return;
    }
    // Step 1 (column elements) is skipped because column elements don't store
    // explicit widths in the current table structure analysis.
    // Step 2: Check first-row cells for explicit width properties.
    let mut col_has_width = vec![false; num_cols];
    for cell_info in &table_ctx.cells {
        if cell_info.row != 0 {
            continue; // Only consider cells in the first row
        }
        if table_ctx.collapsed_columns.contains(&cell_info.column) {
            continue;
        }
        // Look up the cell's CSS width via its dom_node_id
        let dom_id = match tree.get(cell_info.node_index).and_then(|n| n.dom_node_id) {
            Some(id) => id,
            None => continue,
        };
        let node_state = &ctx.styled_dom.styled_nodes.as_container()[dom_id].styled_node_state;
        let css_width = get_css_width(ctx.styled_dom, dom_id, &node_state);
        let explicit_px = match css_width.unwrap_or_default() {
            LayoutWidth::Px(px) => {
                resolve_size_metric(
                    px.metric,
                    px.number.get(),
                    available_width,
                    ctx.viewport_size,
                )
            }
            LayoutWidth::Auto | LayoutWidth::MinContent | LayoutWidth::MaxContent
            | LayoutWidth::Calc(_) | LayoutWidth::FitContent(_) => continue,
        };
        if cell_info.colspan == 1 {
            table_ctx.columns[cell_info.column].computed_width = Some(explicit_px);
            col_has_width[cell_info.column] = true;
        } else {
            let mut visible_span_count = 0;
            for offset in 0..cell_info.colspan {
                let col_idx = cell_info.column + offset;
                if col_idx < num_cols && !table_ctx.collapsed_columns.contains(&col_idx) {
                    visible_span_count += 1;
                }
            }
            if visible_span_count > 0 {
                let per_col = explicit_px / visible_span_count as f32;
                for offset in 0..cell_info.colspan {
                    let col_idx = cell_info.column + offset;
                    if col_idx < num_cols
                        && !table_ctx.collapsed_columns.contains(&col_idx)
                        && !col_has_width[col_idx]
                    {
                        table_ctx.columns[col_idx].computed_width = Some(per_col);
                        col_has_width[col_idx] = true;
                    }
                }
            }
        }
    }
    let used_width: f32 = table_ctx.columns.iter().enumerate()
        .filter(|(idx, _)| col_has_width[*idx] && !table_ctx.collapsed_columns.contains(idx))
        .filter_map(|(_, c)| c.computed_width)
        .sum();
    let remaining_width = (available_width - used_width).max(0.0);
    let num_remaining = table_ctx.columns.iter().enumerate()
        .filter(|(idx, _)| !col_has_width[*idx] && !table_ctx.collapsed_columns.contains(idx))
        .count();
    if num_remaining > 0 {
        let width_per_remaining = remaining_width / num_remaining as f32;
        for (col_idx, col) in table_ctx.columns.iter_mut().enumerate() {
            if table_ctx.collapsed_columns.contains(&col_idx) {
                col.computed_width = Some(0.0);
            } else if !col_has_width[col_idx] {
                col.computed_width = Some(width_per_remaining);
            }
        }
    }
    // Set collapsed columns to zero width
    for (col_idx, col) in table_ctx.columns.iter_mut().enumerate() {
        if table_ctx.collapsed_columns.contains(&col_idx) {
            col.computed_width = Some(0.0);
        }
    }
    let total_col_width: f32 = table_ctx.columns.iter()
        .filter_map(|c| c.computed_width)
        .sum();
    if available_width > total_col_width && num_visible_cols > 0 {
        let extra = available_width - total_col_width;
        let extra_per_col = extra / num_visible_cols as f32;
        for (col_idx, col) in table_ctx.columns.iter_mut().enumerate() {
            if !table_ctx.collapsed_columns.contains(&col_idx) {
                if let Some(ref mut w) = col.computed_width {
                    *w += extra_per_col;
                }
            }
        }
    }
}
/// Recursively clear the layout cache for every node in a subtree.
///
/// A fixed-depth walk is not enough: a table cell like
/// `<td><span><a>text</a></span></td>` has 4+ levels once the anonymous IFC
/// wrapper is inserted, and any stale cache below that level would feed a
/// narrow intrinsic width back into `measure_cell_content_width`.
18900
fn clear_subtree_cache(
18900
    tree: &LayoutTree,
18900
    cache_map: &mut crate::solver3::cache::LayoutCacheMap,
18900
    root: usize,
18900
) {
18900
    if root < cache_map.entries.len() {
18900
        cache_map.entries[root].clear();
18900
    }
18900
    let child_ids: Vec<usize> = tree.children(root).to_vec();
31850
    for child in child_ids {
12950
        clear_subtree_cache(tree, cache_map, child);
12950
    }
18900
}
/// Measure a cell's content width for a given intrinsic sizing mode.
///
/// CSS 2.2 Section 17.5.2.2: shared helper for min-content and max-content
/// width measurement. Lays out the cell subtree in ComputeSize mode and
/// returns the border-box width (content + padding + border).
5950
fn measure_cell_content_width<T: ParsedFontTrait>(
5950
    ctx: &mut LayoutContext<'_, T>,
5950
    tree: &mut LayoutTree,
5950
    text_cache: &mut crate::font_traits::TextLayoutCache,
5950
    cell_index: usize,
5950
    constraints: &LayoutConstraints,
5950
    sizing_mode: crate::text3::cache::AvailableSpace,
5950
) -> Result<f32> {
5950
    let width_type = match sizing_mode {
2975
        crate::text3::cache::AvailableSpace::MinContent => Text3AvailableSpace::MinContent,
2975
        crate::text3::cache::AvailableSpace::MaxContent => Text3AvailableSpace::MaxContent,
        crate::text3::cache::AvailableSpace::Definite(w) => Text3AvailableSpace::Definite(w),
    };
5950
    let cell_constraints = LayoutConstraints {
5950
        available_size: LogicalSize {
5950
            width: sizing_mode.to_f32_for_layout(),
5950
            height: f32::INFINITY,
5950
        },
5950
        writing_mode: constraints.writing_mode,
5950
        writing_mode_ctx: constraints.writing_mode_ctx,
5950
        bfc_state: None,
5950
        text_align: constraints.text_align,
5950
        containing_block_size: constraints.containing_block_size,
5950
        available_width_type: width_type,
5950
    };
5950
    let mut temp_positions: super::PositionVec = Vec::new();
5950
    let mut temp_scrollbar_reflow = false;
5950
    let mut temp_float_cache = HashMap::new();
    // Clear cached layout for this cell and ALL its descendants so that
    // min/max-content measurement uses unconstrained width, not a stale
    // result from a previous pass with narrower constraints. Deeply nested
    // inlines (`<td><span><a>text</a></span></td>`) need recursion; a fixed
    // 2-level walk left the `<a>` at level 3 with a stale cached 0-width.
5950
    clear_subtree_cache(tree, &mut ctx.cache_map, cell_index);
5950
    crate::solver3::cache::calculate_layout_for_subtree(
5950
        ctx,
5950
        tree,
5950
        text_cache,
5950
        cell_index,
5950
        LogicalPosition::zero(),
5950
        cell_constraints.available_size,
5950
        &mut temp_positions,
5950
        &mut temp_scrollbar_reflow,
5950
        &mut temp_float_cache,
5950
        crate::solver3::cache::ComputeMode::ComputeSize,
    )?;
5950
    let cell_bp = tree.get(cell_index)
5950
        .ok_or(LayoutError::InvalidTree)?
5950
        .box_props.unpack();
5950
    let padding = &cell_bp.padding;
5950
    let border = &cell_bp.border;
5950
    let wm = constraints.writing_mode;
    // For min/max-content measurement, use the overflow content size (actual
    // content width) rather than used_size. used_size for auto-width blocks
    // fills the containing block, which is huge (f32::MAX/2) during
    // intrinsic sizing — that would make every column appear infinitely wide.
5950
    let content_width = tree.warm(cell_index)
5950
        .and_then(|w| w.overflow_content_size)
5950
        .map(|s| s.width)
5950
        .unwrap_or_else(|| {
            tree.get(cell_index)
                .and_then(|n| n.used_size)
                .map(|s| s.width)
                .unwrap_or(0.0)
        });
5950
    Ok(content_width
5950
        + padding.cross_start(wm) + padding.cross_end(wm)
5950
        + border.cross_start(wm) + border.cross_end(wm))
5950
}
/// Measure a cell's minimum content width (with maximum wrapping)
2975
fn measure_cell_min_content_width<T: ParsedFontTrait>(
2975
    ctx: &mut LayoutContext<'_, T>,
2975
    tree: &mut LayoutTree,
2975
    text_cache: &mut crate::font_traits::TextLayoutCache,
2975
    cell_index: usize,
2975
    constraints: &LayoutConstraints,
2975
) -> Result<f32> {
2975
    measure_cell_content_width(
2975
        ctx, tree, text_cache, cell_index, constraints,
2975
        crate::text3::cache::AvailableSpace::MinContent,
    )
2975
}
/// Measure a cell's maximum content width (without wrapping)
2975
fn measure_cell_max_content_width<T: ParsedFontTrait>(
2975
    ctx: &mut LayoutContext<'_, T>,
2975
    tree: &mut LayoutTree,
2975
    text_cache: &mut crate::font_traits::TextLayoutCache,
2975
    cell_index: usize,
2975
    constraints: &LayoutConstraints,
2975
) -> Result<f32> {
2975
    measure_cell_content_width(
2975
        ctx, tree, text_cache, cell_index, constraints,
2975
        crate::text3::cache::AvailableSpace::MaxContent,
    )
2975
}
/// Calculate column widths using the auto table layout algorithm
fn calculate_column_widths_auto<T: ParsedFontTrait>(
    table_ctx: &mut TableLayoutContext,
    tree: &mut LayoutTree,
    text_cache: &mut crate::font_traits::TextLayoutCache,
    ctx: &mut LayoutContext<'_, T>,
    constraints: &LayoutConstraints,
) -> Result<()> {
    calculate_column_widths_auto_with_width(
        table_ctx,
        tree,
        text_cache,
        ctx,
        constraints,
        constraints.available_size.width,
    )
}
/// Calculate column widths using the auto table layout algorithm with explicit table width
// +spec:display-property:05c8e8 - CSS 2.2 §17.5.2.2 automatic table layout: column min/max widths, table width = max(W or CB, CAPMIN, MIN), extra width distributed over columns
/// +spec:overflow:29edde - CSS 2.2 §17.5.2.2 automatic table layout: MCW/max-content per cell, column min/max, colspan distribution, final width determination
// +spec:table-layout:23a215 - automatic table layout: MCW/max cell widths, column min/max, colspan distribution, table width from MAX/MIN/CAPMIN
// +spec:table-layout:5e1145 - Automatic table layout: MCW/max-content per cell, column min/max, colspan distribution, final width from MIN/MAX
// +spec:width-calculation:42dfca - CSS 2.2 §17.5.2.2 automatic table layout: MCW/max-content per cell, column min/max, multi-span distribution, final table width
/// +spec:width-calculation:335ef1 - Automatic table layout: width given by column widths and borders (CSS 2.2 §17.5.2.2)
1155
fn calculate_column_widths_auto_with_width<T: ParsedFontTrait>(
1155
    table_ctx: &mut TableLayoutContext,
1155
    tree: &mut LayoutTree,
1155
    text_cache: &mut crate::font_traits::TextLayoutCache,
1155
    ctx: &mut LayoutContext<'_, T>,
1155
    constraints: &LayoutConstraints,
1155
    table_width: f32,
1155
) -> Result<()> {
    // Auto layout: calculate min/max content width for each cell
1155
    let num_cols = table_ctx.columns.len();
1155
    if num_cols == 0 {
        return Ok(());
1155
    }
    // Step 1: Measure all cells to determine column min/max widths
    // CSS 2.2 Section 17.6: Skip cells in collapsed columns
4130
    for cell_info in &table_ctx.cells {
        // Skip cells in collapsed columns
2975
        if table_ctx.collapsed_columns.contains(&cell_info.column) {
            continue;
2975
        }
        // Skip cells that span into collapsed columns
2975
        let mut spans_collapsed = false;
2975
        for col_offset in 0..cell_info.colspan {
2975
            if table_ctx
2975
                .collapsed_columns
2975
                .contains(&(cell_info.column + col_offset))
            {
                spans_collapsed = true;
                break;
2975
            }
        }
2975
        if spans_collapsed {
            continue;
2975
        }
2975
        let min_width = measure_cell_min_content_width(
2975
            ctx,
2975
            tree,
2975
            text_cache,
2975
            cell_info.node_index,
2975
            constraints,
        )?;
2975
        let max_width = measure_cell_max_content_width(
2975
            ctx,
2975
            tree,
2975
            text_cache,
2975
            cell_info.node_index,
2975
            constraints,
        )?;
        // Handle single-column cells
2975
        if cell_info.colspan == 1 {
2975
            let col = &mut table_ctx.columns[cell_info.column];
2975
            col.min_width = col.min_width.max(min_width);
2975
            col.max_width = col.max_width.max(max_width);
2975
        } else {
            // Handle multi-column cells (colspan > 1)
            // Distribute the cell's min/max width across the spanned columns
            distribute_cell_width_across_columns(
                &mut table_ctx.columns,
                cell_info.column,
                cell_info.colspan,
                min_width,
                max_width,
                &table_ctx.collapsed_columns,
            );
        }
    }
    // Step 2: Calculate final column widths based on available space
    // Exclude collapsed columns from total width calculations
1155
    let total_min_width: f32 = table_ctx
1155
        .columns
1155
        .iter()
1155
        .enumerate()
2940
        .filter(|(idx, _)| !table_ctx.collapsed_columns.contains(idx))
1155
        .map(|(_, c)| c.min_width)
1155
        .sum();
1155
    let total_max_width: f32 = table_ctx
1155
        .columns
1155
        .iter()
1155
        .enumerate()
2940
        .filter(|(idx, _)| !table_ctx.collapsed_columns.contains(idx))
1155
        .map(|(_, c)| c.max_width)
1155
        .sum();
1155
    let available_width = table_width; // Use table's content-box width, not constraints
1155
    debug_table_layout!(
1155
        ctx,
1155
        "calculate_column_widths_auto: min={:.2}, max={:.2}, table_width={:.2}",
        total_min_width,
        total_max_width,
        table_width
    );
    // Handle infinity and NaN cases
1155
    if !total_max_width.is_finite() || !available_width.is_finite() {
        // If max_width is infinite or unavailable, distribute available width equally
        let num_non_collapsed = table_ctx.columns.len() - table_ctx.collapsed_columns.len();
        let width_per_column = if num_non_collapsed > 0 {
            available_width / num_non_collapsed as f32
        } else {
            0.0
        };
        for (col_idx, col) in table_ctx.columns.iter_mut().enumerate() {
            if table_ctx.collapsed_columns.contains(&col_idx) {
                col.computed_width = Some(0.0);
            } else {
                // Use the larger of min_width and equal distribution
                col.computed_width = Some(col.min_width.max(width_per_column));
            }
        }
1155
    } else if available_width >= total_max_width {
        // Case 1: More space than max-content - distribute excess proportionally
        //
        // CSS 2.1 Section 17.5.2.2: Distribute extra space proportionally to
        // max-content widths
1155
        let excess_width = available_width - total_max_width;
        // First pass: collect column info (max_width) to avoid borrowing issues
1155
        let column_info: Vec<(usize, f32, bool)> = table_ctx
1155
            .columns
1155
            .iter()
1155
            .enumerate()
2940
            .map(|(idx, c)| (idx, c.max_width, table_ctx.collapsed_columns.contains(&idx)))
1155
            .collect();
        // Calculate total weight for proportional distribution (use max_width as weight)
1155
        let total_weight: f32 = column_info.iter()
2940
            .filter(|(_, _, is_collapsed)| !is_collapsed)
2940
            .map(|(_, max_w, _)| max_w.max(1.0)) // Avoid division by zero
1155
            .sum();
1155
        let num_non_collapsed = column_info
1155
            .iter()
2940
            .filter(|(_, _, is_collapsed)| !is_collapsed)
1155
            .count();
        // Second pass: set computed widths
4095
        for (col_idx, max_width, is_collapsed) in column_info {
2940
            let col = &mut table_ctx.columns[col_idx];
2940
            if is_collapsed {
                col.computed_width = Some(0.0);
            } else {
                // Start with max-content width, then add proportional share of excess
2940
                let weight_factor = if total_weight > 0.0 {
2940
                    max_width.max(1.0) / total_weight
                } else {
                    // If all columns have 0 max_width, distribute equally
                    1.0 / num_non_collapsed.max(1) as f32
                };
2940
                let final_width = max_width + (excess_width * weight_factor);
2940
                col.computed_width = Some(final_width);
            }
        }
    } else if available_width >= total_min_width {
        // Case 2: Between min and max - interpolate proportionally
        // Avoid division by zero if min == max
        let scale = if total_max_width > total_min_width {
            (available_width - total_min_width) / (total_max_width - total_min_width)
        } else {
            0.0 // If min == max, just use min width
        };
        for (col_idx, col) in table_ctx.columns.iter_mut().enumerate() {
            if table_ctx.collapsed_columns.contains(&col_idx) {
                col.computed_width = Some(0.0);
            } else {
                let interpolated = col.min_width + (col.max_width - col.min_width) * scale;
                col.computed_width = Some(interpolated);
            }
        }
    } else {
        // Case 3: Not enough space - scale down from min widths
        let scale = if total_min_width > 0.0 { available_width / total_min_width } else { 1.0 };
        for (col_idx, col) in table_ctx.columns.iter_mut().enumerate() {
            if table_ctx.collapsed_columns.contains(&col_idx) {
                col.computed_width = Some(0.0);
            } else {
                col.computed_width = Some(col.min_width * scale);
            }
        }
    }
1155
    Ok(())
1155
}
/// Distribute a multi-column cell's width across the columns it spans
fn distribute_cell_width_across_columns(
    columns: &mut [TableColumnInfo],
    start_col: usize,
    colspan: usize,
    cell_min_width: f32,
    cell_max_width: f32,
    collapsed_columns: &std::collections::HashSet<usize>,
) {
    let end_col = start_col + colspan;
    if end_col > columns.len() {
        return;
    }
    // Calculate current total of spanned non-collapsed columns
    let current_min_total: f32 = columns[start_col..end_col]
        .iter()
        .enumerate()
        .filter(|(idx, _)| !collapsed_columns.contains(&(start_col + idx)))
        .map(|(_, c)| c.min_width)
        .sum();
    let current_max_total: f32 = columns[start_col..end_col]
        .iter()
        .enumerate()
        .filter(|(idx, _)| !collapsed_columns.contains(&(start_col + idx)))
        .map(|(_, c)| c.max_width)
        .sum();
    // Count non-collapsed columns in the span
    let num_visible_cols = (start_col..end_col)
        .filter(|idx| !collapsed_columns.contains(idx))
        .count();
    if num_visible_cols == 0 {
        return; // All spanned columns are collapsed
    }
    // Only distribute if the cell needs more space than currently available
    if cell_min_width > current_min_total {
        let extra_min = cell_min_width - current_min_total;
        let per_col = extra_min / num_visible_cols as f32;
        for (idx, col) in columns[start_col..end_col].iter_mut().enumerate() {
            if !collapsed_columns.contains(&(start_col + idx)) {
                col.min_width += per_col;
            }
        }
    }
    if cell_max_width > current_max_total {
        let extra_max = cell_max_width - current_max_total;
        let per_col = extra_max / num_visible_cols as f32;
        for (idx, col) in columns[start_col..end_col].iter_mut().enumerate() {
            if !collapsed_columns.contains(&(start_col + idx)) {
                col.max_width += per_col;
            }
        }
    }
}
/// Layout a cell with its computed column width to determine its content height
2975
fn layout_cell_for_height<T: ParsedFontTrait>(
2975
    ctx: &mut LayoutContext<'_, T>,
2975
    tree: &mut LayoutTree,
2975
    text_cache: &mut crate::font_traits::TextLayoutCache,
2975
    cell_index: usize,
2975
    cell_width: f32,
2975
    constraints: &LayoutConstraints,
2975
) -> Result<f32> {
2975
    let cell_node = tree.get(cell_index).ok_or(LayoutError::InvalidTree)?;
2975
    let cell_dom_id = cell_node.dom_node_id.ok_or(LayoutError::InvalidTree)?;
    // Check if cell has text content directly in DOM (not in LayoutTree)
    // Text nodes are intentionally not included in LayoutTree per CSS spec,
    // but we need to measure them for table cell height calculation.
2975
    let has_text_children = cell_dom_id
2975
        .az_children(&ctx.styled_dom.node_hierarchy.as_container())
2975
        .any(|child_id| {
2975
            let node_data = &ctx.styled_dom.node_data.as_container()[child_id];
2975
            matches!(node_data.get_node_type(), NodeType::Text(_))
2975
        });
2975
    debug_table_layout!(
2975
        ctx,
2975
        "layout_cell_for_height: cell_index={}, has_text_children={}",
        cell_index,
        has_text_children
    );
    // Get padding and border to calculate content width
2975
    let cell_node = tree.get(cell_index).ok_or(LayoutError::InvalidTree)?;
2975
    let cell_bp = cell_node.box_props.unpack();
2975
    let padding = &cell_bp.padding;
2975
    let border = &cell_bp.border;
2975
    let writing_mode = constraints.writing_mode;
    // cell_width is the border-box width (includes padding/border from column
    // width calculation) but layout functions need content-box width
2975
    let content_width = cell_width
2975
        - padding.cross_start(writing_mode)
2975
        - padding.cross_end(writing_mode)
2975
        - border.cross_start(writing_mode)
2975
        - border.cross_end(writing_mode);
2975
    debug_table_layout!(
2975
        ctx,
2975
        "Cell width: border_box={:.2}, content_box={:.2}",
        cell_width,
        content_width
    );
2975
    let content_height = if has_text_children {
        // Cell contains text - use IFC to measure it
945
        debug_table_layout!(ctx, "Using IFC to measure text content");
945
        let cell_constraints = LayoutConstraints {
945
            available_size: LogicalSize {
945
                width: content_width, // Use content width, not border-box width
945
                height: f32::INFINITY,
945
            },
945
            writing_mode: constraints.writing_mode,
945
            writing_mode_ctx: constraints.writing_mode_ctx,
945
            bfc_state: None,
945
            text_align: constraints.text_align,
945
            containing_block_size: constraints.containing_block_size,
945
            // Use definite width for final cell layout!
945
            // This replaces any previous MinContent/MaxContent measurement.
945
            available_width_type: Text3AvailableSpace::Definite(content_width),
945
        };
945
        let output = layout_ifc(ctx, text_cache, tree, cell_index, &cell_constraints)?;
        // The cell now owns the authoritative IFC result. Clear any duplicate
        // inline_layout_result from text children that was set during the cell's
        // prior BFC Pass 1 (which ran before layout_cell_for_height).
945
        let cell_children: Vec<usize> = tree.children(cell_index).to_vec();
1890
        for child_idx in cell_children {
945
            if let Some(warm) = tree.warm_mut(child_idx) {
945
                warm.inline_layout_result = None;
945
            }
        }
945
        debug_table_layout!(
945
            ctx,
945
            "IFC returned height={:.2}",
            output.overflow_size.height
        );
945
        output.overflow_size.height
    } else {
        // Cell contains block-level children or is empty - use regular layout
2030
        debug_table_layout!(ctx, "Using regular layout for block children");
2030
        let cell_constraints = LayoutConstraints {
2030
            available_size: LogicalSize {
2030
                width: content_width, // Use content width, not border-box width
2030
                height: f32::INFINITY,
2030
            },
2030
            writing_mode: constraints.writing_mode,
2030
            writing_mode_ctx: constraints.writing_mode_ctx,
2030
            bfc_state: None,
2030
            text_align: constraints.text_align,
2030
            containing_block_size: constraints.containing_block_size,
2030
            // Use Definite width for final cell layout!
2030
            available_width_type: Text3AvailableSpace::Definite(content_width),
2030
        };
2030
        let mut temp_positions: super::PositionVec = Vec::new();
2030
        let mut temp_scrollbar_reflow = false;
2030
        let mut temp_float_cache = HashMap::new();
2030
        crate::solver3::cache::calculate_layout_for_subtree(
2030
            ctx,
2030
            tree,
2030
            text_cache,
2030
            cell_index,
2030
            LogicalPosition::zero(),
2030
            cell_constraints.available_size,
2030
            &mut temp_positions,
2030
            &mut temp_scrollbar_reflow,
2030
            &mut temp_float_cache,
            // PerformLayout: final table cell layout with definite width
2030
            crate::solver3::cache::ComputeMode::PerformLayout,
        )?;
2030
        let cell_node = tree.get(cell_index).ok_or(LayoutError::InvalidTree)?;
2030
        cell_node.used_size.unwrap_or_default().height
    };
    // Add padding and border to get the total height
2975
    let cell_node = tree.get(cell_index).ok_or(LayoutError::InvalidTree)?;
2975
    let cell_bp = cell_node.box_props.unpack();
2975
    let padding = &cell_bp.padding;
2975
    let border = &cell_bp.border;
2975
    let writing_mode = constraints.writing_mode;
2975
    let total_height = content_height
2975
        + padding.main_start(writing_mode)
2975
        + padding.main_end(writing_mode)
2975
        + border.main_start(writing_mode)
2975
        + border.main_end(writing_mode);
2975
    debug_table_layout!(
2975
        ctx,
2975
        "Cell total height: cell_index={}, content={:.2}, padding/border={:.2}, total={:.2}",
        cell_index,
        content_height,
2975
        padding.main_start(writing_mode)
2975
            + padding.main_end(writing_mode)
2975
            + border.main_start(writing_mode)
2975
            + border.main_end(writing_mode),
        total_height
    );
2975
    Ok(total_height)
2975
}
// or bottom of content edge if no such line box exists
// +spec:box-model:b64fa0 - Cell baseline is first in-flow line box or bottom of content edge
// +spec:overflow:3fa86f - Table cell baseline: first in-flow line box or bottom of content edge; scrolling boxes treated as at origin
// +spec:inline-formatting-context:c4a20d - cell baseline: first in-flow line box or bottom of content edge
// +spec:inline-formatting-context:17a9c1 - vertical-align baseline/top/bottom/middle for table cells
9870
fn compute_cell_baseline(cell_index: usize, tree: &LayoutTree) -> f32 {
9870
    let Some(cell_node) = tree.get(cell_index) else {
        return 0.0;
    };
9870
    let cell_bp = cell_node.box_props.unpack();
    // +spec:inline-formatting-context:27be38 - cell baseline is first in-flow line box or bottom of content edge
    // Check if the cell has inline layout (first in-flow line box)
9870
    if let Some(warm_node) = tree.warm(cell_index) {
9870
        if let Some(ref cached_layout) = warm_node.inline_layout_result {
5810
            let inline_result = &cached_layout.layout;
            // The baseline is the ascent of the first item from the top of the cell
5810
            if let Some(first_item) = inline_result.items.first() {
5810
                let (item_ascent, _) = crate::text3::cache::get_item_vertical_metrics_approx(&first_item.item);
5810
                let padding_top = cell_bp.padding.top;
5810
                let border_top = cell_bp.border.top;
5810
                return padding_top + border_top + first_item.position.y + item_ascent;
            }
4060
        }
    }
    // Check children for first in-flow line box
4060
    let children = tree.children(cell_index);
4200
    for &child_idx in children {
4060
        if child_idx < tree.nodes.len() {
4060
            if let Some(child_warm) = tree.warm(child_idx) {
4060
                if child_warm.inline_layout_result.is_some() {
3920
                    let child_baseline = compute_cell_baseline(child_idx, tree);
3920
                    let padding_top = cell_bp.padding.top;
3920
                    let border_top = cell_bp.border.top;
3920
                    return padding_top + border_top + child_baseline;
140
                }
            }
        }
    }
    // No line box found: baseline is the bottom of the content edge
140
    let used_size = cell_node.used_size.unwrap_or_default();
140
    let padding_bottom = cell_bp.padding.bottom;
140
    let border_bottom = cell_bp.border.bottom;
140
    used_size.height - padding_bottom - border_bottom
9870
}
/// +spec:box-model:72b495 - Table row height = max of computed height and MIN required by cells; baseline alignment
// +spec:display-property:728144 - Table height algorithm: row heights from cell content, rowspan distribution, vertical-align in cells (top/middle/bottom/baseline, sub/super/text-top/text-bottom/length/percentage fall back to baseline), cell baseline computation, and horizontal alignment via text-align
// +spec:positioning:3eaadd - Table height algorithms (§17.5.3): row height = max of cell heights/MIN,
//   rowspan distribution, vertical-align in table cells, cell baseline definition
/// Calculate row heights based on cell content after column widths are determined
// +spec:inline-formatting-context:87b90d - Table height algorithms: row height = max(computed height, cell heights, MIN); vertical-align in cells (baseline/top/middle/bottom, sub/super/etc. fall back to baseline)
1155
fn calculate_row_heights<T: ParsedFontTrait>(
1155
    table_ctx: &mut TableLayoutContext,
1155
    tree: &mut LayoutTree,
1155
    text_cache: &mut crate::font_traits::TextLayoutCache,
1155
    ctx: &mut LayoutContext<'_, T>,
1155
    constraints: &LayoutConstraints,
1155
) -> Result<()> {
1155
    debug_table_layout!(
1155
        ctx,
1155
        "calculate_row_heights: num_rows={}, available_size={:?}",
        table_ctx.num_rows,
        constraints.available_size
    );
    // +spec:inline-formatting-context:a7c7a0 - row height = max of computed height, cell heights, and MIN; vertical-align per cell
    // Initialize row heights and baselines
1155
    table_ctx.row_heights = vec![0.0; table_ctx.num_rows];
1155
    table_ctx.row_baselines = vec![0.0; table_ctx.num_rows];
    // CSS 2.2 Section 17.6: Set collapsed rows to height 0
1155
    for &row_idx in &table_ctx.collapsed_rows {
        if row_idx < table_ctx.row_heights.len() {
            table_ctx.row_heights[row_idx] = 0.0;
        }
    }
    // required by content; 'height' property can influence row height but does not
    // increase cell box height
    // First pass: Calculate heights for cells that don't span multiple rows
4130
    for cell_info in &table_ctx.cells {
        // Skip cells in collapsed rows
2975
        if table_ctx.collapsed_rows.contains(&cell_info.row) {
            continue;
2975
        }
        // Get the cell's width (sum of column widths if colspan > 1)
2975
        let mut cell_width = 0.0;
2975
        for col_idx in cell_info.column..(cell_info.column + cell_info.colspan) {
2975
            if let Some(col) = table_ctx.columns.get(col_idx) {
2975
                if let Some(width) = col.computed_width {
2975
                    cell_width += width;
2975
                }
            }
        }
2975
        debug_table_layout!(
2975
            ctx,
2975
            "Cell layout: node_index={}, row={}, col={}, width={:.2}",
            cell_info.node_index,
            cell_info.row,
            cell_info.column,
            cell_width
        );
        // Layout the cell to get its height
2975
        let cell_height = layout_cell_for_height(
2975
            ctx,
2975
            tree,
2975
            text_cache,
2975
            cell_info.node_index,
2975
            cell_width,
2975
            constraints,
        )?;
2975
        debug_table_layout!(
2975
            ctx,
2975
            "Cell height calculated: node_index={}, height={:.2}",
            cell_info.node_index,
            cell_height
        );
        //   row height = max of all single-span cell heights in the row
2975
        if cell_info.rowspan == 1 {
2975
            let current_height = table_ctx.row_heights[cell_info.row];
2975
            table_ctx.row_heights[cell_info.row] = current_height.max(cell_height);
2975
        }
        // +spec:box-model:073652 - Table height: baseline-aligned cells establish row baseline, then top/bottom/middle cells positioned
        // The baseline of a cell is the baseline of its first line box (from inline layout)
        // or the bottom of the content box if no inline content.
2975
        if cell_info.rowspan == 1 {
2975
            let cell_baseline = compute_cell_baseline(cell_info.node_index, tree);
2975
            let current_baseline = table_ctx.row_baselines[cell_info.row];
2975
            table_ctx.row_baselines[cell_info.row] = current_baseline.max(cell_baseline);
2975
        }
    }
    // involved must be great enough to encompass the cell spanning the rows
    // Second pass: Handle cells that span multiple rows (rowspan > 1)
4130
    for cell_info in &table_ctx.cells {
        // Skip cells that start in collapsed rows
2975
        if table_ctx.collapsed_rows.contains(&cell_info.row) {
            continue;
2975
        }
2975
        if cell_info.rowspan > 1 {
            // Get the cell's width
            let mut cell_width = 0.0;
            for col_idx in cell_info.column..(cell_info.column + cell_info.colspan) {
                if let Some(col) = table_ctx.columns.get(col_idx) {
                    if let Some(width) = col.computed_width {
                        cell_width += width;
                    }
                }
            }
            // Layout the cell to get its height
            let cell_height = layout_cell_for_height(
                ctx,
                tree,
                text_cache,
                cell_info.node_index,
                cell_width,
                constraints,
            )?;
            // Calculate the current total height of spanned rows (excluding collapsed rows)
            let end_row = cell_info.row + cell_info.rowspan;
            let current_total: f32 = table_ctx.row_heights[cell_info.row..end_row]
                .iter()
                .enumerate()
                .filter(|(idx, _)| !table_ctx.collapsed_rows.contains(&(cell_info.row + idx)))
                .map(|(_, height)| height)
                .sum();
            // If the cell needs more height, distribute extra height across
            // non-collapsed spanned rows
            if cell_height > current_total {
                let extra_height = cell_height - current_total;
                // Count non-collapsed rows in span
                let non_collapsed_rows = (cell_info.row..end_row)
                    .filter(|row_idx| !table_ctx.collapsed_rows.contains(row_idx))
                    .count();
                if non_collapsed_rows > 0 {
                    let per_row = extra_height / non_collapsed_rows as f32;
                    for row_idx in cell_info.row..end_row {
                        if !table_ctx.collapsed_rows.contains(&row_idx) {
                            table_ctx.row_heights[row_idx] += per_row;
                        }
                    }
                }
            }
2975
        }
    }
    // CSS 2.2 Section 17.6: Final pass - ensure collapsed rows have height 0
1155
    for &row_idx in &table_ctx.collapsed_rows {
        if row_idx < table_ctx.row_heights.len() {
            table_ctx.row_heights[row_idx] = 0.0;
        }
    }
    //   visible content, the row has zero height and v-spacing on only one side
    // +spec:table-layout:7370dc - empty-cells:hide in separated borders model
    // +spec:box-model:1e9cf1 - empty-cells:hide rows get zero height with v-spacing on only one side
    // +spec:overflow:a44925 - CSS 2.2 §17.6.1.1: empty-cells:hide suppresses borders/backgrounds; all-hidden rows get zero height
    // +spec:table-layout:dc8bc3 - separated borders model: border-spacing, empty-cells, row zero-height
1155
    if table_ctx.border_collapse == StyleBorderCollapse::Separate {
1190
        for row_idx in 0..table_ctx.num_rows {
1190
            if table_ctx.collapsed_rows.contains(&row_idx) {
                continue;
1190
            }
            // Collect cells in this row
1190
            let row_cells: Vec<usize> = table_ctx
1190
                .cells
1190
                .iter()
3045
                .filter(|c| c.row == row_idx && c.rowspan == 1)
1190
                .map(|c| c.node_index)
1190
                .collect();
1190
            if row_cells.is_empty() {
                continue;
1190
            }
            // +spec:box-model:0ab9b0 - empty-cells:hide suppresses borders/backgrounds, row gets zero height if all cells hidden+empty
            // Check if ALL cells in this row have empty-cells:hide and are empty
1190
            let all_hidden_empty = row_cells.iter().all(|&cell_idx| {
1190
                if let Some(cell_node) = tree.get(cell_idx) {
1190
                    let ec = get_empty_cells_property(ctx, cell_node);
1190
                    ec == StyleEmptyCells::Hide && is_cell_empty(tree, cell_idx)
                } else {
                    true
                }
1190
            });
1190
            if all_hidden_empty {
                table_ctx.row_heights[row_idx] = 0.0;
                table_ctx.hidden_empty_rows.insert(row_idx);
1190
            }
        }
    }
1155
    Ok(())
1155
}
/// Position all cells in the table grid with calculated widths and heights
1155
fn position_table_cells<T: ParsedFontTrait>(
1155
    table_ctx: &mut TableLayoutContext,
1155
    tree: &mut LayoutTree,
1155
    ctx: &mut LayoutContext<'_, T>,
1155
    table_index: usize,
1155
    constraints: &LayoutConstraints,
1155
) -> Result<BTreeMap<usize, LogicalPosition>> {
1155
    debug_log!(ctx, "Positioning table cells in grid");
1155
    let mut positions = BTreeMap::new();
    // +spec:box-model:54e86a - Separated borders model: individual cell borders, border-spacing between cells, empty-cells handling
    //   rows, columns, row groups, column groups cannot have borders (UA must ignore border props);
    //   row/column/rowgroup/colgroup backgrounds are invisible in border-spacing area (table bg shows through);
    //   distance from table edge to edge-cell border = table padding + border-spacing
    //   (table padding is already accounted for by the containing block; h_spacing is the border-spacing)
    // Get border spacing values if border-collapse is separate
1155
    let (h_spacing, v_spacing) = if table_ctx.border_collapse == StyleBorderCollapse::Separate {
1155
        let styled_dom = ctx.styled_dom;
1155
        let table_id = tree.nodes[table_index].dom_node_id.unwrap();
1155
        let table_state = &styled_dom.styled_nodes.as_container()[table_id].styled_node_state;
1155
        let spacing_context = ResolutionContext {
1155
            element_font_size: get_element_font_size(styled_dom, table_id, table_state),
1155
            parent_font_size: get_parent_font_size(styled_dom, table_id, table_state),
1155
            root_font_size: get_root_font_size(styled_dom, table_state),
1155
            containing_block_size: PhysicalSize::new(0.0, 0.0),
1155
            element_size: None,
1155
            viewport_size: PhysicalSize::new(ctx.viewport_size.width, ctx.viewport_size.height),
1155
        };
1155
        let h = table_ctx
1155
            .border_spacing
1155
            .horizontal
1155
            .resolve_with_context(&spacing_context, PropertyContext::Other)
1155
            .max(0.0);
1155
        let v = table_ctx
1155
            .border_spacing
1155
            .vertical
1155
            .resolve_with_context(&spacing_context, PropertyContext::Other)
1155
            .max(0.0);
1155
        (h, v)
    } else {
        (0.0, 0.0)
    };
1155
    debug_log!(
1155
        ctx,
1155
        "Border spacing: h={:.2}, v={:.2}",
        h_spacing,
        v_spacing
    );
    // Calculate cumulative column positions (x-offsets) with spacing
1155
    let mut col_positions = vec![0.0; table_ctx.columns.len()];
1155
    let mut x_offset = h_spacing; // Start with spacing on the left
2940
    for (i, col) in table_ctx.columns.iter().enumerate() {
2940
        col_positions[i] = x_offset;
2940
        if let Some(width) = col.computed_width {
            // Collapsed columns: gutters on either side collapse (width is 0, skip spacing)
2940
            if table_ctx.collapsed_columns.contains(&i) {
                // No width, no gutter added
2940
            } else {
2940
                x_offset += width + h_spacing; // Add spacing between columns
2940
            }
        }
    }
    // Calculate cumulative row positions (y-offsets) with spacing
1155
    let mut row_positions = vec![0.0; table_ctx.num_rows];
1155
    let mut y_offset = v_spacing; // Start with spacing on the top
1190
    for (i, &height) in table_ctx.row_heights.iter().enumerate() {
1190
        row_positions[i] = y_offset;
        // Collapsed rows: gutters on either side collapse (height is 0, skip spacing)
1190
        if table_ctx.collapsed_rows.contains(&i) {
            // No height, no gutter added
1190
        } else if table_ctx.hidden_empty_rows.contains(&i) {
            // Hidden-empty row: zero height, only one side of spacing
            // (we already added spacing before this row, so skip the spacing after)
            y_offset += height; // height is 0.0
1190
        } else {
1190
            y_offset += height + v_spacing; // Add spacing between rows
1190
        }
    }
    // Store row positions and sizes so paint_element_background can paint row backgrounds.
    // Row width = sum of column widths + spacing. Row height from row_heights.
    {
2940
        let total_col_width: f32 = table_ctx.columns.iter().map(|c| c.computed_width.unwrap_or(0.0)).sum::<f32>()
1155
            + h_spacing * (table_ctx.columns.len().max(1) - 1) as f32
1155
            + h_spacing * 2.0; // border-spacing on left+right edges
1190
        for (i, &row_y) in row_positions.iter().enumerate() {
1190
            if let Some(&row_node_idx) = table_ctx.row_node_indices.get(i) {
1190
                let row_height = table_ctx.row_heights.get(i).copied().unwrap_or(0.0);
1190
                if let Some(row_node) = tree.get_mut(row_node_idx) {
1190
                    row_node.used_size = Some(LogicalSize {
1190
                        width: total_col_width,
1190
                        height: row_height,
1190
                    });
1190
                }
                // Don't add to `positions` map (feeds position_bfc_child_descendants,
                // would double-offset cells). The display list computes row paint
                // rects from the row's cell children.
            }
        }
    }
    // Position each cell
4130
    for cell_info in &table_ctx.cells {
2975
        let precomputed_cell_baseline = compute_cell_baseline(cell_info.node_index, tree);
2975
        let cell_node = tree
2975
            .get_mut(cell_info.node_index)
2975
            .ok_or(LayoutError::InvalidTree)?;
        // Calculate cell position
2975
        let x = col_positions.get(cell_info.column).copied().unwrap_or(0.0);
2975
        let y = row_positions.get(cell_info.row).copied().unwrap_or(0.0);
        // Calculate cell size (sum of spanned columns/rows)
2975
        let mut width = 0.0;
2975
        debug_info!(
2975
            ctx,
2975
            "[position_table_cells] Cell {}: calculating width from cols {}..{}",
            cell_info.node_index,
            cell_info.column,
2975
            cell_info.column + cell_info.colspan
        );
2975
        for col_idx in cell_info.column..(cell_info.column + cell_info.colspan) {
2975
            if let Some(col) = table_ctx.columns.get(col_idx) {
2975
                debug_info!(
2975
                    ctx,
2975
                    "[position_table_cells]   Col {}: computed_width={:?}",
                    col_idx,
                    col.computed_width
                );
2975
                if let Some(col_width) = col.computed_width {
2975
                    width += col_width;
                    // Add spacing between spanned columns (but not after the last one)
2975
                    if col_idx < cell_info.column + cell_info.colspan - 1 {
                        width += h_spacing;
2975
                    }
                } else {
                    debug_info!(
                        ctx,
                        "[position_table_cells]   WARN:  Col {} has NO computed_width!",
                        col_idx
                    );
                }
            } else {
                debug_info!(
                    ctx,
                    "[position_table_cells]   WARN:  Col {} not found in table_ctx.columns!",
                    col_idx
                );
            }
        }
2975
        let mut height = 0.0;
2975
        let end_row = cell_info.row + cell_info.rowspan;
2975
        for row_idx in cell_info.row..end_row {
2975
            if let Some(&row_height) = table_ctx.row_heights.get(row_idx) {
2975
                height += row_height;
                // Add spacing between spanned rows (but not after the last one)
2975
                if row_idx < end_row - 1 {
                    height += v_spacing;
2975
                }
            }
        }
        // Update cell's used size and position
2975
        let writing_mode = constraints.writing_mode;
        // Table layout works in main/cross axes, must convert back to logical width/height
2975
        debug_info!(
2975
            ctx,
2975
            "[position_table_cells] Cell {}: BEFORE from_main_cross: width={}, height={}, \
2975
             writing_mode={:?}",
            cell_info.node_index,
            width,
            height,
            writing_mode
        );
2975
        cell_node.used_size = Some(LogicalSize::from_main_cross(height, width, writing_mode));
2975
        debug_info!(
2975
            ctx,
2975
            "[position_table_cells] Cell {}: AFTER from_main_cross: used_size={:?}",
            cell_info.node_index,
            cell_node.used_size
        );
2975
        debug_info!(
2975
            ctx,
2975
            "[position_table_cells] Cell {}: setting used_size to {}x{} (row_heights={:?})",
            cell_info.node_index,
            width,
            height,
            table_ctx.row_heights
        );
        // Save hot fields needed for vertical alignment before dropping the mutable borrow
2975
        let cell_dom_node_id = cell_node.dom_node_id;
2975
        let cell_box_props = cell_node.box_props.unpack();
2975
        drop(cell_node);
        // +spec:inline-formatting-context:20e8e8 - table cell vertical-align alignment order (baseline first, then top, then bottom/middle)
        // receive extra top or bottom padding; vertical-align determines alignment
        // +spec:inline-formatting-context:4545e8 - vertical-align on table cells maps to align-content: top→start, bottom→end, middle→center
        // +spec:inline-formatting-context:e216be - vertical-align on table cells (baseline, middle, top, bottom)
        // +spec:positioning:156e49 - table cell vertical-align ordering and extra padding per CSS 2.2 §17.5.3
        // Apply vertical-align to cell content if it has inline layout
        // We need to compute the y_offset using immutable borrows first, then apply it mutably.
2975
        let vertical_align_adjustment = if let Some(warm_node) = tree.warm(cell_info.node_index) {
2975
            if let Some(ref cached_layout) = warm_node.inline_layout_result {
945
                let inline_result = &cached_layout.layout;
                use StyleVerticalAlign;
                // Get vertical-align property from styled_dom
945
                let vertical_align = if let Some(dom_id) = cell_dom_node_id {
945
                    let node_state = ctx.styled_dom.styled_nodes.as_container()[dom_id].styled_node_state.clone();
945
                    match get_vertical_align_property(ctx.styled_dom, dom_id, &node_state) {
945
                        MultiValue::Exact(v) => v,
                        _ => StyleVerticalAlign::Baseline,
                    }
                } else {
                    StyleVerticalAlign::Baseline
                };
                // Calculate content height from inline layout bounds
945
                let content_bounds = inline_result.bounds();
945
                let content_height = content_bounds.height;
                // Get padding and border to calculate content-box height
                // height is border-box, but vertical alignment should be within content-box
945
                let padding = &cell_box_props.padding;
945
                let border = &cell_box_props.border;
945
                let content_box_height = height
945
                    - padding.main_start(writing_mode)
945
                    - padding.main_end(writing_mode)
945
                    - border.main_start(writing_mode)
945
                    - border.main_end(writing_mode);
                // top: top of cell box aligned with top of first row it spans
                // bottom: bottom of cell box aligned with bottom of last row it spans
                // middle: center of cell aligned with center of rows it spans
                //   the cell is aligned at the baseline instead
945
                let y_offset = match vertical_align {
                    StyleVerticalAlign::Top => 0.0,
945
                    StyleVerticalAlign::Middle => (content_box_height - content_height) * 0.5,
                    StyleVerticalAlign::Bottom => content_box_height - content_height,
                    // align with the row baseline. cell_baseline = distance from top of cell box
                    // to cell's baseline; row_baseline = distance from top of row to row's baseline
                    StyleVerticalAlign::Baseline
                    | StyleVerticalAlign::Sub
                    | StyleVerticalAlign::Superscript
                    | StyleVerticalAlign::TextTop
                    | StyleVerticalAlign::TextBottom
                    | StyleVerticalAlign::Percentage(_)
                    | StyleVerticalAlign::Length(_) => {
                        let row_baseline = table_ctx.row_baselines.get(cell_info.row).copied().unwrap_or(0.0);
                        (row_baseline - precomputed_cell_baseline).max(0.0)
                    }
                };
945
                debug_info!(
945
                    ctx,
945
                    "[position_table_cells] Cell {}: vertical-align={:?}, border_box_height={}, \
945
                     content_box_height={}, content_height={}, y_offset={}",
                    cell_info.node_index,
                    vertical_align,
                    height,
                    content_box_height,
                    content_height,
                    y_offset
                );
945
                if y_offset.abs() > 0.01 {
105
                    Some((y_offset, cached_layout.available_width, cached_layout.has_floats))
                } else {
840
                    None
                }
            } else {
2030
                None
            }
        } else {
            None
        };
        // Apply the vertical alignment adjustment (requires mutable borrow)
2975
        if let Some((y_offset, available_width, has_floats)) = vertical_align_adjustment {
105
            if let Some(warm_mut) = tree.warm_mut(cell_info.node_index) {
105
                if let Some(ref cached_layout) = warm_mut.inline_layout_result {
                    use std::sync::Arc;
                    use crate::text3::cache::{PositionedItem, UnifiedLayout};
105
                    let adjusted_items: Vec<PositionedItem> = cached_layout.layout
105
                        .items
105
                        .iter()
105
                        .map(|item| PositionedItem {
945
                            item: item.item.clone(),
945
                            position: crate::text3::cache::Point {
945
                                x: item.position.x,
945
                                y: item.position.y + y_offset,
945
                            },
945
                            line_index: item.line_index,
945
                        })
105
                        .collect();
105
                    let adjusted_layout = UnifiedLayout {
105
                        items: adjusted_items,
105
                        overflow: cached_layout.layout.overflow.clone(),
105
                    };
                    // Keep the same constraint type from the cached layout
105
                    warm_mut.inline_layout_result = Some(CachedInlineLayout::new(
105
                        Arc::new(adjusted_layout),
105
                        available_width,
105
                        has_floats,
105
                    ));
                }
            }
2870
        }
        // Store position relative to table origin
2975
        let position = LogicalPosition::from_main_cross(y, x, writing_mode);
        // Insert position into map so cache module can position the cell
2975
        positions.insert(cell_info.node_index, position);
2975
        debug_log!(
2975
            ctx,
2975
            "Cell at row={}, col={}: pos=({:.2}, {:.2}), size=({:.2}x{:.2})",
            cell_info.row,
            cell_info.column,
            x,
            y,
            width,
            height
        );
    }
1155
    Ok(positions)
1155
}
/// Gathers all inline content for `text3`, recursively laying out `inline-block` children
/// to determine their size and baseline before passing them to the text engine.
///
/// This function also assigns IFC membership to all participating nodes:
/// - The IFC root gets an `ifc_id` assigned
/// - Each text/inline child gets `ifc_membership` set with a reference back to the IFC root
///
/// This mapping enables efficient cursor hit-testing: when a text node is clicked,
/// we can find its parent IFC's `inline_layout_result` via `ifc_membership.ifc_root_layout_index`.
// +spec:display-property:63a38b - inline box boundaries and out-of-flow elements are ignored for text adjacency (white space, line-breaking, text-transform)
18205
fn collect_and_measure_inline_content<T: ParsedFontTrait>(
18205
    ctx: &mut LayoutContext<'_, T>,
18205
    text_cache: &mut TextLayoutCache,
18205
    tree: &mut LayoutTree,
18205
    ifc_root_index: usize,
18205
    constraints: &LayoutConstraints,
18205
) -> Result<(Vec<InlineContent>, HashMap<ContentIndex, usize>)> {
    use crate::solver3::layout_tree::{IfcId, IfcMembership};
    use crate::text3::cache::InlineContent;
18205
    let result = collect_and_measure_inline_content_impl(ctx, text_cache, tree, ifc_root_index, constraints)?;
18205
    Ok(result)
18205
}
18205
fn collect_and_measure_inline_content_impl<T: ParsedFontTrait>(
18205
    ctx: &mut LayoutContext<'_, T>,
18205
    text_cache: &mut TextLayoutCache,
18205
    tree: &mut LayoutTree,
18205
    ifc_root_index: usize,
18205
    constraints: &LayoutConstraints,
18205
) -> Result<(Vec<InlineContent>, HashMap<ContentIndex, usize>)> {
    use crate::solver3::layout_tree::{IfcId, IfcMembership};
18205
    debug_ifc_layout!(
18205
        ctx,
18205
        "collect_and_measure_inline_content: node_index={}",
        ifc_root_index
    );
    // Generate a unique IFC ID for this inline formatting context
18205
    let ifc_id = IfcId::unique();
    // Store IFC ID on the IFC root node
18205
    if let Some(cold_node) = tree.cold_mut(ifc_root_index) {
18205
        cold_node.ifc_id = Some(ifc_id);
18205
    }
18205
    let mut content = Vec::new();
    // Maps the `ContentIndex` used by text3 back to the `LayoutNode` index.
18205
    let mut child_map = HashMap::new();
    // Track the current run index for IFC membership assignment
18205
    let mut current_run_index: u32 = 0;
18205
    let ifc_root_node = tree.get(ifc_root_index).ok_or(LayoutError::InvalidTree)?;
    // Check if this is an anonymous IFC wrapper (has no DOM ID)
18205
    let is_anonymous = ifc_root_node.dom_node_id.is_none();
    // Get the DOM node ID of the IFC root, or find it from parent/children for anonymous boxes
    // CSS 2.2 § 9.2.1.1: Anonymous boxes inherit properties from their enclosing box
18205
    let ifc_root_dom_id = match ifc_root_node.dom_node_id {
18135
        Some(id) => id,
        None => {
            // Anonymous box - get DOM ID from parent or first child with DOM ID
70
            let parent_dom_id = ifc_root_node
70
                .parent
70
                .and_then(|p| tree.get(p))
70
                .and_then(|n| n.dom_node_id);
70
            if let Some(id) = parent_dom_id {
70
                id
            } else {
                // Try to find DOM ID from first child
                match tree.children(ifc_root_index)
                    .iter()
                    .filter_map(|&child_idx| tree.get(child_idx))
                    .filter_map(|n| n.dom_node_id)
                    .next()
                {
                    Some(id) => id,
                    None => {
                        debug_warning!(ctx, "IFC root and all ancestors/children have no DOM ID");
                        return Ok((content, child_map));
                    }
                }
            }
        }
    };
    // Collect children to avoid holding an immutable borrow during iteration
18205
    let children: Vec<_> = tree.children(ifc_root_index).to_vec();
18205
    drop(ifc_root_node);
18205
    debug_ifc_layout!(
18205
        ctx,
18205
        "Node {} has {} layout children, is_anonymous={}",
        ifc_root_index,
18205
        children.len(),
        is_anonymous
    );
    // For anonymous IFC wrappers, we collect content from layout tree children
    // For regular IFC roots, we also check DOM children for text nodes
18205
    if is_anonymous {
        // Anonymous IFC wrapper - iterate over layout tree children and collect their content
70
        for (item_idx, &child_index) in children.iter().enumerate() {
70
            let content_index = ContentIndex {
70
                run_index: ifc_root_index as u32,
70
                item_index: item_idx as u32,
70
            };
70
            let child_node = tree.get(child_index).ok_or(LayoutError::InvalidTree)?;
70
            let Some(dom_id) = child_node.dom_node_id else {
                debug_warning!(
                    ctx,
                    "Anonymous IFC child at index {} has no DOM ID",
                    child_index
                );
                continue;
            };
70
            let node_data = &ctx.styled_dom.node_data.as_container()[dom_id];
            // Check if this is a text node
70
            if let NodeType::Text(ref text_content) = node_data.get_node_type() {
70
                debug_info!(
70
                    ctx,
70
                    "[collect_and_measure_inline_content] OK: Found text node (DOM {:?}) in anonymous wrapper: '{}'",
                    dom_id,
70
                    text_content.as_str()
                );
                // Get style from the TEXT NODE itself (dom_id), not the IFC root
                // This ensures inline styles like color: #666666 are applied to the text
70
                let style = Arc::new(get_style_properties(ctx.styled_dom, dom_id, ctx.system_style.as_ref(), PhysicalSize::new(ctx.viewport_size.width, ctx.viewport_size.height)));
70
                let text_items = split_text_for_whitespace(
70
                    ctx.styled_dom,
70
                    dom_id,
70
                    text_content.as_str(),
70
                    style,
                );
70
                content.extend(text_items);
70
                child_map.insert(content_index, child_index);
                // Set IFC membership on the text node - drop child_node borrow first
70
                drop(child_node);
70
                if let Some(warm_mut) = tree.warm_mut(child_index) {
70
                    warm_mut.ifc_membership = Some(IfcMembership {
70
                        ifc_id,
70
                        ifc_root_layout_index: ifc_root_index,
70
                        run_index: current_run_index,
70
                    });
70
                }
70
                current_run_index += 1;
70
                continue;
            }
            // Non-text inline child - add as shape for inline-block
            let display = get_display_property(ctx.styled_dom, Some(dom_id)).unwrap_or_default();
            if display != LayoutDisplay::Inline {
                // +spec:display-property:a37a9a - atomic inline-level boxes treated as neutral characters in bidi reordering
                // This is an atomic inline-level box (e.g., inline-block, image).
                // We must determine its size and baseline before passing it to text3.
                // The intrinsic sizing pass has already calculated its preferred size.
                let intrinsic_size = tree.warm(child_index).and_then(|w| w.intrinsic_sizes.clone()).unwrap_or_default();
                let box_props = child_node.box_props.unpack();
                let styled_node_state = ctx
                    .styled_dom
                    .styled_nodes
                    .as_container()
                    .get(dom_id)
                    .map(|n| n.styled_node_state.clone())
                    .unwrap_or_default();
                // Calculate tentative border-box size based on CSS properties
                let tentative_size = crate::solver3::sizing::calculate_used_size_for_node(
                    ctx.styled_dom,
                    Some(dom_id),
                    &constraints.containing_block_size,
                    intrinsic_size,
                    &box_props,
                    &ctx.viewport_size,
                )?;
                let writing_mode = get_writing_mode(ctx.styled_dom, dom_id, &styled_node_state)
                    .unwrap_or_default();
                // Determine content-box size for laying out children
                let content_box_size = box_props.inner_size(tentative_size, writing_mode);
                // To find its height and baseline, we must lay out its contents.
                let child_wm_ctx = super::geometry::WritingModeContext::new(
                    writing_mode,
                    get_direction_property(ctx.styled_dom, dom_id, &styled_node_state)
                        .unwrap_or_default(),
                    get_text_orientation_property(ctx.styled_dom, dom_id, &styled_node_state)
                        .unwrap_or_default(),
                );
                let child_constraints = LayoutConstraints {
                    available_size: LogicalSize::new(content_box_size.width, f32::INFINITY),
                    writing_mode,
                    writing_mode_ctx: child_wm_ctx,
                    bfc_state: None,
                    text_align: TextAlign::Start,
                    containing_block_size: constraints.containing_block_size,
                    available_width_type: Text3AvailableSpace::Definite(content_box_size.width),
                };
                // Drop the immutable borrow before calling layout_formatting_context
                drop(child_node);
                // Recursively lay out the inline-block to get its final height and baseline.
                let mut empty_float_cache = HashMap::new();
                let layout_result = layout_formatting_context(
                    ctx,
                    tree,
                    text_cache,
                    child_index,
                    &child_constraints,
                    &mut empty_float_cache,
                )?;
                let css_height = get_css_height(ctx.styled_dom, dom_id, &styled_node_state);
                // Determine final border-box height
                let final_height = match css_height.unwrap_or_default() {
                    LayoutHeight::Auto => {
                        let content_height = layout_result.output.overflow_size.height;
                        content_height
                            + box_props.padding.main_sum(writing_mode)
                            + box_props.border.main_sum(writing_mode)
                    }
                    _ => tentative_size.height,
                };
                let final_size = LogicalSize::new(tentative_size.width, final_height);
                // Update the node in the tree with its now-known used size.
                tree.get_mut(child_index).unwrap().used_size = Some(final_size);
                // CSS 2.2 § 10.8.1: inline-block baseline fallback
                // If overflow is not 'visible', use bottom margin edge as baseline
                let overflow_x = get_overflow_x(ctx.styled_dom, dom_id, &styled_node_state).unwrap_or_default();
                let overflow_y = get_overflow_y(ctx.styled_dom, dom_id, &styled_node_state).unwrap_or_default();
                let overflow_is_visible = matches!(
                    (overflow_x, overflow_y),
                    (LayoutOverflow::Visible, LayoutOverflow::Visible)
                );
                let baseline_offset = if overflow_is_visible {
                    layout_result.output.baseline.unwrap_or(final_height)
                } else {
                    final_height
                };
                // +spec:box-model:66ad24 - inline-axis margins, borders, padding respected for inline-level boxes (no collapsing)
                // The margin-box size is used so text3 positions inline-blocks with proper spacing
                let margin = &box_props.margin;
                let margin_box_width = final_size.width + margin.left + margin.right;
                let margin_box_height = final_size.height + margin.top + margin.bottom;
                // For inline-block shapes, text3 uses the content array index as run_index
                // and always item_index=0 for objects. We must match this when inserting into child_map.
                let shape_content_index = ContentIndex {
                    run_index: content.len() as u32,
                    item_index: 0,
                };
                content.push(InlineContent::Shape(InlineShape {
                    shape_def: ShapeDefinition::Rectangle {
                        size: crate::text3::cache::Size {
                            // Use margin-box size for positioning in inline flow
                            width: margin_box_width,
                            height: margin_box_height,
                        },
                        corner_radius: None,
                    },
                    fill: None,
                    stroke: None,
                    // Adjust baseline offset by top margin
                    baseline_offset: baseline_offset + margin.top,
                    alignment: crate::solver3::getters::get_vertical_align_for_node(ctx.styled_dom, dom_id),
                    source_node_id: Some(dom_id),
                }));
                child_map.insert(shape_content_index, child_index);
            } else {
                // Regular inline element - collect its text children
                let span_style = get_style_properties(ctx.styled_dom, dom_id, ctx.system_style.as_ref(), PhysicalSize::new(ctx.viewport_size.width, ctx.viewport_size.height));
                collect_inline_span_recursive(
                    ctx,
                    tree,
                    dom_id,
                    span_style,
                    &mut content,
                    &mut child_map,
                    &children,
                    constraints,
                )?;
            }
        }
70
        return Ok((content, child_map));
18135
    }
    // Regular (non-anonymous) IFC root - check for list markers and use DOM traversal
    // Check if this IFC root OR its parent is a list-item and needs a marker
    // Case 1: IFC root itself is list-item (e.g., <li> with display: list-item)
    // Case 2: IFC root's parent is list-item (e.g., <li><text>...</text></li>)
18135
    let ifc_root_node = tree.get(ifc_root_index).ok_or(LayoutError::InvalidTree)?;
18135
    let mut list_item_dom_id: Option<NodeId> = None;
    // Check IFC root itself
18135
    if let Some(dom_id) = ifc_root_node.dom_node_id {
        use crate::solver3::getters::get_display_property;
18135
        if let MultiValue::Exact(display) = get_display_property(ctx.styled_dom, Some(dom_id)) {
            use LayoutDisplay;
18135
            if display == LayoutDisplay::ListItem {
                debug_ifc_layout!(ctx, "IFC root NodeId({:?}) is list-item", dom_id);
                list_item_dom_id = Some(dom_id);
18135
            }
        }
    }
    // Check IFC root's parent
18135
    if list_item_dom_id.is_none() {
18135
        if let Some(parent_idx) = ifc_root_node.parent {
17995
            if let Some(parent_node) = tree.get(parent_idx) {
17995
                if let Some(parent_dom_id) = parent_node.dom_node_id {
                    use crate::solver3::getters::get_display_property;
17987
                    if let MultiValue::Exact(display) = get_display_property(ctx.styled_dom, Some(parent_dom_id)) {
                        use LayoutDisplay;
17987
                        if display == LayoutDisplay::ListItem {
                            debug_ifc_layout!(
                                ctx,
                                "IFC root parent NodeId({:?}) is list-item",
                                parent_dom_id
                            );
                            list_item_dom_id = Some(parent_dom_id);
17987
                        }
                    }
8
                }
            }
140
        }
    }
    // If we found a list-item, generate markers
18135
    if let Some(list_dom_id) = list_item_dom_id {
        debug_ifc_layout!(
            ctx,
            "Found list-item (NodeId({:?})), generating marker",
            list_dom_id
        );
        // Find the layout node index for the list-item DOM node
        let list_item_layout_idx = tree
            .nodes
            .iter()
            .enumerate()
            .find(|(idx, node)| {
                node.dom_node_id == Some(list_dom_id) && tree.warm(*idx).and_then(|w| w.pseudo_element).is_none()
            })
            .map(|(idx, _)| idx);
        if let Some(list_idx) = list_item_layout_idx {
            // Per CSS spec, the ::marker pseudo-element is the first child of the list-item
            // Find the ::marker pseudo-element in the list-item's children
            let marker_idx = tree.children(list_idx)
                .iter()
                .find(|&&child_idx| {
                    tree.warm(child_idx)
                        .map(|w| w.pseudo_element == Some(PseudoElement::Marker))
                        .unwrap_or(false)
                })
                .copied();
            if let Some(marker_idx) = marker_idx {
                debug_ifc_layout!(ctx, "Found ::marker pseudo-element at index {}", marker_idx);
                // Get the DOM ID for style resolution (marker references the same DOM node as
                // list-item)
                let list_dom_id_for_style = tree
                    .get(marker_idx)
                    .and_then(|n| n.dom_node_id)
                    .unwrap_or(list_dom_id);
                // Get list-style-position to determine marker positioning
                // Default is 'outside' per CSS Lists Module Level 3
                let list_style_position =
                    get_list_style_position(ctx.styled_dom, Some(list_dom_id));
                let position_outside =
                    matches!(list_style_position, StyleListStylePosition::Outside);
                debug_ifc_layout!(
                    ctx,
                    "List marker list-style-position: {:?} (outside={})",
                    list_style_position,
                    position_outside
                );
                // Generate marker text segments - font fallback happens during shaping
                let base_style =
                    Arc::new(get_style_properties(ctx.styled_dom, list_dom_id_for_style, ctx.system_style.as_ref(), PhysicalSize::new(ctx.viewport_size.width, ctx.viewport_size.height)));
                let marker_segments = generate_list_marker_segments(
                    tree,
                    ctx.styled_dom,
                    marker_idx, // Pass the marker index, not the list-item index
                    ctx.counters,
                    base_style,
                    ctx.debug_messages,
                );
                debug_ifc_layout!(
                    ctx,
                    "Generated {} list marker segments",
                    marker_segments.len()
                );
                // Add markers as InlineContent::Marker with position information
                // Outside markers will be positioned in the padding gutter by the layout engine
                for segment in marker_segments {
                    content.push(InlineContent::Marker {
                        run: segment,
                        position_outside,
                    });
                }
            } else {
                debug_ifc_layout!(
                    ctx,
                    "WARNING: List-item at index {} has no ::marker pseudo-element",
                    list_idx
                );
            }
        }
18135
    }
18135
    drop(ifc_root_node);
    // IMPORTANT: We need to traverse the DOM, not just the layout tree!
    //
    // According to CSS spec, a block container with inline-level children establishes
    // an IFC and should collect ALL inline content, including text nodes.
    // Text nodes exist in the DOM but might not have their own layout tree nodes.
    // Debug: Check what the node_hierarchy says about this node
18135
    let node_hier_item = &ctx.styled_dom.node_hierarchy.as_container()[ifc_root_dom_id];
18135
    debug_info!(
18135
        ctx,
18135
        "[collect_and_measure_inline_content] DEBUG: node_hier_item.first_child={:?}, \
18135
         last_child={:?}",
18135
        node_hier_item.first_child_id(ifc_root_dom_id),
18135
        node_hier_item.last_child_id()
    );
18135
    let dom_children: Vec<NodeId> = ifc_root_dom_id
18135
        .az_children(&ctx.styled_dom.node_hierarchy.as_container())
18135
        .collect();
18135
    let ifc_root_node_data = &ctx.styled_dom.node_data.as_container()[ifc_root_dom_id];
    // SPECIAL CASE: If the IFC root itself is a text node (leaf node),
    // add its text content directly instead of iterating over children
18135
    if let NodeType::Text(ref text_content) = ifc_root_node_data.get_node_type() {
4068
        let style = Arc::new(get_style_properties(ctx.styled_dom, ifc_root_dom_id, ctx.system_style.as_ref(), PhysicalSize::new(ctx.viewport_size.width, ctx.viewport_size.height)));
4068
        let text_items = split_text_for_whitespace(
4068
            ctx.styled_dom,
4068
            ifc_root_dom_id,
4068
            text_content.as_str(),
4068
            style,
        );
4068
        content.extend(text_items);
4068
        return Ok((content, child_map));
14067
    }
14067
    let ifc_root_node_type = match ifc_root_node_data.get_node_type() {
1741
        NodeType::Div => "Div",
        NodeType::Text(_) => "Text",
70
        NodeType::Body => "Body",
12256
        _ => "Other",
    };
14067
    debug_info!(
14067
        ctx,
14067
        "[collect_and_measure_inline_content] IFC root has {} DOM children",
14067
        dom_children.len()
    );
14071
    for (item_idx, &dom_child_id) in dom_children.iter().enumerate() {
14071
        let content_index = ContentIndex {
14071
            run_index: ifc_root_index as u32,
14071
            item_index: item_idx as u32,
14071
        };
14071
        let node_data = &ctx.styled_dom.node_data.as_container()[dom_child_id];
        // Check if this is a text node
14071
        if let NodeType::Text(ref text_content) = node_data.get_node_type() {
5280
            debug_info!(
5280
                ctx,
5280
                "[collect_and_measure_inline_content] OK: Found text node (DOM child {:?}): '{}'",
                dom_child_id,
5280
                text_content.as_str()
            );
            // Get style from the TEXT NODE itself (dom_child_id), not the IFC root
            // This ensures inline styles like color: #666666 are applied to the text
            // Uses split_text_for_whitespace to correctly handle white-space: pre with \n
5280
            let style = Arc::new(get_style_properties(ctx.styled_dom, dom_child_id, ctx.system_style.as_ref(), PhysicalSize::new(ctx.viewport_size.width, ctx.viewport_size.height)));
5280
            let text_items = split_text_for_whitespace(
5280
                ctx.styled_dom,
5280
                dom_child_id,
5280
                text_content.as_str(),
5280
                style,
            );
5280
            content.extend(text_items);
            // Set IFC membership on the text node's layout node (if it exists)
            // Text nodes may or may not have their own layout tree entry depending on
            // whether they're wrapped in an anonymous IFC wrapper
5280
            if let Some(&layout_idx) = tree.dom_to_layout.get(&dom_child_id).and_then(|v| v.first()) {
5280
                if let Some(warm_mut) = tree.warm_mut(layout_idx) {
5280
                    warm_mut.ifc_membership = Some(IfcMembership {
5280
                        ifc_id,
5280
                        ifc_root_layout_index: ifc_root_index,
5280
                        run_index: current_run_index,
5280
                    });
5280
                }
            }
5280
            current_run_index += 1;
5280
            continue;
8791
        }
        // For non-text nodes, find their corresponding layout tree node
8791
        let child_index = children
8791
            .iter()
8795
            .find(|&&idx| {
8795
                tree.get(idx)
8795
                    .and_then(|n| n.dom_node_id)
8795
                    .map(|id| id == dom_child_id)
8795
                    .unwrap_or(false)
8795
            })
8791
            .copied();
8791
        let Some(child_index) = child_index else {
            debug_info!(
                ctx,
                "[collect_and_measure_inline_content] WARN: DOM child {:?} has no layout node",
                dom_child_id
            );
            continue;
        };
8791
        let child_node = tree.get(child_index).ok_or(LayoutError::InvalidTree)?;
        // At this point we have a non-text DOM child with a layout node
8791
        let dom_id = child_node.dom_node_id.unwrap();
8791
        let display = get_display_property(ctx.styled_dom, Some(dom_id)).unwrap_or_default();
8791
        if display != LayoutDisplay::Inline {
            // This is an atomic inline-level box (e.g., inline-block, image).
            // We must determine its size and baseline before passing it to text3.
            // The intrinsic sizing pass has already calculated its preferred size.
140
            let intrinsic_size = tree.warm(child_index).and_then(|w| w.intrinsic_sizes.clone()).unwrap_or_default();
140
            let box_props = child_node.box_props.unpack();
140
            let styled_node_state = ctx
140
                .styled_dom
140
                .styled_nodes
140
                .as_container()
140
                .get(dom_id)
140
                .map(|n| n.styled_node_state.clone())
140
                .unwrap_or_default();
            // Calculate tentative border-box size based on CSS properties
            // This correctly handles explicit width/height, box-sizing, and constraints
140
            let tentative_size = crate::solver3::sizing::calculate_used_size_for_node(
140
                ctx.styled_dom,
140
                Some(dom_id),
140
                &constraints.containing_block_size,
140
                intrinsic_size,
140
                &box_props,
140
                &ctx.viewport_size,
            )?;
140
            let writing_mode =
140
                get_writing_mode(ctx.styled_dom, dom_id, &styled_node_state).unwrap_or_default();
            // Determine content-box size for laying out children
140
            let content_box_size = box_props.inner_size(tentative_size, writing_mode);
140
            debug_info!(
140
                ctx,
140
                "[collect_and_measure_inline_content] Inline-block NodeId({:?}): \
140
                 tentative_border_box={:?}, content_box={:?}",
                dom_id,
                tentative_size,
                content_box_size
            );
            // To find its height and baseline, we must lay out its contents.
140
            let child_wm_ctx = super::geometry::WritingModeContext::new(
140
                writing_mode,
140
                get_direction_property(ctx.styled_dom, dom_id, &styled_node_state)
140
                    .unwrap_or_default(),
140
                get_text_orientation_property(ctx.styled_dom, dom_id, &styled_node_state)
140
                    .unwrap_or_default(),
            );
140
            let child_constraints = LayoutConstraints {
140
                available_size: LogicalSize::new(content_box_size.width, f32::INFINITY),
140
                writing_mode,
140
                writing_mode_ctx: child_wm_ctx,
140
                // Inline-blocks establish a new BFC, so no state is passed in.
140
                bfc_state: None,
140
                // Does not affect size/baseline of the container.
140
                text_align: TextAlign::Start,
140
                containing_block_size: constraints.containing_block_size,
140
                available_width_type: Text3AvailableSpace::Definite(content_box_size.width),
140
            };
            // Drop the immutable borrow before calling layout_formatting_context
140
            drop(child_node);
            // Recursively lay out the inline-block to get its final height and baseline.
            // Note: This does not affect its final position, only its dimensions.
140
            let mut empty_float_cache = HashMap::new();
140
            let layout_result = layout_formatting_context(
140
                ctx,
140
                tree,
140
                text_cache,
140
                child_index,
140
                &child_constraints,
140
                &mut empty_float_cache,
            )?;
140
            let css_height = get_css_height(ctx.styled_dom, dom_id, &styled_node_state);
            // Determine final border-box height
140
            let final_height = match css_height.clone().unwrap_or_default() {
                LayoutHeight::Auto => {
                    // For auto height, add padding and border to the content height
140
                    let content_height = layout_result.output.overflow_size.height;
140
                    content_height
140
                        + box_props.padding.main_sum(writing_mode)
140
                        + box_props.border.main_sum(writing_mode)
                }
                // For explicit height, calculate_used_size_for_node already gave us the correct border-box height
                _ => tentative_size.height,
            };
140
            debug_info!(
140
                ctx,
140
                "[collect_and_measure_inline_content] Inline-block NodeId({:?}): \
140
                 layout_content_height={}, css_height={:?}, final_border_box_height={}",
                dom_id,
                layout_result.output.overflow_size.height,
                css_height,
                final_height
            );
140
            let final_size = LogicalSize::new(tentative_size.width, final_height);
            // Update the node in the tree with its now-known used size.
140
            tree.get_mut(child_index).unwrap().used_size = Some(final_size);
            // CSS 2.2 § 10.8.1: For inline-block elements, the baseline is the baseline of the
            // last line box in the normal flow, unless it has either no in-flow line boxes or
            // if its 'overflow' property has a computed value other than 'visible', in which
            // case the baseline is the bottom margin edge.
            //
            // `layout_result.output.baseline` returns the Y-position of the baseline measured
            // from the TOP of the content box. But `get_item_vertical_metrics` expects
            // `baseline_offset` to be the distance from the BOTTOM to the baseline.
            //
            // Conversion: baseline_offset_from_bottom = height - baseline_from_top
            //
            // If no baseline is found (e.g., the inline-block has no text), or if
            // overflow is not 'visible', we fall back to the bottom margin edge
            // (baseline_offset = 0, meaning baseline at bottom).
140
            let overflow_x = get_overflow_x(ctx.styled_dom, dom_id, &styled_node_state).unwrap_or_default();
140
            let overflow_y = get_overflow_y(ctx.styled_dom, dom_id, &styled_node_state).unwrap_or_default();
140
            let overflow_is_visible = matches!(
140
                (overflow_x, overflow_y),
                (LayoutOverflow::Visible, LayoutOverflow::Visible)
            );
140
            let baseline_from_top = layout_result.output.baseline;
140
            let baseline_offset = match baseline_from_top {
                Some(baseline_y) if overflow_is_visible => {
                    // baseline_y is measured from top of content box
                    // We need to add padding and border to get the position within the border-box
                    let content_box_top = box_props.padding.top + box_props.border.top;
                    let baseline_from_border_box_top = baseline_y + content_box_top;
                    // Convert to distance from bottom
                    (final_height - baseline_from_border_box_top).max(0.0)
                }
                _ => {
                    // No baseline found or overflow != visible - use bottom margin edge
140
                    0.0
                }
            };
140
            debug_info!(
140
                ctx,
140
                "[collect_and_measure_inline_content] Inline-block NodeId({:?}): \
140
                 baseline_from_top={:?}, final_height={}, baseline_offset_from_bottom={}",
                dom_id,
                baseline_from_top,
                final_height,
                baseline_offset
            );
            // Get margins for inline-block positioning
            // For inline-blocks, we need to include margins in the shape size
            // so that text3 positions them correctly with spacing
140
            let margin = &box_props.margin;
140
            let margin_box_width = final_size.width + margin.left + margin.right;
140
            let margin_box_height = final_size.height + margin.top + margin.bottom;
            // For inline-block shapes, text3 uses the content array index as run_index
            // and always item_index=0 for objects. We must match this when inserting into child_map.
140
            let shape_content_index = ContentIndex {
140
                run_index: content.len() as u32,
140
                item_index: 0,
140
            };
            // the box used for alignment is the margin box" - using margin_box_width/height here
140
            content.push(InlineContent::Shape(InlineShape {
140
                shape_def: ShapeDefinition::Rectangle {
140
                    size: crate::text3::cache::Size {
140
                        // Use margin-box size for positioning in inline flow
140
                        width: margin_box_width,
140
                        height: margin_box_height,
140
                    },
140
                    corner_radius: None,
140
                },
140
                fill: None,
140
                stroke: None,
140
                // Adjust baseline offset by top margin
140
                baseline_offset: baseline_offset + margin.top,
140
                alignment: crate::solver3::getters::get_vertical_align_for_node(ctx.styled_dom, dom_id),
140
                source_node_id: Some(dom_id),
140
            }));
140
            child_map.insert(shape_content_index, child_index);
        } else if let NodeType::Image(image_ref) =
8651
            ctx.styled_dom.node_data.as_container()[dom_id].get_node_type()
        {
            // +spec:replaced-elements:31a782 - replaced elements (img) not rendered purely by CSS box concepts
            // Images are replaced elements - they have intrinsic dimensions
            // and CSS width/height can constrain them
            // Re-get child_node since we dropped it earlier for the inline-block case
            let child_node = tree.get(child_index).ok_or(LayoutError::InvalidTree)?;
            let box_props = child_node.box_props.unpack();
            // Get intrinsic size from the image data or fall back to layout node
            let intrinsic_size = tree.warm(child_index)
                .and_then(|w| w.intrinsic_sizes.clone())
                .unwrap_or(IntrinsicSizes {
                    max_content_width: 50.0,
                    max_content_height: 50.0,
                    ..Default::default()
                });
            // Get styled node state for CSS property lookup
            let styled_node_state = ctx
                .styled_dom
                .styled_nodes
                .as_container()
                .get(dom_id)
                .map(|n| n.styled_node_state.clone())
                .unwrap_or_default();
            // Calculate the used size respecting CSS width/height constraints
            let tentative_size = crate::solver3::sizing::calculate_used_size_for_node(
                ctx.styled_dom,
                Some(dom_id),
                &constraints.containing_block_size,
                intrinsic_size.clone(),
                &box_props,
                &ctx.viewport_size,
            )?;
            // Drop immutable borrow before mutable access
            drop(child_node);
            // Set the used_size on the layout node so paint_rect works correctly
            let final_size = LogicalSize::new(tentative_size.width, tentative_size.height);
            tree.get_mut(child_index).unwrap().used_size = Some(final_size);
            // Calculate display size for text3 (this is what text3 uses for positioning)
            let display_width = if final_size.width > 0.0 { 
                Some(final_size.width) 
            } else { 
                None 
            };
            let display_height = if final_size.height > 0.0 { 
                Some(final_size.height) 
            } else { 
                None 
            };
            content.push(InlineContent::Image(InlineImage {
                source: ImageSource::Ref(image_ref.as_ref().clone()),
                intrinsic_size: crate::text3::cache::Size {
                    width: intrinsic_size.max_content_width,
                    height: intrinsic_size.max_content_height,
                },
                display_size: if display_width.is_some() || display_height.is_some() {
                    Some(crate::text3::cache::Size {
                        width: display_width.unwrap_or(intrinsic_size.max_content_width),
                        height: display_height.unwrap_or(intrinsic_size.max_content_height),
                    })
                } else {
                    None
                },
                // Images are bottom-aligned with the baseline by default
                baseline_offset: 0.0,
                alignment: crate::text3::cache::VerticalAlign::Baseline,
                object_fit: ObjectFit::Fill,
            }));
            // For images, text3 uses the content array index as run_index
            // and always item_index=0 for objects. We must match this.
            let image_content_index = ContentIndex {
                run_index: (content.len() - 1) as u32,  // -1 because we just pushed
                item_index: 0,
            };
            child_map.insert(image_content_index, child_index);
        } else {
            // This is a regular inline box (display: inline) - e.g., <span>, <em>, <strong>
            //
            // According to CSS Inline-3 spec §2, inline boxes are "transparent" wrappers
            // We must recursively collect their text children with inherited style
8651
            debug_info!(
8651
                ctx,
8651
                "[collect_and_measure_inline_content] Found inline span (DOM {:?}), recursing",
                dom_id
            );
8651
            let span_style = get_style_properties(ctx.styled_dom, dom_id, ctx.system_style.as_ref(), PhysicalSize::new(ctx.viewport_size.width, ctx.viewport_size.height));
8651
            collect_inline_span_recursive(
8651
                ctx,
8651
                tree,
8651
                dom_id,
8651
                span_style,
8651
                &mut content,
8651
                &mut child_map,
8651
                &children,
8651
                constraints,
            )?;
        }
    }
14067
    Ok((content, child_map))
18205
}
// +spec:display-property:c05c53 - inlinifying boxes can't contain block-level boxes; children are recursively inlinified
// it recursively inlinifies all of its in-flow children, so that no block-level descendants
// break up the inline formatting context in which it participates.
// +spec:display-property:aee879 - recursively inlinifies in-flow children of inline boxes
/// Recursively collects inline content from an inline span (display: inline) element.
///
/// According to CSS Inline Layout Module Level 3 §2:
///
/// "Inline boxes are transparent wrappers that wrap their content."
///
/// They don't create a new formatting context - their children participate in the
/// same IFC as the parent. This function processes:
///
/// - Text nodes: collected with the span's inherited style
/// - Nested inline spans: recursively descended
/// - Inline-blocks, images: measured and added as shapes
9316
fn collect_inline_span_recursive<T: ParsedFontTrait>(
9316
    ctx: &mut LayoutContext<'_, T>,
9316
    tree: &mut LayoutTree,
9316
    span_dom_id: NodeId,
9316
    span_style: StyleProperties,
9316
    content: &mut Vec<InlineContent>,
9316
    child_map: &mut HashMap<ContentIndex, usize>,
9316
    parent_children: &[usize], // Layout tree children of parent IFC
9316
    constraints: &LayoutConstraints,
9316
) -> Result<()> {
9316
    debug_info!(
9316
        ctx,
9316
        "[collect_inline_span_recursive] Processing inline span {:?}",
        span_dom_id
    );
    // Get DOM children of this span
9316
    let span_dom_children: Vec<NodeId> = span_dom_id
9316
        .az_children(&ctx.styled_dom.node_hierarchy.as_container())
9316
        .collect();
9316
    debug_info!(
9316
        ctx,
9316
        "[collect_inline_span_recursive] Span has {} DOM children",
9316
        span_dom_children.len()
    );
    // +spec:box-model:b7428d - empty inline boxes still have margins, padding, borders, line-height
    // +spec:box-model:cc79a4 - empty inline elements still have margins, padding, borders and line height
9316
    if span_dom_children.is_empty() {
        let node_state = &ctx.styled_dom.styled_nodes.as_container()[span_dom_id].styled_node_state;
        let font_size = get_element_font_size(ctx.styled_dom, span_dom_id, node_state);
        let line_height_value = crate::solver3::getters::get_line_height_value(
            ctx.styled_dom, span_dom_id, &node_state
        );
        let line_height = line_height_value
            .map(|v| text3::cache::LineHeight::Px(v.inner.normalized() * font_size))
            .unwrap_or(text3::cache::LineHeight::Normal);
        let cb_width = constraints.containing_block_size.main(constraints.writing_mode);
        let padding_top = crate::solver3::getters::get_css_padding_top(ctx.styled_dom, span_dom_id, &node_state)
            .exact().map(|pv| pv.to_pixels_internal(cb_width, font_size, DEFAULT_FONT_SIZE)).unwrap_or(0.0);
        let padding_bottom = crate::solver3::getters::get_css_padding_bottom(ctx.styled_dom, span_dom_id, &node_state)
            .exact().map(|pv| pv.to_pixels_internal(cb_width, font_size, DEFAULT_FONT_SIZE)).unwrap_or(0.0);
        let padding_left = crate::solver3::getters::get_css_padding_left(ctx.styled_dom, span_dom_id, &node_state)
            .exact().map(|pv| pv.to_pixels_internal(cb_width, font_size, DEFAULT_FONT_SIZE)).unwrap_or(0.0);
        let padding_right = crate::solver3::getters::get_css_padding_right(ctx.styled_dom, span_dom_id, &node_state)
            .exact().map(|pv| pv.to_pixels_internal(cb_width, font_size, DEFAULT_FONT_SIZE)).unwrap_or(0.0);
        let border_top = crate::solver3::getters::get_css_border_top_width(ctx.styled_dom, span_dom_id, &node_state)
            .exact().map(|pv| pv.to_pixels_internal(cb_width, font_size, DEFAULT_FONT_SIZE)).unwrap_or(0.0);
        let border_bottom = crate::solver3::getters::get_css_border_bottom_width(ctx.styled_dom, span_dom_id, &node_state)
            .exact().map(|pv| pv.to_pixels_internal(cb_width, font_size, DEFAULT_FONT_SIZE)).unwrap_or(0.0);
        let border_left = crate::solver3::getters::get_css_border_left_width(ctx.styled_dom, span_dom_id, &node_state)
            .exact().map(|pv| pv.to_pixels_internal(cb_width, font_size, DEFAULT_FONT_SIZE)).unwrap_or(0.0);
        let border_right = crate::solver3::getters::get_css_border_right_width(ctx.styled_dom, span_dom_id, &node_state)
            .exact().map(|pv| pv.to_pixels_internal(cb_width, font_size, DEFAULT_FONT_SIZE)).unwrap_or(0.0);
        let margin_left = crate::solver3::getters::get_css_margin_left(ctx.styled_dom, span_dom_id, &node_state)
            .exact().map(|pv| pv.to_pixels_internal(cb_width, font_size, DEFAULT_FONT_SIZE)).unwrap_or(0.0);
        let margin_right = crate::solver3::getters::get_css_margin_right(ctx.styled_dom, span_dom_id, &node_state)
            .exact().map(|pv| pv.to_pixels_internal(cb_width, font_size, DEFAULT_FONT_SIZE)).unwrap_or(0.0);
        let resolved_line_height = line_height.resolve(font_size, 0.0, 0.0, 0.0, 0);
        let total_height = resolved_line_height + padding_top + padding_bottom + border_top + border_bottom;
        let total_width = margin_left + padding_left + border_left
            + border_right + padding_right + margin_right;
        content.push(InlineContent::Shape(InlineShape {
            shape_def: ShapeDefinition::Rectangle {
                size: crate::text3::cache::Size {
                    width: total_width,
                    height: total_height,
                },
                corner_radius: None,
            },
            fill: None,
            stroke: None,
            baseline_offset: 0.0,
            alignment: crate::solver3::getters::get_vertical_align_for_node(ctx.styled_dom, span_dom_id),
            source_node_id: Some(span_dom_id),
        }));
        return Ok(());
9316
    }
18632
    for &child_dom_id in &span_dom_children {
9316
        let node_data = &ctx.styled_dom.node_data.as_container()[child_dom_id];
        // CASE 1: Text node - collect with span's style
9316
        if let NodeType::Text(ref text_content) = node_data.get_node_type() {
8651
            debug_info!(
8651
                ctx,
8651
                "[collect_inline_span_recursive] ✓ Found text in span: '{}'",
8651
                text_content.as_str()
            );
8651
            let text_items = split_text_for_whitespace(
8651
                ctx.styled_dom,
8651
                child_dom_id,
8651
                text_content.as_str(),
8651
                Arc::new(span_style.clone()),
            );
8651
            content.extend(text_items);
8651
            continue;
665
        }
        // CASE 2: Element node - check its display type
665
        let child_display =
665
            get_display_property(ctx.styled_dom, Some(child_dom_id)).unwrap_or_default();
        // Find the corresponding layout tree node
665
        let child_index = parent_children
665
            .iter()
665
            .find(|&&idx| {
665
                tree.get(idx)
665
                    .and_then(|n| n.dom_node_id)
665
                    .map(|id| id == child_dom_id)
665
                    .unwrap_or(false)
665
            })
665
            .copied();
665
        match child_display {
            LayoutDisplay::Inline => {
                // Nested inline span - recurse with child's style
665
                debug_info!(
665
                    ctx,
665
                    "[collect_inline_span_recursive] Found nested inline span {:?}",
                    child_dom_id
                );
665
                let child_style = get_style_properties(ctx.styled_dom, child_dom_id, ctx.system_style.as_ref(), PhysicalSize::new(ctx.viewport_size.width, ctx.viewport_size.height));
665
                collect_inline_span_recursive(
665
                    ctx,
665
                    tree,
665
                    child_dom_id,
665
                    child_style,
665
                    content,
665
                    child_map,
665
                    parent_children,
665
                    constraints,
                )?;
            }
            LayoutDisplay::InlineBlock => {
                // Inline-block inside span - measure and add as shape
                let Some(child_index) = child_index else {
                    debug_info!(
                        ctx,
                        "[collect_inline_span_recursive] WARNING: inline-block {:?} has no layout \
                         node",
                        child_dom_id
                    );
                    continue;
                };
                let child_node = tree.get(child_index).ok_or(LayoutError::InvalidTree)?;
                let intrinsic_size = tree.warm(child_index).and_then(|w| w.intrinsic_sizes.clone()).unwrap_or_default();
                let width = intrinsic_size.max_content_width;
                let styled_node_state = ctx
                    .styled_dom
                    .styled_nodes
                    .as_container()
                    .get(child_dom_id)
                    .map(|n| n.styled_node_state.clone())
                    .unwrap_or_default();
                let writing_mode =
                    get_writing_mode(ctx.styled_dom, child_dom_id, &styled_node_state)
                        .unwrap_or_default();
                let child_wm_ctx = super::geometry::WritingModeContext::new(
                    writing_mode,
                    get_direction_property(ctx.styled_dom, child_dom_id, &styled_node_state)
                        .unwrap_or_default(),
                    get_text_orientation_property(ctx.styled_dom, child_dom_id, &styled_node_state)
                        .unwrap_or_default(),
                );
                let child_constraints = LayoutConstraints {
                    available_size: LogicalSize::new(width, f32::INFINITY),
                    writing_mode,
                    writing_mode_ctx: child_wm_ctx,
                    bfc_state: None,
                    text_align: TextAlign::Start,
                    containing_block_size: constraints.containing_block_size,
                    available_width_type: Text3AvailableSpace::Definite(width),
                };
                drop(child_node);
                let mut empty_float_cache = HashMap::new();
                let layout_result = layout_formatting_context(
                    ctx,
                    tree,
                    &mut TextLayoutCache::default(),
                    child_index,
                    &child_constraints,
                    &mut empty_float_cache,
                )?;
                let final_height = layout_result.output.overflow_size.height;
                let final_size = LogicalSize::new(width, final_height);
                tree.get_mut(child_index).unwrap().used_size = Some(final_size);
                // CSS 2.2 § 10.8.1: inline-block baseline fallback
                let overflow_x = get_overflow_x(ctx.styled_dom, child_dom_id, &styled_node_state).unwrap_or_default();
                let overflow_y = get_overflow_y(ctx.styled_dom, child_dom_id, &styled_node_state).unwrap_or_default();
                let overflow_is_visible = matches!(
                    (overflow_x, overflow_y),
                    (LayoutOverflow::Visible, LayoutOverflow::Visible)
                );
                let baseline_offset = if overflow_is_visible {
                    layout_result.output.baseline.unwrap_or(final_height)
                } else {
                    final_height
                };
                content.push(InlineContent::Shape(InlineShape {
                    shape_def: ShapeDefinition::Rectangle {
                        size: crate::text3::cache::Size {
                            width,
                            height: final_height,
                        },
                        corner_radius: None,
                    },
                    fill: None,
                    stroke: None,
                    baseline_offset,
                    alignment: crate::solver3::getters::get_vertical_align_for_node(ctx.styled_dom, child_dom_id),
                    source_node_id: Some(child_dom_id),
                }));
                // Note: We don't add to child_map here because this is inside a span
                debug_info!(
                    ctx,
                    "[collect_inline_span_recursive] Added inline-block shape {}x{}",
                    width,
                    final_height
                );
            }
            _ => {
                // +spec:display-property:0684c4 - block box inlinified: inner display becomes flow-root (treated as atomic inline)
                // in-flow children of an inline box are recursively inlinified so they
                // don't break the IFC. Treat them as inline spans and recurse into their
                // children to collect text and inline content.
                debug_info!(
                    ctx,
                    "[collect_inline_span_recursive] Inlinifying block-level child {:?} \
                     (display: {:?}) inside inline span per css-display-3 §2.7",
                    child_dom_id,
                    child_display
                );
                let child_style = get_style_properties(ctx.styled_dom, child_dom_id, ctx.system_style.as_ref(), PhysicalSize::new(ctx.viewport_size.width, ctx.viewport_size.height));
                collect_inline_span_recursive(
                    ctx,
                    tree,
                    child_dom_id,
                    child_style,
                    content,
                    child_map,
                    parent_children,
                    constraints,
                )?;
            }
        }
    }
9316
    Ok(())
9316
}
/// Positions a floated child within the BFC and updates the floating context.
/// This function is fully writing-mode aware.
fn position_floated_child(
    _child_index: usize,
    child_margin_box_size: LogicalSize,
    float_type: LayoutFloat,
    constraints: &LayoutConstraints,
    _bfc_content_box: LogicalRect,
    current_main_offset: f32,
    floating_context: &mut FloatingContext,
) -> Result<LogicalPosition> {
    let wm = constraints.writing_mode;
    let child_main_size = child_margin_box_size.main(wm);
    let child_cross_size = child_margin_box_size.cross(wm);
    let bfc_cross_size = constraints.available_size.cross(wm);
    let mut placement_main_offset = current_main_offset;
    loop {
        // 1. Determine the available cross-axis space at the current
        // `placement_main_offset`.
        let (available_cross_start, available_cross_end) = floating_context
            .available_line_box_space(
                placement_main_offset,
                placement_main_offset + child_main_size,
                bfc_cross_size,
                wm,
            );
        let available_cross_width = available_cross_end - available_cross_start;
        // 2. Check if the new float can fit in the available space.
        if child_cross_size <= available_cross_width {
            // It fits! Determine the final position and add it to the context.
            // +spec:floats:5cfc93 - float:right positions box at cross-end, content flows on left
            let final_cross_pos = match float_type {
                LayoutFloat::Left => available_cross_start,
                // +spec:floats:5cfc93 - float:right positions box at cross-end, content flows on left
                LayoutFloat::Right => available_cross_end - child_cross_size,
                LayoutFloat::None => {
                    return Err(LayoutError::PositioningFailed);
                }
            };
            let final_pos =
                LogicalPosition::from_main_cross(placement_main_offset, final_cross_pos, wm);
            let new_float_box = FloatBox {
                kind: float_type,
                rect: LogicalRect::new(final_pos, child_margin_box_size),
                margin: EdgeSizes::default(), // TODO: Pass actual margin if this function is used
            };
            floating_context.floats.push(new_float_box);
            return Ok(final_pos);
        } else {
            // +spec:floats:3d89d8 - shift float downward when not enough horizontal room
            // It doesn't fit. We must move the float down past an obstacle.
            // Find the lowest main-axis end of all floats that are blocking
            // the current line.
            let mut next_main_offset = f32::INFINITY;
            for existing_float in &floating_context.floats {
                let float_main_start = existing_float.rect.origin.main(wm);
                let float_main_end = float_main_start + existing_float.rect.size.main(wm);
                // Consider only floats that are above or at the current placement line.
                if placement_main_offset < float_main_end {
                    next_main_offset = next_main_offset.min(float_main_end);
                }
            }
            if next_main_offset.is_infinite() {
                // This indicates an unrecoverable state, e.g., a float wider
                // than the container.
                return Err(LayoutError::PositioningFailed);
            }
            placement_main_offset = next_main_offset;
        }
    }
}
// CSS Property Getters
/// Get the CSS `float` property for a node.
15715
fn get_float_property(styled_dom: &StyledDom, dom_id: Option<NodeId>) -> LayoutFloat {
15715
    let Some(id) = dom_id else {
        return LayoutFloat::None;
    };
15715
    let node_state = &styled_dom.styled_nodes.as_container()[id].styled_node_state;
15715
    get_float(styled_dom, id, node_state).unwrap_or(LayoutFloat::None)
15715
}
15715
fn get_clear_property(styled_dom: &StyledDom, dom_id: Option<NodeId>) -> LayoutClear {
15715
    let Some(id) = dom_id else {
        return LayoutClear::None;
    };
15715
    let node_state = &styled_dom.styled_nodes.as_container()[id].styled_node_state;
15715
    get_clear(styled_dom, id, node_state).unwrap_or(LayoutClear::None)
15715
}
/// Helper to determine if scrollbars are needed.
///
/// # CSS Spec Reference
/// CSS Overflow Module Level 3 § 3: Scrollable overflow
// +spec:block-formatting-context:50d915 - overflow-x handles horizontal, overflow-y handles vertical
// +spec:box-model:63d6f2 - scrollable overflow extends beyond padding edge, needs scroll mechanism
// +spec:box-model:45b5fb - scrollbar space subtracted from content area, inserted between inner border edge and outer padding edge
// +spec:box-model:70a0a4 - UAs must start assuming no scrollbars needed, recalculate if they are
// +spec:box-model:c1b0b2 - scrollbar gutter is space between inner border edge and outer padding edge
// +spec:overflow:4f5b99 - scrollable overflow rectangle: content_size is the minimal axis-aligned rect containing scrollable overflow
// +spec:overflow:e983f4 - overflow:auto/scroll boxes must allow user to access overflowed content via scrollbars
// +spec:overflow:97c257 - relative positioning causing overflow in auto/scroll boxes must trigger scrollbar creation
21350
pub fn check_scrollbar_necessity(
21350
    content_size: LogicalSize,
21350
    container_size: LogicalSize,
21350
    overflow_x: OverflowBehavior,
21350
    overflow_y: OverflowBehavior,
21350
    scrollbar_width_px: f32,
21350
) -> ScrollbarRequirements {
    // Use epsilon for float comparisons to avoid showing scrollbars due to 
    // floating-point rounding errors. Without this, content that exactly fits
    // may show scrollbars due to sub-pixel differences (e.g., 299.9999 vs 300.0).
    const EPSILON: f32 = 1.0;
    // +spec:height-calculation:c5af64 - assume no scrollbars initially; only add if content overflows
    // Determine if scrolling is needed based on overflow properties.
    // +spec:overflow:30a49c - start assuming no scrollbars, recalculate if needed
    // Note: scrollbar_width_px can be 0 for overlay scrollbars (e.g. macOS),
    // but we still need to register scroll nodes so that scrolling works —
    // overlay scrollbars just don't reserve any layout space.
21350
    let mut needs_horizontal = match overflow_x {
20965
        OverflowBehavior::Visible | OverflowBehavior::Hidden | OverflowBehavior::Clip => false,
35
        OverflowBehavior::Scroll => true,
350
        OverflowBehavior::Auto => content_size.width > container_size.width + EPSILON,
    };
21350
    let mut needs_vertical = match overflow_y {
20930
        OverflowBehavior::Visible | OverflowBehavior::Hidden | OverflowBehavior::Clip => false,
35
        OverflowBehavior::Scroll => true,
385
        OverflowBehavior::Auto => content_size.height > container_size.height + EPSILON,
    };
    // +spec:box-model:c3d73f - scrollbar presence affects available content area; padding preserved at scroll end
    // +spec:overflow:d79159 - scrollbar sizing: adding a scrollbar reduces available space,
    // which may cause content to overflow, confirming the scrollbar is needed (two-pass check)
    // A classic layout problem: a vertical scrollbar can reduce horizontal space,
    // causing a horizontal scrollbar to appear, which can reduce vertical space...
    // A full solution involves a loop, but this two-pass check handles most cases.
    // Only relevant when scrollbars reserve layout space (non-overlay).
21350
    if scrollbar_width_px > 0.0 {
21315
        if needs_vertical && !needs_horizontal && overflow_x == OverflowBehavior::Auto {
105
            if content_size.width > (container_size.width - scrollbar_width_px) + EPSILON {
35
                needs_horizontal = true;
70
            }
21210
        }
21315
        if needs_horizontal && !needs_vertical && overflow_y == OverflowBehavior::Auto {
105
            if content_size.height > (container_size.height - scrollbar_width_px) + EPSILON {
35
                needs_vertical = true;
70
            }
21210
        }
35
    }
    ScrollbarRequirements {
21350
        needs_horizontal,
21350
        needs_vertical,
21350
        scrollbar_width: if needs_vertical {
280
            scrollbar_width_px
        } else {
21070
            0.0
        },
21350
        scrollbar_height: if needs_horizontal {
210
            scrollbar_width_px
        } else {
21140
            0.0
        },
        // visual_width_px is set by the caller (compute_scrollbar_info_core)
        // since this function doesn't have access to the CSS style context.
        visual_width_px: 0.0,
    }
21350
}
/// Calculates a single collapsed margin from two adjoining vertical margins.
///
/// Implements the rules from CSS 2.1 section 8.3.1:
/// - If both margins are positive, the result is the larger of the two.
/// - If both margins are negative, the result is the more negative of the two.
/// - If the margins have mixed signs, they are effectively summed.
// +spec:margin-collapsing:814a26 - vertical margins between sibling blocks collapse
16030
pub fn collapse_margins(a: f32, b: f32) -> f32 {
16030
    if a.is_sign_positive() && b.is_sign_positive() {
15540
        a.max(b)
490
    } else if a.is_sign_negative() && b.is_sign_negative() {
210
        a.min(b)
    } else {
280
        a + b
    }
16030
}
/// Helper function to advance the pen position with margin collapsing.
///
/// This implements CSS 2.1 margin collapsing for adjacent block-level boxes in a BFC.
///
/// - `pen` - Current main-axis position (will be modified)
/// - `last_margin_bottom` - The bottom margin of the previous in-flow element
/// - `current_margin_top` - The top margin of the current element
///
/// # Returns
///
/// The new `last_margin_bottom` value (the bottom margin of the current element)
///
/// # CSS Spec Compliance
///
/// Per CSS 2.1 Section 8.3.1 "Collapsing margins":
///
/// - Adjacent vertical margins of block boxes collapse
/// - The resulting margin width is the maximum of the adjoining margins (if both positive)
/// - Or the sum of the most positive and most negative (if signs differ)
fn advance_pen_with_margin_collapse(
    pen: &mut f32,
    last_margin_bottom: f32,
    current_margin_top: f32,
) -> f32 {
    // Collapse the previous element's bottom margin with current element's top margin
    let collapsed_margin = collapse_margins(last_margin_bottom, current_margin_top);
    // Advance pen by the collapsed margin
    *pen += collapsed_margin;
    // Return collapsed_margin so caller knows how much space was actually added
    collapsed_margin
}
/// Checks if an element's border or padding prevents margin collapsing.
///
/// Per CSS 2.1 Section 8.3.1:
///
/// - Border between margins prevents collapsing
/// - Padding between margins prevents collapsing
///
/// # Arguments
///
/// - `box_props` - The box properties containing border and padding
/// - `writing_mode` - The writing mode to determine main axis
/// - `check_start` - If true, check main-start (top); if false, check main-end (bottom)
///
/// # Returns
///
/// `true` if border or padding exists and prevents collapsing
// +spec:box-model:ca8ceb - margin collapsing uses block-start/block-end per writing mode
92225
fn has_margin_collapse_blocker(
92225
    box_props: &crate::solver3::geometry::BoxProps,
92225
    writing_mode: LayoutWritingMode,
92225
    check_start: bool, // true = check top/start, false = check bottom/end
92225
) -> bool {
92225
    if check_start {
        // Check if there's border-top or padding-top
39165
        let border_start = box_props.border.main_start(writing_mode);
39165
        let padding_start = box_props.padding.main_start(writing_mode);
39165
        border_start > 0.0 || padding_start > 0.0
    } else {
        // Check if there's border-bottom or padding-bottom
53060
        let border_end = box_props.border.main_end(writing_mode);
53060
        let padding_end = box_props.padding.main_end(writing_mode);
53060
        border_end > 0.0 || padding_end > 0.0
    }
92225
}
/// Checks if an element is empty (has no content).
///
/// Per CSS 2.1 Section 8.3.1:
///
/// > If a block element has no border, padding, inline content, height, or min-height,
/// > then its top and bottom margins collapse with each other.
///
/// # Arguments
///
/// - `node` - The layout node to check
///
/// # Returns
///
/// `true` if the element is empty and its margins can collapse internally
14980
fn is_empty_block(tree: &LayoutTree, node_index: usize) -> bool {
14980
    let node = match tree.get(node_index) {
14980
        Some(n) => n,
        None => return true,
    };
    // Per CSS 2.2 § 8.3.1: An empty block is one that:
    // - Has zero computed 'min-height'
    // - Has zero or 'auto' computed 'height'
    // - Has no in-flow children
    // - Has no line boxes (no text/inline content)
    // Check if node has children
14980
    if !tree.children(node_index).is_empty() {
10815
        return false;
4165
    }
    // Check if node has inline content (text)
4165
    if tree.warm(node_index).and_then(|w| w.inline_layout_result.as_ref()).is_some() {
2170
        return false;
1995
    }
    // Check if node has explicit height > 0
    // CSS 2.2 § 8.3.1: Elements with explicit height are NOT empty
1995
    if let Some(size) = node.used_size {
1995
        if size.height > 0.0 {
1925
            return false;
70
        }
    }
    // Empty block: no children, no inline content, no height
70
    true
14980
}
/// Generates marker text for a list item marker.
///
/// This function looks up the counter value from the cache and formats it
/// according to the list-style-type property.
///
/// Per CSS Lists Module Level 3, the ::marker pseudo-element is the first child
/// of the list-item, and references the same DOM node. Counter resolution happens
/// on the list-item (parent) node.
fn generate_list_marker_text(
    tree: &LayoutTree,
    styled_dom: &StyledDom,
    marker_index: usize,
    counters: &HashMap<(usize, String), i32>,
    debug_messages: &mut Option<Vec<LayoutDebugMessage>>,
) -> String {
    use crate::solver3::counters::format_counter;
    // Get the marker node
    let marker_node = match tree.get(marker_index) {
        Some(n) => n,
        None => return String::new(),
    };
    // Verify this is actually a ::marker pseudo-element
    // Per spec, markers must be pseudo-elements, not anonymous boxes
    let marker_pseudo = tree.warm(marker_index).and_then(|w| w.pseudo_element);
    let marker_anonymous_type = tree.cold(marker_index).and_then(|c| c.anonymous_type);
    if marker_pseudo != Some(PseudoElement::Marker) {
        if let Some(msgs) = debug_messages {
            msgs.push(LayoutDebugMessage::warning(format!(
                "[generate_list_marker_text] WARNING: Node {} is not a ::marker pseudo-element \
                 (pseudo={:?}, anonymous_type={:?})",
                marker_index, marker_pseudo, marker_anonymous_type
            )));
        }
        // Fallback for old-style anonymous markers during transition
        if marker_anonymous_type != Some(AnonymousBoxType::ListItemMarker) {
            return String::new();
        }
    }
    // Get the parent list-item node (::marker is first child of list-item)
    let list_item_index = match marker_node.parent {
        Some(p) => p,
        None => {
            if let Some(msgs) = debug_messages {
                msgs.push(LayoutDebugMessage::error(
                    "[generate_list_marker_text] ERROR: Marker has no parent".to_string(),
                ));
            }
            return String::new();
        }
    };
    let list_item_node = match tree.get(list_item_index) {
        Some(n) => n,
        None => return String::new(),
    };
    let list_item_dom_id = match list_item_node.dom_node_id {
        Some(id) => id,
        None => {
            if let Some(msgs) = debug_messages {
                msgs.push(LayoutDebugMessage::error(
                    "[generate_list_marker_text] ERROR: List-item has no DOM ID".to_string(),
                ));
            }
            return String::new();
        }
    };
    if let Some(msgs) = debug_messages {
        msgs.push(LayoutDebugMessage::info(format!(
            "[generate_list_marker_text] marker_index={}, list_item_index={}, \
             list_item_dom_id={:?}",
            marker_index, list_item_index, list_item_dom_id
        )));
    }
    // Get list-style-type from the list-item or its container
    let list_container_dom_id = if let Some(grandparent_index) = list_item_node.parent {
        if let Some(grandparent) = tree.get(grandparent_index) {
            grandparent.dom_node_id
        } else {
            None
        }
    } else {
        None
    };
    // Try to get list-style-type from the list container first,
    // then fall back to the list-item
    let list_style_type = if let Some(container_id) = list_container_dom_id {
        let container_type = get_list_style_type(styled_dom, Some(container_id));
        if container_type != StyleListStyleType::default() {
            container_type
        } else {
            get_list_style_type(styled_dom, Some(list_item_dom_id))
        }
    } else {
        get_list_style_type(styled_dom, Some(list_item_dom_id))
    };
    // Get the counter value for "list-item" counter from the LIST-ITEM node
    // Per CSS spec, counters are scoped to elements, and the list-item counter
    // is incremented at the list-item element, not the marker pseudo-element
    let counter_value = counters
        .get(&(list_item_index, "list-item".to_string()))
        .copied()
        .unwrap_or_else(|| {
            if let Some(msgs) = debug_messages {
                msgs.push(LayoutDebugMessage::warning(format!(
                    "[generate_list_marker_text] WARNING: No counter found for list-item at index \
                     {}, defaulting to 1",
                    list_item_index
                )));
            }
            1
        });
    if let Some(msgs) = debug_messages {
        msgs.push(LayoutDebugMessage::info(format!(
            "[generate_list_marker_text] counter_value={} for list_item_index={}",
            counter_value, list_item_index
        )));
    }
    // Format the counter according to the list-style-type
    let marker_text = format_counter(counter_value, list_style_type);
    // For ordered lists (non-symbolic markers), add a period and space
    // For unordered lists (symbolic markers like •, ◦, ▪), just add a space
    if matches!(
        list_style_type,
        StyleListStyleType::Decimal
            | StyleListStyleType::DecimalLeadingZero
            | StyleListStyleType::LowerAlpha
            | StyleListStyleType::UpperAlpha
            | StyleListStyleType::LowerRoman
            | StyleListStyleType::UpperRoman
            | StyleListStyleType::LowerGreek
            | StyleListStyleType::UpperGreek
    ) {
        format!("{}. ", marker_text)
    } else {
        format!("{} ", marker_text)
    }
}
/// Generates marker text segments for a list item marker.
///
/// Simply returns a single StyledRun with the marker text using the base_style.
/// The font stack in base_style already includes fallbacks with 100% Unicode coverage,
/// so font resolution happens during text shaping, not here.
fn generate_list_marker_segments(
    tree: &LayoutTree,
    styled_dom: &StyledDom,
    marker_index: usize,
    counters: &HashMap<(usize, String), i32>,
    base_style: Arc<StyleProperties>,
    debug_messages: &mut Option<Vec<LayoutDebugMessage>>,
) -> Vec<StyledRun> {
    // Generate the marker text
    let marker_text =
        generate_list_marker_text(tree, styled_dom, marker_index, counters, debug_messages);
    if marker_text.is_empty() {
        return Vec::new();
    }
    if let Some(msgs) = debug_messages {
        let font_families: Vec<&str> = match &base_style.font_stack {
            crate::text3::cache::FontStack::Stack(selectors) => {
                selectors.iter().map(|f| f.family.as_str()).collect()
            }
            crate::text3::cache::FontStack::Ref(_) => vec!["<embedded-font>"],
        };
        msgs.push(LayoutDebugMessage::info(format!(
            "[generate_list_marker_segments] Marker text: '{}' with font stack: {:?}",
            marker_text,
            font_families
        )));
    }
    // Return single segment - font fallback happens during shaping
    // List markers are generated content, not from DOM nodes
    vec![StyledRun {
        text: marker_text,
        style: base_style,
        logical_start_byte: 0,
        source_node_id: None,
    }]
}
/// Returns true if a character has Unicode line breaking class BK (mandatory break)
/// or NL (next line). Per CSS Text 3 §5.1, these must be treated as forced line
/// breaks regardless of the white-space property value.
#[inline]
332395
fn is_bk_or_nl_class(c: char) -> bool {
332395
    matches!(c, '\u{000B}' | '\u{000C}' | '\u{0085}' | '\u{2028}' | '\u{2029}')
332395
}
/// Splits text at all forced break points: newlines (\n, \r\n, \r) and BK/NL class chars.
/// Used for white-space modes that preserve segment breaks (pre, pre-wrap, pre-line, break-spaces).
// +spec:white-space-processing:af4e3f - each newline/segment break in text is treated as a segment break, interpreted per white-space property
700
fn split_at_forced_breaks(text: &str) -> Vec<String> {
700
    let mut segments = Vec::new();
700
    let mut current = String::new();
700
    let mut chars = text.chars().peekable();
14140
    while let Some(c) = chars.next() {
13440
        if c == '\n' {
560
            segments.push(std::mem::take(&mut current));
12880
        } else if c == '\r' {
            segments.push(std::mem::take(&mut current));
            if chars.peek() == Some(&'\n') {
                chars.next();
            }
12880
        } else if is_bk_or_nl_class(c) {
            segments.push(std::mem::take(&mut current));
12880
        } else {
12880
            current.push(c);
12880
        }
    }
700
    segments.push(current);
700
    segments
700
}
/// Splits text only at BK/NL class characters (not \n which is collapsed in normal/nowrap).
/// Used for white-space: normal/nowrap where \n is collapsed to space but BK/NL chars
/// still produce forced breaks per CSS Text 3 §5.1.
31115
fn split_at_bk_nl_chars(text: &str) -> Vec<String> {
31115
    let mut segments = Vec::new();
31115
    let mut current = String::new();
319515
    for c in text.chars() {
319515
        if is_bk_or_nl_class(c) {
            segments.push(std::mem::take(&mut current));
319515
        } else {
319515
            current.push(c);
319515
        }
    }
31115
    segments.push(current);
31115
    segments
31115
}
/// Returns true if the character is East Asian (CJK) for the purposes of
/// segment break transformation rules (CSS Text Level 3, §4.1.3).
140
fn is_east_asian_wide(c: char) -> bool {
140
    let cp = c as u32;
    // CJK Unified Ideographs
140
    (0x4E00..=0x9FFF).contains(&cp)
140
    || (0x3400..=0x4DBF).contains(&cp)
140
    || (0x20000..=0x2A6DF).contains(&cp)
140
    || (0xF900..=0xFAFF).contains(&cp)
    // Hiragana
140
    || (0x3040..=0x309F).contains(&cp)
    // Katakana
140
    || (0x30A0..=0x30FF).contains(&cp)
140
    || (0x31F0..=0x31FF).contains(&cp)
    // CJK Radicals / Kangxi / Ideographic Description
140
    || (0x2E80..=0x2EFF).contains(&cp)
140
    || (0x2F00..=0x2FDF).contains(&cp)
140
    || (0x2FF0..=0x2FFF).contains(&cp)
    // CJK Symbols and Punctuation
140
    || (0x3000..=0x303F).contains(&cp)
140
    || (0x3200..=0x32FF).contains(&cp)
140
    || (0x3300..=0x33FF).contains(&cp)
    // Bopomofo
140
    || (0x3100..=0x312F).contains(&cp)
    // Hangul Syllables
140
    || (0xAC00..=0xD7AF).contains(&cp)
    // Fullwidth forms
140
    || (0xFF01..=0xFF60).contains(&cp)
140
    || (0xFFE0..=0xFFE6).contains(&cp)
140
}
// +spec:block-formatting-context:b78223 - fullwidth/wide chars treated as vertical script, halfwidth as horizontal per UAX#11
140
fn is_east_asian_fullwidth_or_wide(ch: char) -> bool {
140
    let cp = ch as u32;
    // Exclude Hangul
140
    if (0x1100..=0x11FF).contains(&cp)
140
        || (0x3130..=0x318F).contains(&cp)
140
        || (0xAC00..=0xD7AF).contains(&cp)
140
        || (0xA960..=0xA97F).contains(&cp)
140
        || (0xD7B0..=0xD7FF).contains(&cp)
    {
        return false;
140
    }
140
    is_east_asian_wide(ch)
140
        || (0xFF61..=0xFFDC).contains(&cp)
140
        || (0xFFE8..=0xFFEE).contains(&cp)
140
        || (0xA000..=0xA4CF).contains(&cp)
140
}
/// +spec:white-space-processing:159dbf - segment breaks converted to spaces (default transform)
/// +spec:white-space-processing:79891b - segment break transform: convert to space or remove
// +spec:white-space-processing:7e9529 - Segment break transformation rules (§4.1.3): collapse consecutive breaks, remove around ZWSP/CJK, else convert to space
/// Transforms segment breaks (newlines) in text according to CSS Text Level 3 §4.1.3.
/// - If adjacent to a zero-width space (U+200B), the segment break is removed.
/// - If both adjacent chars are East Asian F/W/H (not Hangul), removed entirely.
/// - Otherwise, converted to a single space.
31115
fn apply_segment_break_transform(text: &str) -> String {
31115
    let chars: Vec<char> = text.chars().collect();
31115
    let len = chars.len();
31115
    let mut result = String::with_capacity(text.len());
31115
    let mut i = 0;
349790
    while i < len {
318675
        let ch = chars[i];
318675
        if ch == '\n' || ch == '\r' {
350
            let break_end = if ch == '\r' && i + 1 < len && chars[i + 1] == '\n' {
                i + 2
            } else {
350
                i + 1
            };
            // +spec:white-space-processing:3c3680 - remove tabs/spaces around segment break before transform
            // §4.1.1: remove collapsible whitespace around segment breaks
490
            while result.ends_with(' ') || result.ends_with('\t') {
140
                result.pop();
140
            }
350
            let mut after_idx = break_end;
1190
            while after_idx < len && (chars[after_idx] == ' ' || chars[after_idx] == '\t') {
840
                after_idx += 1;
840
            }
350
            let char_before = result.chars().last();
350
            let char_after = if after_idx < len { Some(chars[after_idx]) } else { None };
            // Rule 1: adjacent to zero-width space → remove
350
            if char_before == Some('\u{200B}') || char_after == Some('\u{200B}') {
                // remove segment break
            }
            // Rule 2: both sides East Asian F/W/H (not Hangul) → remove
350
            else if let (Some(before), Some(after)) = (char_before, char_after) {
140
                if is_east_asian_fullwidth_or_wide(before) && is_east_asian_fullwidth_or_wide(after) {
                    // remove segment break
140
                } else {
140
                    result.push(' ');
140
                }
210
            } else {
210
                result.push(' ');
210
            }
350
            i = after_idx;
318325
        } else {
318325
            result.push(ch);
318325
            i += 1;
318325
        }
    }
31115
    result
31115
}
// ============================================================================
// WHITE-SPACE PROCESSING PIPELINE (CSS Text Level 3 §4)
// ============================================================================
//
// +spec:white-space-processing:b64e38 - parser may normalize/collapse whitespace before CSS; CSS cannot restore
// The white-space processing pipeline is organized into four phases per the
// CSS Text Level 3 specification:
//
//   Phase 1 (Collapse): Collapse whitespace sequences per §4.1.1
//   Phase 2 (Segment Break Transform): Transform segment breaks per §4.1.3
//   Phase 3 (Edge Trimming): Trim spaces at line start/end per §4.1.2
//   Phase 4 (Tab Resolution): Resolve tab stops per §4.2
//
// Each phase is a standalone function that transforms a string, allowing
// spec patches to modify individual phases without touching others.
/// Phase 1: Collapse consecutive whitespace to a single space.
/// CSS Text 3 §4.1.1 - applies to `normal`, `nowrap`, and `pre-line` modes.
pub fn ws_phase1_collapse(text: &str) -> String {
    let mut result = String::with_capacity(text.len());
    let mut prev_was_space = false;
    for ch in text.chars() {
        if ch == ' ' || ch == '\t' {
            if !prev_was_space {
                result.push(' ');
                prev_was_space = true;
            }
        } else {
            result.push(ch);
            prev_was_space = false;
        }
    }
    result
}
/// Phase 2: Transform segment breaks (newlines) per CSS Text 3 §4.1.3.
/// Delegates to `apply_segment_break_transform` for the actual transformation rules.
pub fn ws_phase2_segment_break_transform(text: &str) -> String {
    apply_segment_break_transform(text)
}
/// Phase 3: Trim leading/trailing collapsible whitespace at line boundaries.
/// CSS Text 3 §4.1.2 - this is a no-op during text collection; actual trimming
/// happens during line breaking when line start/end positions are known.
/// Provided as a pipeline slot for patches to hook into.
pub fn ws_phase3_trim_edges(text: &str) -> String {
    text.to_string()
}
/// Phase 4: Resolve tab characters to spaces based on tab-size.
/// CSS Text 3 §4.2 - for `normal`/`nowrap`, tabs are collapsed to spaces in Phase 1.
/// For `pre`/`pre-wrap`/`break-spaces`, tabs are emitted as `InlineContent::Tab`
/// and resolved during line layout. This phase is a no-op during text collection.
pub fn ws_phase4_resolve_tabs(text: &str) -> String {
    text.to_string()
}
/// Splits text content into InlineContent items based on white-space CSS property.
///
///
/// For `white-space: pre`, `pre-wrap`, and `pre-line`, newlines (`\n`) are treated as
/// forced line breaks per CSS Text Level 3 specification:
/// https://www.w3.org/TR/css-text-3/#white-space-property
///
/// Additionally, Unicode characters with BK or NL line breaking class (VT, FF, NEL, LS, PS)
/// are always treated as forced line breaks regardless of the white-space value.
///
/// This function:
/// 1. Checks the white-space property of the node (or its parent for text nodes)
/// 2. If `pre`, `pre-wrap`, or `pre-line`: splits text by `\n` and inserts `InlineContent::LineBreak`
/// 3. Otherwise: returns the text as a single `InlineContent::Text`
/// 4. In ALL modes: BK/NL class chars (VT, FF, NEL, LS, PS) produce forced breaks
///
/// Returns a Vec of InlineContent items that correctly represent line breaks.
// +spec:display-property:1389e3 - bidi control characters per UAX #9 for Unicode bidirectional algorithm
// +spec:display-property:aad99b - inline boxes can be split into fragments due to bidi text processing
// Bidi_Control property (UAX #9). These characters are ignored during white-space processing.
332955
fn is_bidi_control(c: char) -> bool {
332955
    matches!(c,
        '\u{200E}' | // LEFT-TO-RIGHT MARK
        '\u{200F}' | // RIGHT-TO-LEFT MARK
        '\u{202A}' | // LEFT-TO-RIGHT EMBEDDING
        '\u{202B}' | // RIGHT-TO-LEFT EMBEDDING
        '\u{202C}' | // POP DIRECTIONAL FORMATTING
        '\u{202D}' | // LEFT-TO-RIGHT OVERRIDE
        '\u{202E}' | // RIGHT-TO-LEFT OVERRIDE
        '\u{2066}' | // LEFT-TO-RIGHT ISOLATE
        '\u{2067}' | // RIGHT-TO-LEFT ISOLATE
        '\u{2068}' | // FIRST STRONG ISOLATE
        '\u{2069}' | // POP DIRECTIONAL ISOLATE
        '\u{061C}'   // ARABIC LETTER MARK
    )
332955
}
/// +spec:white-space-processing:1188f6 - only spaces, tabs, and segment breaks are document white space
/// Returns true if `c` is a CSS "document white space character" per CSS Text Level 3 §4.1.
/// Only spaces (U+0020), tabs (U+0009), and segment breaks (LF, CR, FF) qualify.
/// Other Unicode whitespace (e.g. U+00A0 non-breaking space) is NOT document white space.
#[inline]
382725
pub fn is_css_document_whitespace(c: char) -> bool {
382725
    matches!(c, ' ' | '\t' | '\n' | '\r' | '\x0C')
382725
}
// +spec:white-space-processing:efbece - white-space property controls collapsing/preserving of formatting characters for rendering
// +spec:writing-modes:b87688 - inlines laid out with bidi reordering and white-space wrapping
// +spec:writing-modes:cdd4f1 - white space trimming before bidi reordering preserves end-of-line spaces per UAX9 L1
// white space characters are processed prior to line breaking and bidi reordering
// +spec:inline-block:381c0c - white-space property: collapsing, wrapping, and forced breaks per mode
// +spec:display-property:8acfaa - Phase I white-space collapsing for each inline in an IFC, ignoring bidi controls
31815
pub fn split_text_for_whitespace(
31815
    styled_dom: &StyledDom,
31815
    dom_id: NodeId,
31815
    text: &str,
31815
    style: Arc<StyleProperties>,
31815
) -> Vec<InlineContent> {
    use crate::text3::cache::{BreakType, ClearType, InlineBreak};
    // (characters with the Bidi_Control property) as if they were not there"
    // Strip bidi control characters before white-space processing so they don't
    // interfere with collapsing (e.g. a bidi mark between two spaces).
    let text_owned;
332955
    let text: &str = if text.chars().any(|c| is_bidi_control(c)) {
        text_owned = text.chars().filter(|c| !is_bidi_control(*c)).collect::<String>();
        &text_owned
    } else {
31815
        text
    };
    // Get the white-space property - TEXT NODES inherit from parent!
    // We need to check the parent element's white-space, not the text node itself
31815
    let node_hierarchy = styled_dom.node_hierarchy.as_container();
31815
    let parent_id = node_hierarchy[dom_id].parent_id();
    // Try parent first, then fall back to the node itself
31815
    let white_space = if let Some(parent) = parent_id {
31815
        let styled_nodes = styled_dom.styled_nodes.as_container();
31815
        let parent_state = styled_nodes
31815
            .get(parent)
31815
            .map(|n| n.styled_node_state.clone())
31815
            .unwrap_or_default();
31815
        match get_white_space_property(styled_dom, parent, &parent_state) {
31815
            MultiValue::Exact(ws) => ws,
            _ => StyleWhiteSpace::Normal,
        }
    } else {
        StyleWhiteSpace::Normal
    };
31815
    let mut result = Vec::new();
    // +spec:white-space-processing:3a0f58 - HTML newlines normalized to U+000A, each treated as segment break
    // +spec:white-space-processing:6eb1a2 - CR (U+000D) not treated as segment break by HTML; handle if inserted via DOM
    // HTML parsers convert \r to \n during preprocessing, but \r can survive
    // via escape sequences (e.g. &#x0d;). Any remaining U+000D must be
    // treated identically to U+000A (line feed).
    let text_cr;
31815
    let text: &str = if text.contains('\r') {
        text_cr = text.replace("\r\n", "\n").replace('\r', "\n");
        &text_cr
    } else {
31815
        text
    };
    // +spec:white-space-processing:bd11da - white-space property: new lines, spaces/tabs, wrapping per value table
    // +spec:white-space-processing:b166c5 - segment breaks preserved as forced line feeds for pre/pre-wrap/break-spaces/pre-line
    // For `pre`, `pre-wrap`, `pre-line`, and `break-spaces`, newlines must be preserved as forced breaks
    // CSS Text Level 3: "Newlines in the source will be honored as forced line breaks."
31815
    match white_space {
        StyleWhiteSpace::Pre | StyleWhiteSpace::PreWrap | StyleWhiteSpace::BreakSpaces => {
            // Pre, pre-wrap, break-spaces: preserve whitespace and honor newlines
            // Split by newlines and BK/NL class chars, insert LineBreak between parts
            // Also handle tab characters (\t) by inserting InlineContent::Tab
560
            let segments = split_at_forced_breaks(text);
560
            let segment_count = segments.len();
560
            let mut content_index = 0;
910
            for (seg_idx, segment) in segments.into_iter().enumerate() {
                // Split the segment by tab characters and insert Tab elements
910
                let mut tab_parts = segment.split('\t').peekable();
1960
                while let Some(part) = tab_parts.next() {
1050
                    if !part.is_empty() {
910
                        result.push(InlineContent::Text(StyledRun {
910
                            text: part.to_string(),
910
                            style: Arc::clone(&style),
910
                            logical_start_byte: 0,
910
                            source_node_id: Some(dom_id),
910
                        }));
910
                    }
1050
                    if tab_parts.peek().is_some() {
140
                        result.push(InlineContent::Tab { style: Arc::clone(&style) });
910
                    }
                }
910
                if seg_idx + 1 < segment_count {
350
                    result.push(InlineContent::LineBreak(InlineBreak {
350
                        break_type: BreakType::Hard,
350
                        clear: ClearType::None,
350
                        content_index,
350
                    }));
350
                    content_index += 1;
560
                }
            }
        }
        StyleWhiteSpace::PreLine => {
            // Pre-line: collapse whitespace but honor newlines and BK/NL class chars
140
            let segments = split_at_forced_breaks(text);
140
            let segment_count = segments.len();
140
            let mut content_index = 0;
350
            for (seg_idx, segment) in segments.into_iter().enumerate() {
                // Collapse only CSS document white space within the line (not all Unicode whitespace)
350
                let collapsed: String = segment
2380
                    .split(|c: char| is_css_document_whitespace(c))
770
                    .filter(|s| !s.is_empty())
350
                    .collect::<Vec<_>>()
350
                    .join(" ");
350
                if !collapsed.is_empty() {
280
                    result.push(InlineContent::Text(StyledRun {
280
                        text: collapsed,
280
                        style: Arc::clone(&style),
280
                        logical_start_byte: 0,
280
                        source_node_id: Some(dom_id),
280
                    }));
280
                }
350
                if seg_idx + 1 < segment_count {
210
                    result.push(InlineContent::LineBreak(InlineBreak {
210
                        break_type: BreakType::Hard,
210
                        clear: ClearType::None,
210
                        content_index,
210
                    }));
210
                    content_index += 1;
210
                }
            }
        }
        StyleWhiteSpace::Normal | StyleWhiteSpace::Nowrap => {
            // +spec:white-space-processing:adbebb - Phase I collapsing for normal/nowrap modes
            // CSS Text Level 3, Section 4.1.1 - Phase I: Collapsing and Transformation
            // https://www.w3.org/TR/css-text-3/#white-space-phase-1
            //
            // For `white-space: normal` and `nowrap`:
            // 1. Segment breaks are transformed per §4.1.3
            // 2. Any sequence of consecutive spaces/tabs is collapsed to a single space
            // 3. Leading/trailing spaces at line boundaries are handled during line layout
            //
            // are forced breaks regardless of white-space value. Split on them first,
            // then collapse whitespace within each segment.
31115
            let segments = split_at_bk_nl_chars(text);
31115
            let segment_count = segments.len();
31115
            let mut content_index = 0;
31115
            for (seg_idx, segment) in segments.into_iter().enumerate() {
31115
                let after_segment_breaks = apply_segment_break_transform(&segment);
                // Collapse document white space within this segment (normal/nowrap rules)
31115
                let collapsed: String = after_segment_breaks
31115
                    .chars()
318535
                    .map(|c| if is_css_document_whitespace(c) { ' ' } else { c })
31115
                    .collect::<String>()
31115
                    .split(' ')
65765
                    .filter(|s| !s.is_empty())
31115
                    .collect::<Vec<_>>()
31115
                    .join(" ");
31115
                let final_text = if collapsed.is_empty() && !segment.is_empty() {
210
                    " ".to_string()
30905
                } else if !collapsed.is_empty() {
                    // Check if original had leading/trailing document whitespace
30905
                    let had_leading = segment.chars().next().map(|c| is_css_document_whitespace(c)).unwrap_or(false);
30905
                    let had_trailing = segment.chars().last().map(|c| is_css_document_whitespace(c)).unwrap_or(false);
30905
                    let mut r = String::new();
30905
                    if had_leading { r.push(' '); }
30905
                    r.push_str(&collapsed);
30905
                    if had_trailing && !had_leading { r.push(' '); }
30905
                    else if had_trailing && had_leading && collapsed.is_empty() { /* already have one space */ }
30905
                    else if had_trailing { r.push(' '); }
30905
                    r
                } else {
                    collapsed
                };
31115
                if !final_text.is_empty() {
31115
                    result.push(InlineContent::Text(StyledRun {
31115
                        text: final_text,
31115
                        style: Arc::clone(&style),
31115
                        logical_start_byte: 0,
31115
                        source_node_id: Some(dom_id),
31115
                    }));
31115
                }
                // Insert forced break between segments (for BK/NL chars)
31115
                if seg_idx + 1 < segment_count {
                    result.push(InlineContent::LineBreak(InlineBreak {
                        break_type: BreakType::Hard,
                        clear: ClearType::None,
                        content_index,
                    }));
                    content_index += 1;
31115
                }
            }
        }
    }
    // +spec:white-space-processing:5e3f70 - text-transform applied after Phase I collapsing, before Phase II trimming
    // This means full-width only transforms spaces (U+0020) to U+3000 IDEOGRAPHIC SPACE
    // within preserved white space, because non-preserved spaces were already collapsed in Phase I above.
31815
    let text_transform = style.text_transform;
31815
    if text_transform != crate::text3::cache::TextTransform::None {
        for item in result.iter_mut() {
            if let InlineContent::Text(run) = item {
                run.text = apply_text_transform(&run.text, text_transform);
            }
        }
31815
    }
31815
    result
31815
}
fn apply_text_transform(text: &str, transform: crate::text3::cache::TextTransform) -> String {
    use crate::text3::cache::TextTransform;
    match transform {
        TextTransform::None => text.to_string(),
        TextTransform::Uppercase => text.to_uppercase(),
        TextTransform::Lowercase => text.to_lowercase(),
        TextTransform::Capitalize => {
            let mut result = String::with_capacity(text.len());
            let mut prev_is_word_boundary = true;
            for c in text.chars() {
                if prev_is_word_boundary && c.is_alphabetic() {
                    for uc in c.to_uppercase() {
                        result.push(uc);
                    }
                    prev_is_word_boundary = false;
                } else {
                    result.push(c);
                    prev_is_word_boundary = c.is_whitespace() || c.is_ascii_punctuation();
                }
            }
            result
        }
        TextTransform::FullWidth => {
            // Full-width transforms ASCII characters to their full-width equivalents.
            // Spaces (U+0020) become U+3000 IDEOGRAPHIC SPACE — but only those that
            // survived Phase I collapsing (i.e. preserved white space).
            text.chars().map(|c| match c {
                ' ' => '\u{3000}',  // U+0020 SPACE -> U+3000 IDEOGRAPHIC SPACE
                '!' ..= '~' => {
                    // ASCII printable range U+0021..U+007E -> fullwidth U+FF01..U+FF5E
                    char::from_u32(c as u32 - 0x0021 + 0xFF01).unwrap_or(c)
                }
                _ => c,
            }).collect()
        }
    }
}
// ============================================================================
// INITIAL LETTER / DROP CAPS STUB
// ============================================================================
/// Computes the geometric exclusion area for an initial letter (drop cap).
///
/// CSS Inline Layout Module Level 3, section 3:
/// The `initial-letter` property specifies styling for dropped, raised, and sunken
/// initial letters. When set, the first glyph(s) of the first line are enlarged to
/// span multiple lines, with the remaining text wrapping around them.
///
// +spec:box-model:c93797 - initial-letter alignment points determined from contents (not border-box)
///
/// # Algorithm
///
/// 1. The letter box height spans `size` lines: `height = size * line_height`.
/// 2. The letter box width is estimated using a typical capital letter aspect ratio
///    (cap-height-to-advance-width ~0.7 for Latin text). A proper implementation
///    would measure the actual glyph, but this gives a reasonable default.
/// 3. The letter is positioned at the inline-start of the first line.
/// 4. The `sink` value determines how many lines the letter drops below the
///    first baseline. When `sink == size`, this is a classic drop cap.
///    When `sink < size`, the letter rises above the first line (raised cap).
/// 5. A small gap (4px default) is added between the letter box and adjacent text.
///
/// # Parameters
/// - `initial_letter_size`: The number of lines the initial letter should span (e.g., 3.0)
/// - `initial_letter_sink`: How many lines the letter sinks below the first line
/// - `content_box_width`: Available width in the content box (for clamping)
/// - `line_height`: The computed line height for the containing block
///
/// # Returns
/// A tuple of `(letter_width, letter_height)` representing the space reserved for
/// the initial letter exclusion, or `(0.0, 0.0)` if the parameters are invalid.
///
/// The caller should use these dimensions to create a float-like exclusion at the
/// start of the block container, causing subsequent lines to wrap around the letter.
// +spec:width-calculation:7f4f68 - initial-letter-wrap exclusion area (none behavior; first/grid require glyph outlines)
pub fn layout_initial_letter(
    initial_letter_size: f32,
    initial_letter_sink: u32,
    content_box_width: f32,
    line_height: f32,
) -> (f32, f32) {
    // Guard against degenerate values
    if initial_letter_size <= 0.0 || line_height <= 0.0 || content_box_width <= 0.0 {
        return (0.0, 0.0);
    }
    // +spec:overflow:dd0679 - auto-sized initial letter content box fits exactly to content; alignment props do not apply
    // +spec:width-calculation:170742 - atomic initial letters with auto block size use inline initial letter sizing
    // CSS Inline Level 3 section 3.3: The initial letter box height spans `size` lines.
    let letter_height = initial_letter_size * line_height;
    // Estimate the letter width using a typical Latin capital letter aspect ratio.
    // The advance width of a capital letter is approximately 0.7x the cap height.
    // This is a heuristic; a full implementation would measure the actual glyph(s).
    const CAP_WIDTH_RATIO: f32 = 0.7;
    let letter_width_raw = letter_height * CAP_WIDTH_RATIO;
    // Add a small gap between the letter box and the adjacent inline content.
    // CSS Inline Level 3 section 3.5: browsers typically add ~4px padding.
    const LETTER_GAP: f32 = 4.0;
    let letter_width = (letter_width_raw + LETTER_GAP).min(content_box_width);
    // +spec:containing-block:67fd99 - block-axis positioning: size >= sink shifts by (sink-1)*line_height toward block-end
    // The actual exclusion height accounts for the sink value.
    // sink == size means the letter is fully dropped (classic drop cap).
    // sink < size means part of the letter rises above the first line (raised cap).
    // The exclusion area height is always `sink * line_height` since that's how
    // many lines of subsequent text need to wrap around the letter.
    let exclusion_height = (initial_letter_sink as f32) * line_height;
    // Use the larger of exclusion_height and letter_height as the actual
    // vertical space consumed. For raised caps (sink < size), the letter
    // extends above the first line but the exclusion only covers sink lines.
    // For sunken caps (sink >= size), the exclusion covers the full letter height.
    let effective_height = exclusion_height.max(letter_height);
    (letter_width, effective_height)
}