Skip to content

Truly independent web browser

LibWeb: Honor `order` when painting flex and grid items

Callum Lawc16d6349ca82
diff --git a/Documentation/CSSGeneratedFiles.md b/Documentation/CSSGeneratedFiles.md
index 7ae93cd5bbf..19f85faa16d 100644
--- a/Documentation/CSSGeneratedFiles.md
+++ b/Documentation/CSSGeneratedFiles.md
@@ -22,7 +22,7 @@ Each property will have some set of these fields on it:
| Field | Required | Default | Description | Generated functions |
|-------------------------------------|----------|------------|-------------------------------------------------------------------------------------------------------------|-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| `affects-layout` | No | `true` | Boolean. Whether changing this property will invalidate the element's layout. | `bool property_affects_layout(PropertyID)` |
-| `affects-stacking-context` | No | `false` | Boolean. Whether this property can cause a new stacking context for the element. | `bool property_affects_stacking_context(PropertyID)` |
+| `affects-stacking-context` | No | `false` | Boolean. Whether this property can create a stacking context or change stacking order. | `bool property_affects_stacking_context(PropertyID)` |
| `animation-type` | Yes | | String. How the property should be animated. Defined by the spec. See below. | `AnimationType animation_type_from_longhand_property(PropertyID)` |
| `inherited` | Yes | | Boolean. Whether the property is inherited by its child elements. Only applicable to longhand properties. | `bool is_inherited_property(PropertyID)` |
| `initial` | Yes | | String. The property's initial value if it is not specified. | `NonnullRefPtr<StyleValue const> property_initial_value(PropertyID)` |
diff --git a/Libraries/LibWeb/CSS/Properties.json b/Libraries/LibWeb/CSS/Properties.json
index 0aa8a2402ed..4bb06e5ec73 100644
--- a/Libraries/LibWeb/CSS/Properties.json
+++ b/Libraries/LibWeb/CSS/Properties.json
@@ -3971,6 +3971,7 @@
},
"order": {
"style-group": "AlignmentValues",
+ "affects-stacking-context": true,
"animation-type": "by-computed-value",
"inherited": false,
"initial": "0",
diff --git a/Libraries/LibWeb/Rust/src/css/style/style_invalidation.rs b/Libraries/LibWeb/Rust/src/css/style/style_invalidation.rs
index 7014afe5f08..68ff20a31a5 100644
--- a/Libraries/LibWeb/Rust/src/css/style/style_invalidation.rs
+++ b/Libraries/LibWeb/Rust/src/css/style/style_invalidation.rs
@@ -672,8 +672,9 @@ fn property_invalidation(property: u16, old: ComputedValuesView<'_>, new: Comput
}
let will_change_covers_property =
will_change_covers_property(property, old) && will_change_covers_property(property, new);
+ // 'order' and 'z-index' can change stacking order without changing which boxes establish stacking contexts.
if property_metadata::property_affects_stacking_context(property)
- && (property == property_id::Z_INDEX
+ && (matches!(property, property_id::ORDER | property_id::Z_INDEX)
|| (!will_change_covers_property
&& value_creates_stacking_context(property, old) != value_creates_stacking_context(property, new)))
{
diff --git a/Libraries/LibWeb/Rust/src/painting/paint_order.rs b/Libraries/LibWeb/Rust/src/painting/paint_order.rs
index 8c620a3c0d5..4cb796006c0 100644
--- a/Libraries/LibWeb/Rust/src/painting/paint_order.rs
+++ b/Libraries/LibWeb/Rust/src/painting/paint_order.rs
@@ -4,9 +4,53 @@
* SPDX-License-Identifier: BSD-2-Clause
*/
+use crate::layout::LayoutNodeArena;
use crate::layout::node_data::NodeSlotId;
use crate::painting::node_painting;
use crate::painting::paint_read::PaintRead;
+use crate::painting::style_queries;
+use std::cmp::Ordering;
+
+pub(crate) fn order_for_painting(arena: &impl PaintRead, slot: NodeSlotId) -> i32 {
+ // Absolutely positioned children of flex/grid containers have order: 0 for painting.
+ // https://drafts.csswg.org/css-display-3/#order-modified-document-order
+ if style_queries::is_flex_or_grid_item(arena, slot) {
+ arena.node_style_if_live(slot).map_or(0, |style| style.order())
+ } else {
+ 0
+ }
+}
+
+impl LayoutNodeArena {
+ pub(crate) fn compare_order_modified_document_order(
+ &self,
+ mut left: NodeSlotId,
+ mut right: NodeSlotId,
+ ) -> Ordering {
+ let document_order = self.node_pre_order_label(left).cmp(&self.node_pre_order_label(right));
+ // Hoisted positioned descendants follow the order of the items containing them.
+ // Walk up the parent with the later pre-order label to find the siblings whose
+ // subtrees contain the boxes. An ancestor stays before its descendants.
+ while left != right {
+ let left_parent = self.data(left).parent.get();
+ let right_parent = self.data(right).parent.get();
+ if left_parent == right_parent {
+ return order_for_painting(self, left)
+ .cmp(&order_for_painting(self, right))
+ .then(document_order);
+ }
+ if left_parent.is_invalid() || right_parent.is_invalid() {
+ break;
+ }
+ if self.node_pre_order_label(left_parent) > self.node_pre_order_label(right_parent) {
+ left = left_parent;
+ } else {
+ right = right_parent;
+ }
+ }
+ document_order
+ }
+}
pub(crate) fn paint_parent(layout_arena: &impl PaintRead, slot: NodeSlotId) -> Option<NodeSlotId> {
if !layout_arena.paintable_row_is_populated(slot)
diff --git a/Libraries/LibWeb/Rust/src/painting/paint_order_plan.rs b/Libraries/LibWeb/Rust/src/painting/paint_order_plan.rs
index d1d26a6979f..8ea832314ba 100644
--- a/Libraries/LibWeb/Rust/src/painting/paint_order_plan.rs
+++ b/Libraries/LibWeb/Rust/src/painting/paint_order_plan.rs
@@ -13,7 +13,7 @@
use crate::layout::node_data::{NodeKind, NodeSlotId};
use crate::painting::paint_read::PaintRead;
use crate::painting::record::PaintPhase;
-use crate::painting::{node_painting, style_queries};
+use crate::painting::{node_painting, paint_order, style_queries};
use smallvec::SmallVec;
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
@@ -547,6 +547,7 @@ impl<R: PaintRead> PaintOrderBuilder<'_, R> {
phase: StackingContextPaintPhase,
matches: impl Fn(&Self, NodeSlotId) -> bool,
) {
+ let begin = self.items.len();
let mut next_child = crate::painting::paint_order::first_paint_child(self.layout_arena, paintable);
while let Some(child) = next_child {
next_child = crate::painting::paint_order::next_paint_sibling(self.layout_arena, child);
@@ -558,6 +559,14 @@ impl<R: PaintRead> PaintOrderBuilder<'_, R> {
kind: PaintScopeKind::Descendants(phase),
}));
}
+ let display = style_queries::display(self.layout_arena, paintable);
+ if display.is_flex_inside() || display.is_grid_inside() {
+ // A stable sort preserves document order for items in the same ordinal group.
+ self.items[begin..].sort_by_key(|item| match item {
+ PaintOrderItem::Scope(scope) => paint_order::order_for_painting(self.layout_arena, scope.owner),
+ PaintOrderItem::Producer(_) => unreachable!("only descendant scopes were appended"),
+ });
+ }
}
fn descendant_phase_is_empty(&self, child: NodeSlotId, phase: StackingContextPaintPhase) -> bool {
diff --git a/Libraries/LibWeb/Rust/src/painting/stacking_context/entries.rs b/Libraries/LibWeb/Rust/src/painting/stacking_context/entries.rs
index 29c83baf5dd..46d91b495af 100644
--- a/Libraries/LibWeb/Rust/src/painting/stacking_context/entries.rs
+++ b/Libraries/LibWeb/Rust/src/painting/stacking_context/entries.rs
@@ -118,20 +118,22 @@ impl LayoutNodeArena {
let Some(entries) = self.stacking_context_entries(enclosing) else {
return;
};
- // The places the box takes in the lists ordered by pre-order label, found before the table is written.
+ // Find the box's place in order-modified document order before writing the table.
let nonzero_z_index_insertion = facts.nonzero_z_index_child_contribution().map(|z_index| {
- let label = self.node_pre_order_label(slot);
- let position = entries
- .child_contexts_with_nonzero_z_index
- .partition_point(|entry| (entry.z_index, self.node_pre_order_label(entry.slot)) < (z_index, label));
+ let position = entries.child_contexts_with_nonzero_z_index.partition_point(|entry| {
+ entry
+ .z_index
+ .cmp(&z_index)
+ .then_with(|| self.compare_order_modified_document_order(entry.slot, slot))
+ .is_lt()
+ });
(position, z_index)
});
let stack_level_zero_position =
(nonzero_z_index_insertion.is_none() && facts.contributes_to_stack_level_zero()).then(|| {
- let label = self.node_pre_order_label(slot);
entries
.stack_level_zero_boxes
- .partition_point(|entry| self.node_pre_order_label(*entry) < label)
+ .partition_point(|entry| self.compare_order_modified_document_order(*entry, slot).is_lt())
});
let Some(mut table) = table_mut(&mut self.paintable_rows.stacking_context_entries, enclosing) else {
return;
@@ -221,6 +223,11 @@ impl LayoutNodeArena {
was_reattached: bool,
) {
if let Some(previous) = previous {
+ if previous.order != next.order {
+ self.note_paint_order_changed(slot);
+ self.flag_stacking_context_entries_for_resort(previous.enclosing_stacking_context);
+ self.flag_stacking_context_entries_for_resort(next.enclosing_stacking_context);
+ }
if previous.contribution_is_registered && previous.same_contributions_as(next) && !was_reattached {
next.contribution_is_registered = true;
return;
@@ -260,14 +267,17 @@ impl LayoutNodeArena {
continue;
};
table.needs_resort = false;
- // The lists are ordered by pre-order label, which the arena answers while the table is not borrowed.
+ // Comparing tree positions reads the arena while the table is not borrowed.
let mut child_contexts_with_nonzero_z_index =
std::mem::take(&mut table.child_contexts_with_nonzero_z_index);
let mut stack_level_zero_boxes = std::mem::take(&mut table.stack_level_zero_boxes);
drop(table);
- child_contexts_with_nonzero_z_index
- .sort_by_key(|entry| (entry.z_index, self.node_pre_order_label(entry.slot)));
- stack_level_zero_boxes.sort_by_key(|entry| self.node_pre_order_label(*entry));
+ child_contexts_with_nonzero_z_index.sort_by(|left, right| {
+ left.z_index
+ .cmp(&right.z_index)
+ .then_with(|| self.compare_order_modified_document_order(left.slot, right.slot))
+ });
+ stack_level_zero_boxes.sort_by(|&left, &right| self.compare_order_modified_document_order(left, right));
let mut table = table_mut(&mut self.paintable_rows.stacking_context_entries, root)
.expect("the root's table was there before its lists were sorted");
table.child_contexts_with_nonzero_z_index = child_contexts_with_nonzero_z_index;
diff --git a/Libraries/LibWeb/Rust/src/painting/stacking_context/mod.rs b/Libraries/LibWeb/Rust/src/painting/stacking_context/mod.rs
index 185856dc0a0..ebe39d64c0c 100644
--- a/Libraries/LibWeb/Rust/src/painting/stacking_context/mod.rs
+++ b/Libraries/LibWeb/Rust/src/painting/stacking_context/mod.rs
@@ -14,6 +14,7 @@ use crate::painting::style_queries::{self, effective_z_index};
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub(crate) struct StackingContextFacts {
+ pub order: i32,
pub establishes_stacking_context: bool,
pub effective_z_index: Option<i32>,
pub is_positioned: bool,
@@ -30,6 +31,7 @@ impl StackingContextFacts {
let is_inline_or_replaced =
style_queries::is_inline(arena, slot) || style_queries::is_replaced_box(arena, slot);
Self {
+ order: crate::painting::paint_order::order_for_painting(arena, slot),
establishes_stacking_context,
effective_z_index: effective_z_index(arena, slot),
is_positioned,
@@ -42,6 +44,7 @@ impl StackingContextFacts {
pub(crate) fn for_viewport() -> Self {
Self {
+ order: 0,
establishes_stacking_context: true,
effective_z_index: None,
is_positioned: false,
diff --git a/Libraries/LibWeb/Rust/src/painting/stacking_context/verify.rs b/Libraries/LibWeb/Rust/src/painting/stacking_context/verify.rs
index 60488f5377b..9bc98decc41 100644
--- a/Libraries/LibWeb/Rust/src/painting/stacking_context/verify.rs
+++ b/Libraries/LibWeb/Rust/src/painting/stacking_context/verify.rs
@@ -146,7 +146,14 @@ impl Verifier<'_> {
.expect("every expected root has an expected entry set");
expected
.child_contexts_with_nonzero_z_index
- .sort_by_key(|(z_index, _)| *z_index);
+ .sort_by(|(left_z_index, left), (right_z_index, right)| {
+ left_z_index
+ .cmp(right_z_index)
+ .then_with(|| self.arena.compare_order_modified_document_order(*left, *right))
+ });
+ expected
+ .stack_level_zero_boxes
+ .sort_by(|&left, &right| self.arena.compare_order_modified_document_order(left, right));
let Some(table) = self.arena.stacking_context_entries(root) else {
self.report
.note(format_args!("establishing box {root:?} has no entries table"));
diff --git a/Tests/LibWeb/Ref/expected/wpt-import/css/css-flexbox/flexbox-items-as-stacking-contexts-002-ref.html b/Tests/LibWeb/Ref/expected/wpt-import/css/css-flexbox/flexbox-items-as-stacking-contexts-002-ref.html
new file mode 100644
index 00000000000..a67a31936d9
--- /dev/null
+++ b/Tests/LibWeb/Ref/expected/wpt-import/css/css-flexbox/flexbox-items-as-stacking-contexts-002-ref.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 Reftest Reference</title>
+ <link rel="author" title="Daniel Holbert" href="mailto:[email protected]">
+ <style>
+ body { font: 10px sans-serif }
+ .flexContainer {
+ background: orange;
+ width: 70px;
+ padding: 2px;
+ margin-bottom: 2px;
+ }
+
+ .flexContainer > div:first-child {
+ margin-right: 10px; /* the space between the flex items, in testcase */
+ }
+
+ .item1 {
+ display: inline-block;
+ background: lightblue;
+ width: 30px;
+ }
+ .item2 {
+ display: inline-block;
+ background: yellow;
+ width: 30px;
+ }
+ </style>
+</head>
+<body>
+ <div class="flexContainer"
+ ><div class="item1">ThisIsALongUnbrokenString</div
+ ><div class="item2">HereIsSomeMoreLongText</div
+ ></div>
+
+ <div class="flexContainer"
+ ><div class="item1" style="position:relative">ThisIsALongUnbrokenString</div
+ ><div class="item2">HereIsSomeMoreLongText</div
+ ></div>
+
+ <div class="flexContainer"
+ ><div class="item1" style="position:relative">ThisIsALongUnbrokenString</div
+ ><div class="item2">HereIsSomeMoreLongText</div
+ ></div>
+
+ <div class="flexContainer"
+ ><div class="item2">HereIsSomeMoreLongText</div
+ ><div class="item1">ThisIsALongUnbrokenString</div
+ ></div>
+
+ <div class="flexContainer"
+ ><div class="item2">HereIsSomeMoreLongText</div
+ ><div class="item1">ThisIsALongUnbrokenString</div
+ ></div>
+
+ <div class="flexContainer"
+ ><div class="item2" style="position:relative">HereIsSomeMoreLongText</div
+ ><div class="item1">ThisIsALongUnbrokenString</div
+ ></div>
+</body>
+</html>
diff --git a/Tests/LibWeb/Ref/expected/wpt-import/css/css-flexbox/flexbox-paint-ordering-002-ref.xhtml b/Tests/LibWeb/Ref/expected/wpt-import/css/css-flexbox/flexbox-paint-ordering-002-ref.xhtml
new file mode 100644
index 00000000000..55ca62df74a
--- /dev/null
+++ b/Tests/LibWeb/Ref/expected/wpt-import/css/css-flexbox/flexbox-paint-ordering-002-ref.xhtml
@@ -0,0 +1,170 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!--
+ Any copyright is dedicated to the Public Domain.
+ http://creativecommons.org/publicdomain/zero/1.0/
+-->
+<!-- Reference case for flex items containing overlapping content.
+ This reference uses inline-block in place of inline-flex, with floated
+ children in place of flex items, and with hardcoded DOM-reordering in
+ place of "order" reordering. -->
+<html xmlns="http://www.w3.org/1999/xhtml">
+ <head>
+ <title>CSS Reftest Reference</title>
+ <link rel="author" title="Daniel Holbert" href="mailto:[email protected]"/>
+ <style>
+ body {
+ line-height: 0;
+ }
+
+ .flexbox {
+ display: inline-block;
+ width: 20px;
+ height: 10px;
+ border: 2px solid gray;
+ margin-bottom: 10px;
+ margin-right: 10px;
+ }
+ .a {
+ width: 10px;
+ height: 10px;
+ background: lightblue;
+ float: left; /* to stack horizontally, like a flex item */
+ }
+ .b {
+ width: 10px;
+ height: 10px;
+ background: pink;
+ float: left; /* to stack horizontally, like a flex item */
+ }
+ .aKid {
+ margin-left: 3px;
+ margin-top: 3px;
+ width: 10px;
+ height: 10px;
+ background: steelblue;
+ border: 1px solid blue;
+ }
+ .bKid {
+ margin-left: 3px;
+ margin-top: 6px;
+ width: 10px;
+ height: 10px;
+ background: violet;
+ border: 1px solid purple;
+ }
+
+ /* Need to set 'position' for z-index to take effect. */
+ .zn2 { z-index: -2; position: relative; }
+ .zn1 { z-index: -1; position: relative; }
+ .z0 { z-index: 0; position: relative; }
+ .z1 { z-index: 1; position: relative; }
+
+ </style>
+ </head>
+ <body>
+ <!-- order not set: -->
+ <div class="flexbox">
+ <div class="a"><div class="aKid"/></div>
+ <div class="b"><div class="bKid"/></div>
+ </div>
+
+ <br/>
+
+ <!-- order set, but it matches content order, so it shouldn't matter: -->
+ <div class="flexbox">
+ <div class="a"><div class="aKid"/></div>
+ <div class="b"><div class="bKid"/></div>
+ </div
+ ><div class="flexbox">
+ <div class="a"><div class="aKid"/></div>
+ <div class="b"><div class="bKid"/></div>
+ </div
+ ><div class="flexbox">
+ <div class="a"><div class="aKid"/></div>
+ <div class="b"><div class="bKid"/></div>
+ </div
+ ><div class="flexbox">
+ <div class="a"><div class="aKid"/></div>
+ <div class="b"><div class="bKid"/></div>
+ </div
+ ><div class="flexbox">
+ <div class="a"><div class="aKid"/></div>
+ <div class="b"><div class="bKid"/></div>
+ </div
+ ><div class="flexbox">
+ <div class="a"><div class="aKid"/></div>
+ <div class="b"><div class="bKid"/></div>
+ </div>
+
+ <br/>
+
+ <!-- order set to reverse of content-order: -->
+ <div class="flexbox">
+ <div class="b"><div class="bKid"/></div>
+ <div class="a"><div class="aKid"/></div>
+ </div
+ ><div class="flexbox">
+ <div class="b"><div class="bKid"/></div>
+ <div class="a"><div class="aKid"/></div>
+ </div
+ ><div class="flexbox">
+ <div class="b"><div class="bKid"/></div>
+ <div class="a"><div class="aKid"/></div>
+ </div
+ ><div class="flexbox">
+ <div class="b"><div class="bKid"/></div>
+ <div class="a"><div class="aKid"/></div>
+ </div>
+
+ <br/>
+
+ <!-- order set to reverse of content-order, AND with z-index set on
+ one or both items, but not such that it changes the paint order -->
+ <div class="flexbox">
+ <div class="b"><div class="bKid"/></div>
+ <div class="a"><div class="aKid"/></div>
+ </div
+ ><div class="flexbox">
+ <div class="b"><div class="bKid"/></div>
+ <div class="a"><div class="aKid"/></div>
+ </div
+ ><div class="flexbox">
+ <div class="b"><div class="bKid"/></div>
+ <div class="a"><div class="aKid"/></div>
+ </div
+ ><div class="flexbox">
+ <div class="b"><div class="bKid"/></div>
+ <div class="a"><div class="aKid"/></div>
+ </div>
+
+ <br/>
+
+ <!-- order set to reverse of content-order, AND with z-index set on
+ one or both items, in such a way that it affects paint order -->
+ <div class="flexbox">
+ <!-- 'a' is behind the container's border -->
+ <div class="b"><div class="bKid"/></div>
+ <div class="a zn1"><div class="aKid"/></div>
+ </div
+ ><div class="flexbox">
+ <!-- 'a' and 'b' are both behind the container's border -->
+ <div class="b zn1"><div class="bKid"/></div>
+ <div class="a zn1"><div class="aKid"/></div>
+ </div
+ ><div class="flexbox">
+ <!-- 'a' and 'b' are both behind the container's border,
+ and 'a' is behind 'b' despite coming after it in the 'order'
+ ordering-->
+ <div class="b zn1"><div class="bKid"/></div>
+ <div class="a zn2"><div class="aKid"/></div>
+ </div
+ ><div class="flexbox">
+ <!-- 'a' and 'b' are both in front of the container's border,
+ and 'a' is behind 'b' despite coming after it in the 'order'
+ ordering-->
+ <div class="b z1"><div class="bKid"/></div>
+ <div class="a z0"><div class="aKid"/></div>
+ </div>
+
+ </body>
+</html>
diff --git a/Tests/LibWeb/Ref/input/wpt-import/css/css-flexbox/flexbox-items-as-stacking-contexts-002.html b/Tests/LibWeb/Ref/input/wpt-import/css/css-flexbox/flexbox-items-as-stacking-contexts-002.html
new file mode 100644
index 00000000000..093cb9d0ef4
--- /dev/null
+++ b/Tests/LibWeb/Ref/input/wpt-import/css/css-flexbox/flexbox-items-as-stacking-contexts-002.html
@@ -0,0 +1,90 @@
+<!DOCTYPE html>
+<!--
+ Any copyright is dedicated to the Public Domain.
+ http://creativecommons.org/publicdomain/zero/1.0/
+-->
+<!-- This testcase checks flex items are painted atomically. In particular,
+ if one item has content that overflows into the region of another item,
+ then one item is painted "behind" the other; there shouldn't normally
+ any interleaving of backgrounds and content between the two items.
+
+ This testcase also tests some special cases that will change the paint
+ ordering - specifically, the properties "position", "z-index", and
+ "order" on flex items.
+ -->
+<!-- This was resolved by the CSSWG in April 2013:
+ http://krijnhoetmer.nl/irc-logs/css/20130403#l-455 -->
+<html>
+<head>
+ <title>CSS Test: Testing that flex items paint as pseudo-stacking contexts (like inline-blocks): atomically, in the absence of 'z-index' on descendants</title>
+ <link rel="author" title="Daniel Holbert" href="mailto:[email protected]">
+ <link rel="help" href="http://www.w3.org/TR/css-flexbox-1/#painting">
+ <link rel="match" href="../../../../expected/wpt-import/css/css-flexbox/flexbox-items-as-stacking-contexts-002-ref.html">
+ <style>
+ body { font: 10px sans-serif }
+ .flexContainer {
+ background: orange;
+ display: flex;
+ justify-content: space-between;
+ width: 70px;
+ padding: 2px;
+ margin-bottom: 2px;
+ }
+ .item1 {
+ background: lightblue;
+ width: 30px;
+ min-width: 0; /* disable default min-width:auto behavior */
+ }
+ .item2 {
+ background: yellow;
+ width: 30px;
+ min-width: 0; /* disable default min-width:auto behavior */
+ }
+ </style>
+</head>
+<body>
+ <!-- This container has two flex items, the first of which has content
+ sticking out & overlapping the second. If they're painting atomically
+ (and in the right order), the second item's background should cover the
+ first item's overflowing content. -->
+ <div class="flexContainer"
+ ><div class="item1">ThisIsALongUnbrokenString</div
+ ><div class="item2">HereIsSomeMoreLongText</div
+ ></div>
+
+ <!-- Now, the first item is relatively positioned, which should make it paint
+ on top of everything. -->
+ <div class="flexContainer"
+ ><div class="item1" style="position:relative">ThisIsALongUnbrokenString</div
+ ><div class="item2">HereIsSomeMoreLongText</div
+ ></div>
+
+ <!-- Now, the first item is has "z-index" set, which should make it paint on
+ top of everything. -->
+ <div class="flexContainer"
+ ><div class="item1" style="z-index: 1">ThisIsALongUnbrokenString</div
+ ><div class="item2">HereIsSomeMoreLongText</div
+ ></div>
+
+ <!-- Now, the first item has "order" set to a higher value than default,
+ which should make it paint on top (and at the far right) -->
+ <div class="flexContainer"
+ ><div class="item1" style="order: 1">ThisIsALongUnbrokenString</div
+ ><div class="item2">HereIsSomeMoreLongText</div
+ ></div>
+
+ <!-- And for thoroughness, let's set "order" to a lower value than default,
+ on the second item. (Should render the same as previous example.) -->
+ <div class="flexContainer"
+ ><div class="item1">ThisIsALongUnbrokenString</div
+ ><div class="item2" style="order: -1">HereIsSomeMoreLongText</div
+ ></div>
+
+ <!-- ...but if we relatively position that second item, it should paint
+ on top again, despite its low "order" value. -->
+ <div class="flexContainer"
+ ><div class="item1">ThisIsALongUnbrokenString</div
+ ><div class="item2" style="order: -1; position: relative">HereIsSomeMoreLongText</div
+ ></div>
+</body>
+</html>
diff --git a/Tests/LibWeb/Ref/input/wpt-import/css/css-flexbox/flexbox-paint-ordering-002.xhtml b/Tests/LibWeb/Ref/input/wpt-import/css/css-flexbox/flexbox-paint-ordering-002.xhtml
new file mode 100644
index 00000000000..7c0b78e61eb
--- /dev/null
+++ b/Tests/LibWeb/Ref/input/wpt-import/css/css-flexbox/flexbox-paint-ordering-002.xhtml
@@ -0,0 +1,176 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!--
+ Any copyright is dedicated to the Public Domain.
+ http://creativecommons.org/publicdomain/zero/1.0/
+-->
+<!-- Testcase with flex items containing overlapping content, with
+ "order" and "z-index" set on some of them, to test how that affects
+ paint-order. -->
+<html xmlns="http://www.w3.org/1999/xhtml">
+ <head>
+ <title>CSS Test: Testing the paint-order of overlapping flex items with 'order' and 'z-index' set</title>
+ <link rel="author" title="Daniel Holbert" href="mailto:[email protected]"/>
+ <link rel="help" href="http://www.w3.org/TR/css-flexbox-1/#painting"/>
+ <link rel="match" href="../../../../expected/wpt-import/css/css-flexbox/flexbox-paint-ordering-002-ref.xhtml"/>
+ <style>
+ body {
+ line-height: 0;
+ }
+
+ .flexbox {
+ display: inline-flex;
+ width: 20px;
+ height: 10px;
+ border: 2px solid gray;
+ margin-bottom: 10px;
+ margin-right: 10px;
+ }
+ .a {
+ width: 10px;
+ height: 10px;
+ background: lightblue;
+ min-width: 0;
+ }
+ .b {
+ width: 10px;
+ height: 10px;
+ background: pink;
+ min-width: 0;
+ }
+
+ .aKid {
+ margin-left: 3px;
+ margin-top: 3px;
+ width: 10px;
+ height: 10px;
+ background: steelblue;
+ border: 1px solid blue;
+ }
+ .bKid {
+ margin-left: 3px;
+ margin-top: 6px;
+ width: 10px;
+ height: 10px;
+ background: violet;
+ border: 1px solid purple;
+ }
+
+ .on1 { order: -1; }
+ .o0 { order: 0; }
+ .o1 { order: 1; }
+ .o2 { order: 2; }
+
+ .zn2 { z-index: -2; }
+ .zn1 { z-index: -1; }
+ .z0 { z-index: 0; }
+ .z1 { z-index: 1; }
+
+ </style>
+ </head>
+ <body>
+ <!-- order not set: -->
+ <div class="flexbox">
+ <div class="a"><div class="aKid"/></div>
+ <div class="b"><div class="bKid"/></div>
+ </div>
+
+ <br/>
+
+ <!-- order set, but it matches content order, so it shouldn't matter: -->
+ <div class="flexbox">
+ <div class="a on1"><div class="aKid"/></div>
+ <div class="b"><div class="bKid"/></div>
+ </div
+ ><div class="flexbox">
+ <div class="a o0"><div class="aKid"/></div>
+ <div class="b"><div class="bKid"/></div>
+ </div
+ ><div class="flexbox">
+ <div class="a o0"><div class="aKid"/></div>
+ <div class="b o0"><div class="bKid"/></div>
+ </div
+ ><div class="flexbox">
+ <div class="a o2"><div class="aKid"/></div>
+ <div class="b o2"><div class="bKid"/></div>
+ </div
+ ><div class="flexbox">
+ <div class="a"><div class="aKid"/></div>
+ <div class="b o0"><div class="bKid"/></div>
+ </div
+ ><div class="flexbox">
+ <div class="a"><div class="aKid"/></div>
+ <div class="b o1"><div class="bKid"/></div>
+ </div>
+
+ <br/>
+
+ <!-- order set to reverse of content-order: -->
+ <div class="flexbox">
+ <div class="a o1"><div class="aKid"/></div>
+ <div class="b"><div class="bKid"/></div>
+ </div
+ ><div class="flexbox">
+ <div class="a"><div class="aKid"/></div>
+ <div class="b on1"><div class="bKid"/></div>
+ </div
+ ><div class="flexbox">
+ <div class="a o0"><div class="aKid"/></div>
+ <div class="b on1"><div class="bKid"/></div>
+ </div
+ ><div class="flexbox">
+ <div class="a o2"><div class="aKid"/></div>
+ <div class="b o1"><div class="bKid"/></div>
+ </div>
+
+ <br/>
+
+ <!-- order set to reverse of content-order, AND with z-index set on
+ one or both items, but not such that it changes the paint order -->
+ <div class="flexbox">
+ <div class="a o1 z0"><div class="aKid"/></div>
+ <div class="b"><div class="bKid"/></div>
+ </div
+ ><div class="flexbox">
+ <div class="a o1 z1"><div class="aKid"/></div>
+ <div class="b"><div class="bKid"/></div>
+ </div
+ ><div class="flexbox">
+ <div class="a o1 z0"><div class="aKid"/></div>
+ <div class="b z0"><div class="bKid"/></div>
+ </div
+ ><div class="flexbox">
+ <div class="a o1 z1"><div class="aKid"/></div>
+ <div class="b z0"><div class="bKid"/></div>
+ </div>
+
+ <br/>
+
+ <!-- order set to reverse of content-order, AND with z-index set on
+ one or both items, in such a way that it affects paint order -->
+ <div class="flexbox">
+ <!-- 'a' is behind the container's border -->
+ <div class="a o1 zn1"><div class="aKid"/></div>
+ <div class="b"><div class="bKid"/></div>
+ </div
+ ><div class="flexbox">
+ <!-- 'a' and 'b' are both behind the container's border -->
+ <div class="a o1 zn1"><div class="aKid"/></div>
+ <div class="b zn1"><div class="bKid"/></div>
+ </div
+ ><div class="flexbox">
+ <!-- 'a' and 'b' are both behind the container's border,
+ and 'a' is behind 'b' despite coming after it in the 'order'
+ ordering-->
+ <div class="a o1 zn2"><div class="aKid"/></div>
+ <div class="b zn1"><div class="bKid"/></div>
+ </div
+ ><div class="flexbox">
+ <!-- 'a' and 'b' are both in front of the container's border,
+ and 'a' is behind 'b' despite coming after it in the 'order'
+ ordering-->
+ <div class="a o1 z0"><div class="aKid"/></div>
+ <div class="b z1"><div class="bKid"/></div>
+ </div>
+
+ </body>
+</html>
diff --git a/Tests/LibWeb/Ref/input/wpt-import/css/css-flexbox/order/order-abs-children-painting-order.html b/Tests/LibWeb/Ref/input/wpt-import/css/css-flexbox/order/order-abs-children-painting-order.html
new file mode 100644
index 00000000000..cc29afe6fb2
--- /dev/null
+++ b/Tests/LibWeb/Ref/input/wpt-import/css/css-flexbox/order/order-abs-children-painting-order.html
@@ -0,0 +1,49 @@
+<!DOCTYPE html>
+<title>CSS Test: flex order affects painting order of absolutely positioned boxes inside flex</title>
+<link rel="author" title="Dennis Schubert" href="mailto:[email protected]">
+<link rel="author" href="https://mozilla.org" title="Mozilla">
+<link rel="help" href="https://drafts.csswg.org/css-flexbox/#propdef-order">
+<link rel="help" href="https://bugs.chromium.org/p/chromium/issues/detail?id=606208">
+<link rel="match" href="../../../../../expected/wpt-import/css/css-flexbox/order/../../reference/ref-filled-green-100px-square-only.html">
+<meta name="assert" content="This test check that absolutely positioned boxes inside flex items have their painting orders affected by 'order'.">
+<style>
+ #test {
+ display: flex;
+ position: relative;
+ height: 100px;
+ width: 100px;
+ }
+
+ #order-1 {
+ order: 1;
+ }
+
+ #order-2 {
+ order: 2;
+ }
+
+ .inner {
+ position: absolute;
+ bottom: 0;
+ left: 0;
+ right: 0;
+ top: 0;
+ }
+
+ #order-1 .inner {
+ background-color: red;
+ }
+
+ #order-2 .inner {
+ background-color: green;
+ }
+</style>
+<p>Test passes if there is a filled green square.</p>
+<div id="test">
+ <div id="order-2">
+ <div class="inner"></div>
+ </div>
+ <div id="order-1">
+ <div class="inner"></div>
+ </div>
+</div>
diff --git a/Tests/LibWeb/Ref/input/wpt-import/css/css-grid/grid-items/grid-inline-order-property-painting-005.html b/Tests/LibWeb/Ref/input/wpt-import/css/css-grid/grid-items/grid-inline-order-property-painting-005.html
new file mode 100644
index 00000000000..e452e91f7d9
--- /dev/null
+++ b/Tests/LibWeb/Ref/input/wpt-import/css/css-grid/grid-items/grid-inline-order-property-painting-005.html
@@ -0,0 +1,35 @@
+<!DOCTYPE html>
+<meta charset="utf-8">
+<title>CSS Grid Layout Test: 'order' property affects grid items painting order within an inline grid</title>
+<link rel="author" title="Manuel Rego Casasnovas" href="mailto:[email protected]">
+<link rel="help" href="http://www.w3.org/TR/css-grid-1/#order-property" title="4.2 Reordered Grid Items: the order property">
+<link rel="help" href="https://drafts.csswg.org/css-flexbox-1/#order-property" title="5.4. Display Order: the order property">
+<link rel="match" href="../../../../../expected/wpt-import/css/css-grid/grid-items/../../reference/ref-filled-green-100px-square.xht">
+<meta name="flags" content="ahem">
+<link rel="stylesheet" type="text/css" href="../../../fonts/ahem.css" />
+<style>
+ #inline-grid {
+ display: inline-grid;
+ font: 100px/1 Ahem;
+ }
+
+ #test-item-overlapping-green {
+ color: green;
+ order: 1;
+ }
+
+ #reference-item-overlapped-red {
+ color: red;
+ order: -1;
+ }
+
+ .first-row-first-column {
+ grid-area: 1 / 1;
+ }
+</style>
+<p>Test passes if there is a filled green square and <strong>no red</strong>.</p>
+
+<div id="inline-grid">
+ <div id="test-item-overlapping-green" class="first-row-first-column">G</div>
+ <div id="reference-item-overlapped-red" class="first-row-first-column">R</div>
+</div>
diff --git a/Tests/LibWeb/Ref/input/wpt-import/css/css-grid/grid-items/grid-order-property-painting-001.html b/Tests/LibWeb/Ref/input/wpt-import/css/css-grid/grid-items/grid-order-property-painting-001.html
new file mode 100644
index 00000000000..017f548e8f3
--- /dev/null
+++ b/Tests/LibWeb/Ref/input/wpt-import/css/css-grid/grid-items/grid-order-property-painting-001.html
@@ -0,0 +1,34 @@
+<!DOCTYPE html>
+<meta charset="utf-8">
+<title>CSS Grid Layout Test: 'order' property affects grid items painting order</title>
+<link rel="author" title="Manuel Rego Casasnovas" href="mailto:[email protected]">
+<link rel="help" href="http://www.w3.org/TR/css-grid-1/#order-property" title="4.2 Reordered Grid Items: the order property">
+<link rel="help" href="https://drafts.csswg.org/css-flexbox-1/#order-property" title="5.4. Display Order: the order property">
+<link rel="match" href="../../../../../expected/wpt-import/css/css-grid/grid-items/../../reference/ref-filled-green-100px-square.xht">
+<meta name="flags" content="ahem">
+<link rel="stylesheet" type="text/css" href="../../../fonts/ahem.css" />
+<style>
+ #grid {
+ display: grid;
+ font: 100px/1 Ahem;
+ }
+
+ #test-item-overlapping-green {
+ color: green;
+ order: 1;
+ }
+
+ #reference-item-overlapped-red {
+ color: red;
+ }
+
+ .first-row-first-column {
+ grid-area: 1 / 1;
+ }
+</style>
+<p>Test passes if there is a filled green square and <strong>no red</strong>.</p>
+
+<div id="grid">
+ <div id="test-item-overlapping-green" class="first-row-first-column">G</div>
+ <div id="reference-item-overlapped-red" class="first-row-first-column">R</div>
+</div>
diff --git a/Tests/LibWeb/Text/expected/grid-order-painting.txt b/Tests/LibWeb/Text/expected/grid-order-painting.txt
new file mode 100644
index 00000000000..83d5d9ae1cc
--- /dev/null
+++ b/Tests/LibWeb/Text/expected/grid-order-painting.txt
@@ -0,0 +1,7 @@
+first
+second
+first
+second
+first-child
+second-child
+first
diff --git a/Tests/LibWeb/Text/expected/wpt-import/css/css-flexbox/hittest-overlapping-order.txt b/Tests/LibWeb/Text/expected/wpt-import/css/css-flexbox/hittest-overlapping-order.txt
index 7676850f971..bef796ddefc 100644
--- a/Tests/LibWeb/Text/expected/wpt-import/css/css-flexbox/hittest-overlapping-order.txt
+++ b/Tests/LibWeb/Text/expected/wpt-import/css/css-flexbox/hittest-overlapping-order.txt
@@ -2,5 +2,5 @@ Harness status: OK
Found 1 tests
-1 Fail
-Fail Flexboxes should perform hit testing in reverse paint order for overlapping elements: flex order case (crbug.com/844505)
\ No newline at end of file
+1 Pass
+Pass Flexboxes should perform hit testing in reverse paint order for overlapping elements: flex order case (crbug.com/844505)
\ No newline at end of file
diff --git a/Tests/LibWeb/Text/input/grid-order-painting.html b/Tests/LibWeb/Text/input/grid-order-painting.html
new file mode 100644
index 00000000000..dbb73fc0637
--- /dev/null
+++ b/Tests/LibWeb/Text/input/grid-order-painting.html
@@ -0,0 +1,58 @@
+<!doctype html>
+<script src="include.js"></script>
+<style>
+ body {
+ margin: 0;
+ }
+ #grid {
+ display: grid;
+ position: relative;
+ isolation: isolate;
+ width: 100px;
+ height: 100px;
+ }
+ #grid > div {
+ grid-area: 1 / 1;
+ width: 100px;
+ height: 100px;
+ }
+ .positioned {
+ position: absolute;
+ inset: 0;
+ z-index: 1;
+ }
+</style>
+<div id="grid">
+ <div id="first"></div>
+ <div id="second"></div>
+</div>
+<script>
+ test(() => {
+ const first = document.getElementById("first");
+ const second = document.getElementById("second");
+
+ for (const zIndex of ["auto", "0"]) {
+ first.style.zIndex = second.style.zIndex = zIndex;
+ for (const order of ["1", "-1"]) {
+ first.style.order = order;
+ println(document.elementFromPoint(50, 50).id);
+ }
+ }
+
+ first.style.zIndex = second.style.zIndex = "auto";
+ first.innerHTML = '<div><div><div id="first-child" class="positioned"></div></div></div>';
+ second.innerHTML = '<div><div><div id="second-child" class="positioned"></div></div></div>';
+ first.style.order = "1";
+ println(document.elementFromPoint(50, 50).id);
+ first.style.order = "-1";
+ println(document.elementFromPoint(50, 50).id);
+
+ first.replaceChildren();
+ second.replaceChildren();
+ first.style.zIndex = "0";
+ first.style.order = "1";
+ second.style.position = "absolute";
+ second.style.order = "100";
+ println(document.elementFromPoint(50, 50).id);
+ });
+</script>