layout/fragment_tree/
box_fragment.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 app_units::{Au, MAX_AU, MIN_AU};
6use atomic_refcell::AtomicRefCell;
7use base::id::ScrollTreeNodeId;
8use base::print_tree::PrintTree;
9use euclid::Rect;
10use malloc_size_of_derive::MallocSizeOf;
11use servo_arc::Arc as ServoArc;
12use servo_geometry::f32_rect_to_au_rect;
13use style::Zero;
14use style::computed_values::border_collapse::T as BorderCollapse;
15use style::computed_values::overflow_x::T as ComputedOverflow;
16use style::computed_values::position::T as ComputedPosition;
17use style::logical_geometry::WritingMode;
18use style::properties::ComputedValues;
19
20use super::{BaseFragment, BaseFragmentInfo, CollapsedBlockMargins, Fragment, FragmentFlags};
21use crate::SharedStyle;
22use crate::display_list::ToWebRender;
23use crate::formatting_contexts::Baselines;
24use crate::fragment_tree::BaseFragmentStyleRef;
25use crate::geom::{
26    AuOrAuto, LengthPercentageOrAuto, PhysicalPoint, PhysicalRect, PhysicalSides, ToLogical,
27};
28use crate::style_ext::ComputedValuesExt;
29use crate::table::SpecificTableGridInfo;
30use crate::taffy::SpecificTaffyGridInfo;
31
32/// Describes how a [`BoxFragment`] paints its background.
33#[derive(MallocSizeOf)]
34pub(crate) enum BackgroundMode {
35    /// Draw the normal [`BoxFragment`] background as well as the extra backgrounds
36    /// based on the style and positioning rectangles in this data structure.
37    Extra(Vec<ExtraBackground>),
38    /// Do not draw a background for this Fragment. This is used for elements like
39    /// table tracks and table track groups, which rely on cells to paint their
40    /// backgrounds.
41    None,
42    /// Draw the background normally, getting information from the Fragment style.
43    Normal,
44}
45#[derive(MallocSizeOf)]
46pub(crate) struct ExtraBackground {
47    pub style: SharedStyle,
48    pub rect: PhysicalRect<Au>,
49}
50
51#[derive(Clone, Debug, MallocSizeOf)]
52pub(crate) enum SpecificLayoutInfo {
53    Grid(Box<SpecificTaffyGridInfo>),
54    TableCellWithCollapsedBorders,
55    TableGridWithCollapsedBorders(Box<SpecificTableGridInfo>),
56    TableWrapper,
57}
58
59#[derive(MallocSizeOf)]
60pub(crate) struct BlockLevelLayoutInfo {
61    /// When the `clear` property is not set to `none`, it may introduce clearance.
62    /// Clearance is some extra spacing that is added above the top margin,
63    /// so that the element doesn't overlap earlier floats in the same BFC.
64    /// The presence of clearance prevents the top margin from collapsing with
65    /// earlier margins or with the bottom margin of the parent block.
66    /// <https://drafts.csswg.org/css2/#clearance>
67    pub clearance: Option<Au>,
68
69    pub block_margins_collapsed_with_children: CollapsedBlockMargins,
70}
71
72#[derive(MallocSizeOf)]
73pub(crate) struct BoxFragmentRareData {
74    /// Information that is specific to a layout system (e.g., grid, table, etc.).
75    pub specific_layout_info: Option<SpecificLayoutInfo>,
76}
77
78impl BoxFragmentRareData {
79    /// Create a new rare data based on information given to the fragment. Ideally, We should
80    /// avoid creating rare data as much as possible to reduce the memory cost.
81    fn try_boxed_from(specific_layout_info: Option<SpecificLayoutInfo>) -> Option<Box<Self>> {
82        specific_layout_info.map(|info| {
83            Box::new(BoxFragmentRareData {
84                specific_layout_info: Some(info),
85            })
86        })
87    }
88}
89
90#[derive(MallocSizeOf)]
91pub(crate) struct BoxFragment {
92    pub base: BaseFragment,
93
94    pub children: Vec<Fragment>,
95
96    /// This [`BoxFragment`]'s containing block rectangle in coordinates relative to
97    /// the initial containing block, but not taking into account any transforms.
98    pub cumulative_containing_block_rect: PhysicalRect<Au>,
99
100    pub padding: PhysicalSides<Au>,
101    pub border: PhysicalSides<Au>,
102    pub margin: PhysicalSides<Au>,
103
104    /// When this [`BoxFragment`] is for content that has a baseline, this tracks
105    /// the first and last baselines of that content. This is used to propagate baselines
106    /// to things such as tables and inline formatting contexts.
107    baselines: Baselines,
108
109    /// The scrollable overflow of this box fragment in the same coordiante system as
110    /// [`Self::content_rect`] ie a rectangle within the parent fragment's content
111    /// rectangle. This does not take into account any transforms this fragment applies.
112    /// This is handled when calling [`Self::scrollable_overflow_for_parent`].
113    scrollable_overflow: Option<PhysicalRect<Au>>,
114
115    /// The resolved box insets if this box is `position: sticky`. These are calculated
116    /// during `StackingContextTree` construction because they rely on the size of the
117    /// scroll container.
118    pub(crate) resolved_sticky_insets: AtomicRefCell<Option<PhysicalSides<AuOrAuto>>>,
119
120    pub background_mode: BackgroundMode,
121
122    /// Rare data that not all kinds of [`BoxFragment`] would have.
123    pub rare_data: Option<Box<BoxFragmentRareData>>,
124
125    /// Additional information for block-level boxes.
126    pub block_level_layout_info: Option<Box<BlockLevelLayoutInfo>>,
127
128    /// The containing spatial tree node of this [`BoxFragment`]. This is assigned during
129    /// `StackingContextTree` construction, so isn't available before that time. This is
130    /// used to for determining final viewport size and position of this node and will
131    /// also be used in the future for hit testing.
132    pub spatial_tree_node: AtomicRefCell<Option<ScrollTreeNodeId>>,
133}
134
135impl BoxFragment {
136    #[allow(clippy::too_many_arguments)]
137    pub fn new(
138        base_fragment_info: BaseFragmentInfo,
139        style: ServoArc<ComputedValues>,
140        children: Vec<Fragment>,
141        content_rect: PhysicalRect<Au>,
142        padding: PhysicalSides<Au>,
143        border: PhysicalSides<Au>,
144        margin: PhysicalSides<Au>,
145        specific_layout_info: Option<SpecificLayoutInfo>,
146    ) -> BoxFragment {
147        let rare_data = BoxFragmentRareData::try_boxed_from(specific_layout_info);
148
149        BoxFragment {
150            base: BaseFragment::new(base_fragment_info, style.into(), content_rect),
151            children,
152            cumulative_containing_block_rect: Default::default(),
153            padding,
154            border,
155            margin,
156            baselines: Baselines::default(),
157            scrollable_overflow: None,
158            resolved_sticky_insets: AtomicRefCell::default(),
159            background_mode: BackgroundMode::Normal,
160            rare_data,
161            block_level_layout_info: None,
162            spatial_tree_node: AtomicRefCell::default(),
163        }
164    }
165
166    pub fn with_baselines(mut self, baselines: Baselines) -> Self {
167        self.baselines = baselines;
168        self
169    }
170
171    pub(crate) fn style<'a>(&'a self) -> BaseFragmentStyleRef<'a> {
172        self.base.style()
173    }
174
175    /// Get the baselines for this [`BoxFragment`] if they are compatible with the given [`WritingMode`].
176    /// If they are not compatible, [`Baselines::default()`] is returned.
177    pub fn baselines(&self, writing_mode: WritingMode) -> Baselines {
178        let style = self.style();
179        let mut baselines = if writing_mode.is_horizontal() == style.writing_mode.is_horizontal() {
180            self.baselines
181        } else {
182            // If the writing mode of the container requesting baselines is not
183            // compatible, ensure that the baselines established by this fragment are
184            // not used.
185            Baselines::default()
186        };
187
188        // From the https://drafts.csswg.org/css-align-3/#baseline-export section on "block containers":
189        // > However, for legacy reasons if its baseline-source is auto (the initial
190        // > value) a block-level or inline-level block container that is a scroll container
191        // > always has a last baseline set, whose baselines all correspond to its block-end
192        // > margin edge.
193        //
194        // This applies even if there is no baseline set, so we unconditionally set the value here
195        // and ignore anything that is set via [`Self::with_baselines`].
196        if style.establishes_scroll_container(self.base.flags) {
197            let content_rect_size = self.content_rect().size.to_logical(writing_mode);
198            let padding = self.padding.to_logical(writing_mode);
199            let border = self.border.to_logical(writing_mode);
200            let margin = self.margin.to_logical(writing_mode);
201            baselines.last = Some(
202                content_rect_size.block + padding.block_end + border.block_end + margin.block_end,
203            )
204        }
205        baselines
206    }
207
208    pub fn add_extra_background(&mut self, extra_background: ExtraBackground) {
209        match self.background_mode {
210            BackgroundMode::Extra(ref mut backgrounds) => backgrounds.push(extra_background),
211            _ => self.background_mode = BackgroundMode::Extra(vec![extra_background]),
212        }
213    }
214
215    pub fn set_does_not_paint_background(&mut self) {
216        self.background_mode = BackgroundMode::None;
217    }
218
219    pub fn specific_layout_info(&self) -> Option<&SpecificLayoutInfo> {
220        self.rare_data.as_ref()?.specific_layout_info.as_ref()
221    }
222
223    pub fn with_block_level_layout_info(
224        mut self,
225        block_margins_collapsed_with_children: CollapsedBlockMargins,
226        clearance: Option<Au>,
227    ) -> Self {
228        self.block_level_layout_info = Some(Box::new(BlockLevelLayoutInfo {
229            block_margins_collapsed_with_children,
230            clearance,
231        }));
232        self
233    }
234
235    /// Get the scrollable overflow for this [`BoxFragment`] relative to its
236    /// containing block.
237    pub fn scrollable_overflow(&self) -> PhysicalRect<Au> {
238        self.scrollable_overflow
239            .expect("Should only call `scrollable_overflow()` after calculating overflow")
240    }
241
242    /// This is an implementation of:
243    /// - <https://drafts.csswg.org/css-overflow-3/#scrollable>.
244    /// - <https://drafts.csswg.org/cssom-view/#scrolling-area>
245    pub(crate) fn calculate_scrollable_overflow(&mut self) {
246        let physical_padding_rect = self.padding_rect();
247        let content_origin = self.base.rect.origin.to_vector();
248
249        // > The scrollable overflow area is the union of:
250        // > * The scroll container’s own padding box.
251        // > * All line boxes directly contained by the scroll container.
252        // > * The border boxes of all boxes for which it is the containing block and
253        // >   whose border boxes are positioned not wholly in the unreachable
254        // >   scrollable overflow region, accounting for transforms by projecting
255        // >   each box onto the plane of the element that establishes its 3D
256        // >   rendering context.
257        // > * The margin areas of grid item and flex item boxes for which the box
258        // >   establishes a containing block.
259        // > * The scrollable overflow areas of all of the above boxes (including zero-area
260        // >   boxes and accounting for transforms as described above), provided they
261        // >   themselves have overflow: visible (i.e. do not themselves trap the overflow)
262        // >   and that scrollable overflow is not already clipped (e.g. by the clip property
263        // >   or the contain property).
264        // > * Additional padding added to the scrollable overflow rectangle as necessary
265        //     to enable scroll positions that satisfy the requirements of both place-content:
266        //     start and place-content: end alignment.
267        //
268        // TODO(mrobinson): Below we are handling the border box and the scrollable
269        // overflow together, but from the specification it seems that if the border
270        // box of an item is in the "wholly unreachable scrollable overflow region", but
271        // its scrollable overflow is not, it should also be excluded.
272        let scrollable_overflow = self
273            .children
274            .iter()
275            .fold(physical_padding_rect, |acc, child| {
276                let scrollable_overflow_from_child = child
277                    .calculate_scrollable_overflow_for_parent()
278                    .translate(content_origin);
279
280                // Note that this doesn't just exclude scrollable overflow outside the
281                // wholly unrechable scrollable overflow area, but also clips it. This
282                // makes the resulting value more like the "scroll area" rather than the
283                // "scrollable overflow."
284                let scrollable_overflow_from_child = self
285                    .clip_wholly_unreachable_scrollable_overflow(
286                        scrollable_overflow_from_child,
287                        physical_padding_rect,
288                    );
289                acc.union(&scrollable_overflow_from_child)
290            });
291
292        // Fragments with `IS_COLLAPSED` (currently only table cells that are part of
293        // table tracks with `visibility: collapse`) should not contribute to scrollable
294        // overflow. This behavior matches Chrome, but not Firefox.
295        // See https://github.com/w3c/csswg-drafts/issues/12689
296        if self.base.flags.contains(FragmentFlags::IS_COLLAPSED) {
297            self.scrollable_overflow = Some(Rect::zero());
298            return;
299        }
300
301        self.scrollable_overflow = Some(scrollable_overflow)
302    }
303
304    pub(crate) fn set_containing_block(&mut self, containing_block: &PhysicalRect<Au>) {
305        self.cumulative_containing_block_rect = *containing_block;
306    }
307
308    pub fn offset_by_containing_block(&self, rect: &PhysicalRect<Au>) -> PhysicalRect<Au> {
309        rect.translate(self.cumulative_containing_block_rect.origin.to_vector())
310    }
311
312    pub(crate) fn cumulative_content_box_rect(&self) -> PhysicalRect<Au> {
313        self.offset_by_containing_block(&self.base.rect)
314    }
315
316    pub(crate) fn cumulative_padding_box_rect(&self) -> PhysicalRect<Au> {
317        self.offset_by_containing_block(&self.padding_rect())
318    }
319
320    pub(crate) fn cumulative_border_box_rect(&self) -> PhysicalRect<Au> {
321        self.offset_by_containing_block(&self.border_rect())
322    }
323
324    pub(crate) fn content_rect(&self) -> PhysicalRect<Au> {
325        self.base.rect
326    }
327
328    pub(crate) fn padding_rect(&self) -> PhysicalRect<Au> {
329        self.content_rect().outer_rect(self.padding)
330    }
331
332    pub(crate) fn border_rect(&self) -> PhysicalRect<Au> {
333        self.padding_rect().outer_rect(self.border)
334    }
335
336    pub(crate) fn margin_rect(&self) -> PhysicalRect<Au> {
337        self.border_rect().outer_rect(self.margin)
338    }
339
340    pub(crate) fn padding_border_margin(&self) -> PhysicalSides<Au> {
341        self.margin + self.border + self.padding
342    }
343
344    pub(crate) fn is_root_element(&self) -> bool {
345        self.base.flags.intersects(FragmentFlags::IS_ROOT_ELEMENT)
346    }
347
348    pub(crate) fn is_body_element_of_html_element_root(&self) -> bool {
349        self.base
350            .flags
351            .intersects(FragmentFlags::IS_BODY_ELEMENT_OF_HTML_ELEMENT_ROOT)
352    }
353
354    pub fn print(&self, tree: &mut PrintTree) {
355        tree.new_level(format!(
356            "Box\
357                \nbase={:?}\
358                \ncontent={:?}\
359                \npadding rect={:?}\
360                \nborder rect={:?}\
361                \nmargin={:?}\
362                \nscrollable_overflow={:?}\
363                \nbaselines={:?}\
364                \noverflow={:?}",
365            self.base,
366            self.content_rect(),
367            self.padding_rect(),
368            self.border_rect(),
369            self.margin,
370            self.scrollable_overflow(),
371            self.baselines,
372            self.style().effective_overflow(self.base.flags),
373        ));
374
375        for child in &self.children {
376            child.print(tree);
377        }
378        tree.end_level();
379    }
380
381    pub(crate) fn scrollable_overflow_for_parent(&self) -> PhysicalRect<Au> {
382        let style = self.style();
383        let mut overflow = self.border_rect();
384        if !style.establishes_scroll_container(self.base.flags) {
385            // https://www.w3.org/TR/css-overflow-3/#scrollable
386            // Only include the scrollable overflow of a child box if it has overflow: visible.
387            let scrollable_overflow = self.scrollable_overflow();
388            let bottom_right = PhysicalPoint::new(
389                overflow.max_x().max(scrollable_overflow.max_x()),
390                overflow.max_y().max(scrollable_overflow.max_y()),
391            );
392
393            let overflow_style = style.effective_overflow(self.base.flags);
394            if overflow_style.y == ComputedOverflow::Visible {
395                overflow.origin.y = overflow.origin.y.min(scrollable_overflow.origin.y);
396                overflow.size.height = bottom_right.y - overflow.origin.y;
397            }
398
399            if overflow_style.x == ComputedOverflow::Visible {
400                overflow.origin.x = overflow.origin.x.min(scrollable_overflow.origin.x);
401                overflow.size.width = bottom_right.x - overflow.origin.x;
402            }
403        }
404
405        if !style.has_effective_transform_or_perspective(self.base.flags) {
406            return overflow;
407        }
408
409        // <https://drafts.csswg.org/css-overflow-3/#scrollable-overflow-region>
410        // > ...accounting for transforms by projecting each box onto the plane of
411        // > the element that establishes its 3D rendering context. [CSS3-TRANSFORMS]
412        // Both boxes and its scrollable overflow (if it is included) should be transformed accordingly.
413        //
414        // TODO(stevennovaryo): We are supposed to handle perspective transform and 3d
415        // contexts, but it is yet to happen.
416        self.calculate_transform_matrix(&self.border_rect())
417            .and_then(|transform| {
418                transform.outer_transformed_rect(&overflow.to_webrender().to_rect())
419            })
420            .map(|transformed_rect| f32_rect_to_au_rect(transformed_rect).cast_unit())
421            .unwrap_or(overflow)
422    }
423
424    /// Return the clipped the scrollable overflow based on its scroll origin, determined
425    /// by overflow direction. For an element, the clip rect is the padding rect and for
426    /// viewport, it is the initial containing block.
427    pub(crate) fn clip_wholly_unreachable_scrollable_overflow(
428        &self,
429        scrollable_overflow: PhysicalRect<Au>,
430        clipping_rect: PhysicalRect<Au>,
431    ) -> PhysicalRect<Au> {
432        // From <https://drafts.csswg.org/css-overflow/#unreachable-scrollable-overflow-region>:
433        // > Unless otherwise adjusted (e.g. by content alignment [css-align-3]), the area
434        // > beyond the scroll origin in either axis is considered the unreachable scrollable
435        // > overflow region: content rendered here is not accessible to the reader, see § 2.2
436        // > Scrollable Overflow. A scroll container is said to be scrolled to its scroll
437        // > origin when its scroll origin coincides with the corresponding corner of its
438        // > scrollport. This scroll position, the scroll origin position, usually, but not
439        // > always, coincides with the initial scroll position.
440        let scrolling_direction = self.style().overflow_direction();
441        let mut clipping_box = clipping_rect.to_box2d();
442        if scrolling_direction.rightward {
443            clipping_box.max.x = MAX_AU;
444        } else {
445            clipping_box.min.x = MIN_AU;
446        }
447
448        if scrolling_direction.downward {
449            clipping_box.max.y = MAX_AU;
450        } else {
451            clipping_box.min.y = MIN_AU;
452        }
453
454        let scrollable_overflow_box = scrollable_overflow
455            .to_box2d()
456            .intersection_unchecked(&clipping_box);
457
458        match scrollable_overflow_box.is_negative() {
459            true => PhysicalRect::zero(),
460            false => scrollable_overflow_box.to_rect(),
461        }
462    }
463
464    pub(crate) fn calculate_resolved_insets_if_positioned(&self) -> PhysicalSides<AuOrAuto> {
465        let style = self.style();
466        let position = style.get_box().position;
467        debug_assert_ne!(
468            position,
469            ComputedPosition::Static,
470            "Should not call this method on statically positioned box."
471        );
472
473        if let Some(resolved_sticky_insets) = *self.resolved_sticky_insets.borrow() {
474            return resolved_sticky_insets;
475        }
476
477        let convert_to_au_or_auto = |sides: PhysicalSides<Au>| {
478            PhysicalSides::new(
479                AuOrAuto::LengthPercentage(sides.top),
480                AuOrAuto::LengthPercentage(sides.right),
481                AuOrAuto::LengthPercentage(sides.bottom),
482                AuOrAuto::LengthPercentage(sides.left),
483            )
484        };
485
486        // "A resolved value special case property like top defined in another
487        // specification If the property applies to a positioned element and the
488        // resolved value of the display property is not none or contents, and
489        // the property is not over-constrained, then the resolved value is the
490        // used value. Otherwise the resolved value is the computed value."
491        // https://drafts.csswg.org/cssom/#resolved-values
492        let insets = style.physical_box_offsets();
493        let (cb_width, cb_height) = (
494            self.cumulative_containing_block_rect.width(),
495            self.cumulative_containing_block_rect.height(),
496        );
497        if position == ComputedPosition::Relative {
498            let get_resolved_axis = |start: &LengthPercentageOrAuto,
499                                     end: &LengthPercentageOrAuto,
500                                     container_length: Au| {
501                let start = start.map(|value| value.to_used_value(container_length));
502                let end = end.map(|value| value.to_used_value(container_length));
503                match (start.non_auto(), end.non_auto()) {
504                    (None, None) => (Au::zero(), Au::zero()),
505                    (None, Some(end)) => (-end, end),
506                    (Some(start), None) => (start, -start),
507                    // This is the overconstrained case, for which the resolved insets will
508                    // simply be the computed insets.
509                    (Some(start), Some(end)) => (start, end),
510                }
511            };
512            let (left, right) = get_resolved_axis(&insets.left, &insets.right, cb_width);
513            let (top, bottom) = get_resolved_axis(&insets.top, &insets.bottom, cb_height);
514            return convert_to_au_or_auto(PhysicalSides::new(top, right, bottom, left));
515        }
516
517        debug_assert!(position.is_absolutely_positioned());
518
519        let margin_rect = self.margin_rect();
520        let (top, bottom) = match (&insets.top, &insets.bottom) {
521            (
522                LengthPercentageOrAuto::LengthPercentage(top),
523                LengthPercentageOrAuto::LengthPercentage(bottom),
524            ) => (
525                top.to_used_value(cb_height),
526                bottom.to_used_value(cb_height),
527            ),
528            _ => (margin_rect.origin.y, cb_height - margin_rect.max_y()),
529        };
530        let (left, right) = match (&insets.left, &insets.right) {
531            (
532                LengthPercentageOrAuto::LengthPercentage(left),
533                LengthPercentageOrAuto::LengthPercentage(right),
534            ) => (left.to_used_value(cb_width), right.to_used_value(cb_width)),
535            _ => (margin_rect.origin.x, cb_width - margin_rect.max_x()),
536        };
537
538        convert_to_au_or_auto(PhysicalSides::new(top, right, bottom, left))
539    }
540
541    /// Whether this is a non-replaced inline-level box whose inner display type is `flow`.
542    /// <https://drafts.csswg.org/css-display-3/#inline-box>
543    pub(crate) fn is_inline_box(&self) -> bool {
544        self.style().is_inline_box(self.base.flags)
545    }
546
547    /// Whether this is an atomic inline-level box.
548    /// <https://drafts.csswg.org/css-display-3/#atomic-inline>
549    pub(crate) fn is_atomic_inline_level(&self) -> bool {
550        self.style().is_atomic_inline_level(self.base.flags)
551    }
552
553    /// Whether this is a table wrapper box.
554    /// <https://www.w3.org/TR/css-tables-3/#table-wrapper-box>
555    pub(crate) fn is_table_wrapper(&self) -> bool {
556        matches!(
557            self.specific_layout_info(),
558            Some(SpecificLayoutInfo::TableWrapper)
559        )
560    }
561
562    pub(crate) fn has_collapsed_borders(&self) -> bool {
563        match self.specific_layout_info() {
564            Some(SpecificLayoutInfo::TableCellWithCollapsedBorders) => true,
565            Some(SpecificLayoutInfo::TableGridWithCollapsedBorders(_)) => true,
566            Some(SpecificLayoutInfo::TableWrapper) => {
567                self.style().get_inherited_table().border_collapse == BorderCollapse::Collapse
568            },
569            _ => false,
570        }
571    }
572
573    pub(crate) fn spatial_tree_node(&self) -> Option<ScrollTreeNodeId> {
574        *self.spatial_tree_node.borrow()
575    }
576}