[FlexNG] Expand row cross size for fragmentation

Previously, for flex rows, when items expanded as a result of
fragmentation, we would only adjust the subsequent rows' offsets based
on the expansion amount. However, we also want to expand the row
block-size to allow items in that row to stretch to the newly expanded
size.

Because flex items in a row can break before, we couldn't implement
this in the same way as grid (i.e. by storing the results and
offsets and then adding them to the builder once all expansion is
handled). Otherwise, we could end up with a child break before added
to the builder before the previous items in the row had been added.

Instead, we need to abort layout and re-run with the updated row
block-sizes (using the newly added layout result status of
kNeedsRelayoutWithRowCrossSizeChanges and the newly added
RelayoutWithNewRowSizes() method).

Since we will re-layout as we go, we can no longer wait to detect
the row expansion until items have completed layout, but we need to
find the max row expansion in each fragmentainer as we go (and as
items cross the current row block-size). As such, we now calculate
row expansion without making use of NGFlexItem's
total_remaining_block_size (as we do for columns).

Because more than one row can expand per fragmentainer, we can avoid
aborting layout more than once by adjusting subsequent row offsets
as expansion is discovered and by keeping track of the expansion
of all rows in a given fragmentainer (there should only be at most two
row expansions at a time, though).

While testing, I ran into a few additional issues:

1. I had noticed a bug with how row expansion was handled for items
that broke before. We would previously adjust the offset of all items
in the row rather than just the offset of the item itself. This is now
fixed by not updating |line_output.item_offset_adjustment| in this
case.

2. When checking if the |broke_before_row| is associated with the
current row, we can't just check if we are at an item at index 0
since a previous item may have broken as a result of overflow.
Check instead if the current item is at index 0 and that it broke
before.

3. When adding |item_offset_adjustment| of one row to the next,
check if the last item has a break token or if the line has seen
all children so that we do so even if the last item overflows. (But
if the last item overflowed, we only want to adjust
|item_offset_adjustment| if there aren't any additional break tokens
to process, in which case the next row would have already started
making progress).

Note: We will stop row expansion if the container's consumed block-size
hits the max to avoid infinite expansion. This keeps the following test
from timing out:
external/wpt/css/css-break/flexbox/flexbox-fragmentation-layout-001-crash.html

multi-line-row-flex-fragmentation-016.html was previously incorrect
since item stretching as a result of row expansion was not yet
implemented. The test is now updated to account for the new
behavior.

Bug: 660611
Change-Id: If4fca22d10a43052a2f0218333d3a74752762b79
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/3561235
Reviewed-by: Morten Stenshorne <mstensho@chromium.org>
Commit-Queue: Alison Maher <almaher@microsoft.com>
Cr-Commit-Position: refs/heads/main@{#988612}
diff --git a/css/css-break/flexbox/multi-line-row-flex-fragmentation-016.html b/css/css-break/flexbox/multi-line-row-flex-fragmentation-016.html
index fbd557c..f723663 100644
--- a/css/css-break/flexbox/multi-line-row-flex-fragmentation-016.html
+++ b/css/css-break/flexbox/multi-line-row-flex-fragmentation-016.html
@@ -12,27 +12,26 @@
   }
   #flex > div {
     background: green;
-    width: 10px;
+    width: 20px;
   }
 </style>
 <p>Test passes if there is a filled green square and <strong>no red</strong>.</p>
-<div style="width: 100px; height: 100px; columns: 5; column-gap: 0; column-fill: auto; background: red;">
+<div style="width: 100px; height: 100px; columns: 5; column-gap: 0; column-fill: auto; position: relative; background: red;">
+  <div style="position: absolute; top: 40px; left: 20px; width: 20px; height: 60px; background: green;"></div>
   <div id="flex">
-    <div style="position: relative;">
+    <div style="width: 10px;">
       <div style="contain: size; width: 10px; height: 80px;"></div>
       <div style="contain: size; width: 10px; height: 30px;"></div>
-      <div style="position: absolute; width: 10px; height: 10px; background: green;"></div>
     </div>
-    <div>
+    <div style="width: 10px;">
       <div style="contain: size; width: 10px; height: 70px;"></div>
       <div style="contain: size; width: 10px; height: 40px;"></div>
     </div>
-    <div style="margin-top: 10px; width: 20px; position: relative;">
+    <div style="margin-top: 10px;">
       <div style="contain: size; width: 20px; height: 80px;"></div>
       <div style="contain: size; width: 20px; height: 40px;"></div>
-      <div style="position: absolute; top: -60px; width: 20px; height: 60px; background: green;"></div>
     </div>
-    <div style="height: 100px; width: 20px;"></div>
-    <div style="height: 60px; width: 20px;"></div>
+    <div style="height: 100px;"></div>
+    <div style="height: 60px;"></div>
   </div>
 </div>
diff --git a/css/css-break/flexbox/multi-line-row-flex-fragmentation-052.html b/css/css-break/flexbox/multi-line-row-flex-fragmentation-052.html
new file mode 100644
index 0000000..8f5a612
--- /dev/null
+++ b/css/css-break/flexbox/multi-line-row-flex-fragmentation-052.html
@@ -0,0 +1,33 @@
+<!DOCTYPE html>
+<title>
+  Multi-line row flex fragmentation: Row expansion and stretching.
+</title>
+<link rel="help" href="https://drafts.csswg.org/css-flexbox-1/#pagination">
+<link rel="match" href="../../reference/ref-filled-green-100px-square.xht">
+<style>
+  #flex {
+    display: flex;
+    flex-wrap: wrap;
+  }
+  #flex > div {
+    background: green;
+    width: 10px;
+  }
+</style>
+<p>Test passes if there is a filled green square and <strong>no red</strong>.</p>
+<div style="width: 100px; height: 100px; columns: 5; column-gap: 0; column-fill: auto; position: relative; background: red;">
+  <div style="position: absolute; top: 50px; width: 10px; height: 50px; background: green;"></div>
+  <div style="position: absolute; top: 50px; left: 40px; width: 20px; height: 50px; background: green;"></div>
+  <div style="position: absolute; left: 70px; width: 10px; height: 50px; background: green;"></div>
+  <div style="position: absolute; top: 70px; left: 60px; width: 10px; height: 30px; background: green;"></div>
+  <div id="flex">
+    <div style="width: 20px; height: 50px;"></div>
+    <div style="margin-top: 40px; height: 60px; contain: size;"></div>
+    <div></div>
+    <div style="width: 20px; height: 90px;"></div>
+    <div style="margin-top: 40px; height: 70px; contain: size;"></div>
+    <div style="margin-top: 100px; height: 50px; contain: size;"></div>
+    <div style="height: 100px; break-before: column;"></div>
+    <div></div>
+  </div>
+</div>
diff --git a/css/css-break/flexbox/multi-line-row-flex-fragmentation-053.html b/css/css-break/flexbox/multi-line-row-flex-fragmentation-053.html
new file mode 100644
index 0000000..f42d578
--- /dev/null
+++ b/css/css-break/flexbox/multi-line-row-flex-fragmentation-053.html
@@ -0,0 +1,37 @@
+<!DOCTYPE html>
+<title>
+  Multi-line row flex fragmentation: Row expansion and stretching with item overflow.
+</title>
+<link rel="help" href="https://drafts.csswg.org/css-flexbox-1/#pagination">
+<link rel="match" href="../../reference/ref-filled-green-100px-square.xht">
+<style>
+  #flex {
+    display: flex;
+    flex-wrap: wrap;
+  }
+  #flex > div {
+    background: green;
+    width: 50%;
+  }
+</style>
+<p>Test passes if there is a filled green square and <strong>no red</strong>.</p>
+<div style="width: 100px; height: 100px; columns: 5; column-gap: 0; column-fill: auto; position: relative; background: red;">
+  <div id="flex">
+    <div>
+      <div style="height: 30px;"></div>
+      <div style="height: 50px; break-before: column;"></div> <!-- The break-before forces the grid-item into the next column, and grows the row. -->
+    </div>
+    <div></div>
+    <div>
+      <div style="height: 30px;"></div>
+      <div style="height: 10px; break-before: column;"></div> <!-- The break-before forces the grid-item into the next column, and grows the row. -->
+    </div>
+    <div></div>
+    <div></div>
+    <div style="height: 100px;">
+      <div style="height: 200px;"></div>
+    </div>
+    <div style="height: 190px;"></div>
+    <div></div>
+  </div>
+</div>
diff --git a/css/css-break/flexbox/multi-line-row-flex-fragmentation-054.html b/css/css-break/flexbox/multi-line-row-flex-fragmentation-054.html
new file mode 100644
index 0000000..9752217
--- /dev/null
+++ b/css/css-break/flexbox/multi-line-row-flex-fragmentation-054.html
@@ -0,0 +1,32 @@
+<!DOCTYPE html>
+<title>
+  Multi-line row flex fragmentation: Row expansion and stretching.
+</title>
+<link rel="help" href="https://drafts.csswg.org/css-flexbox-1/#pagination">
+<link rel="match" href="../../reference/ref-filled-green-100px-square.xht">
+<style>
+  #flex {
+    display: flex;
+    flex-wrap: wrap;
+  }
+  #flex > div {
+    background: green;
+    width: 10px;
+  }
+</style>
+<p>Test passes if there is a filled green square and <strong>no red</strong>.</p>
+<div style="width: 100px; height: 100px; columns: 5; column-gap: 0; column-fill: auto; position: relative; background: red;">
+  <div style="position: absolute; top: 40px; left: 20px; width: 20px; height: 60px; background: green;"></div>
+  <div id="flex">
+    <div>
+      <div style="contain: size; width: 10px; height: 80px;"></div>
+      <div style="contain: size; width: 10px; height: 30px;"></div>
+    </div>
+    <div>
+      <div style="contain: size; width: 10px; height: 70px;"></div>
+      <div style="contain: size; width: 10px; height: 40px;"></div>
+    </div>
+    <div style="width: 20px; height: 100px; break-inside: avoid;"></div>
+    <div style="height: 200px; width: 20px;"></div>
+  </div>
+</div>
diff --git a/css/css-break/flexbox/multi-line-row-flex-fragmentation-055.html b/css/css-break/flexbox/multi-line-row-flex-fragmentation-055.html
new file mode 100644
index 0000000..39d2c64
--- /dev/null
+++ b/css/css-break/flexbox/multi-line-row-flex-fragmentation-055.html
@@ -0,0 +1,27 @@
+<!DOCTYPE html>
+<title>
+  Multi-line row flex fragmentation: Item overflow and stretching.
+</title>
+<link rel="help" href="https://drafts.csswg.org/css-flexbox-1/#pagination">
+<link rel="match" href="../../reference/ref-filled-green-100px-square.xht">
+<style>
+  #flex {
+    display: flex;
+    flex-wrap: wrap;
+  }
+  #flex > div {
+    background: green;
+    width: 50%;
+  }
+</style>
+<p>Test passes if there is a filled green square and <strong>no red</strong>.</p>
+<div style="width: 100px; height: 100px; columns: 5; column-gap: 0; column-fill: auto; position: relative; background: red;">
+  <div id="flex">
+    <div style="height: 100px;">
+      <div style="height: 200px;"></div>
+    </div>
+    <div></div>
+    <div style="height: 400px;"></div>
+    <div></div>
+  </div>
+</div>
diff --git a/css/css-break/flexbox/single-line-row-flex-fragmentation-029.html b/css/css-break/flexbox/single-line-row-flex-fragmentation-029.html
new file mode 100644
index 0000000..5d7e72d
--- /dev/null
+++ b/css/css-break/flexbox/single-line-row-flex-fragmentation-029.html
@@ -0,0 +1,27 @@
+<!DOCTYPE html>
+<title>
+  Single-line row flex fragmentation: Row expansion, stretching and column balancing.
+</title>
+<link rel="help" href="https://drafts.csswg.org/css-flexbox-1/#pagination">
+<link rel="match" href="../../reference/ref-filled-green-100px-square.xht">
+<style>
+  #flex {
+    display: flex;
+  }
+  #flex > div {
+    background: green;
+    width: 25px;
+  }
+</style>
+<p>Test passes if there is a filled green square and <strong>no red</strong>.</p>
+<div style="width: 100px; height: 100px; background: red;">
+  <div style="width: 100px; columns: 2; column-gap: 0;">
+    <div id="flex">
+      <div></div>
+      <div>
+        <div style="contain: size; width: 10px; height: 70px;"></div>
+        <div style="contain: size; width: 10px; height: 100px;"></div>
+      </div>
+    </div>
+  </div>
+</div>