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
use super::*;

/// Returns whether a block's own start and end margins are adjoining.
///
/// CSS 2.2 defines the margins of a box with no border, padding, min-height,
/// fixed height, line box, or in-flow non-self-collapsing children as
/// adjoining. Such a child keeps its parent's block-start collapsed margin set
/// open, which matters for `margin-trim: block-start`.
///
/// <https://www.w3.org/TR/CSS22/box.html#collapsing-margins>
/// <https://drafts.csswg.org/css-box-4/#margin-trim>
pub(in crate::layout) fn is_self_collapsing_block_box(
    element: &Element,
    style: &ComputedStyle,
    child_boxes: &[box_tree::FormattingBox<'_>],
    overflow_context: DocumentCanvasResolution,
) -> bool {
    // Generated content participates in the generated pseudo-element's box
    // just like an inline child, even though it is collected from `content`
    // during inline layout rather than stored in this formatting-box slice.
    // It therefore prevents the pseudo-element from being self-collapsing.
    // <https://drafts.csswg.org/css-pseudo-4/#generated-content>
    let has_line_box_content = has_direct_inline_content_box(child_boxes)
        || has_atomic_inline_formatting_box(child_boxes)
        || generated_content_has_non_phantom_inline_content(style)
        || super::dom_backed::style_has_non_phantom_generated_pseudo_content(style)
        || super::dom_backed::style_has_in_flow_marker_line(style);
    // Clearance changes the placement of an otherwise empty box without
    // creating an own block-size. Its margins therefore remain adjoining for
    // self-collapsing classification; the block-flow cursor independently
    // retains clearance for following in-flow content.
    // <https://www.w3.org/TR/CSS22/box.html#collapsing-margins>
    // <https://www.w3.org/TR/CSS22/visuren.html#flow-control>
    is_collapsible_block_child(element, style)
        && can_collapse_own_block_margins(
            element,
            style,
            used_border_widths(style),
            has_line_box_content,
            overflow_context.used_overflow(element, style),
        )
        && !(style.display.establishes_block_formatting_context()
            && child_boxes.iter().any(|child| {
                formatting_box_is_in_normal_flow(child)
                    && !formatting_box_can_only_create_phantom_line_boxes(child)
            }))
        && child_boxes
            .iter()
            .all(|child| formatting_box_keeps_self_collapsing_parent(child, overflow_context))
}

pub(in crate::layout) fn self_collapsing_block_margin_set_for_box(
    style: &ComputedStyle,
    descendant_start_margin: Option<AdjoiningMarginSet>,
) -> AdjoiningMarginSet {
    let mut set = AdjoiningMarginSet::from_margin(layout_pt(style.margin.top));
    if let Some(descendant) = descendant_start_margin {
        set.merge(descendant);
    }
    set.include(layout_pt(style.margin.bottom));
    set
}

pub(in crate::layout) fn formatting_box_keeps_self_collapsing_parent(
    box_: &box_tree::FormattingBox<'_>,
    overflow_context: DocumentCanvasResolution,
) -> bool {
    if box_tree::is_out_of_flow_box(box_) || box_tree::is_floated_box(box_) {
        return true;
    }
    if formatting_box_can_only_create_phantom_line_boxes(box_) {
        return true;
    }
    // CSS 2.2 block-in-inline splitting materializes this transparent
    // context solely to retain the originating inline box's painting and
    // positioning state. Its block child still participates directly in the
    // enclosing block formatting context, including adjoining margin sets.
    // <https://www.w3.org/TR/CSS22/visuren.html#anonymous-block-level>
    // <https://www.w3.org/TR/CSS22/box.html#collapsing-margins>
    if let box_tree::FormattingBox::InlineSplitBlockContext(context) = box_ {
        return context
            .core
            .children
            .iter()
            .all(|child| formatting_box_keeps_self_collapsing_parent(child, overflow_context));
    }
    if let box_tree::FormattingBox::AnonymousBlock(box_) = box_ {
        return box_
            .children
            .iter()
            .all(|child| formatting_box_keeps_self_collapsing_parent(child, overflow_context));
    }
    let Some((element, _, style, children)) = box_.element_parts() else {
        return false;
    };
    is_normal_block_flow_child(element, style)
        && is_self_collapsing_block_box(element, style, children, overflow_context)
}

pub(in crate::layout) fn collapsible_first_child_start_margin_from_boxes(
    child_boxes: &[box_tree::FormattingBox<'_>],
    parent: &Element,
    parent_style: &ComputedStyle,
    overflow_context: DocumentCanvasResolution,
) -> Option<AdjoiningMarginSet> {
    let mut has_preceding_css_float = false;
    for child_box in child_boxes {
        // An inline split context is transparent to the parent block's
        // margin-collapse chain. Recurse into its generated block segment
        // instead of treating the originating inline wrapper as a separator.
        // <https://www.w3.org/TR/CSS22/visuren.html#anonymous-block-level>
        // <https://www.w3.org/TR/CSS22/box.html#collapsing-margins>
        if let box_tree::FormattingBox::InlineSplitBlockContext(context) = child_box {
            if let Some(margin) = collapsible_first_child_start_margin_from_boxes(
                &context.core.children,
                parent,
                parent_style,
                overflow_context,
            ) {
                return Some(margin);
            }
            continue;
        }
        let Some((child_element, _, child_style, child_children)) = child_box.element_parts()
        else {
            if matches!(
                child_box,
                box_tree::FormattingBox::Inline(_) | box_tree::FormattingBox::Text(_)
            ) && !formatting_box_can_only_create_phantom_line_boxes(child_box)
            {
                return None;
            }
            continue;
        };
        // Out-of-flow descendants do not participate in the parent's
        // adjoining-margin chain. In particular, the document-canvas
        // exception below must not make a floated first child look like the
        // first in-flow child and collapse its margin through the body.
        // <https://www.w3.org/TR/CSS22/box.html#collapsing-margins>
        if child_style.float != Float::None {
            has_preceding_css_float = true;
            continue;
        }
        if matches!(child_style.position, Position::Absolute | Position::Fixed) {
            continue;
        }
        let is_flow_child = is_normal_block_flow_child(child_element, child_style)
            || overflow_context.is_document_canvas_flow_element(parent)
            || is_replaced_element(child_element);
        if !is_flow_child {
            if !inline_text(child_element).is_empty() {
                return None;
            }
            continue;
        }
        // A cleared first in-flow child after an adjoining float needs the
        // parent-start `clear:none` hypothesis rather than a pre-collapsed
        // used start margin. Other cleared descendants retain the ordinary
        // margin-collapse probe; block layout resolves whether they actually
        // introduce clearance in their containing float context.
        // <https://www.w3.org/TR/CSS22/visuren.html#flow-control>
        if child_style.clear != Clear::None && has_preceding_css_float {
            return None;
        }
        if parent_style.margin_trim.block_start {
            return Some(AdjoiningMarginSet::from_margin(layout_pt(0.0)));
        }
        return Some(collapsible_start_margin_for_box(
            child_element,
            child_style,
            child_children,
            overflow_context,
        ));
    }
    None
}

/// Return the complete adjoining start-margin set used for a box's CSS2
/// `clear:none` hypothetical position.
///
/// Unlike ordinary child placement, this probe must continue through a
/// self-collapsing first child and its following siblings. Floats and
/// positioned boxes are outside normal flow and do not close that adjoining
/// chain.
/// <https://www.w3.org/TR/CSS22/box.html#collapsing-margins>
/// <https://www.w3.org/TR/CSS22/visuren.html#flow-control>
pub(in crate::layout) fn clear_none_hypothetical_start_margin_from_boxes(
    element: &Element,
    style: &ComputedStyle,
    child_boxes: &[box_tree::FormattingBox<'_>],
    overflow_context: DocumentCanvasResolution,
) -> Option<LayoutLength> {
    if style.margin_trim.block_start {
        return None;
    }
    let mut set = AdjoiningMarginSet::from_margin(layout_pt(style.margin.top));
    complete_adjoining_child_margin_set_from_boxes(&mut set, child_boxes, element, overflow_context)
        .found_adjoining_child()
        .then(|| set.collapsed())
}

#[derive(Clone, Copy, Debug, PartialEq, Eq)]
enum AdjoiningChildMarginScan {
    NoAdjoiningChild,
    Open,
    Closed,
}

impl AdjoiningChildMarginScan {
    fn found_adjoining_child(self) -> bool {
        self != Self::NoAdjoiningChild
    }
}

fn complete_adjoining_child_margin_set_from_boxes(
    set: &mut AdjoiningMarginSet,
    child_boxes: &[box_tree::FormattingBox<'_>],
    parent: &Element,
    overflow_context: DocumentCanvasResolution,
) -> AdjoiningChildMarginScan {
    let mut found_in_flow_child = false;
    let mut has_preceding_css_float = false;
    for child_box in child_boxes {
        if let box_tree::FormattingBox::InlineSplitBlockContext(context) = child_box {
            match complete_adjoining_child_margin_set_from_boxes(
                set,
                &context.core.children,
                parent,
                overflow_context,
            ) {
                AdjoiningChildMarginScan::NoAdjoiningChild => {}
                AdjoiningChildMarginScan::Open => found_in_flow_child = true,
                AdjoiningChildMarginScan::Closed => {
                    return AdjoiningChildMarginScan::Closed;
                }
            }
            continue;
        }
        let Some((child_element, _, child_style, child_children)) = child_box.element_parts()
        else {
            if matches!(
                child_box,
                box_tree::FormattingBox::Inline(_) | box_tree::FormattingBox::Text(_)
            ) && !formatting_box_can_only_create_phantom_line_boxes(child_box)
            {
                return if found_in_flow_child {
                    AdjoiningChildMarginScan::Closed
                } else {
                    AdjoiningChildMarginScan::NoAdjoiningChild
                };
            }
            continue;
        };
        if child_style.float != Float::None {
            has_preceding_css_float = true;
            continue;
        }
        if matches!(child_style.position, Position::Absolute | Position::Fixed) {
            continue;
        }
        let is_flow_child = is_normal_block_flow_child(child_element, child_style)
            || overflow_context.is_document_canvas_flow_element(parent)
            || is_replaced_element(child_element);
        if !is_flow_child {
            if !inline_text(child_element).is_empty() {
                return if found_in_flow_child {
                    AdjoiningChildMarginScan::Closed
                } else {
                    AdjoiningChildMarginScan::NoAdjoiningChild
                };
            }
            continue;
        }
        if child_style.clear != Clear::None && has_preceding_css_float {
            return if found_in_flow_child {
                AdjoiningChildMarginScan::Closed
            } else {
                AdjoiningChildMarginScan::NoAdjoiningChild
            };
        }

        found_in_flow_child = true;
        let child_collapses_through = is_self_collapsing_block_box(
            child_element,
            child_style,
            child_children,
            overflow_context,
        );
        let mut child_set = AdjoiningMarginSet::from_margin(layout_pt(child_style.margin.top));
        if !child_style.margin_trim.block_start
            && can_collapse_block_start_margin(
                child_element,
                child_style,
                UsedEdges::from_css_edges(used_border_widths(child_style)),
                has_direct_inline_content_box(child_children),
                overflow_context.used_overflow(child_element, child_style),
            )
        {
            complete_adjoining_child_margin_set_from_boxes(
                &mut child_set,
                child_children,
                child_element,
                overflow_context,
            );
        }
        if child_collapses_through {
            child_set.include(layout_pt(child_style.margin.bottom));
        }
        set.merge(child_set);
        if !child_collapses_through {
            return AdjoiningChildMarginScan::Closed;
        }
    }
    if found_in_flow_child {
        AdjoiningChildMarginScan::Open
    } else {
        AdjoiningChildMarginScan::NoAdjoiningChild
    }
}

pub(in crate::layout) fn collapsible_start_margin_for_box(
    element: &Element,
    style: &ComputedStyle,
    child_boxes: &[box_tree::FormattingBox<'_>],
    overflow_context: DocumentCanvasResolution,
) -> AdjoiningMarginSet {
    if can_collapse_block_start_margin(
        element,
        style,
        UsedEdges::from_css_edges(used_border_widths(style)),
        has_direct_inline_content_box(child_boxes),
        overflow_context.used_overflow(element, style),
    ) && let Some(descendant_margin) = collapsible_first_child_start_margin_from_boxes(
        child_boxes,
        element,
        style,
        overflow_context,
    ) {
        if is_self_collapsing_block_box(element, style, child_boxes, overflow_context) {
            return self_collapsing_block_margin_set_for_box(style, Some(descendant_margin));
        }
        AdjoiningMarginSet::from_margin(layout_pt(style.margin.top)).merged(descendant_margin)
    } else if is_self_collapsing_block_box(element, style, child_boxes, overflow_context) {
        self_collapsing_block_margin_set_for_box(style, None)
    } else {
        AdjoiningMarginSet::from_margin(layout_pt(style.margin.top))
    }
}

/// Return whether a block container has direct inline content that prevents
/// its own block-start/end edges from adjoining child margins.
///
/// Anonymous blocks are block-level siblings, even though they contain inline
/// content, so they do not make the enclosing block container have direct
/// inline content: <https://www.w3.org/TR/CSS22/visuren.html#anonymous-block-level>.
pub(in crate::layout) fn has_direct_inline_content_box(
    child_boxes: &[box_tree::FormattingBox<'_>],
) -> bool {
    child_boxes.iter().any(|child| {
        matches!(
            child,
            box_tree::FormattingBox::Inline(_) | box_tree::FormattingBox::Text(_)
        ) && !formatting_box_can_only_create_phantom_line_boxes(child)
    })
}

pub(in crate::layout) fn has_non_inline_formatting_box(
    child_boxes: &[box_tree::FormattingBox<'_>],
) -> bool {
    child_boxes.iter().any(|child| {
        if box_tree::is_out_of_flow_box(child) {
            return false;
        }
        match child {
            box_tree::FormattingBox::AnonymousBlock(_) => {
                !formatting_box_can_only_create_phantom_line_boxes(child)
            }
            box_tree::FormattingBox::Table(_) => child.style().display.is_block_level(),
            box_tree::FormattingBox::InlineSplitBlockContext(_)
            | box_tree::FormattingBox::Block(_)
            | box_tree::FormattingBox::Flex(_) => true,
            // Replaced elements retain their own durable formatting-box
            // variant, but their outer display decides whether they form a
            // block-flow child or one atomic participant in an inline line.
            // <https://drafts.csswg.org/css-display-3/#replaced-elements>
            box_tree::FormattingBox::Replaced(_) => child.style().display.is_block_level(),
            _ => false,
        }
    })
}

pub(in crate::layout) fn formatting_box_can_only_create_phantom_line_boxes(
    box_: &box_tree::FormattingBox<'_>,
) -> bool {
    match box_ {
        box_tree::FormattingBox::Text(text) => {
            text.text.is_empty()
                || (text.style.white_space.collapses_spaces()
                    && text.text.chars().all(is_css_collapsible_whitespace))
        }
        box_tree::FormattingBox::Inline(box_) => {
            inline_box_has_no_nonzero_inline_axis_component(&box_.core.style)
                && !box_.core.style.content.is_generated()
                && !box_
                    .core
                    .style
                    .before_style
                    .as_deref()
                    .is_some_and(|style| style.content.is_generated())
                && !box_
                    .core
                    .style
                    .after_style
                    .as_deref()
                    .is_some_and(|style| style.content.is_generated())
                && box_
                    .core
                    .children
                    .iter()
                    .all(formatting_box_can_only_create_phantom_line_boxes)
        }
        box_tree::FormattingBox::AnonymousBlock(box_) => box_
            .children
            .iter()
            .all(formatting_box_can_only_create_phantom_line_boxes),
        box_tree::FormattingBox::InlineSplitBlockContext(box_) => box_
            .core
            .children
            .iter()
            .all(formatting_box_can_only_create_phantom_line_boxes),
        box_tree::FormattingBox::AtomicInline(_)
        | box_tree::FormattingBox::Block(_)
        | box_tree::FormattingBox::Table(_)
        | box_tree::FormattingBox::Flex(_)
        | box_tree::FormattingBox::Replaced(_) => false,
    }
}

/// Whether generated `content` contributes a non-phantom inline line box.
///
/// An empty (or collapsible-whitespace-only) generated string still creates a
/// pseudo-element box, but it does not make a line box non-adjoining for CSS
/// margin collapse. Other generated content can resolve to visible text or an
/// atomic inline, so it remains a line-box contribution.
/// <https://drafts.csswg.org/css-inline/#invisible-line-boxes>
/// <https://www.w3.org/TR/CSS22/box.html#collapsing-margins>
pub(in crate::layout) fn generated_content_has_non_phantom_inline_content(
    style: &ComputedStyle,
) -> bool {
    style.content.generated_parts().is_some_and(|parts| {
        parts.iter().any(|part| match part {
            css::GeneratedContentPart::Text(text) => {
                !(text.is_empty()
                    || style.white_space.collapses_spaces()
                        && text.chars().all(is_css_collapsible_whitespace))
            }
            _ => true,
        })
    })
}

pub(super) fn inline_box_has_no_nonzero_inline_axis_component(style: &ComputedStyle) -> bool {
    let borders = used_border_widths(style);
    [
        inline_start_side(style.writing_mode, style.used_direction()),
        inline_end_side(style.writing_mode, style.used_direction()),
    ]
    .into_iter()
    .all(|side| {
        edge_value(style.margin, side).abs() <= 0.001
            && edge_value(borders, side).abs() <= 0.001
            && edge_value(style.padding, side).abs() <= 0.001
    })
}

fn edge_value(edges: css::Edges, side: PhysicalSide) -> f32 {
    match side {
        PhysicalSide::Top => edges.top,
        PhysicalSide::Right => edges.right,
        PhysicalSide::Bottom => edges.bottom,
        PhysicalSide::Left => edges.left,
    }
}

pub(in crate::layout) fn has_atomic_inline_formatting_box(
    child_boxes: &[box_tree::FormattingBox<'_>],
) -> bool {
    child_boxes.iter().any(|child| match child {
        box_tree::FormattingBox::AtomicInline(_) => true,
        box_tree::FormattingBox::AnonymousBlock(box_) => {
            has_atomic_inline_formatting_box(&box_.children)
        }
        box_tree::FormattingBox::InlineSplitBlockContext(box_) => {
            has_atomic_inline_formatting_box(&box_.core.children)
        }
        box_tree::FormattingBox::Inline(box_) => {
            has_atomic_inline_formatting_box(&box_.core.children)
        }
        box_tree::FormattingBox::Block(_)
        | box_tree::FormattingBox::Text(_)
        | box_tree::FormattingBox::Table(_)
        | box_tree::FormattingBox::Flex(_)
        | box_tree::FormattingBox::Replaced(_) => false,
    })
}