layout: Resolve canvas background properties during painting (#36917)

Instead of resolving the canvas background properties (essentially
keeping a possible reference to the `<body>`'s style) during fragment
tree construction, wait until painting to possibly find the style on an
appropriate `<body>` fragment. This is possible now because `Fragment`
keeps a list of flags with relevant information about the root and
`<body>` elements.

A benefit of this approach is that styles aren't cached in the fragment
tree, which would be problematic for incremental layout. In addition,
the old code was making an effort to transform the `<body>`'s background
by the root element's transform. Only Safari does this and there was
a resolution the WG that this should not happen in
https://github.com/w3c/csswg-drafts/issues/6683.

Testing:
 - `/css/css-transforms/transform-translate-background-001.html`
 - `/css/css-transforms/transform-translate-background-002.html`
 - `/css/CSS2/floats/float-root.html`
 
Fixes: #30475.
Closes: #30569.

Signed-off-by: Martin Robinson <mrobinson@igalia.com>
Co-authored-by: Oriol Brufau <obrufau@igalia.com>
This commit is contained in:
Martin Robinson 2025-05-09 12:36:53 +02:00 committed by GitHub
parent c6f61e6b6e
commit 53be79a5b5
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
9 changed files with 116 additions and 195 deletions

View file

@ -11,10 +11,10 @@ use malloc_size_of_derive::MallocSizeOf;
use style::animation::AnimationSetKey;
use webrender_api::units;
use super::{ContainingBlockManager, Fragment};
use super::{BoxFragment, ContainingBlockManager, Fragment};
use crate::ArcRefCell;
use crate::context::LayoutContext;
use crate::display_list::StackingContext;
use crate::flow::CanvasBackground;
use crate::geom::PhysicalRect;
#[derive(MallocSizeOf)]
@ -36,9 +36,6 @@ pub struct FragmentTree {
/// The containing block used in the layout of this fragment tree.
pub(crate) initial_containing_block: PhysicalRect<Au>,
/// <https://drafts.csswg.org/css-backgrounds/#special-backgrounds>
pub(crate) canvas_background: CanvasBackground,
/// Whether or not the viewport is sensitive to scroll input events.
pub viewport_scroll_sensitivity: AxesScrollSensitivity,
}
@ -49,14 +46,12 @@ impl FragmentTree {
root_fragments: Vec<Fragment>,
scrollable_overflow: PhysicalRect<Au>,
initial_containing_block: PhysicalRect<Au>,
canvas_background: CanvasBackground,
viewport_scroll_sensitivity: AxesScrollSensitivity,
) -> Self {
let fragment_tree = Self {
root_fragments,
scrollable_overflow,
initial_containing_block,
canvas_background,
viewport_scroll_sensitivity,
};
@ -102,11 +97,7 @@ impl FragmentTree {
root_stacking_context: &StackingContext,
) {
// Paint the canvas background (if any) before/under everything else
root_stacking_context.build_canvas_background_display_list(
builder,
self,
&self.initial_containing_block,
);
root_stacking_context.build_canvas_background_display_list(builder, self);
root_stacking_context.build_display_list(builder);
}
@ -160,4 +151,45 @@ impl FragmentTree {
scroll_area
}
}
/// Find the `<body>` element's [`Fragment`], if it exists in this [`FragmentTree`].
pub(crate) fn body_fragment(&self) -> Option<ArcRefCell<BoxFragment>> {
fn find_body(children: &[Fragment]) -> Option<ArcRefCell<BoxFragment>> {
children.iter().find_map(|fragment| {
match fragment {
Fragment::Box(box_fragment) | Fragment::Float(box_fragment) => {
let borrowed_box_fragment = box_fragment.borrow();
if borrowed_box_fragment.is_body_element_of_html_element_root() {
return Some(box_fragment.clone());
}
// The fragment for the `<body>` element is typically a child of the root (though,
// not if it's absolutely positioned), so we need to recurse into the children of
// the root to find it.
//
// Additionally, recurse into any anonymous fragments, as the `<body>` fragment may
// have created anonymous parents (for instance by creating an inline formatting context).
if borrowed_box_fragment.is_root_element() ||
borrowed_box_fragment.base.is_anonymous()
{
find_body(&borrowed_box_fragment.children)
} else {
None
}
},
Fragment::Positioning(positioning_context)
if positioning_context.borrow().base.is_anonymous() =>
{
// If the `<body>` element is a `display: inline` then it might be nested inside of a
// `PositioningFragment` for the purposes of putting it on the first line of the implied
// inline formatting context.
find_body(&positioning_context.borrow().children)
},
_ => None,
}
})
}
find_body(&self.root_fragments)
}
}