Skip to content

Truly independent web browser

LibWeb: Honor safe and unsafe overflow alignment in flex layout

Aliaksandr Kalenik1a5d6c3a89cd
diff --git a/Libraries/LibWeb/Rust/src/css/computed_value_types.rs b/Libraries/LibWeb/Rust/src/css/computed_value_types.rs
index cde1c269445..d25e545a5be 100644
--- a/Libraries/LibWeb/Rust/src/css/computed_value_types.rs
+++ b/Libraries/LibWeb/Rust/src/css/computed_value_types.rs
@@ -100,6 +100,7 @@ pub struct AlignmentValues {
pub align_self_overflow: u8,
pub justify_self_overflow: u8,
pub align_content_overflow: u8,
+ pub justify_content_overflow: u8,
pub column_gap: ComputedGap,
pub row_gap: ComputedGap,
}
diff --git a/Libraries/LibWeb/Rust/src/css/computed_value_views.rs b/Libraries/LibWeb/Rust/src/css/computed_value_views.rs
index 191a52abc18..54c21858cb1 100644
--- a/Libraries/LibWeb/Rust/src/css/computed_value_views.rs
+++ b/Libraries/LibWeb/Rust/src/css/computed_value_views.rs
@@ -663,6 +663,7 @@ scalar_accessors! {
align_self_overflow: u8 => align_self_overflow,
justify_self_overflow: u8 => justify_self_overflow,
align_content_overflow: u8 => align_content_overflow,
+ justify_content_overflow: u8 => justify_content_overflow,
flex_basis_is_content: bool => flex_basis.is_content,
}
}
diff --git a/Libraries/LibWeb/Rust/src/css/computed_values.rs b/Libraries/LibWeb/Rust/src/css/computed_values.rs
index cc74fc1da2c..09f573c366c 100644
--- a/Libraries/LibWeb/Rust/src/css/computed_values.rs
+++ b/Libraries/LibWeb/Rust/src/css/computed_values.rs
@@ -5730,6 +5730,7 @@ impl AlignmentValues {
align_self_overflow: crate::css::computed_value_types::OVERFLOW_ALIGNMENT_DEFAULT,
justify_self_overflow: crate::css::computed_value_types::OVERFLOW_ALIGNMENT_DEFAULT,
align_content_overflow: crate::css::computed_value_types::OVERFLOW_ALIGNMENT_DEFAULT,
+ justify_content_overflow: crate::css::computed_value_types::OVERFLOW_ALIGNMENT_DEFAULT,
column_gap: ComputedGap {
is_normal: true,
value: ComputedStyleValueHandle::empty(),
@@ -6598,7 +6599,7 @@ fn alignment_is_last_baseline(data: &RetainedStyleValueData) -> bool {
Some(StyleValueData::Keyword { keyword: crate::css::css_enums::keyword::LAST })))
}
-/// The explicit overflow alignment of a computed self-alignment value.
+/// The explicit overflow alignment of a computed alignment value.
fn alignment_overflow(data: &RetainedStyleValueData) -> u8 {
use crate::css::css_enums::keyword as k;
use crate::css::style_value::StyleValueData;
@@ -6676,6 +6677,7 @@ pub(crate) fn rust_build_alignment_group(
align_self_overflow: alignment_overflow(align_self),
justify_self_overflow: alignment_overflow(justify_self),
align_content_overflow: alignment_overflow(align_content),
+ justify_content_overflow: alignment_overflow(justify_content),
column_gap: ComputedGap::from_data(column_gap),
row_gap: ComputedGap::from_data(row_gap),
};
@@ -7124,6 +7126,7 @@ pub(crate) fn copy_fieldset_content_alignment(source: &AlignmentValues, target:
target.align_items_is_last_baseline = source.align_items_is_last_baseline;
target.align_items_overflow = source.align_items_overflow;
target.justify_content = source.justify_content;
+ target.justify_content_overflow = source.justify_content_overflow;
target.justify_items = source.justify_items;
target.justify_items_overflow = source.justify_items_overflow;
target.row_gap = source.row_gap.clone();
diff --git a/Libraries/LibWeb/Rust/src/layout/box_alignment.rs b/Libraries/LibWeb/Rust/src/layout/box_alignment.rs
index 6b918bc32e2..79ed1bd0615 100644
--- a/Libraries/LibWeb/Rust/src/layout/box_alignment.rs
+++ b/Libraries/LibWeb/Rust/src/layout/box_alignment.rs
@@ -170,22 +170,7 @@ impl SelfKeyword {
container_style: ComputedValuesView<'_>,
) -> (Self, OverflowSafety) {
let keyword = match style.align_self() {
- align_self::AUTO => {
- let keyword = match container_style.align_items() {
- align_items::NORMAL => Self::Normal,
- align_items::STRETCH => Self::Stretch,
- align_items::BASELINE => Self::Baseline,
- align_items::CENTER => Self::Center,
- align_items::START => Self::Start,
- align_items::END => Self::End,
- align_items::SELF_START => Self::SelfStart,
- align_items::SELF_END => Self::SelfEnd,
- align_items::FLEX_START => Self::FlexStart,
- align_items::FLEX_END => Self::FlexEnd,
- _ => unreachable!("invalid align-items value"),
- };
- return (keyword, OverflowSafety::of(container_style.align_items_overflow()));
- }
+ align_self::AUTO => return Self::align_items_of(container_style),
align_self::NORMAL => Self::Normal,
align_self::STRETCH => Self::Stretch,
align_self::BASELINE => Self::Baseline,
@@ -202,6 +187,23 @@ impl SelfKeyword {
(keyword, OverflowSafety::of(style.align_self_overflow()))
}
+ pub(crate) fn align_items_of(style: ComputedValuesView<'_>) -> (Self, OverflowSafety) {
+ let keyword = match style.align_items() {
+ align_items::NORMAL => Self::Normal,
+ align_items::STRETCH => Self::Stretch,
+ align_items::BASELINE => Self::Baseline,
+ align_items::CENTER => Self::Center,
+ align_items::START => Self::Start,
+ align_items::END => Self::End,
+ align_items::SELF_START => Self::SelfStart,
+ align_items::SELF_END => Self::SelfEnd,
+ align_items::FLEX_START => Self::FlexStart,
+ align_items::FLEX_END => Self::FlexEnd,
+ _ => unreachable!("invalid align-items value"),
+ };
+ (keyword, OverflowSafety::of(style.align_items_overflow()))
+ }
+
/// The alignment of a subject along an axis with the directions `directions`.
/// https://drafts.csswg.org/css-anchor-position-1/#valdef-justify-self-anchor-center
/// If the box is not absolutely positioned, or does not have a default anchor box, this value behaves as center
@@ -295,7 +297,7 @@ impl ContentKeyword {
justify_content::RIGHT => Self::Right,
_ => unreachable!("invalid justify-content value"),
};
- (keyword, OverflowSafety::Default)
+ (keyword, OverflowSafety::of(style.justify_content_overflow()))
}
/// The align-content keyword of a container styled `style` and its safety.
@@ -386,6 +388,20 @@ impl ContentAlignment {
self.edge.offset(free_space)
}
+ /// This alignment where the container leaves `free_space` beside its alignment subjects, in a layout mode whose
+ /// overflow alignment is `layout_mode_default` where the style specifies neither safe nor unsafe. A safe alignment
+ /// packs overflowing subjects against the container's start.
+ pub(crate) fn for_free_space(self, free_space: CssPixels, layout_mode_default: OverflowSafety) -> Self {
+ if free_space < CssPixels::default() && self.safety.is_safe(layout_mode_default) {
+ return Self {
+ distribution: None,
+ edge: self.container_start,
+ ..self
+ };
+ }
+ self
+ }
+
/// The free space the distribution places before and between `count` alignment subjects, where the container
/// leaves `free_space` beside them. It distributes no negative free space.
pub(crate) fn distributed_space(self, free_space: CssPixels, count: usize) -> SpaceDistribution {
diff --git a/Libraries/LibWeb/Rust/src/layout/flex_formatting_context.rs b/Libraries/LibWeb/Rust/src/layout/flex_formatting_context.rs
index 41a9b3a551e..50212255492 100644
--- a/Libraries/LibWeb/Rust/src/layout/flex_formatting_context.rs
+++ b/Libraries/LibWeb/Rust/src/layout/flex_formatting_context.rs
@@ -377,8 +377,8 @@ impl<'pass> FlexContainerConstants<'pass> {
flex_start_is_low: !is_direction_reverse,
..AxisDirections::of(!main_axis_writing_mode_start_is_reverse, main_axis_line_left_is_low)
};
- let (justify_content, _) = ContentKeyword::justify_content_of(*style);
- let (align_content, _) = ContentKeyword::align_content_of(*style);
+ let (justify_content, justify_content_safety) = ContentKeyword::justify_content_of(*style);
+ let (align_content, align_content_safety) = ContentKeyword::align_content_of(*style);
let (main_gap, cross_gap) = if is_row_layout {
(style.column_gap(), style.row_gap())
} else {
@@ -393,9 +393,8 @@ impl<'pass> FlexContainerConstants<'pass> {
used_flex_wrap,
is_direction_reverse,
cross_axis_is_reverse,
- // FIXME: Honor safe and unsafe overflow alignment.
- justify_content: justify_content.resolve(OverflowSafety::Unsafe, main_axis_directions),
- align_content: align_content.resolve(OverflowSafety::Unsafe, cross_axis_directions),
+ justify_content: justify_content.resolve(justify_content_safety, main_axis_directions),
+ align_content: align_content.resolve(align_content_safety, cross_axis_directions),
cross_axis_directions,
main_gap,
cross_gap,
@@ -2185,16 +2184,22 @@ impl<'pass> FlexFormattingContext<'pass> {
/// The alignment along the cross axis of a box styled `style`.
fn cross_alignment_of(&self, style: StyleValues<'_>) -> SelfAlignment {
- let (keyword, _) = SelfKeyword::align_self_of(*style, *self.container.style);
+ // AD-HOC: The items of a legacy webkit box align as its -webkit-box-align, an alias of align-items, says,
+ // whatever their align-self and its safety are, matching other engines.
+ let (keyword, safety) = if self.is_legacy_webkit_box() {
+ let (keyword, _) = SelfKeyword::align_items_of(*self.container.style);
+ (keyword, OverflowSafety::Unsafe)
+ } else {
+ SelfKeyword::align_self_of(*style, *self.container.style)
+ };
let self_start_is_low = geometry::start_is_low_side_in_frame(
self.run.pass.frame,
style.writing_mode(),
style.direction(),
self.cross_axis_is_horizontal(),
);
- // FIXME: Honor safe and unsafe overflow alignment.
keyword.resolve(
- OverflowSafety::Unsafe,
+ safety,
AxisDirections {
self_start_is_low,
..self.container.cross_axis_directions
@@ -2774,8 +2779,11 @@ impl<'pass> FlexFormattingContext<'pass> {
// NB: Where the cursor starts at the right or bottom, the items are placed backwards from there.
let mut cursor_right = false;
if auto_margins == 0 && item_count > 0 {
- let justify = self.container.justify_content;
let inner_main_size = self.inner_main_size_used(&self.container_cells);
+ let justify = self
+ .container
+ .justify_content
+ .for_free_space(inner_main_size - used_main_space, OverflowSafety::Unsafe);
space_distribution = justify.distributed_space(remaining.unwrap_or_default(), item_count);
let leading_space = if justify.distribution.is_none() && justify.edge == AxisEdge::Center {
(inner_main_size - used_main_space) / 2
@@ -2892,7 +2900,13 @@ impl<'pass> FlexFormattingContext<'pass> {
- item.cross_size.unwrap()
- item.margins.cross_after
- item.borders_and_padding.cross_after;
- let offset = match alignment.position {
+ let free_space = self.flex_lines[line_index].cross_size
+ - item.cross_size.unwrap()
+ - item.margins.cross_before
+ - item.borders_and_padding.cross_before
+ - item.margins.cross_after
+ - item.borders_and_padding.cross_after;
+ let offset = match alignment.position_for(free_space, OverflowSafety::Unsafe) {
SelfPosition::Baseline => {
// https://drafts.csswg.org/css-flexbox-1/#valdef-align-items-baseline
// NB: Baseline-aligned items are initially placed at the cross-start edge (like flex-start). Their
@@ -2949,8 +2963,11 @@ impl<'pass> FlexFormattingContext<'pass> {
// CSS-FLEXBOX-2: Account for gap between flex lines.
sum += self.cross_gap() * self.flex_lines.len().wrapping_sub(1);
// NB: The leftover free space that align-content distributes around the lines.
- let align = self.container.align_content;
let leftover = container_cross_size - sum;
+ let align = self
+ .container
+ .align_content
+ .for_free_space(leftover, OverflowSafety::Unsafe);
let space_distribution = align.distributed_space(leftover, self.flex_lines.len());
// NB: Centered lines, and lines that space-around or space-evenly cannot distribute negative leftover free
// space around, are placed from the left or top.
@@ -3360,6 +3377,7 @@ impl<'pass> FlexFormattingContext<'pass> {
// would be positioned if it were the sole flex item in the flex container,
// assuming both the child and the flex container were fixed-size boxes of their used size.
// (For this purpose, auto margins are treated as zero.
+ // FIXME: Honor safe overflow alignment, which depends on the size of the child.
let main_alignment = self.container.justify_content.edge;
let main_size = self.inner_main_size_used(&self.container_cells);
let cross_size = self.inner_cross_size_used(&self.container_cells);
diff --git a/Tests/LibWeb/Ref/expected/wpt-import/css/css-flexbox/flexbox-safe-overflow-position-001-ref.html b/Tests/LibWeb/Ref/expected/wpt-import/css/css-flexbox/flexbox-safe-overflow-position-001-ref.html
new file mode 100644
index 00000000000..b921f35ca3b
--- /dev/null
+++ b/Tests/LibWeb/Ref/expected/wpt-import/css/css-flexbox/flexbox-safe-overflow-position-001-ref.html
@@ -0,0 +1,73 @@
+<!DOCTYPE html>
+<!--
+ Any copyright is dedicated to the Public Domain.
+ http://creativecommons.org/publicdomain/zero/1.0
+-->
+<html>
+<head>
+ <title>Reference: Testing safe overflow-position for align-content, justify-content, and align-items in flex containers</title>
+ <link rel="author" title="Mihir Iyer" href="mailto:[email protected]">
+ <meta charset="utf-8">
+ <style>
+ .flex {
+ display: flex;
+ width: 85px;
+ height: 65px;
+ border: 1px solid black;
+ align-content: flex-end;
+ justify-content: center;
+ align-items: center;
+ float: left;
+ clear: both;
+ margin-top: 100px;
+ }
+ .rowNoWrap {
+ flex-flow: row nowrap;
+ }
+ .columnNoWrap {
+ flex-flow: column wrap;
+ }
+ .item {
+ border: 1px solid blue;
+ background: lightblue;
+ width: 28px;
+ height: 28px;
+ flex-shrink: 0;
+ }
+ .bigItem {
+ border: 1px solid blue;
+ background: lightblue;
+ width: 28px;
+ height: 90px;
+ flex-shrink: 0;
+ }
+ .alignContentStart {
+ align-content: start;
+ }
+ .justifyContentStart {
+ justify-content: start;
+ }
+ .alignSelfStart {
+ align-self: start;
+ }
+ </style>
+</head>
+<body>
+ <div class="flex rowNoWrap justifyContentStart">
+ <div class="item"></div>
+ <div class="item"></div>
+ <div class="bigItem alignSelfStart"></div>
+ <div class="item"></div>
+ <div class="item"></div>
+ <div class="item"></div>
+ </div>
+ <div class="flex columnNoWrap alignContentStart">
+ <div class="item"></div>
+ <div class="item"></div>
+ <div class="item"></div>
+ <div class="item"></div>
+ <div class="item"></div>
+ <div class="item"></div>
+ </div>
+</body>
+</html>
diff --git a/Tests/LibWeb/Ref/input/wpt-import/css/css-flexbox/flexbox-safe-overflow-position-001.html b/Tests/LibWeb/Ref/input/wpt-import/css/css-flexbox/flexbox-safe-overflow-position-001.html
new file mode 100644
index 00000000000..7bca3765a5c
--- /dev/null
+++ b/Tests/LibWeb/Ref/input/wpt-import/css/css-flexbox/flexbox-safe-overflow-position-001.html
@@ -0,0 +1,66 @@
+<!DOCTYPE html>
+<!--
+ Any copyright is dedicated to the Public Domain.
+ http://creativecommons.org/publicdomain/zero/1.0
+-->
+<html>
+<head>
+ <title>CSS Test: Testing safe overflow-position for align-content, justify-content, and align-items in flex containers</title>
+ <link rel="author" title="Mihir Iyer" href="mailto:[email protected]">
+ <link rel="help" href="https://drafts.csswg.org/css-align-3/#overflow-values">
+ <link rel="match" href="../../../../expected/wpt-import/css/css-flexbox/flexbox-safe-overflow-position-001-ref.html">
+ <meta charset="utf-8">
+ <style>
+ .flex {
+ display: flex;
+ width: 85px;
+ height: 65px;
+ border: 1px solid black;
+ align-content: safe flex-end;
+ justify-content: safe center;
+ align-items: safe center;
+ float: left;
+ clear: both;
+ margin-top: 100px;
+ }
+ .rowNoWrap {
+ flex-flow: row nowrap;
+ }
+ .columnNoWrap {
+ flex-flow: column wrap;
+ }
+ .item {
+ border: 1px solid blue;
+ background: lightblue;
+ width: 28px;
+ height: 28px;
+ flex-shrink: 0;
+ }
+ .bigItem {
+ border: 1px solid blue;
+ background: lightblue;
+ width: 28px;
+ height: 90px;
+ flex-shrink: 0;
+ }
+ </style>
+</head>
+<body>
+ <div class="flex rowNoWrap">
+ <div class="item"></div>
+ <div class="item"></div>
+ <div class="bigItem"></div>
+ <div class="item"></div>
+ <div class="item"></div>
+ <div class="item"></div>
+ </div>
+ <div class="flex columnNoWrap">
+ <div class="item"></div>
+ <div class="item"></div>
+ <div class="item"></div>
+ <div class="item"></div>
+ <div class="item"></div>
+ <div class="item"></div>
+ </div>
+</body>
+</html>
diff --git a/Tests/LibWeb/Ref/input/wpt-import/css/css-flexbox/flexbox-safe-overflow-position-002.html b/Tests/LibWeb/Ref/input/wpt-import/css/css-flexbox/flexbox-safe-overflow-position-002.html
new file mode 100644
index 00000000000..2f86a43f950
--- /dev/null
+++ b/Tests/LibWeb/Ref/input/wpt-import/css/css-flexbox/flexbox-safe-overflow-position-002.html
@@ -0,0 +1,41 @@
+<!DOCTYPE html>
+<link rel="author" title="David Grogan" href="mailto:[email protected]">
+<link rel="help" href="https://drafts.csswg.org/css-align-3/#overflow-values">
+<link rel="match" href="../../../../expected/wpt-import/css/css-flexbox/../reference/ref-filled-green-100px-square.xht">
+<meta name="assert"
+ content="safe flex-start allows for overflow past the end edge, which is bottom/right even in the case of wrap-reverse" />
+
+<style>
+ #reference-overlapped-red {
+ position: absolute;
+ background-color: red;
+ width: 100px;
+ height: 100px;
+ z-index: -1;
+ }
+
+ .flex {
+ display: flex;
+ width: 90px;
+ height: 90px;
+ align-content: safe flex-start;
+ justify-content: safe flex-start;
+ flex-wrap: wrap-reverse;
+ }
+
+ .item {
+ flex: 0 0 100px;
+ width: 100px;
+ height: 100px;
+ background: green;
+ }
+
+</style>
+
+<p>Test passes if there is a filled green square and <strong>no red</strong>.
+</p>
+<div id="reference-overlapped-red"></div>
+
+<div class="flex">
+ <div class="item"></div>
+</div>
diff --git a/Tests/LibWeb/Ref/input/wpt-import/css/css-flexbox/flexbox-safe-overflow-position-003.html b/Tests/LibWeb/Ref/input/wpt-import/css/css-flexbox/flexbox-safe-overflow-position-003.html
new file mode 100644
index 00000000000..82692241463
--- /dev/null
+++ b/Tests/LibWeb/Ref/input/wpt-import/css/css-flexbox/flexbox-safe-overflow-position-003.html
@@ -0,0 +1,41 @@
+<!DOCTYPE html>
+<link rel="author" title="David Grogan" href="mailto:[email protected]">
+<link rel="help" href="https://drafts.csswg.org/css-align-3/#overflow-values">
+<link rel="match" href="../../../../expected/wpt-import/css/css-flexbox/../reference/ref-filled-green-100px-square.xht">
+<meta name="assert"
+ content="safe flex-start allows for overflow past the end edge, which is bottom/right even in the case of row-reverse" />
+
+<style>
+ #reference-overlapped-red {
+ position: absolute;
+ background-color: red;
+ width: 100px;
+ height: 100px;
+ z-index: -1;
+ }
+
+ .flex {
+ display: flex;
+ flex-flow: row-reverse;
+ width: 90px;
+ height: 90px;
+ align-content: safe flex-start;
+ justify-content: safe flex-start;
+ }
+
+ .item {
+ flex: 0 0 100px;
+ width: 100px;
+ height: 100px;
+ background: green;
+ }
+
+</style>
+
+<p>Test passes if there is a filled green square and <strong>no red</strong>.
+</p>
+<div id="reference-overlapped-red"></div>
+
+<div class="flex">
+ <div class="item"></div>
+</div>
diff --git a/Tests/LibWeb/Ref/input/wpt-import/css/css-flexbox/flexbox-safe-overflow-position-004.html b/Tests/LibWeb/Ref/input/wpt-import/css/css-flexbox/flexbox-safe-overflow-position-004.html
new file mode 100644
index 00000000000..33c8223ff3a
--- /dev/null
+++ b/Tests/LibWeb/Ref/input/wpt-import/css/css-flexbox/flexbox-safe-overflow-position-004.html
@@ -0,0 +1,41 @@
+<!DOCTYPE html>
+<link rel="author" title="David Grogan" href="mailto:[email protected]">
+<link rel="help" href="https://drafts.csswg.org/css-align-3/#overflow-values">
+<link rel="match" href="../../../../expected/wpt-import/css/css-flexbox/../reference/ref-filled-green-100px-square.xht">
+<meta name="assert"
+ content="safe flex-start allows for overflow past the end edge, which is bottom/right even in the case of column-reverse" />
+
+<style>
+ #reference-overlapped-red {
+ position: absolute;
+ background-color: red;
+ width: 100px;
+ height: 100px;
+ z-index: -1;
+ }
+
+ .flex {
+ display: flex;
+ flex-flow: column-reverse;
+ width: 90px;
+ height: 90px;
+ align-content: safe flex-start;
+ justify-content: safe flex-start;
+ }
+
+ .item {
+ flex: 0 0 100px;
+ width: 100px;
+ height: 100px;
+ background: green;
+ }
+
+</style>
+
+<p>Test passes if there is a filled green square and <strong>no red</strong>.
+</p>
+<div id="reference-overlapped-red"></div>
+
+<div class="flex">
+ <div class="item"></div>
+</div>
diff --git a/Tests/LibWeb/Ref/input/wpt-import/css/css-flexbox/flexbox-safe-overflow-position-005.html b/Tests/LibWeb/Ref/input/wpt-import/css/css-flexbox/flexbox-safe-overflow-position-005.html
new file mode 100644
index 00000000000..86505b7baab
--- /dev/null
+++ b/Tests/LibWeb/Ref/input/wpt-import/css/css-flexbox/flexbox-safe-overflow-position-005.html
@@ -0,0 +1,42 @@
+<!DOCTYPE html>
+<link rel="author" title="David Grogan" href="mailto:[email protected]">
+<link rel="help" href="https://drafts.csswg.org/css-align-3/#overflow-values">
+<link rel="match" href="../../../../expected/wpt-import/css/css-flexbox/../reference/ref-filled-green-100px-square.xht">
+<meta name="assert"
+ content="safe flex-start allows for overflow past the end edge, which is bottom/right in the case of wrap-reverse + vertical-lr" />
+
+<style>
+ #reference-overlapped-red {
+ position: absolute;
+ background-color: red;
+ width: 100px;
+ height: 100px;
+ z-index: -1;
+ }
+
+ .flex {
+ display: flex;
+ flex-flow: wrap-reverse;
+ writing-mode: vertical-lr;
+ inline-size: 90px;
+ block-size: 90px;
+ align-content: safe flex-start;
+ justify-content: safe flex-start;
+ }
+
+ .item {
+ flex: 0 0 100px;
+ width: 100px;
+ height: 100px;
+ background: green;
+ }
+
+</style>
+
+<p>Test passes if there is a filled green square and <strong>no red</strong>.
+</p>
+<div id="reference-overlapped-red"></div>
+
+<div class="flex">
+ <div class="item"></div>
+</div>
diff --git a/Tests/LibWeb/Ref/input/wpt-import/css/css-flexbox/flexbox-safe-overflow-position-006.html b/Tests/LibWeb/Ref/input/wpt-import/css/css-flexbox/flexbox-safe-overflow-position-006.html
new file mode 100644
index 00000000000..d0bb70adde0
--- /dev/null
+++ b/Tests/LibWeb/Ref/input/wpt-import/css/css-flexbox/flexbox-safe-overflow-position-006.html
@@ -0,0 +1,40 @@
+<!DOCTYPE html>
+<link rel="author" title="David Grogan" href="mailto:[email protected]">
+<link rel="help" href="https://drafts.csswg.org/css-align-3/#overflow-values">
+<link rel="match" href="../../../../expected/wpt-import/css/css-flexbox/../reference/ref-filled-green-100px-square.xht">
+<meta name="assert" content="align-self:safe has no effect on -webkit-box" />
+
+<style>
+ #reference-overlapped-red {
+ position: absolute;
+ background-color: red;
+ width: 100px;
+ height: 100px;
+ z-index: -1;
+ }
+
+ .flex {
+ display: -webkit-box;
+ -webkit-box-align: end;
+ width: 90px;
+ height: 90px;
+ /* Make the green square cover the red square. */
+ translate: 0 10px;
+ }
+
+ .item {
+ min-width: 100px;
+ min-height: 100px;
+ background: green;
+ align-self: safe center;
+ }
+
+</style>
+
+<p>Test passes if there is a filled green square and <strong>no red</strong>.
+</p>
+<div id="reference-overlapped-red"></div>
+
+<div class="flex">
+ <div class="item"></div>
+</div>
diff --git a/Tests/LibWeb/Text/expected/flex/safe-overflow-alignment.txt b/Tests/LibWeb/Text/expected/flex/safe-overflow-alignment.txt
new file mode 100644
index 00000000000..50e107455fa
--- /dev/null
+++ b/Tests/LibWeb/Text/expected/flex/safe-overflow-alignment.txt
@@ -0,0 +1,19 @@
+{alignItems: safe center} {}: 0,0
+{alignItems: unsafe center} {}: 0,-50
+{} {alignSelf: safe center}: 0,0
+{} {alignSelf: safe center; width: 50px; height: 50px}: 0,25
+{justifyContent: safe center} {}: 0,0
+{justifyContent: unsafe center} {}: -50,0
+{justifyContent: safe flex-end} {}: 0,0
+{flexWrap: wrap; alignContent: safe center} {}: 0,0
+{flexDirection: column; justifyContent: safe center} {}: 0,0
+{flexDirection: row-reverse; justifyContent: safe flex-start} {}: 0,0
+{flexDirection: row-reverse; justifyContent: safe center} {}: 0,0
+{flexDirection: row-reverse; justifyContent: unsafe center} {}: -50,0
+{flexDirection: column-reverse; justifyContent: safe flex-start} {}: 0,0
+{flexDirection: column-reverse; justifyContent: safe center} {}: 0,0
+{flexWrap: wrap-reverse; alignContent: safe flex-start} {}: 0,0
+{flexWrap: wrap-reverse; alignContent: safe center} {}: 0,0
+{flexWrap: wrap-reverse; alignItems: safe flex-start} {}: 0,-100
+{direction: rtl; justifyContent: safe center} {}: -100,0
+{display: -webkit-box; webkitBoxAlign: end} {alignSelf: safe center}: 0,-100
diff --git a/Tests/LibWeb/Text/input/flex/safe-overflow-alignment.html b/Tests/LibWeb/Text/input/flex/safe-overflow-alignment.html
new file mode 100644
index 00000000000..4335a5ee07a
--- /dev/null
+++ b/Tests/LibWeb/Text/input/flex/safe-overflow-alignment.html
@@ -0,0 +1,55 @@
+<!DOCTYPE html>
+<style>
+ .container {
+ position: relative;
+ display: flex;
+ width: 100px;
+ height: 100px;
+ }
+ .item {
+ flex-shrink: 0;
+ width: 200px;
+ height: 200px;
+ }
+</style>
+<script src="../include.js"></script>
+<script>
+ test(() => {
+ const cases = [
+ [{ alignItems: "safe center" }],
+ [{ alignItems: "unsafe center" }],
+ [{}, { alignSelf: "safe center" }],
+ [{}, { alignSelf: "safe center", width: "50px", height: "50px" }],
+ [{ justifyContent: "safe center" }],
+ [{ justifyContent: "unsafe center" }],
+ [{ justifyContent: "safe flex-end" }],
+ [{ flexWrap: "wrap", alignContent: "safe center" }],
+ [{ flexDirection: "column", justifyContent: "safe center" }],
+ [{ flexDirection: "row-reverse", justifyContent: "safe flex-start" }],
+ [{ flexDirection: "row-reverse", justifyContent: "safe center" }],
+ [{ flexDirection: "row-reverse", justifyContent: "unsafe center" }],
+ [{ flexDirection: "column-reverse", justifyContent: "safe flex-start" }],
+ [{ flexDirection: "column-reverse", justifyContent: "safe center" }],
+ [{ flexWrap: "wrap-reverse", alignContent: "safe flex-start" }],
+ [{ flexWrap: "wrap-reverse", alignContent: "safe center" }],
+ [{ flexWrap: "wrap-reverse", alignItems: "safe flex-start" }],
+ [{ direction: "rtl", justifyContent: "safe center" }],
+ [{ display: "-webkit-box", webkitBoxAlign: "end" }, { alignSelf: "safe center" }],
+ ];
+ for (const [containerStyle, itemStyle] of cases) {
+ const container = document.createElement("div");
+ container.className = "container";
+ Object.assign(container.style, containerStyle);
+ const item = document.createElement("div");
+ item.className = "item";
+ Object.assign(item.style, itemStyle);
+ container.appendChild(item);
+ document.body.appendChild(container);
+ const containerRect = container.getBoundingClientRect();
+ const itemRect = item.getBoundingClientRect();
+ const describe = style => Object.entries(style ?? {}).map(([name, value]) => `${name}: ${value}`).join("; ");
+ println(`{${describe(containerStyle)}} {${describe(itemStyle)}}: ${itemRect.left - containerRect.left},${itemRect.top - containerRect.top}`);
+ container.remove();
+ }
+ });
+</script>