servo-layout 0.1.0

A component of the servo web-engine.
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
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at https://mozilla.org/MPL/2.0/. */

use std::marker::PhantomData;

use atomic_refcell::{AtomicRef, AtomicRefCell, AtomicRefMut};
use html5ever::{local_name, ns};
use layout_api::wrapper_traits::{LayoutDataTrait, ThreadSafeLayoutElement, ThreadSafeLayoutNode};
use layout_api::{
    GenericLayoutDataTrait, LayoutElementType, LayoutNodeType as ScriptLayoutNodeType,
    SVGElementData,
};
use malloc_size_of_derive::MallocSizeOf;
use script::layout_dom::ServoThreadSafeLayoutNode;
use servo_arc::Arc as ServoArc;
use smallvec::SmallVec;
use style::context::SharedStyleContext;
use style::properties::ComputedValues;
use style::selector_parser::PseudoElement;
use style::values::specified::box_::DisplayOutside as StyloDisplayOutside;

use crate::cell::{ArcRefCell, WeakRefCell};
use crate::context::LayoutContext;
use crate::dom_traversal::{Contents, NodeAndStyleInfo};
use crate::flexbox::FlexLevelBox;
use crate::flow::inline::{InlineItem, SharedInlineStyles, WeakInlineItem};
use crate::flow::{BlockLevelBox, BlockLevelCreator};
use crate::fragment_tree::{Fragment, FragmentFlags};
use crate::geom::PhysicalSize;
use crate::layout_box_base::LayoutBoxBase;
use crate::replaced::{CanvasInfo, IFrameInfo, ImageInfo, VideoInfo};
use crate::style_ext::{
    ComputedValuesExt, Display, DisplayGeneratingBox, DisplayLayoutInternal, DisplayOutside,
};
use crate::table::{TableLevelBox, WeakTableLevelBox};
use crate::taffy::TaffyItemBox;

#[derive(MallocSizeOf)]
pub struct PseudoLayoutData {
    pseudo: PseudoElement,
    data: ArcRefCell<InnerDOMLayoutData>,
}

/// The data that is stored in each DOM node that is used by layout.
#[derive(Default, MallocSizeOf)]
pub struct InnerDOMLayoutData {
    pub(super) self_box: ArcRefCell<Option<LayoutBox>>,
    pub(super) pseudo_boxes: SmallVec<[PseudoLayoutData; 2]>,
}

impl InnerDOMLayoutData {
    fn pseudo_layout_data(
        &self,
        pseudo_element: PseudoElement,
    ) -> Option<ArcRefCell<InnerDOMLayoutData>> {
        for pseudo_layout_data in self.pseudo_boxes.iter() {
            if pseudo_element == pseudo_layout_data.pseudo {
                return Some(pseudo_layout_data.data.clone());
            }
        }
        None
    }

    fn create_pseudo_layout_data(
        &mut self,
        pseudo_element: PseudoElement,
    ) -> ArcRefCell<InnerDOMLayoutData> {
        let data: ArcRefCell<InnerDOMLayoutData> = Default::default();
        self.pseudo_boxes.push(PseudoLayoutData {
            pseudo: pseudo_element,
            data: data.clone(),
        });
        data
    }

    fn fragments(&self) -> Vec<Fragment> {
        self.self_box
            .borrow()
            .as_ref()
            .and_then(|layout_box| layout_box.with_base(LayoutBoxBase::fragments))
            .unwrap_or_default()
    }

    fn repair_style(&self, node: &ServoThreadSafeLayoutNode, context: &SharedStyleContext) {
        if let Some(layout_object) = &*self.self_box.borrow() {
            layout_object.repair_style(context, node, &node.style(context));
        }

        for pseudo_layout_data in self.pseudo_boxes.iter() {
            let Some(node_with_pseudo) = node.with_pseudo(pseudo_layout_data.pseudo) else {
                continue;
            };
            pseudo_layout_data
                .data
                .borrow()
                .repair_style(&node_with_pseudo, context);
        }
    }

    fn with_layout_box_base(&self, callback: impl Fn(&LayoutBoxBase)) {
        if let Some(data) = self.self_box.borrow().as_ref() {
            data.with_base(callback);
        }
    }

    fn with_layout_box_base_including_pseudos(&self, callback: impl Fn(&LayoutBoxBase)) {
        self.with_layout_box_base(&callback);
        for pseudo_layout_data in self.pseudo_boxes.iter() {
            pseudo_layout_data
                .data
                .borrow()
                .with_layout_box_base(&callback);
        }
    }
}

/// A box that is stored in one of the `DOMLayoutData` slots.
#[derive(Debug, MallocSizeOf)]
pub(super) enum LayoutBox {
    DisplayContents(SharedInlineStyles),
    BlockLevel(ArcRefCell<BlockLevelBox>),
    InlineLevel(InlineItem),
    FlexLevel(ArcRefCell<FlexLevelBox>),
    TableLevelBox(TableLevelBox),
    TaffyItemBox(ArcRefCell<TaffyItemBox>),
}

impl LayoutBox {
    pub(crate) fn with_base<T>(&self, callback: impl FnOnce(&LayoutBoxBase) -> T) -> Option<T> {
        Some(match self {
            LayoutBox::DisplayContents(..) => return None,
            LayoutBox::BlockLevel(block_level_box) => block_level_box.borrow().with_base(callback),
            LayoutBox::InlineLevel(inline_item) => inline_item.with_base(callback),
            LayoutBox::FlexLevel(flex_level_box) => flex_level_box.borrow().with_base(callback),
            LayoutBox::TaffyItemBox(taffy_item_box) => taffy_item_box.borrow().with_base(callback),
            LayoutBox::TableLevelBox(table_box) => table_box.with_base(callback),
        })
    }

    pub(crate) fn with_base_mut<T>(
        &mut self,
        callback: impl FnOnce(&mut LayoutBoxBase) -> T,
    ) -> Option<T> {
        Some(match self {
            LayoutBox::DisplayContents(..) => return None,
            LayoutBox::BlockLevel(block_level_box) => {
                block_level_box.borrow_mut().with_base_mut(callback)
            },
            LayoutBox::InlineLevel(inline_item) => inline_item.with_base_mut(callback),
            LayoutBox::FlexLevel(flex_level_box) => {
                flex_level_box.borrow_mut().with_base_mut(callback)
            },
            LayoutBox::TaffyItemBox(taffy_item_box) => {
                taffy_item_box.borrow_mut().with_base_mut(callback)
            },
            LayoutBox::TableLevelBox(table_box) => table_box.with_base_mut(callback),
        })
    }

    fn repair_style(
        &self,
        context: &SharedStyleContext,
        node: &ServoThreadSafeLayoutNode,
        new_style: &ServoArc<ComputedValues>,
    ) {
        match self {
            LayoutBox::DisplayContents(inline_shared_styles) => {
                *inline_shared_styles.style.borrow_mut() = new_style.clone();
                *inline_shared_styles.selected.borrow_mut() = node.selected_style(context);
            },
            LayoutBox::BlockLevel(block_level_box) => {
                block_level_box
                    .borrow_mut()
                    .repair_style(context, node, new_style);
            },
            LayoutBox::InlineLevel(inline_item) => {
                inline_item.repair_style(context, node, new_style);
            },
            LayoutBox::FlexLevel(flex_level_box) => flex_level_box
                .borrow_mut()
                .repair_style(context, node, new_style),
            LayoutBox::TableLevelBox(table_level_box) => {
                table_level_box.repair_style(context, node, new_style)
            },
            LayoutBox::TaffyItemBox(taffy_item_box) => taffy_item_box
                .borrow_mut()
                .repair_style(context, node, new_style),
        }
    }

    fn attached_to_tree(&self, layout_box: WeakLayoutBox) {
        match self {
            Self::DisplayContents(_) => {
                // This box can't have children, its contents get reparented to its parent.
                // Therefore, no need to do anything.
            },
            Self::BlockLevel(block_level_box) => {
                block_level_box.borrow().attached_to_tree(layout_box)
            },
            Self::InlineLevel(inline_item) => inline_item.attached_to_tree(layout_box),
            Self::FlexLevel(flex_level_box) => flex_level_box.borrow().attached_to_tree(layout_box),
            Self::TableLevelBox(table_level_box) => table_level_box.attached_to_tree(layout_box),
            Self::TaffyItemBox(taffy_item_box) => {
                taffy_item_box.borrow().attached_to_tree(layout_box)
            },
        }
    }

    fn downgrade(&self) -> WeakLayoutBox {
        match self {
            Self::DisplayContents(inline_shared_styles) => {
                WeakLayoutBox::DisplayContents(inline_shared_styles.clone())
            },
            Self::BlockLevel(block_level_box) => {
                WeakLayoutBox::BlockLevel(block_level_box.downgrade())
            },
            Self::InlineLevel(inline_item) => WeakLayoutBox::InlineLevel(inline_item.downgrade()),
            Self::FlexLevel(flex_level_box) => WeakLayoutBox::FlexLevel(flex_level_box.downgrade()),
            Self::TableLevelBox(table_level_box) => {
                WeakLayoutBox::TableLevelBox(table_level_box.downgrade())
            },
            Self::TaffyItemBox(taffy_item_box) => {
                WeakLayoutBox::TaffyItemBox(taffy_item_box.downgrade())
            },
        }
    }
}

#[derive(Clone, Debug, MallocSizeOf)]
pub(super) enum WeakLayoutBox {
    DisplayContents(SharedInlineStyles),
    BlockLevel(WeakRefCell<BlockLevelBox>),
    InlineLevel(WeakInlineItem),
    FlexLevel(WeakRefCell<FlexLevelBox>),
    TableLevelBox(WeakTableLevelBox),
    TaffyItemBox(WeakRefCell<TaffyItemBox>),
}

impl WeakLayoutBox {
    pub(crate) fn upgrade(&self) -> Option<LayoutBox> {
        Some(match self {
            Self::DisplayContents(inline_shared_styles) => {
                LayoutBox::DisplayContents(inline_shared_styles.clone())
            },
            Self::BlockLevel(block_level_box) => LayoutBox::BlockLevel(block_level_box.upgrade()?),
            Self::InlineLevel(inline_item) => LayoutBox::InlineLevel(inline_item.upgrade()?),
            Self::FlexLevel(flex_level_box) => LayoutBox::FlexLevel(flex_level_box.upgrade()?),
            Self::TableLevelBox(table_level_box) => {
                LayoutBox::TableLevelBox(table_level_box.upgrade()?)
            },
            Self::TaffyItemBox(taffy_item_box) => {
                LayoutBox::TaffyItemBox(taffy_item_box.upgrade()?)
            },
        })
    }
}

/// A wrapper for [`InnerDOMLayoutData`]. This is necessary to give the entire data
/// structure interior mutability, as we will need to mutate the layout data of
/// non-mutable DOM nodes.
#[derive(Default, MallocSizeOf)]
pub struct DOMLayoutData(AtomicRefCell<InnerDOMLayoutData>);

// The implementation of this trait allows the data to be stored in the DOM.
impl LayoutDataTrait for DOMLayoutData {}
impl GenericLayoutDataTrait for DOMLayoutData {
    fn as_any(&self) -> &dyn std::any::Any {
        self
    }
}

pub struct BoxSlot<'dom> {
    pub(crate) slot: ArcRefCell<Option<LayoutBox>>,
    pub(crate) marker: PhantomData<&'dom ()>,
}

impl From<ArcRefCell<Option<LayoutBox>>> for BoxSlot<'_> {
    fn from(slot: ArcRefCell<Option<LayoutBox>>) -> Self {
        Self {
            slot,
            marker: PhantomData,
        }
    }
}

/// A mutable reference to a `LayoutBox` stored in a DOM element.
impl BoxSlot<'_> {
    pub(crate) fn set(self, layout_box: LayoutBox) {
        layout_box.attached_to_tree(layout_box.downgrade());
        *self.slot.borrow_mut() = Some(layout_box);
    }

    pub(crate) fn take_layout_box(&self) -> Option<LayoutBox> {
        self.slot.borrow_mut().take()
    }
}

impl Drop for BoxSlot<'_> {
    fn drop(&mut self) {
        if !std::thread::panicking() {
            assert!(self.slot.borrow().is_some(), "failed to set a layout box");
        }
    }
}

pub(crate) trait NodeExt<'dom> {
    /// Returns the relevant data wrapping into respective struct and its size in pixels.
    fn as_image(&self) -> Option<(ImageInfo, PhysicalSize<f64>)>;
    fn as_canvas(&self) -> Option<(CanvasInfo, PhysicalSize<f64>)>;
    fn as_iframe(&self) -> Option<IFrameInfo>;
    fn as_video(&self) -> Option<(VideoInfo, Option<PhysicalSize<f64>>)>;
    fn as_svg(&self) -> Option<SVGElementData<'dom>>;
    fn as_typeless_object_with_data_attribute(&self) -> Option<String>;

    fn ensure_inner_layout_data(&self) -> AtomicRefMut<'dom, InnerDOMLayoutData>;
    fn inner_layout_data(&self) -> Option<AtomicRef<'dom, InnerDOMLayoutData>>;
    fn inner_layout_data_mut(&self) -> Option<AtomicRefMut<'dom, InnerDOMLayoutData>>;
    fn box_slot(&self) -> BoxSlot<'dom>;

    /// Remove boxes for the element itself, and all of its pseudo-element boxes.
    fn unset_all_boxes(&self);

    fn fragments_for_pseudo(&self, pseudo_element: Option<PseudoElement>) -> Vec<Fragment>;
    fn with_layout_box_base_including_pseudos(&self, callback: impl Fn(&LayoutBoxBase));

    fn repair_style(&self, context: &SharedStyleContext);

    /// Whether or not this node isolates downward flowing box tree rebuild damage and
    /// fragment tree layout cache damage. Roughly, this corresponds to independent
    /// formatting context boundaries.
    ///
    /// - The node's boxes themselves will be rebuilt, but not the descendant node's
    ///   boxes.
    /// - The node's fragment tree layout will be rebuilt, not the descendent node's
    ///   fragment tree layout cache.
    ///
    /// When this node has no box yet, `false` is returned.
    fn isolates_damage_for_damage_propagation(&self) -> bool;

    /// Try to re-run box tree reconstruction from this point. This can succeed if the
    /// node itself is still valid and isolates box tree damage from ancestors (for
    /// instance, if it starts an independent formatting context). **Note:** This assumes
    /// that no ancestors have box damage.
    ///
    /// Returns `true` if box tree reconstruction was sucessful and `false` otherwise.
    fn rebuild_box_tree_from_independent_formatting_context(
        &self,
        layout_context: &LayoutContext,
    ) -> bool;
}

impl<'dom> NodeExt<'dom> for ServoThreadSafeLayoutNode<'dom> {
    fn as_image(&self) -> Option<(ImageInfo, PhysicalSize<f64>)> {
        let (resource, metadata) = self.image_data()?;
        let width = metadata.map(|metadata| metadata.width).unwrap_or_default();
        let height = metadata.map(|metadata| metadata.height).unwrap_or_default();
        let (mut width, mut height) = (width as f64, height as f64);
        // Take `image_density` into account for calculating the size in pixels for images.
        if let Some(density) = self.image_density().filter(|density| *density != 1.) {
            width /= density;
            height /= density;
        }
        Some((
            ImageInfo {
                image: resource,
                showing_broken_image_icon: self.showing_broken_image_icon(),
                url: self.image_url(),
            },
            PhysicalSize::new(width, height),
        ))
    }

    fn as_svg(&self) -> Option<SVGElementData<'dom>> {
        self.svg_data()
    }

    fn as_video(&self) -> Option<(VideoInfo, Option<PhysicalSize<f64>>)> {
        let data = self.media_data()?;
        let natural_size = if let Some(frame) = data.current_frame {
            Some(PhysicalSize::new(frame.width.into(), frame.height.into()))
        } else {
            data.metadata
                .map(|meta| PhysicalSize::new(meta.width.into(), meta.height.into()))
        };
        Some((
            VideoInfo {
                image_key: data.current_frame.map(|frame| frame.image_key),
            },
            natural_size,
        ))
    }

    fn as_canvas(&self) -> Option<(CanvasInfo, PhysicalSize<f64>)> {
        let canvas_data = self.canvas_data()?;
        let source = canvas_data.image_key;
        Some((
            CanvasInfo { source },
            PhysicalSize::new(canvas_data.width.into(), canvas_data.height.into()),
        ))
    }

    fn as_iframe(&self) -> Option<IFrameInfo> {
        match (self.iframe_pipeline_id(), self.iframe_browsing_context_id()) {
            (Some(pipeline_id), Some(browsing_context_id)) => Some(IFrameInfo {
                pipeline_id,
                browsing_context_id,
            }),
            _ => None,
        }
    }

    fn as_typeless_object_with_data_attribute(&self) -> Option<String> {
        if self.type_id() !=
            Some(ScriptLayoutNodeType::Element(
                LayoutElementType::HTMLObjectElement,
            ))
        {
            return None;
        }

        // TODO: This is the what the legacy layout system did, but really if Servo
        // supports any `<object>` that's an image, it should support those with URLs
        // and `type` attributes with image mime types.
        let element = self.as_element()?;
        if element.get_attr(&ns!(), &local_name!("type")).is_some() {
            return None;
        }
        element
            .get_attr(&ns!(), &local_name!("data"))
            .map(|string| string.to_owned())
    }

    fn ensure_inner_layout_data(&self) -> AtomicRefMut<'dom, InnerDOMLayoutData> {
        if self.layout_data().is_none() {
            self.initialize_layout_data::<DOMLayoutData>();
        }
        self.layout_data()
            .unwrap()
            .as_any()
            .downcast_ref::<DOMLayoutData>()
            .unwrap()
            .0
            .borrow_mut()
    }

    fn inner_layout_data(&self) -> Option<AtomicRef<'dom, InnerDOMLayoutData>> {
        self.layout_data().map(|data| {
            data.as_any()
                .downcast_ref::<DOMLayoutData>()
                .unwrap()
                .0
                .borrow()
        })
    }

    fn inner_layout_data_mut(&self) -> Option<AtomicRefMut<'dom, InnerDOMLayoutData>> {
        self.layout_data().map(|data| {
            data.as_any()
                .downcast_ref::<DOMLayoutData>()
                .unwrap()
                .0
                .borrow_mut()
        })
    }

    fn box_slot(&self) -> BoxSlot<'dom> {
        let pseudo_element_chain = self.pseudo_element_chain();
        let Some(primary) = pseudo_element_chain.primary else {
            return self.ensure_inner_layout_data().self_box.clone().into();
        };

        let Some(secondary) = pseudo_element_chain.secondary else {
            let primary_layout_data = self
                .ensure_inner_layout_data()
                .create_pseudo_layout_data(primary);
            return primary_layout_data.borrow().self_box.clone().into();
        };

        // It's *very* important that this not borrow the element's main
        // `InnerLayoutData`. Primary pseudo-elements are processed at the same recursion
        // level as the main data, so the `BoxSlot` is created sequentially with other
        // primary pseudo-elements and the element itself. The secondary pseudo-element is
        // one level deep, so could be happening in parallel with the primary
        // pseudo-elements or main element layout.
        let primary_layout_data = self
            .inner_layout_data()
            .expect("Should already have element InnerLayoutData here.")
            .pseudo_layout_data(primary)
            .expect("Should already have primary pseudo-element InnerLayoutData here");
        let secondary_layout_data = primary_layout_data
            .borrow_mut()
            .create_pseudo_layout_data(secondary);
        secondary_layout_data.borrow().self_box.clone().into()
    }

    fn unset_all_boxes(&self) {
        let mut layout_data = self.ensure_inner_layout_data();
        *layout_data.self_box.borrow_mut() = None;
        layout_data.pseudo_boxes.clear();

        // Stylo already takes care of removing all layout data
        // for DOM descendants of elements with `display: none`.
    }

    fn with_layout_box_base_including_pseudos(&self, callback: impl Fn(&LayoutBoxBase)) {
        if let Some(inner_layout_data) = self.inner_layout_data() {
            inner_layout_data.with_layout_box_base_including_pseudos(callback);
        }
    }

    fn fragments_for_pseudo(&self, pseudo_element: Option<PseudoElement>) -> Vec<Fragment> {
        let Some(layout_data) = self.inner_layout_data() else {
            return vec![];
        };
        match pseudo_element {
            Some(pseudo_element) => layout_data
                .pseudo_layout_data(pseudo_element)
                .map(|pseudo_layout_data| pseudo_layout_data.borrow().fragments())
                .unwrap_or_default(),
            None => layout_data.fragments(),
        }
    }

    fn repair_style(&self, context: &SharedStyleContext) {
        if let Some(layout_data) = self.inner_layout_data() {
            layout_data.repair_style(self, context);
        }
    }

    fn isolates_damage_for_damage_propagation(&self) -> bool {
        // Do not run incremental box and fragment tree layout at the `<body>` or root element as
        // there is some special processing that must happen for these elements and it currently
        // only happens when doing a full box tree construction traversal.
        if self.as_element().is_some_and(|element| {
            element.is_body_element_of_html_element_root() || element.is_root()
        }) {
            return false;
        }

        let Some(inner_layout_data) = self.inner_layout_data() else {
            return false;
        };
        let self_box = inner_layout_data.self_box.borrow();
        let Some(self_box) = &*self_box else {
            return false;
        };

        match self_box {
            LayoutBox::DisplayContents(..) => false,
            LayoutBox::BlockLevel(block_level) => matches!(
                &*block_level.borrow(),
                BlockLevelBox::Independent(..) |
                    BlockLevelBox::OutOfFlowFloatBox(..) |
                    BlockLevelBox::OutOfFlowAbsolutelyPositionedBox(..)
            ),
            LayoutBox::InlineLevel(inline_level) => matches!(
                inline_level,
                InlineItem::OutOfFlowAbsolutelyPositionedBox(..) | InlineItem::Atomic(..)
            ),
            LayoutBox::FlexLevel(..) => true,
            LayoutBox::TableLevelBox(table_level_box) => matches!(
                table_level_box,
                TableLevelBox::Cell(..) | TableLevelBox::Caption(..),
            ),
            LayoutBox::TaffyItemBox(..) => true,
        }
    }

    fn rebuild_box_tree_from_independent_formatting_context(
        &self,
        layout_context: &LayoutContext,
    ) -> bool {
        // Do not run incremental box tree layout at the `<body>` or root element as there
        // is some special processing that must happen for these elements and it currently
        // only happens when doing a full box tree construction traversal.
        if self.as_element().is_some_and(|element| {
            element.is_body_element_of_html_element_root() || element.is_root()
        }) {
            return false;
        }

        let layout_box = {
            let Some(mut inner_layout_data) = self.inner_layout_data_mut() else {
                return false;
            };
            inner_layout_data.pseudo_boxes.clear();
            inner_layout_data.self_box.clone()
        };

        let layout_box = layout_box.borrow();
        let Some(layout_box) = &*layout_box else {
            return false;
        };

        let info = NodeAndStyleInfo::new(*self, self.style(&layout_context.style_context));
        let box_style = info.style.get_box();
        let Display::GeneratingBox(display) = box_style.display.into() else {
            return false;
        };
        let contents = || {
            assert!(
                self.pseudo_element_chain().is_empty(),
                "Shouldn't try to rebuild box tree from a pseudo-element"
            );
            Contents::for_element(info.node, layout_context)
        };
        match layout_box {
            LayoutBox::DisplayContents(..) => false,
            LayoutBox::BlockLevel(block_level) => {
                let mut block_level = block_level.borrow_mut();
                match &mut *block_level {
                    BlockLevelBox::Independent(independent_formatting_context) => {
                        let DisplayGeneratingBox::OutsideInside {
                            outside: DisplayOutside::Block,
                            inside: display_inside,
                        } = display
                        else {
                            return false;
                        };
                        if !matches!(
                            BlockLevelCreator::new_for_inflow_block_level_element(
                                &info,
                                display_inside,
                                contents(),
                                independent_formatting_context.propagated_data,
                            ),
                            BlockLevelCreator::Independent { .. }
                        ) {
                            return false;
                        }
                        independent_formatting_context.rebuild(layout_context, &info);
                        true
                    },
                    BlockLevelBox::OutOfFlowFloatBox(float_box) => {
                        if !info.style.clone_float().is_floating() {
                            return false;
                        }
                        float_box.contents.rebuild(layout_context, &info);
                        true
                    },
                    BlockLevelBox::OutOfFlowAbsolutelyPositionedBox(positioned_box) => {
                        // Even if absolute positioning blockifies the outer display type, if the
                        // original display was inline-level, then the box needs to be handled as
                        // an inline-level in order to compute the static position correctly.
                        // See `BlockContainerBuilder::handle_absolutely_positioned_element()`.
                        if !info.style.clone_position().is_absolutely_positioned() ||
                            box_style.original_display.outside() != StyloDisplayOutside::Block
                        {
                            return false;
                        }
                        positioned_box
                            .borrow_mut()
                            .context
                            .rebuild(layout_context, &info);
                        true
                    },
                    _ => false,
                }
            },
            LayoutBox::InlineLevel(inline_level) => match inline_level {
                InlineItem::OutOfFlowAbsolutelyPositionedBox(positioned_box, ..) => {
                    if !info.style.clone_position().is_absolutely_positioned() {
                        return false;
                    }
                    positioned_box
                        .borrow_mut()
                        .context
                        .rebuild(layout_context, &info);
                    true
                },
                InlineItem::Atomic(atomic_box, _, _) => {
                    let flags = match contents() {
                        Contents::NonReplaced(_) => FragmentFlags::empty(),
                        Contents::Replaced(_) => FragmentFlags::IS_REPLACED,
                        Contents::Widget(_) => FragmentFlags::IS_WIDGET,
                    };
                    if !info.style.is_atomic_inline_level(flags) {
                        return false;
                    }
                    atomic_box.borrow_mut().rebuild(layout_context, &info);
                    true
                },
                _ => false,
            },
            LayoutBox::FlexLevel(flex_level_box) => {
                let mut flex_level_box = flex_level_box.borrow_mut();
                match &mut *flex_level_box {
                    FlexLevelBox::FlexItem(flex_item_box) => {
                        if info.style.clone_position().is_absolutely_positioned() ||
                            flex_item_box.style().clone_order() != info.style.clone_order()
                        {
                            return false;
                        }
                        flex_item_box
                            .independent_formatting_context
                            .rebuild(layout_context, &info)
                    },
                    FlexLevelBox::OutOfFlowAbsolutelyPositionedBox(positioned_box) => {
                        if !info.style.clone_position().is_absolutely_positioned() {
                            return false;
                        }
                        positioned_box
                            .borrow_mut()
                            .context
                            .rebuild(layout_context, &info);
                    },
                }
                true
            },
            LayoutBox::TableLevelBox(table_level_box) => match table_level_box {
                TableLevelBox::Caption(caption) => {
                    if display !=
                        DisplayGeneratingBox::LayoutInternal(DisplayLayoutInternal::TableCaption)
                    {
                        return false;
                    }
                    caption.borrow_mut().context.rebuild(layout_context, &info);
                    true
                },
                TableLevelBox::Cell(table_cell) => {
                    if display !=
                        DisplayGeneratingBox::LayoutInternal(DisplayLayoutInternal::TableCell)
                    {
                        return false;
                    }
                    table_cell
                        .borrow_mut()
                        .context
                        .rebuild(layout_context, &info);
                    true
                },
                _ => false,
            },
            LayoutBox::TaffyItemBox(..) => false,
        }
    }
}