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
use super::*;
use crate::layout::inline_collect::TextDecorationPropagationContext;
impl<'a> LayoutBuilder<'a> {
pub(in crate::layout) fn layout_formatting_box(
&mut self,
formatting_box: &box_tree::FormattingBox<'_>,
stylesheets: &Stylesheets<'_>,
) {
self.layout_formatting_box_with_parent_decoration(formatting_box, stylesheets, None);
}
/// Lay out a frozen formatting box with the decoration origins propagated
/// by its in-flow parent.
///
/// Frozen box trees retain computed styles, so normal CSS inheritance
/// cannot carry line-decoration provenance across this boundary. Resolve
/// the layout-only propagation context here before dispatching the box's
/// formatting algorithm.
/// <https://drafts.csswg.org/css-text-decor-4/#line-decoration>
pub(in crate::layout) fn layout_formatting_box_with_parent_decoration(
&mut self,
formatting_box: &box_tree::FormattingBox<'_>,
stylesheets: &Stylesheets<'_>,
parent_style: Option<&ComputedStyle>,
) {
let decoration_context = parent_style
.map(TextDecorationPropagationContext::from_style)
.unwrap_or_default();
match formatting_box {
box_tree::FormattingBox::Block(box_) => {
let used_style = decoration_context.used_child_style(&box_.core.style);
// The document box can recur through an anonymous root-flow
// wrapper before it reaches its own descendants. Keep the
// stored style computed, but apply the principal-flow axes at
// every formatting entry for that one principal box.
let layout_style = box_
.core
.element
.tag
.eq_ignore_ascii_case("html")
.then_some(())
.filter(|_| matches!(&box_.core.source, box_tree::BoxSource::Principal))
.map(|_| self.principal_flow.root_layout_style(&used_style));
self.layout_element_box(
box_.core.element,
layout_style.as_ref().unwrap_or(&used_style),
stylesheets,
box_.core.signature.clone(),
&box_.core.source,
&box_.run_in_children,
&box_.core.children,
);
}
box_tree::FormattingBox::Inline(box_) => {
let used_style = decoration_context.used_child_style(&box_.core.style);
self.layout_element_box(
box_.core.element,
&used_style,
stylesheets,
box_.core.signature.clone(),
&box_.core.source,
&[],
&box_.core.children,
)
}
box_tree::FormattingBox::AnonymousBlock(box_) => {
let used_style = decoration_context.used_child_style(&box_.style);
self.layout_anonymous_block(&used_style, &box_.children, stylesheets, None);
}
box_tree::FormattingBox::InlineSplitBlockContext(box_) => self
.layout_inline_split_block_context_with_parent_decoration(
box_,
stylesheets,
parent_style,
),
box_tree::FormattingBox::AtomicInline(box_) => {
let used_style = decoration_context.used_child_style(&box_.core.style);
self.layout_element_box(
box_.core.element,
&used_style,
stylesheets,
box_.core.signature.clone(),
&box_.core.source,
&[],
&box_.core.children,
)
}
box_tree::FormattingBox::Table(box_) => {
let used_style = decoration_context.used_child_style(&box_.core.style);
self.layout_table_box(
box_.core.element,
&used_style,
stylesheets,
box_.core.signature.clone(),
&box_.core.source,
&box_.core.children,
&box_.fragment,
);
}
box_tree::FormattingBox::Flex(box_) => {
let used_style = decoration_context.used_child_style(&box_.core.style);
self.layout_element_box(
box_.core.element,
&used_style,
stylesheets,
box_.core.signature.clone(),
&box_.core.source,
&[],
&box_.core.children,
)
}
box_tree::FormattingBox::Replaced(box_) => {
let used_style = decoration_context.used_child_style(&box_.core.style);
self.layout_element_box(
box_.core.element,
&used_style,
stylesheets,
box_.core.signature.clone(),
&box_.core.source,
&[],
&box_.core.children,
)
}
box_tree::FormattingBox::Text(box_) => {
let used_style = decoration_context.used_child_style(&box_.style);
let text = normalized_text_for_style(&box_.text, &used_style);
if !text.is_empty() {
self.layout_text_block(&text, &used_style, 0.0, 0.0, None);
}
}
}
}
#[allow(clippy::too_many_arguments)]
pub(in crate::layout) fn layout_element_box(
&mut self,
element: &Element,
style: &ComputedStyle,
stylesheets: &Stylesheets<'_>,
signature: ElementSignature,
source: &box_tree::BoxSource<'_>,
run_in_children: &[box_tree::FormattingBox<'_>],
children: &[box_tree::FormattingBox<'_>],
) {
self.push_ancestor_signature(signature);
match source {
box_tree::BoxSource::Principal => {
self.capture_suppressed_named_strings_before(element.id);
self.layout_element_with_child_boxes_and_run_ins(
element,
style,
stylesheets,
run_in_children,
Some(children),
);
self.capture_suppressed_named_strings_after(element.id);
}
box_tree::BoxSource::GeneratedPseudo(pseudo) => {
self.layout_generated_pseudo_box(
element,
style,
pseudo.kind.counter_event_source(),
stylesheets,
run_in_children,
Some(children),
None,
PrincipalBoxPaintMode::RootPaints,
);
}
}
self.ancestors.pop();
}
/// Lays out a table formatting box through the generic element entry path.
///
/// CSS Paged Media applies the `page` property to normal-flow boxes before
/// their page context is generated, and CSS Tables uses a table wrapper/grid
/// fragment for layout. This preserves the prebuilt durable table fragment
/// while still applying named-page, counter, running-element, and
/// break-inside entry behavior:
/// <https://www.w3.org/TR/css-page-3/#using-named-pages> and
/// <https://www.w3.org/TR/CSS22/tables.html#model>.
#[allow(clippy::too_many_arguments)]
pub(in crate::layout) fn layout_table_box(
&mut self,
element: &Element,
style: &ComputedStyle,
stylesheets: &Stylesheets<'_>,
signature: ElementSignature,
source: &box_tree::BoxSource<'_>,
children: &[box_tree::FormattingBox<'_>],
fragment: &box_tree::TableFragment<'_>,
) {
self.push_ancestor_signature(signature);
match source {
box_tree::BoxSource::Principal => {
self.capture_suppressed_named_strings_before(element.id);
self.layout_element_with_child_boxes_run_ins_and_table_fragment(
element,
style,
stylesheets,
&[],
Some(children),
Some(fragment),
);
self.capture_suppressed_named_strings_after(element.id);
}
box_tree::BoxSource::GeneratedPseudo(pseudo) => {
self.layout_generated_pseudo_box(
element,
style,
pseudo.kind.counter_event_source(),
stylesheets,
&[],
Some(children),
Some(fragment),
PrincipalBoxPaintMode::RootPaints,
);
}
}
self.ancestors.pop();
}
#[allow(clippy::too_many_arguments)]
pub(in crate::layout) fn layout_generated_pseudo_box(
&mut self,
element: &Element,
style: &ComputedStyle,
source: box_tree::CounterEventSource,
stylesheets: &Stylesheets<'_>,
run_in_children: &[box_tree::FormattingBox<'_>],
child_boxes: Option<&[box_tree::FormattingBox<'_>]>,
table_fragment: Option<&box_tree::TableFragment<'_>>,
principal_box_paint_mode: PrincipalBoxPaintMode,
) {
let counter_scope = self.begin_pseudo_counter_scope(element, source, style);
self.element_side_effect_suppression_depth += 1;
// A sole image in a tree-abiding ::before/::after box is anonymous
// replaced content inside that pseudo's decorated box. Keep the
// pseudo's authored dimensions for its own background/border while
// the image payload retains its zoomed natural size. Principal
// `content: <image>` remains a replacement of the element itself.
// <https://www.w3.org/TR/css-content-3/#content-property>
let mut pseudo_content_style;
let style = if matches!(
style.content,
css::Content::Replacement {
image: css::GeneratedContentPart::Image { .. },
..
}
) {
pseudo_content_style = style.clone();
pseudo_content_style.object_fit = css::ObjectFit::None;
pseudo_content_style.object_position = css::BackgroundPosition::INITIAL;
&pseudo_content_style
} else {
style
};
let consuming_root_canvas =
!style.display.is_block_level() && self.begin_root_inline_canvas_continuation(element);
let previous_positioned_generated_source = self.positioned_generated_source;
self.positioned_generated_source = match source {
box_tree::CounterEventSource::Before => {
Some(InlineStaticPositionSourceId::for_generated_pseudo(
element,
box_tree::GeneratedPseudoKind::Before,
))
}
box_tree::CounterEventSource::After => {
Some(InlineStaticPositionSourceId::for_generated_pseudo(
element,
box_tree::GeneratedPseudoKind::After,
))
}
box_tree::CounterEventSource::FootnoteCall => {
Some(InlineStaticPositionSourceId::for_generated_pseudo(
element,
box_tree::GeneratedPseudoKind::FootnoteCall,
))
}
box_tree::CounterEventSource::Principal
| box_tree::CounterEventSource::Marker
| box_tree::CounterEventSource::FootnoteMarker => None,
};
let previous_root_pseudo_block_projection = self.root_pseudo_block_projection;
let root_before_principal_track_start = (element.tag.eq_ignore_ascii_case("html")
&& source == box_tree::CounterEventSource::Before
&& style.writing_mode == WritingMode::HorizontalTb
&& self.principal_flow.writing_mode == WritingMode::VerticalLr)
.then_some(self.content_left);
if element.tag.eq_ignore_ascii_case("html") {
self.root_pseudo_block_projection =
match (style.writing_mode, self.principal_flow.writing_mode) {
// A root pseudo retains its horizontal computed style, but
// a propagated vertical-lr body establishes the initial
// containing block's used principal flow. Project this one
// direct root child through that flow so the ordinary child
// traversal can advance the horizontal block track before
// entering the body.
// <https://www.w3.org/TR/css-writing-modes-4/#principal-flow>
(WritingMode::HorizontalTb, WritingMode::VerticalLr)
if source == box_tree::CounterEventSource::Before =>
{
Some(RootPseudoBlockProjection {
element: element.id,
block_start: PhysicalSide::Left,
block_end_inset: layout_pt(0.0),
})
}
// The inverse projection retains the propagated body's
// physical block-end canvas inset while a vertical root
// pseudo participates in a horizontal principal flow.
(WritingMode::VerticalLr, WritingMode::HorizontalTb) => {
Some(RootPseudoBlockProjection {
element: element.id,
block_start: PhysicalSide::Left,
block_end_inset: self.principal_body_block_end_inset,
})
}
_ => None,
};
}
self.layout_element_inner_with_principal_effect_context(
element,
style,
stylesheets,
run_in_children,
child_boxes,
table_fragment,
true,
principal_box_paint_mode,
None,
);
if element.tag.eq_ignore_ascii_case("html")
&& source == box_tree::CounterEventSource::Before
&& style.writing_mode == WritingMode::HorizontalTb
&& self.principal_flow.writing_mode == WritingMode::VerticalLr
{
// The generated root pseudo is laid out directly rather than as a
// normal child traversal entry. It therefore must explicitly
// consume its committed margin-box span from the propagated
// body's horizontal track. The outcome span already includes the
// projected logical block-end margin; adding a physical margin
// here would count the horizontal pseudo's margin twice.
// <https://www.w3.org/TR/css-writing-modes-4/#principal-flow>
let advance = self
.last_block_layout_outcome
.physical_border_box_inline_span
.points();
self.content_left = (root_before_principal_track_start
.expect("the vertical principal-track start was captured")
+ advance)
.min(self.content_right);
}
if consuming_root_canvas {
self.finish_root_inline_canvas_continuation();
}
self.root_pseudo_block_projection = previous_root_pseudo_block_projection;
self.positioned_generated_source = previous_positioned_generated_source;
self.element_side_effect_suppression_depth -= 1;
self.end_counter_scope(counter_scope);
}
/// Resolves a propagated body's completed document canvas immediately
/// before the next source-ordered root inline sequence is laid out.
///
/// This is a layout transition, rather than a paint-fragment adjustment:
/// the source page is already committed by the body traversal when this
/// method is reached.
/// <https://www.w3.org/TR/css-writing-modes-4/#principal-flow>
pub(in crate::layout) fn begin_root_inline_canvas_continuation(
&mut self,
element: &Element,
) -> bool {
if !element.tag.eq_ignore_ascii_case("html")
|| !self.principal_flow.has_propagated_body()
|| self
.root_principal_flow_context
.active_root_inline_canvas
.is_some()
{
return false;
}
let axes = WritingModeAxes::new(
self.principal_flow.writing_mode,
self.principal_flow.used_direction(),
);
if !axes.swaps_physical_axes() {
return false;
}
let Some(continuation) = self.root_principal_flow_context.completed_canvas.take() else {
return false;
};
debug_assert_eq!(continuation.source_page.get(), self.pages.len());
let placement = continuation.resolve_root_inline_placement(
axes,
PageInlineSpan::from_edges(self.content_left, self.content_right),
);
match placement {
RootInlineCanvasPlacement::RemainingTrack {
block_track,
inline_origin,
} => {
self.content_left = block_track.left_x();
self.content_right = block_track.right_x();
self.cursor_y = inline_origin.points();
}
RootInlineCanvasPlacement::NextPage { .. } => {
// The preceding body has completed its page-owned canvas
// before this root sequence begins. Mark that normal-flow
// occupancy so page finalization cannot coalesce the source
// page away, then establish the destination inline origin
// before line construction starts.
self.mark_current_page_flow_content();
self.push_page();
self.cursor_y = match inline_start_side(
self.principal_flow.writing_mode,
self.principal_flow.used_direction(),
) {
PhysicalSide::Top => self.page_top(),
// A bottom-origin principal flow reaches the following
// page at the body canvas's inline end. Its inset lies
// beyond the new fragmentainer's physical bottom, so the
// next root inline sequence is clipped there by ordinary
// line layout rather than replaying a translated paint
// fragment from the source page.
PhysicalSide::Bottom => {
self.page_bottom() - continuation.inline_end_inset.points()
}
PhysicalSide::Left | PhysicalSide::Right => {
unreachable!("a vertical principal flow has a vertical inline axis")
}
};
}
}
self.root_principal_flow_context.active_root_inline_canvas = Some(continuation);
true
}
/// Completes the root inline sequence that consumed the propagated body
/// continuation. The state remains live through line layout so nested
/// paint and pagination paths cannot observe a partially consumed canvas.
pub(in crate::layout) fn finish_root_inline_canvas_continuation(&mut self) {
debug_assert!(
self.root_principal_flow_context
.active_root_inline_canvas
.is_some()
);
self.root_principal_flow_context.active_root_inline_canvas = None;
}
}