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
use base64::Engine as _;

use crate as spindrift;
use crate::{BookmarkState, Css, CssColor, Document, Html, PdfOptions, RenderOptions};

trait PdfBytesForTest {
    fn write_pdf_bytes(&self, options: &PdfOptions) -> crate::Result<Vec<u8>>;
}

impl PdfBytesForTest for Document {
    fn write_pdf_bytes(&self, options: &PdfOptions) -> crate::Result<Vec<u8>> {
        let mut bytes = Vec::new();
        self.write_pdf(&mut bytes, options)?;
        Ok(bytes)
    }
}

trait HtmlPdfBytesForTest {
    async fn write_pdf_bytes(
        &self,
        render_options: &RenderOptions,
        pdf_options: &PdfOptions,
    ) -> crate::Result<Vec<u8>>;
}

impl HtmlPdfBytesForTest for Html {
    async fn write_pdf_bytes(
        &self,
        render_options: &RenderOptions,
        pdf_options: &PdfOptions,
    ) -> crate::Result<Vec<u8>> {
        let mut bytes = Vec::new();
        self.write_pdf(&mut bytes, render_options, pdf_options)
            .await?;
        Ok(bytes)
    }
}

const ROW_SUBGRID_AUTO_FILL_WPT: &str = include_str!(
    "../../tests/fixtures/wpt/css/css-grid/grid-lanes/subgrid/grid-subgridded-to-grid-lanes/track-sizing/row-subgrid-auto-fill-002.html"
);
const ROW_SUBGRID_AUTO_FILL_WPT_REFERENCE: &str = include_str!(
    "../../tests/fixtures/wpt/css/css-grid/grid-lanes/subgrid/grid-subgridded-to-grid-lanes/track-sizing/row-subgrid-auto-fill-002-ref.html"
);
const CUSTOM_HIGHLIGHT_PAINTING_IFRAME_REFERENCE: &str = include_str!(
    "../../tests/fixtures/wpt/css/css-highlight-api/painting/custom-highlight-painting-iframe-001-ref.html"
);
const TABLE_WITH_FLOAT_PAINT_WPT: &str =
    include_str!("../../tests/fixtures/wpt/css/css-flexbox/table-with-float-paint.html");
const FILLED_GREEN_SQUARE_REFERENCE: &str =
    include_str!("../../tests/fixtures/wpt/css/reference/ref-filled-green-100px-square-only.html");

/// Local Grid Lanes regression derived from upstream
/// `row-subgrid-auto-fill-002`. Keeping both inputs in the repository makes
/// the reference comparison independent of a developer's WPT checkout.
#[tokio::test]
async fn grid_lanes_row_subgrid_auto_fill_matches_local_reference() {
    let actual = Html::from_string(ROW_SUBGRID_AUTO_FILL_WPT)
        .render(&RenderOptions::default())
        .await
        .unwrap();
    let reference = Html::from_string(ROW_SUBGRID_AUTO_FILL_WPT_REFERENCE)
        .render(&RenderOptions::default())
        .await
        .unwrap();
    assert_eq!(actual.pages.len(), reference.pages.len());
    assert_eq!(actual.pages[0].rects(), reference.pages[0].rects());
    assert_eq!(
        actual.pages[0].rounded_rects(),
        reference.pages[0].rounded_rects()
    );
}

/// WPT regression: `css/css-flexbox/table-with-float-paint.html`.
///
/// Flex's final normal-flow line-box measurement replays the already-sized
/// table item. That replay must be detached from the committed document so a
/// floated table descendant paints only during the final item replay.
#[tokio::test]
async fn flex_table_float_paint_matches_local_reference() {
    let actual = Html::from_string(TABLE_WITH_FLOAT_PAINT_WPT)
        .render(&RenderOptions::default())
        .await
        .unwrap();
    let reference = Html::from_string(FILLED_GREEN_SQUARE_REFERENCE)
        .render(&RenderOptions::default())
        .await
        .unwrap();

    assert_eq!(actual.pages.len(), reference.pages.len());
    assert_eq!(actual.pages[0].rects(), reference.pages[0].rects());
    assert_eq!(
        actual.pages[0].rounded_rects(),
        reference.pages[0].rounded_rects()
    );
}

/// WPT reference: css/css-highlight-api/painting/
/// custom-highlight-painting-iframe-001-ref.html.
///
/// The reference itself uses `srcdoc` rather than a fetched `src`, so verify
/// the nested document's paint fragment is composed into the iframe viewport.
#[tokio::test]
async fn iframe_srcdoc_paints_embedded_document() {
    let document = Html::from_string(CUSTOM_HIGHLIGHT_PAINTING_IFRAME_REFERENCE)
        .render(&RenderOptions::default())
        .await
        .unwrap();
    let page = &document.pages[0];
    let cyan = CssColor::new(0, 255, 255);
    let blue = CssColor::new(0, 0, 255);

    assert!(
        page.rects().iter().any(|rect| rect.fill == Some(cyan)),
        "srcdoc background was not painted: {:?}",
        page.rects()
    );
    assert!(
        page.lines()
            .iter()
            .any(|line| line.text == "abc" && line.color == blue),
        "srcdoc text was not painted in blue: {:?}",
        page.lines()
    );
}

/// HTML's legacy body margins are resolved inside the child document's
/// cascade, including the immediate iframe's fallback attributes. Compare the
/// two embedding forms to their equivalent child CSS rather than asserting
/// implementation-specific paint coordinates.
/// <https://html.spec.whatwg.org/multipage/rendering.html#the-page>
#[tokio::test]
async fn iframe_legacy_body_margins_match_equivalent_child_css() {
    for (name, source_attribute) in [
        (
            "srcdoc",
            "srcdoc=\"<!doctype html><body>frame margins</body>\"".to_string(),
        ),
        (
            "data URL",
            "src=\"data:text/html,<!doctype html><body>frame margins</body>\"".to_string(),
        ),
    ] {
        let actual = Html::from_string(format!(
            "<style>@page{{size:420px 240px;margin:0}}body{{margin:0}}</style>\
             <iframe width=300 height=160 marginwidth=100 marginheight=60 {source_attribute}></iframe>"
        ))
        .render(&RenderOptions::default())
        .await
        .unwrap();
        let reference = Html::from_string(
            "<style>@page{size:420px 240px;margin:0}body{margin:0}</style>\
             <iframe width=300 height=160 srcdoc=\"<!doctype html><body style='margin-left:100px;margin-right:100px;margin-top:60px;margin-bottom:60px'>frame margins</body>\"></iframe>"
        .to_string())
        .render(&RenderOptions::default())
        .await
        .unwrap();

        let actual_line = actual.pages[0]
            .lines()
            .iter()
            .find(|line| line.text == "frame")
            .expect("iframe child text should paint");
        let reference_line = reference.pages[0]
            .lines()
            .iter()
            .find(|line| line.text == "frame")
            .expect("reference iframe child text should paint");
        assert_eq!(
            (actual_line.x(), actual_line.y()),
            (reference_line.x(), reference_line.y()),
            "{name} iframe margins differed from equivalent child CSS"
        );
    }
}

fn line_font<'a>(
    document: &'a spindrift::Document,
    line: &crate::document::paint::text::RenderedLine,
) -> &'a crate::document::DocumentFont {
    let font_id = line
        .font_id
        .expect("rendered line should have a resolved font");
    &document.fonts[font_id]
}

fn font_label(font: &crate::document::DocumentFont) -> String {
    format!("{} {}", font.family, font.post_script_name).to_ascii_lowercase()
}

fn font_label_contains_any(font: &crate::document::DocumentFont, needles: &[&str]) -> bool {
    let label = font_label(font);
    needles.iter().any(|needle| label.contains(needle))
}

fn line_font_contains_any(
    document: &spindrift::Document,
    line: &crate::document::paint::text::RenderedLine,
    needles: &[&str],
) -> bool {
    font_label_contains_any(line_font(document, line), needles)
}

fn line_font_is_italic(
    document: &spindrift::Document,
    line: &crate::document::paint::text::RenderedLine,
) -> bool {
    let font = line_font(document, line);
    font.italic_angle != 0 || font_label_contains_any(font, &["italic", "oblique"])
}

fn line_font_is_bold(
    document: &spindrift::Document,
    line: &crate::document::paint::text::RenderedLine,
) -> bool {
    font_label_contains_any(line_font(document, line), &["bold", "black", "heavy"])
}

fn line_run_font_is_italic(
    document: &spindrift::Document,
    line: &crate::document::paint::text::RenderedLine,
    text: &str,
) -> bool {
    line.runs.iter().any(|run| {
        run.text.as_ref() == text
            && run
                .font_id
                .and_then(|font_id| document.fonts.get(font_id))
                .is_some_and(|font| {
                    font.italic_angle != 0 || font_label_contains_any(font, &["italic", "oblique"])
                })
    })
}

fn line_run_font_is_bold(
    document: &spindrift::Document,
    line: &crate::document::paint::text::RenderedLine,
    text: &str,
) -> bool {
    line.runs.iter().any(|run| {
        run.text.as_ref() == text
            && run
                .font_id
                .and_then(|font_id| document.fonts.get(font_id))
                .is_some_and(|font| font_label_contains_any(font, &["bold", "black", "heavy"]))
    })
}

fn line_font_is_monospace(
    document: &spindrift::Document,
    line: &crate::document::paint::text::RenderedLine,
) -> bool {
    let font = line_font(document, line);
    let Ok(face) = ttf_parser::Face::parse(&font.data, font.face_index) else {
        return false;
    };
    let Some(narrow) = face
        .glyph_index('i')
        .and_then(|glyph| face.glyph_hor_advance(glyph))
    else {
        return false;
    };
    let Some(wide) = face
        .glyph_index('W')
        .and_then(|glyph| face.glyph_hor_advance(glyph))
    else {
        return false;
    };
    narrow == wide
}

fn horizontal_table_border_widths(document: &spindrift::Document) -> Vec<f32> {
    document.pages[0]
        .rects()
        .iter()
        .filter(|rect| {
            rect.fill == Some(CssColor::BLACK) && rect.height() <= 1.01 && rect.width() > 1.01
        })
        .step_by(2)
        .map(|rect| rect.width())
        .collect()
}

fn vertical_table_border_heights(document: &spindrift::Document) -> Vec<f32> {
    document.pages[0]
        .rects()
        .iter()
        .filter(|rect| {
            rect.fill == Some(CssColor::BLACK) && rect.width() <= 1.01 && rect.height() > 1.01
        })
        .map(|rect| rect.height())
        .collect()
}

fn first_rect_paint_operation_index(page: &spindrift::Page, color: CssColor) -> usize {
    page.paint_operations()
        .iter()
        .position(|operation| {
            matches!(
                operation,
                crate::document::paint::page::PaintOperation::Rect(index)
                    if page.rects().get(*index).is_some_and(|rect| rect.fill == Some(color))
            )
        })
        .expect("rect with expected fill should be present in paint operations")
}

fn final_rect_fill_at(page: &spindrift::Page, x: f32, y: f32) -> Option<CssColor> {
    page.paint_operations()
        .iter()
        .filter_map(|operation| {
            let crate::document::paint::page::PaintOperation::Rect(index) = operation else {
                return None;
            };
            let rect = page.rects().get(*index)?;
            (x >= rect.x()
                && x <= rect.x() + rect.width()
                && y >= rect.y()
                && y <= rect.y() + rect.height())
            .then_some(rect.fill)
            .flatten()
        })
        .next_back()
}

/// Return PDF syntax together with decoded textual streams for assertions.
///
/// PDF streams may use `/FlateDecode` (ISO 32000-1:2008, 7.4.4). Spindrift emits
/// direct numeric `/Length` entries, so the test harness can decode its own
/// streams without coupling operator assertions to uncompressed output.
fn pdf_searchable_text(pdf: &[u8]) -> String {
    let mut text = String::new();
    let mut search_start = 0;
    let mut copied_until = 0;

    while let Some(stream_marker) = find_pdf_bytes_from(pdf, b"stream\n", search_start) {
        let stream_start = stream_marker + b"stream\n".len();
        let Some((dictionary_start, stream_end)) = pdf_stream_bounds(pdf, stream_start) else {
            search_start = stream_start;
            continue;
        };
        text.push_str(&String::from_utf8_lossy(&pdf[copied_until..stream_start]));

        let dictionary = &pdf[dictionary_start..stream_marker];
        let stream = &pdf[stream_start..stream_end];
        let decoded = if dictionary
            .windows(b"/Filter /FlateDecode".len())
            .any(|window| window == b"/Filter /FlateDecode")
        {
            miniz_oxide::inflate::decompress_to_vec_zlib(stream).ok()
        } else {
            Some(stream.to_vec())
        };
        if let Some(decoded) = decoded
            && let Ok(decoded) = String::from_utf8(decoded)
        {
            text.push_str(&decoded);
        }

        copied_until = stream_end;
        search_start = stream_end.saturating_add(b"\nendstream".len());
    }
    text.push_str(&String::from_utf8_lossy(&pdf[copied_until..]));
    text
}

/// Return whether a tagged sRGB PDF/A fill uses the requested components.
///
/// PDF/A content uses the page's ICCBased sRGB resource (`cs`) rather than an
/// uncalibrated device-RGB `rg` operator (ISO 32000-2:2020, 8.6.8).
fn has_srgb_fill(rendered: &str, components: &str) -> bool {
    rendered.contains(&format!("/CSsRGB cs\n{components} scn"))
}

fn srgb_fill_count(rendered: &str, components: &str) -> usize {
    rendered
        .matches(&format!("/CSsRGB cs\n{components} scn"))
        .count()
}

fn has_srgb_stroke(rendered: &str, components: &str) -> bool {
    rendered.contains(&format!("/CSsRGB CS\n{components} SCN"))
}

fn pdf_stream_bounds(pdf: &[u8], stream_start: usize) -> Option<(usize, usize)> {
    let length_marker = b"/Length ";
    let dictionary_start = pdf[..stream_start]
        .windows(length_marker.len())
        .rposition(|window| window == length_marker)?;
    let length_start = dictionary_start + length_marker.len();
    let length_end = pdf[length_start..]
        .iter()
        .position(|byte| !byte.is_ascii_digit())?
        + length_start;
    let length = std::str::from_utf8(&pdf[length_start..length_end])
        .ok()?
        .parse::<usize>()
        .ok()?;
    let stream_end = stream_start.checked_add(length)?;
    (stream_end <= pdf.len()).then_some((dictionary_start, stream_end))
}

fn find_pdf_bytes_from(haystack: &[u8], needle: &[u8], start: usize) -> Option<usize> {
    haystack
        .get(start..)?
        .windows(needle.len())
        .position(|window| window == needle)
        .map(|position| start + position)
}

fn rendered_line_advance(line: &crate::document::paint::text::RenderedLine) -> f32 {
    line.runs
        .iter()
        .flat_map(|run| run.glyphs.as_deref().unwrap_or_default())
        .map(|glyph| glyph.x_advance)
        .sum()
}

fn rendered_line_baseline_y_for_top(
    document: &spindrift::Document,
    line: &crate::document::paint::text::RenderedLine,
    top_y: f32,
) -> f32 {
    let adjustment = line
        .font_id
        .and_then(|font_id| document.fonts.get(font_id))
        .map(|font| {
            let ascent =
                font.layout_metrics.ascender as f32 * line.font_size / font.units_per_em as f32;
            line.font_size - ascent
        })
        .unwrap_or(0.0);
    top_y - line.font_size + adjustment
}

fn rendered_line_baseline_top(
    document: &spindrift::Document,
    line: &crate::document::paint::text::RenderedLine,
) -> f32 {
    let adjustment = line
        .font_id
        .and_then(|font_id| document.fonts.get(font_id))
        .map(|font| {
            let ascent =
                font.layout_metrics.ascender as f32 * line.font_size / font.units_per_em as f32;
            line.font_size - ascent
        })
        .unwrap_or(0.0);
    line.y() + line.font_size - adjustment
}

fn assert_line_baseline_at_top(
    document: &spindrift::Document,
    line: &crate::document::paint::text::RenderedLine,
    top_y: f32,
) {
    let expected = rendered_line_baseline_y_for_top(document, line, top_y);
    assert!(
        (line.y() - expected).abs() < 0.01,
        "expected {:?} baseline y {:.4} for top {:.4}, got {:.4}",
        line.text,
        expected,
        top_y,
        line.y()
    );
}

#[tokio::test]
async fn min_width_fit_content_length_clamps_block_width() {
    let document = Html::from_string(
        "<!DOCTYPE html>\
         <style>\
         @page { size: 200px 160px; margin: 0 }\
         body { margin: 0 }\
         #reference-overlapped-red {\
           position: absolute;\
           left: 0;\
           top: 0;\
           background-color: red;\
           width: 100px;\
           height: 100px;\
           z-index: -1;\
         }\
         </style>\
         <div id=\"reference-overlapped-red\"></div>\
         <div style=\"width: 10px; min-width: fit-content(100px); height: 100px; background: green;\">\
           <div style=\"display: inline-block; width: 60px;\"></div>\
           <div style=\"display: inline-block; width: 60px;\"></div>\
         </div>",
    )
    .render(&RenderOptions::default())
    .await
    .unwrap();

    let page = &document.pages[0];
    let green = CssColor::new(0, 128, 0);
    let green_rect = page
        .rects()
        .iter()
        .find(|rect| rect.fill == Some(green))
        .unwrap_or_else(|| panic!("expected green block background: {:?}", page.rects()));

    assert!(
        (green_rect.width() - 75.0).abs() < 0.01 && (green_rect.height() - 75.0).abs() < 0.01,
        "fit-content min-width should produce a 100px square: {green_rect:?}",
    );
    for (x, y) in [
        (green_rect.x() + 1.0, green_rect.y() + 1.0),
        (
            green_rect.x() + green_rect.width() / 2.0,
            green_rect.y() + green_rect.height() / 2.0,
        ),
        (
            green_rect.x() + green_rect.width() - 1.0,
            green_rect.y() + green_rect.height() / 2.0,
        ),
    ] {
        assert_eq!(
            final_rect_fill_at(page, x, y),
            Some(green),
            "green should cover the red backing square at ({x}, {y})",
        );
    }
}

#[tokio::test]
async fn min_height_max_content_clamps_zero_block_height() {
    let document = Html::from_string(
        "<!DOCTYPE html>\
         <style>@page { size: 160px 160px; margin: 0 } body { margin: 0 }</style>\
         <div style=\"background: green; width: 100px; min-height: max-content; height: 0px;\">\
           <div style=\"height: 100px;\"></div>\
         </div>",
    )
    .render(&RenderOptions::default())
    .await
    .unwrap();

    let green = document.pages[0]
        .rects()
        .iter()
        .find(|rect| rect.fill == Some(CssColor::new(0, 128, 0)))
        .unwrap_or_else(|| panic!("expected green background: {:?}", document.pages[0].rects()));

    assert!(
        (green.width() - 75.0).abs() < 0.01 && (green.height() - 75.0).abs() < 0.01,
        "min-height:max-content should grow the zero-height block to its child height: {green:?}",
    );
}

#[tokio::test]
async fn height_max_content_uses_block_child_height() {
    let document = Html::from_string(
        "<!DOCTYPE html>\
         <style>@page { size: 160px 160px; margin: 0 } body { margin: 0 }</style>\
         <div style=\"background: green; width: 100px; height: max-content;\">\
           <div style=\"height: 80px;\"></div>\
         </div>",
    )
    .render(&RenderOptions::default())
    .await
    .unwrap();

    let green = document.pages[0]
        .rects()
        .iter()
        .find(|rect| rect.fill == Some(CssColor::new(0, 128, 0)))
        .unwrap_or_else(|| panic!("expected green background: {:?}", document.pages[0].rects()));

    assert!(
        (green.width() - 75.0).abs() < 0.01 && (green.height() - 60.0).abs() < 0.01,
        "height:max-content should use the block child's content height: {green:?}",
    );
}

#[tokio::test]
async fn max_height_max_content_clamps_definite_block_height() {
    let document = Html::from_string(
        "<!DOCTYPE html>\
         <style>@page { size: 160px 160px; margin: 0 } body { margin: 0 }</style>\
         <div style=\"background: green; width: 100px; height: 150px; max-height: max-content;\">\
           <div style=\"height: 80px;\"></div>\
         </div>",
    )
    .render(&RenderOptions::default())
    .await
    .unwrap();

    let green = document.pages[0]
        .rects()
        .iter()
        .find(|rect| rect.fill == Some(CssColor::new(0, 128, 0)))
        .unwrap_or_else(|| panic!("expected green background: {:?}", document.pages[0].rects()));

    assert!(
        (green.width() - 75.0).abs() < 0.01 && (green.height() - 60.0).abs() < 0.01,
        "max-height:max-content should clamp the definite height to the child height: {green:?}",
    );
}

#[tokio::test]
async fn padded_bordered_definite_block_prebreaks_as_one_border_box() {
    let document = Html::from_string(
        "<!DOCTYPE html>\
         <style>@page { size: 100px 100px; margin: 0 } body { margin: 0 }</style>\
         <div style=\"height: 30px\"></div>\
         <div style=\"height: 50px; padding: 10px; border: 10px solid red; background: green\"></div>",
    )
    .render(&RenderOptions::default())
    .await
    .unwrap();

    assert_eq!(document.pages.len(), 2);
    assert!(
        document.pages[1]
            .rects()
            .iter()
            .any(|rect| rect.fill == Some(CssColor::new(0, 128, 0)))
    );
}

#[tokio::test]
async fn intrinsic_min_height_keeps_cyclic_percentage_child_height_auto() {
    let document = Html::from_string(
        "<!DOCTYPE html>\
         <style>@page { size: 220px 220px; margin: 0 } body { margin: 0 }</style>\
         <div style=\"background: green; width: 100px; height: 100px; min-height: max-content;\">\
           <div style=\"height: 50%;\">\
             <div style=\"height: 150px;\"></div>\
           </div>\
         </div>",
    )
    .render(&RenderOptions::default())
    .await
    .unwrap();

    let green = document.pages[0]
        .rects()
        .iter()
        .find(|rect| rect.fill == Some(CssColor::new(0, 128, 0)))
        .unwrap_or_else(|| panic!("expected green background: {:?}", document.pages[0].rects()));

    assert!(
        (green.width() - 75.0).abs() < 0.01 && (green.height() - 112.5).abs() < 0.01,
        "intrinsic min-height should measure the percentage child as auto: {green:?}",
    );
}

#[tokio::test]
async fn overflow_scroll_preserves_parent_paint_order() {
    let document = Html::from_string(
        "<!DOCTYPE html>\
         <title>Overflow:scroll paint order</title>\
         <style>\
         @page { size: 200px 200px; margin: 0 }\
         body { margin: 0 }\
         #scroller {\
           background: red;\
           padding: 20px;\
           box-sizing: border-box;\
           width: 100px;\
           height: 100px;\
           overflow: scroll;\
         }\
         #negative-margin {\
           width: 100px;\
           height: 100px;\
           background: green;\
           margin-top: -100px;\
         }\
         #foreground1, #foreground2 {\
           display: inline-block;\
           width: 50px;\
           height: 50px;\
         }\
         #foreground1 { background: blue }\
         #foreground2 { background: magenta }\
         </style>\
         <div id=\"scroller\">\
           <div style=\"height: 200px; background: yellow\">\
             <div id=\"foreground1\"></div>\
           </div>\
         </div>\
         <div id=\"negative-margin\">\
           <div id=\"foreground2\"></div>\
         </div>",
    )
    .render(&RenderOptions::default())
    .await
    .unwrap();

    let page = &document.pages[0];
    let red = first_rect_paint_operation_index(page, CssColor::new(255, 0, 0));
    let yellow = first_rect_paint_operation_index(page, CssColor::new(255, 255, 0));
    let green = first_rect_paint_operation_index(page, CssColor::new(0, 128, 0));
    let blue = first_rect_paint_operation_index(page, CssColor::new(0, 0, 255));
    let magenta = first_rect_paint_operation_index(page, CssColor::new(255, 0, 255));

    assert!(red < yellow, "scroller background should paint first");
    assert!(yellow < green, "later block background should cover yellow");
    assert!(
        green < blue,
        "earlier inline foreground should paint over green"
    );
    assert!(blue < magenta, "later inline foreground should paint last");

    let blue_rect = page
        .rects()
        .iter()
        .find(|rect| rect.fill == Some(CssColor::new(0, 0, 255)))
        .expect("blue foreground rect should render");
    assert_eq!(
        final_rect_fill_at(
            page,
            blue_rect.x() + blue_rect.width() - 1.0,
            blue_rect.y() + blue_rect.height() / 2.0,
        ),
        Some(CssColor::new(0, 0, 255))
    );
}

#[path = "smoke/css_assets.rs"]
mod css_assets;
#[path = "smoke/document.rs"]
mod document;
#[path = "smoke/flex_conformance.rs"]
mod flex_conformance;
#[path = "smoke/fonts.rs"]
mod fonts;
#[path = "smoke/grid_lanes_alignment.rs"]
mod grid_lanes_alignment;
#[path = "smoke/grid_lanes_flow_tolerance.rs"]
mod grid_lanes_flow_tolerance;
#[path = "smoke/grid_lanes_subgrid.rs"]
mod grid_lanes_subgrid;
#[path = "smoke/grid_zoom.rs"]
mod grid_zoom;
#[path = "smoke/layout_inline_flex.rs"]
mod layout_inline_flex;
#[path = "smoke/media_links_style.rs"]
mod media_links_style;
#[path = "smoke/multicol_column_height.rs"]
mod multicol_column_height;
#[path = "smoke/multicol_zoom.rs"]
mod multicol_zoom;
#[path = "smoke/normal_flow_zoom.rs"]
mod normal_flow_zoom;
#[path = "smoke/positioned_zoom.rs"]
mod positioned_zoom;
#[path = "smoke/positioning_fragmentation.rs"]
mod positioning_fragmentation;
#[path = "smoke/selectors_flex_columns.rs"]
mod selectors_flex_columns;
#[path = "smoke/table_zoom.rs"]
mod table_zoom;
#[path = "smoke/tables.rs"]
mod tables;
#[path = "smoke/text.rs"]
mod text;