Skip to main content

layout/
dom.rs

1/* This Source Code Form is subject to the terms of the Mozilla Public
2 * License, v. 2.0. If a copy of the MPL was not distributed with this
3 * file, You can obtain one at https://mozilla.org/MPL/2.0/. */
4
5use std::marker::PhantomData;
6
7use atomic_refcell::{AtomicRef, AtomicRefCell, AtomicRefMut};
8use layout_api::{
9    GenericLayoutDataTrait, LayoutDataTrait, LayoutElement, LayoutElementType, LayoutNode,
10    LayoutNodeType as ScriptLayoutNodeType, NodeRenderingType, SVGElementData,
11};
12use malloc_size_of_derive::MallocSizeOf;
13use script::layout_dom::ServoLayoutNode;
14use servo_arc::Arc as ServoArc;
15use smallvec::SmallVec;
16use style::context::SharedStyleContext;
17use style::properties::ComputedValues;
18use style::selector_parser::PseudoElement;
19use style::values::specified::box_::DisplayOutside as StyloDisplayOutside;
20use web_atoms::{local_name, ns};
21
22use crate::cell::{ArcRefCell, WeakRefCell};
23use crate::context::LayoutContext;
24use crate::dom_traversal::{Contents, NodeAndStyleInfo};
25use crate::flexbox::FlexLevelBox;
26use crate::flow::inline::text_run::TextRun;
27use crate::flow::inline::{InlineItem, SharedInlineStyles, WeakInlineItem};
28use crate::flow::{BlockLevelBox, BlockLevelCreator};
29use crate::fragment_tree::{Fragment, FragmentFlags};
30use crate::geom::PhysicalSize;
31use crate::layout_box_base::LayoutBoxBase;
32use crate::replaced::{CanvasInfo, IFrameInfo, ImageInfo, VideoInfo};
33use crate::style_ext::{
34    ComputedValuesExt, Display, DisplayGeneratingBox, DisplayLayoutInternal, DisplayOutside,
35};
36use crate::table::{TableLevelBox, WeakTableLevelBox};
37use crate::taffy::TaffyItemBox;
38
39#[derive(MallocSizeOf)]
40pub struct PseudoLayoutData {
41    pseudo: PseudoElement,
42    data: ArcRefCell<InnerDOMLayoutData>,
43}
44
45/// The data that is stored in each DOM node that is used by layout.
46#[derive(Default, MallocSizeOf)]
47pub struct InnerDOMLayoutData {
48    pub(super) self_box: ArcRefCell<Option<LayoutBox>>,
49    pub(super) pseudo_boxes: SmallVec<[PseudoLayoutData; 2]>,
50}
51
52impl InnerDOMLayoutData {
53    fn pseudo_layout_data(
54        &self,
55        pseudo_element: PseudoElement,
56    ) -> Option<ArcRefCell<InnerDOMLayoutData>> {
57        for pseudo_layout_data in self.pseudo_boxes.iter() {
58            if pseudo_element == pseudo_layout_data.pseudo {
59                return Some(pseudo_layout_data.data.clone());
60            }
61        }
62        None
63    }
64
65    fn create_pseudo_layout_data(
66        &mut self,
67        pseudo_element: PseudoElement,
68    ) -> ArcRefCell<InnerDOMLayoutData> {
69        let data: ArcRefCell<InnerDOMLayoutData> = Default::default();
70        self.pseudo_boxes.push(PseudoLayoutData {
71            pseudo: pseudo_element,
72            data: data.clone(),
73        });
74        data
75    }
76
77    fn fragments(&self) -> Vec<Fragment> {
78        self.self_box
79            .borrow()
80            .as_ref()
81            .and_then(|layout_box| layout_box.with_base(|base| base.fragments().clone()))
82            .unwrap_or_default()
83    }
84
85    fn repair_style(&self, node: &ServoLayoutNode, context: &SharedStyleContext) {
86        if let Some(layout_object) = &*self.self_box.borrow() {
87            layout_object.repair_style(context, node, &node.style(context));
88        }
89
90        for pseudo_layout_data in self.pseudo_boxes.iter() {
91            let Some(node_with_pseudo) = node.with_pseudo(pseudo_layout_data.pseudo) else {
92                continue;
93            };
94            pseudo_layout_data
95                .data
96                .borrow()
97                .repair_style(&node_with_pseudo, context);
98        }
99    }
100
101    fn with_layout_box_base(&self, callback: impl FnOnce(&LayoutBoxBase)) {
102        if let Some(data) = self.self_box.borrow().as_ref() {
103            data.with_base(callback);
104        }
105    }
106
107    fn with_layout_box_base_including_pseudos(&self, mut callback: impl FnMut(&LayoutBoxBase)) {
108        self.with_layout_box_base(&mut callback);
109        for pseudo_layout_data in self.pseudo_boxes.iter() {
110            pseudo_layout_data
111                .data
112                .borrow()
113                .with_layout_box_base(&mut callback);
114        }
115    }
116}
117
118/// A box that is stored in one of the `DOMLayoutData` slots.
119#[derive(Debug, MallocSizeOf)]
120pub(super) enum LayoutBox {
121    DisplayContents(SharedInlineStyles),
122    BlockLevel(ArcRefCell<BlockLevelBox>),
123    InlineLevel(InlineItem),
124    FlexLevel(ArcRefCell<FlexLevelBox>),
125    TableLevelBox(TableLevelBox),
126    TaffyItemBox(ArcRefCell<TaffyItemBox>),
127    Text(ArcRefCell<TextRun>),
128}
129
130impl LayoutBox {
131    pub(crate) fn with_base<T>(&self, callback: impl FnOnce(&LayoutBoxBase) -> T) -> Option<T> {
132        Some(match self {
133            LayoutBox::DisplayContents(..) | LayoutBox::Text(..) => return None,
134            LayoutBox::BlockLevel(block_level_box) => block_level_box.borrow().with_base(callback),
135            LayoutBox::InlineLevel(inline_item) => inline_item.with_base(callback),
136            LayoutBox::FlexLevel(flex_level_box) => flex_level_box.borrow().with_base(callback),
137            LayoutBox::TaffyItemBox(taffy_item_box) => taffy_item_box.borrow().with_base(callback),
138            LayoutBox::TableLevelBox(table_box) => table_box.with_base(callback),
139        })
140    }
141
142    pub(crate) fn with_base_mut<T>(
143        &mut self,
144        callback: impl FnOnce(&mut LayoutBoxBase) -> T,
145    ) -> Option<T> {
146        Some(match self {
147            LayoutBox::DisplayContents(..) | LayoutBox::Text(..) => return None,
148            LayoutBox::BlockLevel(block_level_box) => {
149                block_level_box.borrow_mut().with_base_mut(callback)
150            },
151            LayoutBox::InlineLevel(inline_item) => inline_item.with_base_mut(callback),
152            LayoutBox::FlexLevel(flex_level_box) => {
153                flex_level_box.borrow_mut().with_base_mut(callback)
154            },
155            LayoutBox::TaffyItemBox(taffy_item_box) => {
156                taffy_item_box.borrow_mut().with_base_mut(callback)
157            },
158            LayoutBox::TableLevelBox(table_box) => table_box.with_base_mut(callback),
159        })
160    }
161
162    fn repair_style(
163        &self,
164        context: &SharedStyleContext,
165        node: &ServoLayoutNode,
166        new_style: &ServoArc<ComputedValues>,
167    ) {
168        match self {
169            LayoutBox::DisplayContents(inline_shared_styles) => {
170                *inline_shared_styles.style.borrow_mut() = new_style.clone();
171                *inline_shared_styles.selected.borrow_mut() = node.selected_style(context);
172            },
173            LayoutBox::BlockLevel(block_level_box) => {
174                block_level_box
175                    .borrow_mut()
176                    .repair_style(context, node, new_style);
177            },
178            LayoutBox::InlineLevel(inline_item) => {
179                inline_item.repair_style(context, node, new_style);
180            },
181            LayoutBox::FlexLevel(flex_level_box) => flex_level_box
182                .borrow_mut()
183                .repair_style(context, node, new_style),
184            LayoutBox::TableLevelBox(table_level_box) => {
185                table_level_box.repair_style(context, node, new_style)
186            },
187            LayoutBox::TaffyItemBox(taffy_item_box) => taffy_item_box
188                .borrow_mut()
189                .repair_style(context, node, new_style),
190            LayoutBox::Text(..) => {
191                // There is nothing to update in this case.
192            },
193        }
194    }
195
196    fn attached_to_tree(&self, layout_box: WeakLayoutBox) {
197        match self {
198            Self::DisplayContents(_) => {
199                // This box can't have children, its contents get reparented to its parent.
200                // Therefore, no need to do anything.
201            },
202            Self::BlockLevel(block_level_box) => {
203                block_level_box.borrow().attached_to_tree(layout_box)
204            },
205            Self::InlineLevel(inline_item) => inline_item.attached_to_tree(layout_box),
206            Self::FlexLevel(flex_level_box) => flex_level_box.borrow().attached_to_tree(layout_box),
207            Self::TableLevelBox(table_level_box) => table_level_box.attached_to_tree(layout_box),
208            Self::TaffyItemBox(taffy_item_box) => {
209                taffy_item_box.borrow().attached_to_tree(layout_box)
210            },
211            Self::Text(..) => {
212                // This kind of box cannot have children, so no need to do anything.
213            },
214        }
215    }
216
217    fn downgrade(&self) -> WeakLayoutBox {
218        match self {
219            Self::DisplayContents(inline_shared_styles) => {
220                WeakLayoutBox::DisplayContents(inline_shared_styles.clone())
221            },
222            Self::BlockLevel(block_level_box) => {
223                WeakLayoutBox::BlockLevel(block_level_box.downgrade())
224            },
225            Self::InlineLevel(inline_item) => WeakLayoutBox::InlineLevel(inline_item.downgrade()),
226            Self::FlexLevel(flex_level_box) => WeakLayoutBox::FlexLevel(flex_level_box.downgrade()),
227            Self::TableLevelBox(table_level_box) => {
228                WeakLayoutBox::TableLevelBox(table_level_box.downgrade())
229            },
230            Self::TaffyItemBox(taffy_item_box) => {
231                WeakLayoutBox::TaffyItemBox(taffy_item_box.downgrade())
232            },
233            Self::Text(text_run) => WeakLayoutBox::Text(text_run.downgrade()),
234        }
235    }
236}
237
238#[derive(Clone, Debug, MallocSizeOf)]
239pub(super) enum WeakLayoutBox {
240    DisplayContents(SharedInlineStyles),
241    BlockLevel(WeakRefCell<BlockLevelBox>),
242    InlineLevel(WeakInlineItem),
243    FlexLevel(WeakRefCell<FlexLevelBox>),
244    TableLevelBox(WeakTableLevelBox),
245    TaffyItemBox(WeakRefCell<TaffyItemBox>),
246    Text(WeakRefCell<TextRun>),
247}
248
249impl WeakLayoutBox {
250    pub(crate) fn upgrade(&self) -> Option<LayoutBox> {
251        Some(match self {
252            Self::DisplayContents(inline_shared_styles) => {
253                LayoutBox::DisplayContents(inline_shared_styles.clone())
254            },
255            Self::BlockLevel(block_level_box) => LayoutBox::BlockLevel(block_level_box.upgrade()?),
256            Self::InlineLevel(inline_item) => LayoutBox::InlineLevel(inline_item.upgrade()?),
257            Self::FlexLevel(flex_level_box) => LayoutBox::FlexLevel(flex_level_box.upgrade()?),
258            Self::TableLevelBox(table_level_box) => {
259                LayoutBox::TableLevelBox(table_level_box.upgrade()?)
260            },
261            Self::TaffyItemBox(taffy_item_box) => {
262                LayoutBox::TaffyItemBox(taffy_item_box.upgrade()?)
263            },
264            Self::Text(text_run) => LayoutBox::Text(text_run.upgrade()?),
265        })
266    }
267}
268
269/// A wrapper for [`InnerDOMLayoutData`]. This is necessary to give the entire data
270/// structure interior mutability, as we will need to mutate the layout data of
271/// non-mutable DOM nodes.
272#[derive(Default, MallocSizeOf)]
273pub struct DOMLayoutData(AtomicRefCell<InnerDOMLayoutData>);
274
275// The implementation of this trait allows the data to be stored in the DOM.
276impl LayoutDataTrait for DOMLayoutData {}
277impl GenericLayoutDataTrait for DOMLayoutData {
278    fn as_any(&self) -> &dyn std::any::Any {
279        self
280    }
281}
282
283pub struct BoxSlot<'dom> {
284    pub(crate) slot: ArcRefCell<Option<LayoutBox>>,
285    pub(crate) marker: PhantomData<&'dom ()>,
286}
287
288impl From<ArcRefCell<Option<LayoutBox>>> for BoxSlot<'_> {
289    fn from(slot: ArcRefCell<Option<LayoutBox>>) -> Self {
290        Self {
291            slot,
292            marker: PhantomData,
293        }
294    }
295}
296
297/// A mutable reference to a `LayoutBox` stored in a DOM element.
298impl BoxSlot<'_> {
299    pub(crate) fn set(self, layout_box: LayoutBox) {
300        layout_box.attached_to_tree(layout_box.downgrade());
301        *self.slot.borrow_mut() = Some(layout_box);
302    }
303
304    pub(crate) fn take_layout_box(&self) -> Option<LayoutBox> {
305        self.slot.borrow_mut().take()
306    }
307
308    /// Call [`Self::take_layout_box`] and try to unwrap it into a [`TextRun`], returning `None` if
309    /// the slot is empty or it does not contain a [`TextRun`]. Note that this will *always* clear
310    /// the [`BoxSlot`].
311    pub(crate) fn take_layout_box_as_text_run(&self) -> Option<ArcRefCell<TextRun>> {
312        match self.take_layout_box()? {
313            LayoutBox::Text(old_text_run) => Some(old_text_run),
314            _ => None,
315        }
316    }
317}
318
319impl Drop for BoxSlot<'_> {
320    fn drop(&mut self) {
321        if !std::thread::panicking() {
322            assert!(self.slot.borrow().is_some(), "failed to set a layout box");
323        }
324    }
325}
326
327pub(crate) trait NodeExt<'dom> {
328    /// Returns the relevant data wrapping into respective struct and its size in pixels.
329    fn as_image(&self) -> Option<(ImageInfo, PhysicalSize<f64>)>;
330    fn as_canvas(&self) -> Option<(CanvasInfo, PhysicalSize<f64>)>;
331    fn as_iframe(&self) -> Option<IFrameInfo>;
332    fn as_video(&self) -> Option<(VideoInfo, Option<PhysicalSize<f64>>)>;
333    fn as_svg(&self) -> Option<SVGElementData<'dom>>;
334    fn as_typeless_object_with_data_attribute(&self) -> Option<String>;
335
336    fn ensure_inner_layout_data(&self) -> AtomicRefMut<'dom, InnerDOMLayoutData>;
337    fn inner_layout_data(&self) -> Option<AtomicRef<'dom, InnerDOMLayoutData>>;
338    fn inner_layout_data_mut(&self) -> Option<AtomicRefMut<'dom, InnerDOMLayoutData>>;
339    fn box_slot(&self) -> BoxSlot<'dom>;
340
341    /// Remove boxes for the element itself, and all of its pseudo-element boxes.
342    fn unset_all_boxes(&self);
343
344    /// Returns the [`NodeRenderingType`] for this [`LayoutNode`] which describes whether
345    /// the node is being rendered, delegating rendering, or not being rendered at all
346    /// based on whether it has a [`LayoutBox`] and what kind.
347    fn rendering_type(&self) -> NodeRenderingType;
348
349    fn fragments_for_pseudo(&self, pseudo_element: Option<PseudoElement>) -> Vec<Fragment>;
350    fn with_layout_box_base(&self, callback: impl FnMut(&LayoutBoxBase));
351    fn with_layout_box_base_including_pseudos(&self, callback: impl FnMut(&LayoutBoxBase));
352
353    fn repair_style(&self, context: &SharedStyleContext);
354
355    /// Whether or not this node isolates downward flowing box tree rebuild damage and
356    /// fragment tree layout cache damage. Roughly, this corresponds to independent
357    /// formatting context boundaries.
358    ///
359    /// - The node's boxes themselves will be rebuilt, but not the descendant node's
360    ///   boxes.
361    /// - The node's fragment tree layout will be rebuilt, not the descendent node's
362    ///   fragment tree layout cache.
363    ///
364    /// When this node has no box yet, `false` is returned.
365    fn isolates_damage_for_damage_propagation(&self) -> bool;
366
367    /// Try to re-run box tree reconstruction from this point. This can succeed if the
368    /// node itself is still valid and isolates box tree damage from ancestors (for
369    /// instance, if it starts an independent formatting context). **Note:** This assumes
370    /// that no ancestors have box damage.
371    ///
372    /// Returns `true` if box tree reconstruction was sucessful and `false` otherwise.
373    fn rebuild_box_tree_from_independent_formatting_context(
374        &self,
375        layout_context: &LayoutContext,
376    ) -> bool;
377
378    /// Whether or not the style of this node indicates that it should be absolutely
379    /// positioned.
380    fn is_absolutely_positioned(&self) -> bool;
381}
382
383impl<'dom> NodeExt<'dom> for ServoLayoutNode<'dom> {
384    fn as_image(&self) -> Option<(ImageInfo, PhysicalSize<f64>)> {
385        let (resource, metadata) = self.image_data()?;
386        let width = metadata.map(|metadata| metadata.width).unwrap_or_default();
387        let height = metadata.map(|metadata| metadata.height).unwrap_or_default();
388        let (mut width, mut height) = (width as f64, height as f64);
389        // Take `image_density` into account for calculating the size in pixels for images.
390        if let Some(density) = self.image_density().filter(|density| *density != 1.) {
391            width /= density;
392            height /= density;
393        }
394        Some((
395            ImageInfo {
396                image: resource,
397                showing_broken_image_icon: self.showing_broken_image_icon(),
398                url: self.image_url(),
399            },
400            PhysicalSize::new(width, height),
401        ))
402    }
403
404    fn as_svg(&self) -> Option<SVGElementData<'dom>> {
405        self.svg_data()
406    }
407
408    fn as_video(&self) -> Option<(VideoInfo, Option<PhysicalSize<f64>>)> {
409        let data = self.media_data()?;
410        let natural_size = if let Some(frame) = data.current_frame {
411            Some(PhysicalSize::new(frame.width.into(), frame.height.into()))
412        } else {
413            data.metadata
414                .map(|meta| PhysicalSize::new(meta.width.into(), meta.height.into()))
415        };
416        Some((
417            VideoInfo {
418                image_key: data.current_frame.map(|frame| frame.image_key),
419            },
420            natural_size,
421        ))
422    }
423
424    fn as_canvas(&self) -> Option<(CanvasInfo, PhysicalSize<f64>)> {
425        let canvas_data = self.canvas_data()?;
426        let source = canvas_data.image_key;
427        Some((
428            CanvasInfo { source },
429            PhysicalSize::new(canvas_data.width.into(), canvas_data.height.into()),
430        ))
431    }
432
433    fn as_iframe(&self) -> Option<IFrameInfo> {
434        match (self.iframe_pipeline_id(), self.iframe_browsing_context_id()) {
435            (Some(pipeline_id), Some(browsing_context_id)) => Some(IFrameInfo {
436                pipeline_id,
437                browsing_context_id,
438            }),
439            _ => None,
440        }
441    }
442
443    fn as_typeless_object_with_data_attribute(&self) -> Option<String> {
444        if self.type_id() !=
445            Some(ScriptLayoutNodeType::Element(
446                LayoutElementType::HTMLObjectElement,
447            ))
448        {
449            return None;
450        }
451
452        // TODO: This is the what the legacy layout system did, but really if Servo
453        // supports any `<object>` that's an image, it should support those with URLs
454        // and `type` attributes with image mime types.
455        let element = self.as_element()?;
456        if element.attribute(&ns!(), &local_name!("type")).is_some() {
457            return None;
458        }
459        element
460            .attribute_as_str(&ns!(), &local_name!("data"))
461            .map(|string| string.to_owned())
462    }
463
464    fn ensure_inner_layout_data(&self) -> AtomicRefMut<'dom, InnerDOMLayoutData> {
465        if self.layout_data().is_none() {
466            self.initialize_layout_data::<DOMLayoutData>();
467        }
468        self.layout_data()
469            .unwrap()
470            .as_any()
471            .downcast_ref::<DOMLayoutData>()
472            .unwrap()
473            .0
474            .borrow_mut()
475    }
476
477    fn inner_layout_data(&self) -> Option<AtomicRef<'dom, InnerDOMLayoutData>> {
478        self.layout_data().map(|data| {
479            data.as_any()
480                .downcast_ref::<DOMLayoutData>()
481                .unwrap()
482                .0
483                .borrow()
484        })
485    }
486
487    fn inner_layout_data_mut(&self) -> Option<AtomicRefMut<'dom, InnerDOMLayoutData>> {
488        self.layout_data().map(|data| {
489            data.as_any()
490                .downcast_ref::<DOMLayoutData>()
491                .unwrap()
492                .0
493                .borrow_mut()
494        })
495    }
496
497    fn box_slot(&self) -> BoxSlot<'dom> {
498        let pseudo_element_chain = self.pseudo_element_chain();
499        let Some(primary) = pseudo_element_chain.primary else {
500            return self.ensure_inner_layout_data().self_box.clone().into();
501        };
502
503        let Some(secondary) = pseudo_element_chain.secondary else {
504            let primary_layout_data = self
505                .ensure_inner_layout_data()
506                .create_pseudo_layout_data(primary);
507            return primary_layout_data.borrow().self_box.clone().into();
508        };
509
510        // It's *very* important that this not borrow the element's main
511        // `InnerLayoutData`. Primary pseudo-elements are processed at the same recursion
512        // level as the main data, so the `BoxSlot` is created sequentially with other
513        // primary pseudo-elements and the element itself. The secondary pseudo-element is
514        // one level deep, so could be happening in parallel with the primary
515        // pseudo-elements or main element layout.
516        let primary_layout_data = self
517            .inner_layout_data()
518            .expect("Should already have element InnerLayoutData here.")
519            .pseudo_layout_data(primary)
520            .expect("Should already have primary pseudo-element InnerLayoutData here");
521        let secondary_layout_data = primary_layout_data
522            .borrow_mut()
523            .create_pseudo_layout_data(secondary);
524        secondary_layout_data.borrow().self_box.clone().into()
525    }
526
527    fn unset_all_boxes(&self) {
528        let mut layout_data = self.ensure_inner_layout_data();
529        *layout_data.self_box.borrow_mut() = None;
530        layout_data.pseudo_boxes.clear();
531
532        // Stylo already takes care of removing all layout data
533        // for DOM descendants of elements with `display: none`.
534    }
535
536    fn rendering_type(&self) -> NodeRenderingType {
537        let Some(layout_data) = self.inner_layout_data() else {
538            return NodeRenderingType::NotRendered;
539        };
540        match &*layout_data.self_box.borrow() {
541            Some(LayoutBox::DisplayContents(..)) => NodeRenderingType::DelegatesRendering,
542            Some(..) => NodeRenderingType::Rendered,
543            None => NodeRenderingType::NotRendered,
544        }
545    }
546
547    fn with_layout_box_base(&self, callback: impl FnMut(&LayoutBoxBase)) {
548        if let Some(inner_layout_data) = self.inner_layout_data() {
549            inner_layout_data.with_layout_box_base(callback);
550        }
551    }
552
553    fn with_layout_box_base_including_pseudos(&self, callback: impl FnMut(&LayoutBoxBase)) {
554        if let Some(inner_layout_data) = self.inner_layout_data() {
555            inner_layout_data.with_layout_box_base_including_pseudos(callback);
556        }
557    }
558
559    fn fragments_for_pseudo(&self, pseudo_element: Option<PseudoElement>) -> Vec<Fragment> {
560        let Some(layout_data) = self.inner_layout_data() else {
561            return vec![];
562        };
563        match pseudo_element {
564            Some(pseudo_element) => layout_data
565                .pseudo_layout_data(pseudo_element)
566                .map(|pseudo_layout_data| pseudo_layout_data.borrow().fragments())
567                .unwrap_or_default(),
568            None => layout_data.fragments(),
569        }
570    }
571
572    fn repair_style(&self, context: &SharedStyleContext) {
573        if let Some(layout_data) = self.inner_layout_data() {
574            layout_data.repair_style(self, context);
575        }
576    }
577
578    fn isolates_damage_for_damage_propagation(&self) -> bool {
579        // Do not run incremental box and fragment tree layout at the `<body>` or root element as
580        // there is some special processing that must happen for these elements and it currently
581        // only happens when doing a full box tree construction traversal.
582        if self.as_element().is_some_and(|element| {
583            element.is_body_element_of_html_element_root() || element.is_root()
584        }) {
585            return false;
586        }
587
588        let Some(inner_layout_data) = self.inner_layout_data() else {
589            return false;
590        };
591        let self_box = inner_layout_data.self_box.borrow();
592        let Some(self_box) = &*self_box else {
593            return false;
594        };
595
596        match self_box {
597            LayoutBox::DisplayContents(..) => false,
598            LayoutBox::BlockLevel(block_level) => matches!(
599                &*block_level.borrow(),
600                BlockLevelBox::Independent(..) |
601                    BlockLevelBox::OutOfFlowFloatBox(..) |
602                    BlockLevelBox::OutOfFlowAbsolutelyPositionedBox(..)
603            ),
604            LayoutBox::InlineLevel(inline_level) => matches!(
605                inline_level,
606                InlineItem::OutOfFlowAbsolutelyPositionedBox(..) | InlineItem::Atomic(..)
607            ),
608            LayoutBox::FlexLevel(..) => true,
609            LayoutBox::TableLevelBox(table_level_box) => matches!(
610                table_level_box,
611                TableLevelBox::Cell(..) | TableLevelBox::Caption(..),
612            ),
613            LayoutBox::TaffyItemBox(..) => true,
614            LayoutBox::Text(..) => unreachable!("An element should never be a text node"),
615        }
616    }
617
618    fn rebuild_box_tree_from_independent_formatting_context(
619        &self,
620        layout_context: &LayoutContext,
621    ) -> bool {
622        // Do not run incremental box tree layout at the `<body>` or root element as there
623        // is some special processing that must happen for these elements and it currently
624        // only happens when doing a full box tree construction traversal.
625        if self.as_element().is_some_and(|element| {
626            element.is_body_element_of_html_element_root() || element.is_root()
627        }) {
628            return false;
629        }
630
631        let layout_box = {
632            let Some(mut inner_layout_data) = self.inner_layout_data_mut() else {
633                return false;
634            };
635            inner_layout_data.pseudo_boxes.clear();
636            inner_layout_data.self_box.clone()
637        };
638
639        let layout_box = layout_box.borrow();
640        let Some(layout_box) = &*layout_box else {
641            return false;
642        };
643
644        let info = NodeAndStyleInfo::new(*self, self.style(&layout_context.style_context));
645        let box_style = info.style.get_box();
646        let Display::GeneratingBox(display) = box_style.display.into() else {
647            return false;
648        };
649        let contents = || {
650            assert!(
651                self.pseudo_element_chain().is_empty(),
652                "Shouldn't try to rebuild box tree from a pseudo-element"
653            );
654            Contents::for_element(info.node, layout_context)
655        };
656        match layout_box {
657            LayoutBox::DisplayContents(..) => false,
658            LayoutBox::BlockLevel(block_level) => {
659                let mut block_level = block_level.borrow_mut();
660                match &mut *block_level {
661                    BlockLevelBox::Independent(independent_formatting_context) => {
662                        let DisplayGeneratingBox::OutsideInside {
663                            outside: DisplayOutside::Block,
664                            inside: display_inside,
665                        } = display
666                        else {
667                            return false;
668                        };
669                        if !matches!(
670                            BlockLevelCreator::new_for_inflow_block_level_element(
671                                &info,
672                                display_inside,
673                                contents(),
674                                independent_formatting_context.propagated_data,
675                            ),
676                            BlockLevelCreator::Independent { .. }
677                        ) {
678                            return false;
679                        }
680                        independent_formatting_context.rebuild(layout_context, &info);
681                        true
682                    },
683                    BlockLevelBox::OutOfFlowFloatBox(float_box) => {
684                        if !info.style.clone_float().is_floating() {
685                            return false;
686                        }
687                        float_box.contents.rebuild(layout_context, &info);
688                        true
689                    },
690                    BlockLevelBox::OutOfFlowAbsolutelyPositionedBox(positioned_box) => {
691                        // Even if absolute positioning blockifies the outer display type, if the
692                        // original display was inline-level, then the box needs to be handled as
693                        // an inline-level in order to compute the static position correctly.
694                        // See `BlockContainerBuilder::handle_absolutely_positioned_element()`.
695                        if !info.style.clone_position().is_absolutely_positioned() ||
696                            box_style.original_display.outside() != StyloDisplayOutside::Block
697                        {
698                            return false;
699                        }
700                        positioned_box
701                            .borrow_mut()
702                            .context
703                            .rebuild(layout_context, &info);
704                        true
705                    },
706                    _ => false,
707                }
708            },
709            LayoutBox::InlineLevel(inline_level) => match inline_level {
710                InlineItem::OutOfFlowAbsolutelyPositionedBox(positioned_box, ..) => {
711                    if !info.style.clone_position().is_absolutely_positioned() {
712                        return false;
713                    }
714                    positioned_box
715                        .borrow_mut()
716                        .context
717                        .rebuild(layout_context, &info);
718                    true
719                },
720                InlineItem::Atomic(atomic_box, _, _) => {
721                    let flags = match contents() {
722                        Contents::NonReplaced(_) => FragmentFlags::empty(),
723                        Contents::Replaced(_) => FragmentFlags::IS_REPLACED,
724                        Contents::Widget(_) => FragmentFlags::IS_WIDGET,
725                    };
726                    if !info.style.is_atomic_inline_level(flags) {
727                        return false;
728                    }
729                    atomic_box.borrow_mut().rebuild(layout_context, &info);
730                    true
731                },
732                _ => false,
733            },
734            LayoutBox::FlexLevel(flex_level_box) => {
735                let mut flex_level_box = flex_level_box.borrow_mut();
736                match &mut *flex_level_box {
737                    FlexLevelBox::FlexItem(flex_item_box) => {
738                        if info.style.clone_position().is_absolutely_positioned() ||
739                            flex_item_box.style().clone_order() != info.style.clone_order()
740                        {
741                            return false;
742                        }
743                        flex_item_box
744                            .independent_formatting_context
745                            .rebuild(layout_context, &info)
746                    },
747                    FlexLevelBox::OutOfFlowAbsolutelyPositionedBox(positioned_box) => {
748                        if !info.style.clone_position().is_absolutely_positioned() {
749                            return false;
750                        }
751                        positioned_box
752                            .borrow_mut()
753                            .context
754                            .rebuild(layout_context, &info);
755                    },
756                }
757                true
758            },
759            LayoutBox::TableLevelBox(table_level_box) => match table_level_box {
760                TableLevelBox::Caption(caption) => {
761                    if display !=
762                        DisplayGeneratingBox::LayoutInternal(DisplayLayoutInternal::TableCaption)
763                    {
764                        return false;
765                    }
766                    caption.borrow_mut().context.rebuild(layout_context, &info);
767                    true
768                },
769                TableLevelBox::Cell(table_cell) => {
770                    if display !=
771                        DisplayGeneratingBox::LayoutInternal(DisplayLayoutInternal::TableCell)
772                    {
773                        return false;
774                    }
775                    table_cell
776                        .borrow_mut()
777                        .context
778                        .rebuild(layout_context, &info);
779                    true
780                },
781                _ => false,
782            },
783            LayoutBox::TaffyItemBox(..) => false,
784            LayoutBox::Text(..) => unreachable!("An element should never be a text node"),
785        }
786    }
787
788    fn is_absolutely_positioned(&self) -> bool {
789        self.as_element().is_some_and(|element| {
790            element
791                .element_data()
792                .styles
793                .primary()
794                .clone_position()
795                .is_absolutely_positioned()
796        })
797    }
798}