layout: Implement proper absolute child position for flexbox (#33346)

This implements the requirements outlined in the [flexbox specification]
about how to position absolute children of flex containers. We must
establish a static position rectangle (to use if all insets are auto)
and also align the child into that rectangle.

[flebox specification]: https://drafts.csswg.org/css-flexbox/#abspos-items

Signed-off-by: Martin Robinson <mrobinson@igalia.com>
Co-authored-by: Oriol Brufau <obrufau@igalia.com>
This commit is contained in:
Martin Robinson 2024-09-09 07:44:16 -07:00 committed by GitHub
parent a3a86d5913
commit d169a82d2e
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
20 changed files with 678 additions and 724 deletions

View file

@ -4,11 +4,11 @@
use app_units::Au;
use serde::Serialize;
use style::values::computed::LengthPercentage;
use style::values::specified::align::AlignFlags;
use super::Fragment;
use crate::cell::ArcRefCell;
use crate::geom::LogicalVec2;
use crate::geom::{LogicalVec2, PhysicalRect, PhysicalVec};
/// A reference to a Fragment which is shared between `HoistedAbsolutelyPositionedBox`
/// and its placeholder `AbsoluteOrFixedPositionedFragment` in the original tree position.
@ -16,53 +16,35 @@ use crate::geom::LogicalVec2;
#[derive(Serialize)]
pub(crate) struct HoistedSharedFragment {
pub fragment: Option<ArcRefCell<Fragment>>,
pub box_offsets: LogicalVec2<AbsoluteBoxOffsets>,
/// The "static-position rect" of this absolutely positioned box. This is defined by the
/// layout mode from which the box originates.
///
/// See <https://drafts.csswg.org/css-position-3/#staticpos-rect>
pub static_position_rect: PhysicalRect<Au>,
/// The resolved alignment values used for aligning this absolutely positioned element
/// if the "static-position rect" ends up being the "inset-modified containing block".
/// These values are dependent on the layout mode (currently only interesting for
/// flexbox).
pub resolved_alignment: LogicalVec2<AlignFlags>,
}
impl HoistedSharedFragment {
pub(crate) fn new(box_offsets: LogicalVec2<AbsoluteBoxOffsets>) -> Self {
pub(crate) fn new(
static_position_rect: PhysicalRect<Au>,
resolved_alignment: LogicalVec2<AlignFlags>,
) -> Self {
HoistedSharedFragment {
fragment: None,
box_offsets,
static_position_rect,
resolved_alignment,
}
}
}
impl HoistedSharedFragment {
/// In some cases `inset: auto`-positioned elements do not know their precise
/// position until after they're hoisted. This lets us adjust auto values
/// after the fact.
pub(crate) fn adjust_offsets(&mut self, offsets: LogicalVec2<Au>) {
self.box_offsets.inline.adjust_offset(offsets.inline);
self.box_offsets.block.adjust_offset(offsets.block);
}
}
#[derive(Clone, Debug, Serialize)]
pub(crate) enum AbsoluteBoxOffsets {
StaticStart {
start: Au,
},
Start {
start: LengthPercentage,
},
End {
end: LengthPercentage,
},
Both {
start: LengthPercentage,
end: LengthPercentage,
},
}
impl AbsoluteBoxOffsets {
pub(crate) fn both_specified(&self) -> bool {
matches!(self, AbsoluteBoxOffsets::Both { .. })
}
pub(crate) fn adjust_offset(&mut self, new_offset: Au) {
if let AbsoluteBoxOffsets::StaticStart { ref mut start } = *self {
*start = new_offset
}
/// `inset: auto`-positioned elements do not know their precise position until after
/// they're hoisted. This lets us adjust auto values after the fact.
pub(crate) fn adjust_offsets(&mut self, offset: &PhysicalVec<Au>) {
self.static_position_rect = self.static_position_rect.translate(*offset);
}
}