spindrift 0.1.0

A HTML/CSS to PDF renderer, targeting spec compliance and efficiency
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
use super::*;

pub(in crate::layout) fn has_styled_inline_descendant_with_font_metrics(
    element: &Element,
    parent_style: &ComputedStyle,
    stylesheets: &Stylesheets<'_>,
    ancestors: &[ElementSignature],
    font_system: &mut FontSystem,
) -> bool {
    let mut resolver = DomStyleResolver::with_font_system(font_system);
    has_styled_inline_descendant_with_inline_flow_scope(
        element,
        parent_style,
        stylesheets,
        ancestors,
        false,
        &mut resolver,
    )
}

fn has_styled_inline_descendant_with_inline_flow_scope(
    element: &Element,
    parent_style: &ComputedStyle,
    stylesheets: &Stylesheets<'_>,
    ancestors: &[ElementSignature],
    inside_inline_flow: bool,
    resolver: &mut DomStyleResolver<'_>,
) -> bool {
    let has_non_phantom_direct_text = element.children.iter().any(|child| {
        matches!(
            &child.kind,
            NodeKind::Text(text) if inline_text_has_non_phantom_content(text, parent_style)
        )
    });
    let sibling_tags = element_sibling_signature_list(element);
    let mut element_index = 0usize;
    element.children.iter().any(|child| {
        let NodeKind::Element(child_element) = &child.kind else {
            return false;
        };
        let signature =
            ElementSignature::from_sibling_snapshot(element_index, sibling_tags.clone())
                .expect("source child must have a cached sibling signature");
        element_index += 1;
        let child_style = resolver.style_for_element(
            child_element,
            signature.clone(),
            stylesheets,
            Some(parent_style),
            ancestors,
        );
        // A suppressed descendant still changes the rendered text stream:
        // flattening the parent's DOM text would otherwise resurrect fallback
        // text from `display: none` / unboxed HTML elements. Route this
        // through the item collector, which observes the descendant's used
        // display value before adding its content.
        // <https://www.w3.org/TR/css-display-3/#box-generation>
        if child_style.display.is_none() {
            return true;
        }
        // Absolute and fixed descendants are blockified for their own layout,
        // but they remain at this inline source boundary for static-position
        // selection. The plain-text fast path would discard that boundary and
        // never materialize their positioned paint.
        // <https://www.w3.org/TR/css-position-3/#static-position>
        if matches!(child_style.position, Position::Absolute | Position::Fixed) {
            return true;
        }
        // A float is blockified for its own layout, but stays at this inline
        // source position as a zero-width marker. The scalar-text shortcut
        // would otherwise flatten its surrounding text and lose the marker.
        // <https://www.w3.org/TR/CSS22/visuren.html#floats>
        if child_style.float != Float::None {
            // A direct block float stays in the parent's block-flow child
            // traversal unless direct text forms an inline source run beside
            // it. Once an inline flow scope owns the source, its float marker
            // must likewise be collected with the surrounding text.
            return inside_inline_flow || has_non_phantom_direct_text;
        }
        if child_style.display.is_block_level() {
            return false;
        }
        // Ruby is a non-atomic inline-level formatting context whose base
        // content participates in the parent line. Its annotations cannot be
        // represented by this scalar-text shortcut, so force the inline-item
        // collector even when its inherited typography matches the parent.
        // <https://drafts.csswg.org/css-ruby-1/#ruby-layout>
        if child_style.display.is_ruby() {
            return true;
        }
        // Link annotations belong to the inline fragment sequence. The
        // scalar text fast path can paint the glyphs but has no source range
        // on which to record the hyperlink rectangle.
        // <https://www.w3.org/TR/css-ui-4/#cursor>
        if child_element.attrs.contains_key("href") {
            return true;
        }
        // Atomic inline-level boxes contribute their own dimensions and
        // baseline even when their descendants share the parent's font
        // metrics. Route them through inline-item collection rather than
        // collapsing the fragment to a plain text run.
        // <https://drafts.csswg.org/css-display-3/#atomic-inline>
        if (child_style.display.is_atomic_inline() || is_replaced_element(child_element))
            && child_style.display.is_inline_level()
            || (child_style.display.is_table() && child_style.display.is_inline_or_run_in_level())
        {
            return true;
        }
        inline_style_affects_line(parent_style, &child_style) || {
            let mut child_ancestors = ancestors.to_vec();
            child_ancestors.push(signature);
            has_styled_inline_descendant_with_inline_flow_scope(
                child_element,
                &child_style,
                stylesheets,
                &child_ancestors,
                inside_inline_flow
                    || (child_style.display.is_inline_level() && child_style.display.is_flow()),
                resolver,
            )
        }
    })
}

pub(in crate::layout) fn inline_style_affects_line(
    parent: &ComputedStyle,
    child: &ComputedStyle,
) -> bool {
    child.before_style.is_some()
        || child.after_style.is_some()
        // A scalar text run has no lexical boundary at which to emit the UAX
        // #9 controls required by an inline CSS bidi scope. In particular,
        // `unicode-bidi: isolate` must be externally represented as one
        // neutral object instead of flattening its text into the paragraph.
        // <https://drafts.csswg.org/css-writing-modes-4/#unicode-bidi>
        || inline_bidi_scope_affects_line_ordering(child)
        || child.float != Float::None
        || child.color != parent.color
        || child.font_family != parent.font_family
        || child.font_size != parent.font_size
        || child.font_style != parent.font_style
        || child.font_weight != parent.font_weight
        || child.font_width != parent.font_width
        || child.font_palette != parent.font_palette
        || child.line_height != parent.line_height
        || child.text_decoration != parent.text_decoration
        || child.text_transform != parent.text_transform
        || child.word_space_transform != parent.word_space_transform
        // A preserved tab belongs to the inline style that owns its source
        // character. Flattening a `tab-size` change into the block's scalar
        // text fast path would lose that value before line-level tab-stop
        // resolution can select its period.
        // <https://www.w3.org/TR/css-text-3/#tab-size-property>
        || child.tab_size != parent.tab_size
        || child.vertical_align != parent.vertical_align
        || child.white_space != parent.white_space
        || inline_break_policy_differs(parent, child)
        // A non-inherited inline decoration needs the item collector even
        // when its typography is identical to the parent.  The plain-text
        // fast path has no inline box fragments on which to paint these
        // backgrounds/borders or relative visual offsets.
        // <https://www.w3.org/TR/CSS22/visuren.html#inline-boxes>
        // <https://www.w3.org/TR/css-position-3/#relative-positioning>
        || child.background.background_color.is_potentially_visible()
        || child.background.background_image.is_image()
        || child.background.background_layers.iter().any(|layer| layer.image.is_image())
        || used_border_width(child) > layout_pt(0.0)
        || child.margin != parent.margin
        || child.padding != parent.padding
        || child.box_values.margin != parent.box_values.margin
        || child.box_values.padding != parent.box_values.padding
        || matches!(child.position, Position::Relative | Position::Sticky)
        // Opacity establishes an atomic compositing group. Retain the
        // lexical inline scope so its text, decorations, and descendants are
        // painted into that group instead of being flattened into the parent
        // text run.
        // <https://www.w3.org/TR/css-color-4/#transparency>
        || child.opacity.value() < 1.0
}

/// Whether flattening a descendant inline box into its parent text run would
/// change the available break opportunities or the marker painted at one.
///
/// CSS Text defines hyphenation as a language-sensitive soft wrap opportunity
/// and requires inline element boundaries to be ignored when determining word
/// boundaries. The scalar text fast path therefore remains valid only when it
/// retains the same break policy for every descendant text segment.
/// <https://www.w3.org/TR/css-text-3/#hyphenation>
/// <https://www.w3.org/TR/css-text-3/#line-break-details>
fn inline_break_policy_differs(parent: &ComputedStyle, child: &ComputedStyle) -> bool {
    child.language != parent.language
        || child.hyphens != parent.hyphens
        || child.hyphenate_character != parent.hyphenate_character
        || child.hyphenate_limit_chars != parent.hyphenate_limit_chars
        || child.word_break != parent.word_break
        || child.overflow_wrap != parent.overflow_wrap
        || child.line_break != parent.line_break
        || child.text_wrap_mode != parent.text_wrap_mode
        || child.text_wrap_style != parent.text_wrap_style
}

pub(in crate::layout) fn has_direct_inline_replaced_child(element: &Element) -> bool {
    element.children.iter().any(|child| {
        matches!(&child.kind, NodeKind::Element(child_element) if is_replaced_element(child_element))
    })
}

pub(in crate::layout) fn has_direct_flow_child_with_font_metrics(
    element: &Element,
    parent_style: &ComputedStyle,
    stylesheets: &Stylesheets<'_>,
    font_system: &mut FontSystem,
) -> bool {
    let mut resolver = DomStyleResolver::with_font_system(font_system);
    has_direct_flow_child_with_resolver(element, parent_style, stylesheets, &mut resolver)
}

/// Whether a direct DOM source contains floats but no parent inline-line
/// content or normal-flow child.
///
/// A float's descendants belong to its own formatting context. Therefore a
/// direct floated element is terminal for this parent-source classifier, just
/// as a floated formatting box is terminal after normalization.
/// <https://www.w3.org/TR/CSS22/visuren.html#floats>
pub(in crate::layout) fn has_direct_float_only_source_with_font_metrics(
    element: &Element,
    parent_style: &ComputedStyle,
    stylesheets: &Stylesheets<'_>,
    font_system: &mut FontSystem,
) -> bool {
    let mut resolver = DomStyleResolver::with_font_system(font_system);
    let sibling_tags = element_sibling_signature_list(element);
    let mut element_index = 0usize;
    let mut has_float = false;

    for child in &element.children {
        match &child.kind {
            NodeKind::Text(text) => {
                if inline_text_has_non_phantom_content(text, parent_style) {
                    return false;
                }
            }
            NodeKind::Element(child_element) => {
                let signature = ElementSignature::with_sibling_list(
                    child_element.tag.clone(),
                    child_element.attrs.clone(),
                    element_index,
                    sibling_tags.clone(),
                );
                element_index += 1;
                let style = resolver.structural_style_for_element(
                    child_element,
                    signature,
                    stylesheets,
                    Some(parent_style),
                    &[],
                );
                if style.display.is_none()
                    || matches!(style.position, Position::Absolute | Position::Fixed)
                {
                    continue;
                }
                match style.float.layout_role() {
                    FloatLayoutRole::Exclusion => has_float = true,
                    FloatLayoutRole::None | FloatLayoutRole::Footnote => return false,
                }
            }
        }
    }

    has_float
}

pub(in crate::layout) fn has_direct_flow_child_with_resolver(
    element: &Element,
    parent_style: &ComputedStyle,
    stylesheets: &Stylesheets<'_>,
    resolver: &mut DomStyleResolver<'_>,
) -> bool {
    let sibling_tags = element_sibling_signature_list(element);
    let mut element_index = 0usize;
    element.children.iter().any(|child| {
        let NodeKind::Element(child_element) = &child.kind else {
            return false;
        };
        let signature = ElementSignature::with_sibling_list(
            child_element.tag.clone(),
            child_element.attrs.clone(),
            element_index,
            sibling_tags.clone(),
        );
        element_index += 1;
        let style = resolver.structural_style_for_element(
            child_element,
            signature,
            stylesheets,
            Some(parent_style),
            &[],
        );
        if is_replaced_element(child_element) && style.display.is_inline_level() {
            return false;
        }
        // HTML table semantics select table layout, but do not override the
        // computed outer display role. In particular, `inline-table` remains
        // an inline-level atomic child of its block container.
        // <https://drafts.csswg.org/css-display-3/#outer-role>
        // <https://drafts.csswg.org/css-tables-3/#table-root>
        style.display.is_block_level()
    })
}

pub(in crate::layout) fn has_ordered_mixed_flow_content_with_font_metrics(
    element: &Element,
    parent_style: &ComputedStyle,
    stylesheets: &Stylesheets<'_>,
    ancestors: &[ElementSignature],
    font_system: &mut FontSystem,
) -> bool {
    let mut resolver = DomStyleResolver::with_font_system(font_system);
    has_ordered_mixed_flow_content_with_resolver(
        element,
        parent_style,
        stylesheets,
        ancestors,
        &mut resolver,
    )
}

/// Returns whether a direct suppressed subtree requires source-aware child
/// collection even though it contributes neither inline nor block flow.
///
/// This is deliberately separate from mixed-flow classification: CSS Display
/// removes `display: none` subtrees from box generation, but a traversal that
/// falls back to flattened DOM text would resurrect their text. Keeping the
/// traversal requirement distinct lets flow classification ignore the hidden
/// content while still selecting a collector that observes its computed
/// display.
/// <https://www.w3.org/TR/css-display-3/#box-generation>
pub(in crate::layout) fn requires_suppressed_dom_source_collection_with_font_metrics(
    element: &Element,
    parent_style: &ComputedStyle,
    stylesheets: &Stylesheets<'_>,
    ancestors: &[ElementSignature],
    font_system: &mut FontSystem,
) -> bool {
    let sibling_tags = element_sibling_signature_list(element);
    let mut element_index = 0usize;
    let mut resolver = DomStyleResolver::with_font_system(font_system);

    element.children.iter().any(|child| {
        let NodeKind::Element(child_element) = &child.kind else {
            return false;
        };
        let signature = ElementSignature::with_sibling_list(
            child_element.tag.clone(),
            child_element.attrs.clone(),
            element_index,
            sibling_tags.clone(),
        );
        element_index += 1;
        let child_style = resolver.style_for_element(
            child_element,
            signature,
            stylesheets,
            Some(parent_style),
            ancestors,
        );
        child_style.display.is_none() && !inline_text(child_element).is_empty()
    })
}

/// Returns whether direct-DOM block layout must materialize its child
/// formatting tree to perform CSS block-in-inline splitting.
///
/// A normal-flow block descendant of an inline flow wrapper is not an inline
/// item. CSS 2.2 splits the inline wrapper around it, then places the block
/// between anonymous block boxes in the enclosing block flow. The DOM inline
/// collector intentionally does not lay out ordinary block descendants, so
/// this structural boundary must use the normalized formatting-tree path.
///
/// <https://www.w3.org/TR/CSS22/visuren.html#anonymous-block-level>
pub(in crate::layout) fn has_block_in_inline_split_boundary_with_font_metrics(
    element: &Element,
    parent_style: &ComputedStyle,
    stylesheets: &Stylesheets<'_>,
    ancestors: &[ElementSignature],
    font_system: &mut FontSystem,
) -> bool {
    let mut resolver = DomStyleResolver::with_font_system(font_system);
    has_block_in_inline_split_boundary_with_resolver(
        element,
        parent_style,
        stylesheets,
        ancestors,
        false,
        &mut resolver,
    )
}

/// Returns whether a block's inline source contains a ruby formatting
/// context. Ruby layout has its own anonymous box generation and pairing
/// phase, so its subtree cannot use the scalar DOM-text fast path.
///
/// <https://drafts.csswg.org/css-ruby-1/#anon-gen-ruby>
/// <https://drafts.csswg.org/css-ruby-1/#ruby-layout>
pub(in crate::layout) fn has_ruby_formatting_descendant_with_font_metrics(
    element: &Element,
    parent_style: &ComputedStyle,
    stylesheets: &Stylesheets<'_>,
    ancestors: &[ElementSignature],
    font_system: &mut FontSystem,
    cached_descendants: &mut HashMap<ElementId, bool>,
) -> bool {
    let mut resolver = DomStyleResolver::with_font_system(font_system);
    has_ruby_formatting_descendant_with_resolver(
        element,
        parent_style,
        stylesheets,
        ancestors,
        &mut resolver,
        cached_descendants,
    )
}

/// Return whether a descendant table-internal box needs CSS Tables anonymous
/// wrapper construction before normal block layout can dispatch it.
///
/// A `table-row`, `table-cell`, and the other table-internal display types do
/// not independently establish a table formatting context. When encountered
/// by the direct DOM traversal, they would otherwise be treated as ordinary
/// block/inline content and their table fixup—including anonymous cell block
/// container normalization—would be skipped.
/// <https://drafts.csswg.org/css-tables-3/#fixup-algorithm>
pub(in crate::layout) fn has_unwrapped_table_internal_descendant_with_font_metrics(
    element: &Element,
    parent_style: &ComputedStyle,
    stylesheets: &Stylesheets<'_>,
    ancestors: &[ElementSignature],
    font_system: &mut FontSystem,
) -> bool {
    let mut resolver = DomStyleResolver::with_font_system(font_system);
    let mut ancestor_stack = ancestors.to_vec();
    has_unwrapped_table_internal_descendant_with_resolver(
        element,
        parent_style,
        stylesheets,
        &mut ancestor_stack,
        &mut resolver,
    )
}

/// Return whether direct child normalization must resolve a CSS `run-in`
/// sequence before block layout chooses its child traversal.
///
/// Run-in placement depends on the following in-flow sibling, so the direct
/// DOM walker cannot decide it one child at a time. It must first use the
/// block-container's normalized formatting tree.
/// <https://drafts.csswg.org/css-display-3/#run-in-layout>
pub(in crate::layout) fn has_direct_run_in_child_with_font_metrics(
    element: &Element,
    parent_style: &ComputedStyle,
    stylesheets: &Stylesheets<'_>,
    ancestors: &[ElementSignature],
    font_system: &mut FontSystem,
) -> bool {
    let sibling_tags = element_sibling_signature_list(element);
    let mut resolver = DomStyleResolver::with_font_system(font_system);
    let mut element_index = 0usize;
    element.children.iter().any(|child| {
        let NodeKind::Element(child_element) = &child.kind else {
            return false;
        };
        let signature = ElementSignature::with_sibling_list(
            child_element.tag.clone(),
            child_element.attrs.clone(),
            element_index,
            sibling_tags.clone(),
        );
        element_index += 1;
        resolver
            .structural_style_for_element(
                child_element,
                signature,
                stylesheets,
                Some(parent_style),
                ancestors,
            )
            .display
            .is_run_in()
    })
}

fn has_unwrapped_table_internal_descendant_with_resolver(
    element: &Element,
    parent_style: &ComputedStyle,
    stylesheets: &Stylesheets<'_>,
    ancestors: &mut Vec<ElementSignature>,
    resolver: &mut DomStyleResolver<'_>,
) -> bool {
    let sibling_tags = element_sibling_signature_list(element);
    let mut element_index = 0usize;
    for child in &element.children {
        let NodeKind::Element(child_element) = &child.kind else {
            continue;
        };
        let signature =
            ElementSignature::from_sibling_snapshot(element_index, sibling_tags.clone())
                .expect("source child must have a cached sibling signature");
        element_index += 1;
        let child_style = resolver.structural_style_for_element(
            child_element,
            signature.clone(),
            stylesheets,
            Some(parent_style),
            ancestors,
        );
        if child_style.display.is_none() {
            continue;
        }
        if is_table_internal_display(child_style.display) {
            return true;
        }
        // A proper table root owns its descendants' table fixup. Requiring a
        // parent structural rebuild for it would only bypass the direct table
        // layout path without adding information.
        if child_style.display.is_table() {
            continue;
        }
        ancestors.push(signature);
        let has_unwrapped_descendant = has_unwrapped_table_internal_descendant_with_resolver(
            child_element,
            &child_style,
            stylesheets,
            ancestors,
            resolver,
        );
        let popped = ancestors.pop();
        debug_assert!(
            popped.is_some(),
            "recursive table probe must pop its pushed ancestor"
        );
        if has_unwrapped_descendant {
            return true;
        }
    }
    false
}

fn is_table_internal_display(display: Display) -> bool {
    matches!(
        display.inner,
        DisplayInner::TableCaption
            | DisplayInner::TableColumnGroup
            | DisplayInner::TableColumn
            | DisplayInner::TableHeaderGroup
            | DisplayInner::TableRowGroup
            | DisplayInner::TableFooterGroup
            | DisplayInner::TableRow
            | DisplayInner::TableCell
    )
}

fn has_block_in_inline_split_boundary_with_resolver(
    element: &Element,
    parent_style: &ComputedStyle,
    stylesheets: &Stylesheets<'_>,
    ancestors: &[ElementSignature],
    inside_inline_flow: bool,
    resolver: &mut DomStyleResolver<'_>,
) -> bool {
    let sibling_tags = element_sibling_signature_list(element);
    let mut element_index = 0usize;

    for child in &element.children {
        let NodeKind::Element(child_element) = &child.kind else {
            continue;
        };
        let signature = ElementSignature::with_sibling_list(
            child_element.tag.clone(),
            child_element.attrs.clone(),
            element_index,
            sibling_tags.clone(),
        );
        element_index += 1;
        let child_style = resolver.structural_style_for_element(
            child_element,
            signature.clone(),
            stylesheets,
            Some(parent_style),
            ancestors,
        );
        if child_style.display.is_none() {
            continue;
        }
        if inside_inline_flow && is_normal_block_flow_child(child_element, &child_style) {
            return true;
        }
        // Atomic inline boxes establish their own formatting context and
        // therefore do not take part in their parent's block-in-inline
        // transformation. Display-contents contributes no box, so it
        // preserves an enclosing inline-flow scope for its descendants.
        // Ruby layout-internal boxes establish a separate formatting model,
        // rather than an ordinary inline flow.  They nevertheless need the
        // same structural-tree boundary here: a direct in-flow block child is
        // inlinified by CSS Ruby before CSS Display's block-in-inline split
        // can inspect the enclosing tree.
        // <https://drafts.csswg.org/css-ruby-1/#anon-gen-inlinize>
        let continues_inline_flow = child_style.display.is_contents()
            || ((child_style.display.is_inline_level() && child_style.display.is_flow())
                || child_style.display.is_ruby()
                || child_style.display.is_ruby_internal())
                && child_style.float == Float::None
                && matches!(
                    child_style.position,
                    Position::Static | Position::Relative | Position::Running(_)
                );
        if continues_inline_flow {
            let mut child_ancestors = ancestors.to_vec();
            child_ancestors.push(signature);
            if has_block_in_inline_split_boundary_with_resolver(
                child_element,
                &child_style,
                stylesheets,
                &child_ancestors,
                true,
                resolver,
            ) {
                return true;
            }
        }
    }

    false
}

fn has_ruby_formatting_descendant_with_resolver(
    element: &Element,
    parent_style: &ComputedStyle,
    stylesheets: &Stylesheets<'_>,
    ancestors: &[ElementSignature],
    resolver: &mut DomStyleResolver<'_>,
    cached_descendants: &mut HashMap<ElementId, bool>,
) -> bool {
    if let Some(&contains_ruby) = cached_descendants.get(&element.id) {
        return contains_ruby;
    }
    let sibling_tags = element_sibling_signature_list(element);
    let mut element_index = 0usize;
    for child in &element.children {
        let NodeKind::Element(child_element) = &child.kind else {
            continue;
        };
        let signature = ElementSignature::with_sibling_list(
            child_element.tag.clone(),
            child_element.attrs.clone(),
            element_index,
            sibling_tags.clone(),
        );
        element_index += 1;
        let child_style = resolver.structural_style_for_element(
            child_element,
            signature.clone(),
            stylesheets,
            Some(parent_style),
            ancestors,
        );
        if child_style.display.is_none() {
            continue;
        }
        if child_style.display.is_ruby() {
            cached_descendants.insert(element.id, true);
            return true;
        }
        // Ruby's anonymous-box construction affects the inline formatting
        // context that contains it. A descendant block, float, or atomic
        // inline establishes its own relevant formatting context and checks
        // its own source when it is laid out; walking through it here would
        // make every ancestor repeatedly cascade an unrelated subtree.
        // `display: contents` and ordinary inline flow preserve the current
        // inline formatting context, while ruby-internal boxes remain part of
        // the ruby structure that owns it.
        // <https://drafts.csswg.org/css-display-3/#block-in-inline>
        // <https://drafts.csswg.org/css-ruby-1/#anon-gen-ruby>
        let continues_inline_flow = child_style.display.is_contents()
            || ((child_style.display.is_inline_level() && child_style.display.is_flow())
                || child_style.display.is_ruby_internal())
                && child_style.float == Float::None
                && matches!(
                    child_style.position,
                    Position::Static | Position::Relative | Position::Running(_)
                );
        if !continues_inline_flow {
            continue;
        }
        let mut child_ancestors = ancestors.to_vec();
        child_ancestors.push(signature);
        if has_ruby_formatting_descendant_with_resolver(
            child_element,
            &child_style,
            stylesheets,
            &child_ancestors,
            resolver,
            cached_descendants,
        ) {
            cached_descendants.insert(element.id, true);
            return true;
        }
    }
    cached_descendants.insert(element.id, false);
    false
}

/// Whether a block needs the ordered mixed inline/block child traversal.
///
/// Absolutely and fixed positioned descendants do not establish an
/// auto-height parent's fragmentainer-local flow end. A block-origin
/// positioned sibling only makes the sequence source-sensitive when it lies
/// between an earlier in-flow child and a later CSS float: its static position
/// is selected after the earlier child, before that float. Route that precise
/// sequence through the ordered traversal so the generic inline collector
/// cannot descend into the later float before the block has committed its
/// cursor.
///
/// <https://www.w3.org/TR/css-position-3/#absolute-positioning>
/// <https://www.w3.org/TR/css-break-3/#fragmentation-model>
pub(in crate::layout) fn has_ordered_mixed_flow_content_with_resolver(
    element: &Element,
    parent_style: &ComputedStyle,
    stylesheets: &Stylesheets<'_>,
    ancestors: &[ElementSignature],
    resolver: &mut DomStyleResolver<'_>,
) -> bool {
    if suppresses_ordered_mixed_flow_detection(element) {
        return false;
    }

    let sibling_tags = element_sibling_signature_list(element);
    let mut element_index = 0usize;
    let mut has_inline = false;
    let mut has_flow = false;
    let mut has_positioned_static_boundary = false;
    let mut has_block_static_boundary_after_flow = false;
    let mut has_later_float_after_static_boundary = false;

    for child in &element.children {
        match &child.kind {
            NodeKind::Text(text) => {
                if !normalize_inline_text(text).is_empty() {
                    has_inline = true;
                }
            }
            NodeKind::Element(child_element) => {
                let signature = ElementSignature::with_sibling_list(
                    child_element.tag.clone(),
                    child_element.attrs.clone(),
                    element_index,
                    sibling_tags.clone(),
                );
                element_index += 1;
                let child_style = resolver.style_for_element(
                    child_element,
                    signature.clone(),
                    stylesheets,
                    Some(parent_style),
                    ancestors,
                );
                if child_style.display.is_none() {
                    continue;
                }
                if child_style.float.layout_role() == FloatLayoutRole::Footnote {
                    // A footnote contributes its generated inline call even
                    // when it is the parent's only visible child. Route the
                    // source through ordered inline collection so the call is
                    // committed while the detached body stays out of the
                    // principal block traversal.
                    // <https://www.w3.org/TR/css-gcpm-3/#footnote-calls>
                    return true;
                }
                if matches!(child_style.position, Position::Absolute | Position::Fixed) {
                    // Out-of-flow boxes retain source order only for static
                    // positioning. They do not contribute an in-flow
                    // endpoint to an otherwise block-only auto-height parent.
                    // An inline-origin source nevertheless needs the ordered
                    // traversal at a preceding or following block boundary:
                    // its hypothetical inline line is the input to its
                    // static-position rectangle, even though the box itself
                    // has been blockified for layout.
                    // <https://drafts.csswg.org/css-position-3/#static-position>
                    let is_block_static_boundary = child_style.display.is_block_level();
                    has_positioned_static_boundary |= is_block_static_boundary;
                    has_block_static_boundary_after_flow |= is_block_static_boundary && has_flow;
                    has_inline |= child_style.display.is_inline_level();
                    continue;
                }
                // A float is out of normal flow. Its used display type is
                // blockified, but that does not make it an in-flow block
                // boundary for this source-order classifier. A direct float
                // is handled by the ordinary child traversal, while a float
                // inside an inline source run is collected as an inline
                // marker by that run.
                // <https://www.w3.org/TR/CSS22/visuren.html#floats>
                let is_float_source = child_style.float.layout_role() == FloatLayoutRole::Exclusion;
                let is_normal_flow_child = is_normal_block_flow_child(child_element, &child_style)
                    // HTML table structure still needs source-order traversal
                    // around block siblings, but its computed outer display
                    // decides whether the table itself is dispatched as block
                    // flow or collected as an atomic inline.
                    // <https://drafts.csswg.org/css-display-3/#box-generation>
                    || (is_html_table_element(child_element)
                        && child_style.display.is_block_level())
                    || (is_replaced_element(child_element)
                        && child_style.display.is_block_level());
                if is_float_source || is_normal_flow_child {
                    has_later_float_after_static_boundary |= has_block_static_boundary_after_flow
                        && matches!(
                            child_style.float,
                            Float::Left | Float::Right | Float::InlineStart | Float::InlineEnd
                        );
                    has_flow |= is_normal_flow_child;
                } else if child_style.display.is_contents() {
                    let mut child_ancestors = ancestors.to_vec();
                    child_ancestors.push(signature);
                    if display_contents_has_inline_flow_contribution_with_resolver(
                        child_element,
                        &child_style,
                        stylesheets,
                        &child_ancestors,
                        resolver,
                    ) {
                        has_inline = true;
                    }
                } else if child_style.display.is_inline_level()
                    || is_line_break_element(child_element)
                    || !inline_text(child_element).is_empty()
                {
                    has_inline = true;
                }
            }
        }

        if (has_inline && (has_flow || has_positioned_static_boundary))
            || has_later_float_after_static_boundary
        {
            return true;
        }
    }

    false
}

/// Return whether a `display: contents` subtree contributes inline source to
/// its parent's formatting context.
///
/// A `display: contents` element has no principal box, so its in-flow
/// descendants and tree-abiding generated pseudo-elements retain their source
/// position in the parent's mixed block/inline sequence.  Looking only at DOM
/// text loses generated `::before`/`::after` content and causes the generic
/// parent-inline collector to replay that content before preceding block
/// siblings.
///
/// <https://drafts.csswg.org/css-display-3/#valdef-display-contents>
/// <https://drafts.csswg.org/css-pseudo-4/#treelike>
fn display_contents_has_inline_flow_contribution_with_resolver(
    element: &Element,
    parent_style: &ComputedStyle,
    stylesheets: &Stylesheets<'_>,
    ancestors: &[ElementSignature],
    resolver: &mut DomStyleResolver<'_>,
) -> bool {
    let sibling_tags = element_sibling_signature_list(element);
    let mut element_index = 0usize;

    for child in &element.children {
        match &child.kind {
            NodeKind::Text(text) => {
                if !normalize_inline_text(text).is_empty() {
                    return true;
                }
            }
            NodeKind::Element(child_element) => {
                let signature = ElementSignature::with_sibling_list(
                    child_element.tag.clone(),
                    child_element.attrs.clone(),
                    element_index,
                    sibling_tags.clone(),
                );
                element_index += 1;
                let child_style = resolver.style_for_element(
                    child_element,
                    signature.clone(),
                    stylesheets,
                    Some(parent_style),
                    ancestors,
                );
                if child_style.display.is_none() {
                    continue;
                }

                let preserves_parent_inline_context = child_style.display.is_contents()
                    || (child_style.display.is_inline_level()
                        && child_style.float == Float::None
                        && matches!(
                            child_style.position,
                            Position::Static | Position::Relative | Position::Running(_)
                        ));
                if !preserves_parent_inline_context {
                    continue;
                }

                let has_generated_inline_content = child_style
                    .before_style
                    .as_deref()
                    .is_some_and(generated_content_has_non_phantom_inline_content)
                    || child_style
                        .after_style
                        .as_deref()
                        .is_some_and(generated_content_has_non_phantom_inline_content);
                if has_generated_inline_content || child_style.display.is_inline_level() {
                    return true;
                }

                debug_assert!(child_style.display.is_contents());
                let mut child_ancestors = ancestors.to_vec();
                child_ancestors.push(signature);
                if display_contents_has_inline_flow_contribution_with_resolver(
                    child_element,
                    &child_style,
                    stylesheets,
                    &child_ancestors,
                    resolver,
                ) {
                    return true;
                }
            }
        }
    }

    false
}

#[cfg(test)]
mod tests;