Unify CreatesNewFormattingContext() and AvoidsFloats().

Remove AvoidsFloats() and have CreatesNewFormattingContext() behave
like AvoidsFloats() used to.

The difference between the two seemed subtle, wrong and useless.
IMG fallback (a block-flow) would previously not establish a formatting
context, just avoid floats. IMG fallback is implemented as a floated
"broken image" IMG child, with the ALT text next to it.

Two tests need to be changed/rebaselined. Both were about IMG fallback.
One expected the "broken image" container to not stretch its height to
encompass the child "broken image" image, while the other test expected
margins before and after an IMG to collapse, if the IMG was missing
(which it no longer won't, since it now establishes a formatting
context, and margins cannot collapse through those).

See https://chromium-review.googlesource.com/c/chromium/src/+/875511/
for some interesting discussion. This CL sort-of re-adds some behavior
that was essentially reverted by that CL (although that CL was about
establishing formatting contexts based on having 'appearance', but
that's something all form controls have, as far as I know).

This CL will make all form controls establish new formatting contexts.

Cq-Include-Trybots: luci.chromium.try:linux_layout_tests_layout_ng;luci.chromium.try:linux_layout_tests_composite_after_paint
Change-Id: I612cc1528de4c399a01a5fd94d6a37f8d5444900
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/1507713
Reviewed-by: Ian Kilpatrick <ikilpatrick@chromium.org>
Reviewed-by: Christian Biesinger <cbiesinger@chromium.org>
Reviewed-by: Emil A Eklund <eae@chromium.org>
Reviewed-by: Koji Ishii <kojii@chromium.org>
Commit-Queue: Morten Stenshorne <mstensho@chromium.org>
Cr-Commit-Position: refs/heads/master@{#638930}
diff --git a/third_party/blink/renderer/core/layout/layout_block.cc b/third_party/blink/renderer/core/layout/layout_block.cc
index f2da6f3a..85a22c1 100644
--- a/third_party/blink/renderer/core/layout/layout_block.cc
+++ b/third_party/blink/renderer/core/layout/layout_block.cc
@@ -1579,7 +1579,7 @@
 
     scoped_refptr<const ComputedStyle> child_style = child->Style();
     if (child->IsFloating() ||
-        (child->IsBox() && ToLayoutBox(child)->AvoidsFloats())) {
+        (child->IsBox() && ToLayoutBox(child)->CreatesNewFormattingContext())) {
       LayoutUnit float_total_width = float_left_width + float_right_width;
       EClear c = ResolvedClear(*child_style, style_to_use);
       if (c == EClear::kBoth || c == EClear::kLeft) {
@@ -1624,7 +1624,7 @@
     w = child_max_preferred_logical_width + margin;
 
     if (!child->IsFloating()) {
-      if (child->IsBox() && ToLayoutBox(child)->AvoidsFloats()) {
+      if (child->IsBox() && ToLayoutBox(child)->CreatesNewFormattingContext()) {
         // Determine a left and right max value based off whether or not the
         // floats can fit in the margins of the object. For negative margins, we
         // will attempt to overlap the float if the negative margin is smaller
diff --git a/third_party/blink/renderer/core/layout/layout_block.h b/third_party/blink/renderer/core/layout/layout_block.h
index 7294694..aa9a33e 100644
--- a/third_party/blink/renderer/core/layout/layout_block.h
+++ b/third_party/blink/renderer/core/layout/layout_block.h
@@ -138,8 +138,6 @@
   LayoutUnit MinLineHeightForReplacedObject(bool is_first_line,
                                             LayoutUnit replaced_height) const;
 
-  virtual bool CreatesNewFormattingContext() const { return true; }
-
   const char* GetName() const override;
 
  protected:
diff --git a/third_party/blink/renderer/core/layout/layout_block_flow.cc b/third_party/blink/renderer/core/layout/layout_block_flow.cc
index b34d3c4..9e2b1b33 100644
--- a/third_party/blink/renderer/core/layout/layout_block_flow.cc
+++ b/third_party/blink/renderer/core/layout/layout_block_flow.cc
@@ -688,7 +688,7 @@
   LayoutUnit child_margin_start = MarginStartForChild(child);
   LayoutUnit new_position = start_position + child_margin_start;
 
-  if (child.AvoidsFloats() && ContainsFloats()) {
+  if (child.CreatesNewFormattingContext() && ContainsFloats()) {
     LayoutUnit position_to_avoid_floats = StartOffsetForAvoidingFloats(
         LogicalTopForChild(child), LogicalHeightForChild(child));
 
@@ -739,8 +739,8 @@
   // TODO(mstensho): rework the code to return early when there is no need for
   // marking, instead of this |markDescendantsWithFloats| flag.
   bool mark_descendants_with_floats = false;
-  if (new_logical_top != child.LogicalTop() && !child.AvoidsFloats() &&
-      child.ContainsFloats()) {
+  if (new_logical_top != child.LogicalTop() &&
+      !child.CreatesNewFormattingContext() && child.ContainsFloats()) {
     mark_descendants_with_floats = true;
   } else if (UNLIKELY(new_logical_top.MightBeSaturated())) {
     // The logical top might be saturated for very large elements. Comparing
@@ -748,7 +748,8 @@
     // removing margins, borders etc. from a saturated number might yield
     // incorrect results. If this is the case, always mark for layout.
     mark_descendants_with_floats = true;
-  } else if (!child.AvoidsFloats() || child.ShrinkToAvoidFloats()) {
+  } else if (!child.CreatesNewFormattingContext() ||
+             child.ShrinkToAvoidFloats()) {
     // If an element might be affected by the presence of floats, then always
     // mark it for layout.
     LayoutUnit lowest_float =
@@ -1349,7 +1350,7 @@
 
   // Inline blocks are covered by the isAtomicInlineLevel() check in the
   // avoidFloats method.
-  if (AvoidsFloats() || IsDocumentElement() || IsLayoutView() ||
+  if (CreatesNewFormattingContext() || IsDocumentElement() || IsLayoutView() ||
       IsFloatingOrOutOfFlowPositioned() || IsTableCell()) {
     if (floating_objects_) {
       floating_objects_->Clear();
@@ -1384,7 +1385,6 @@
   bool sibling_float_may_intrude = false;
   LayoutObject* prev = PreviousSibling();
   while (prev && (!prev->IsBox() || !prev->IsLayoutBlock() ||
-                  ToLayoutBlock(prev)->AvoidsFloats() ||
                   ToLayoutBlock(prev)->CreatesNewFormattingContext())) {
     if (prev->IsFloating())
       sibling_float_may_intrude = true;
@@ -1947,7 +1947,8 @@
     bool logical_top_intrudes_into_float =
         logical_top < before_collapse_logical_top;
     if (logical_top_intrudes_into_float && ContainsFloats() &&
-        !child.AvoidsFloats() && LowestFloatLogicalBottom() > logical_top) {
+        !child.CreatesNewFormattingContext() &&
+        LowestFloatLogicalBottom() > logical_top) {
       child.SetNeedsLayoutAndFullPaintInvalidation(
           layout_invalidation_reason::kAncestorMarginCollapsing);
     }
@@ -2847,12 +2848,12 @@
   FloatingObjectSetIterator end = floating_object_set.end();
 
   for (LayoutObject* next = NextSibling(); next; next = next->NextSibling()) {
-    if (!next->IsLayoutBlockFlow() ||
-        (!float_to_remove && (next->IsFloatingOrOutOfFlowPositioned() ||
-                              ToLayoutBlockFlow(next)->AvoidsFloats())))
+    LayoutBlockFlow* next_block = ToLayoutBlockFlowOrNull(next);
+    if (!next_block ||
+        (!float_to_remove && (next_block->IsFloatingOrOutOfFlowPositioned() ||
+                              next_block->CreatesNewFormattingContext())))
       continue;
 
-    LayoutBlockFlow* next_block = ToLayoutBlockFlow(next);
     for (FloatingObjectSetIterator it = floating_object_set.begin(); it != end;
          ++it) {
       LayoutBox* floating_box = (*it)->GetLayoutObject();
@@ -2880,7 +2881,7 @@
   LayoutUnit result = clear != EClear::kNone
                           ? (logical_bottom - logical_top).ClampNegativeToZero()
                           : LayoutUnit();
-  if (!result && child->AvoidsFloats()) {
+  if (!result && child->CreatesNewFormattingContext()) {
     LayoutUnit new_logical_top = logical_top;
     LayoutRect border_box = child->BorderBoxRect();
     LayoutUnit child_logical_width_at_old_logical_top_offset =
@@ -2972,8 +2973,9 @@
 void LayoutBlockFlow::StyleWillChange(StyleDifference diff,
                                       const ComputedStyle& new_style) {
   const ComputedStyle* old_style = Style();
-  can_propagate_float_into_sibling_ =
-      old_style ? !IsFloatingOrOutOfFlowPositioned() && !AvoidsFloats() : false;
+  can_propagate_float_into_sibling_ = old_style &&
+                                      !IsFloatingOrOutOfFlowPositioned() &&
+                                      !CreatesNewFormattingContext();
   if (old_style && Parent() && diff.NeedsFullLayout() &&
       old_style->GetPosition() != new_style.GetPosition() && ContainsFloats() &&
       !IsFloating() && !IsOutOfFlowPositioned() &&
@@ -2995,7 +2997,7 @@
   // and clear all floats from its next sibling blocks that exist in our
   // floating objects list. See crbug.com/56299 and crbug.com/62875.
   bool can_propagate_float_into_sibling =
-      !IsFloatingOrOutOfFlowPositioned() && !AvoidsFloats();
+      !IsFloatingOrOutOfFlowPositioned() && !CreatesNewFormattingContext();
   bool sibling_float_propagation_changed =
       diff.NeedsFullLayout() && can_propagate_float_into_sibling_ &&
       !can_propagate_float_into_sibling && HasOverhangingFloats();
@@ -3966,9 +3968,10 @@
   if (child.LogicalBottom() <= old_logical_top) {
     LayoutObject* next = child.NextSibling();
     if (next && next->IsLayoutBlockFlow()) {
-      LayoutBlockFlow* nextBlock = ToLayoutBlockFlow(next);
-      if (!nextBlock->AvoidsFloats() || nextBlock->ShrinkToAvoidFloats())
-        nextBlock->MarkAllDescendantsWithFloatsForLayout();
+      LayoutBlockFlow* next_block = ToLayoutBlockFlow(next);
+      if (!next_block->CreatesNewFormattingContext() ||
+          next_block->ShrinkToAvoidFloats())
+        next_block->MarkAllDescendantsWithFloatsForLayout();
     }
   }
 
@@ -4030,7 +4033,7 @@
 void LayoutBlockFlow::AddIntrudingFloats(LayoutBlockFlow* prev,
                                          LayoutUnit logical_left_offset,
                                          LayoutUnit logical_top_offset) {
-  DCHECK(!AvoidsFloats());
+  DCHECK(!CreatesNewFormattingContext());
 
   // If we create our own block formatting context then our contents don't
   // interact with floats outside it, even those from our parent.
@@ -4474,11 +4477,10 @@
   if (IsRenderedLegend())
     return true;
 
-  return false;
-}
+  if (ShouldBeConsideredAsReplaced())
+    return true;
 
-bool LayoutBlockFlow::AvoidsFloats() const {
-  return ShouldBeConsideredAsReplaced() || CreatesNewFormattingContext();
+  return false;
 }
 
 void LayoutBlockFlow::MoveChildrenTo(LayoutBoxModelObject* to_box_model_object,
diff --git a/third_party/blink/renderer/core/layout/layout_block_flow.h b/third_party/blink/renderer/core/layout/layout_block_flow.h
index 52beaa4..9f500a39 100644
--- a/third_party/blink/renderer/core/layout/layout_block_flow.h
+++ b/third_party/blink/renderer/core/layout/layout_block_flow.h
@@ -361,7 +361,6 @@
   void PositionSpannerDescendant(LayoutMultiColumnSpannerPlaceholder& child);
 
   bool CreatesNewFormattingContext() const override;
-  bool AvoidsFloats() const final;
 
   using LayoutBoxModelObject::MoveChildrenTo;
   void MoveChildrenTo(LayoutBoxModelObject* to_box_model_object,
diff --git a/third_party/blink/renderer/core/layout/layout_box.cc b/third_party/blink/renderer/core/layout/layout_box.cc
index 8584bb6..b5af813 100644
--- a/third_party/blink/renderer/core/layout/layout_box.cc
+++ b/third_party/blink/renderer/core/layout/layout_box.cc
@@ -3127,7 +3127,7 @@
       MinimumValueForLength(margin_end_length, container_width);
 
   LayoutUnit available_width = container_width;
-  if (AvoidsFloats() && containing_block->IsLayoutBlockFlow() &&
+  if (CreatesNewFormattingContext() && containing_block->IsLayoutBlockFlow() &&
       ToLayoutBlockFlow(containing_block)->ContainsFloats()) {
     available_width = ContainingBlockAvailableLineWidth();
     if (ShrinkToAvoidFloats() && available_width < container_width) {
@@ -5114,7 +5114,7 @@
 bool LayoutBox::ShrinkToAvoidFloats() const {
   // Floating objects don't shrink.  Objects that don't avoid floats don't
   // shrink.
-  if (IsInline() || !AvoidsFloats() || IsFloating())
+  if (IsInline() || !CreatesNewFormattingContext() || IsFloating())
     return false;
 
   // Only auto width objects can possibly shrink to avoid floats.
@@ -5143,10 +5143,6 @@
           IsHTMLImageElement(ToElement(node)));
 }
 
-bool LayoutBox::AvoidsFloats() const {
-  return true;
-}
-
 bool LayoutBox::HasNonCompositedScrollbars() const {
   if (PaintLayerScrollableArea* scrollable_area = GetScrollableArea()) {
     if (scrollable_area->HasHorizontalScrollbar() &&
diff --git a/third_party/blink/renderer/core/layout/layout_box.h b/third_party/blink/renderer/core/layout/layout_box.h
index c7fc0ad..c0f5f8d 100644
--- a/third_party/blink/renderer/core/layout/layout_box.h
+++ b/third_party/blink/renderer/core/layout/layout_box.h
@@ -1203,7 +1203,7 @@
      // inline-block.
 
   bool ShrinkToAvoidFloats() const;
-  virtual bool AvoidsFloats() const;
+  virtual bool CreatesNewFormattingContext() const { return true; }
   bool ShouldBeConsideredAsReplaced() const;
 
   void UpdateFragmentationInfoForChild(LayoutBox&);
diff --git a/third_party/blink/renderer/core/layout/ng/ng_layout_input_node.h b/third_party/blink/renderer/core/layout/ng/ng_layout_input_node.h
index e63405c..4982b14 100644
--- a/third_party/blink/renderer/core/layout/ng/ng_layout_input_node.h
+++ b/third_party/blink/renderer/core/layout/ng/ng_layout_input_node.h
@@ -138,7 +138,7 @@
   }
 
   bool CreatesNewFormattingContext() const {
-    return IsBlock() && box_->AvoidsFloats();
+    return IsBlock() && box_->CreatesNewFormattingContext();
   }
 
   // Returns true if this node should pass its percentage resolution block-size
diff --git a/third_party/blink/web_tests/flag-specific/enable-blink-features=CompositeAfterPaint/images/rendering-broken-block-flow-images-expected.png b/third_party/blink/web_tests/flag-specific/enable-blink-features=CompositeAfterPaint/images/rendering-broken-block-flow-images-expected.png
index 1ce9e7d..3cb7975a 100644
--- a/third_party/blink/web_tests/flag-specific/enable-blink-features=CompositeAfterPaint/images/rendering-broken-block-flow-images-expected.png
+++ b/third_party/blink/web_tests/flag-specific/enable-blink-features=CompositeAfterPaint/images/rendering-broken-block-flow-images-expected.png
Binary files differ
diff --git a/third_party/blink/web_tests/http/tests/images/restyle-decode-error.html b/third_party/blink/web_tests/http/tests/images/restyle-decode-error.html
index b774e2b..4bb9ed20 100644
--- a/third_party/blink/web_tests/http/tests/images/restyle-decode-error.html
+++ b/third_party/blink/web_tests/http/tests/images/restyle-decode-error.html
@@ -1,5 +1,7 @@
 <html>
-<body>
+<!-- Avoid scrollbars; we'd get a scrollbar otherwise, because the (broken and
+     invisible) image is quite large. -->
+<body style="overflow:hidden;">
 <img style="content: url(#MyFilter); display: block;">
 <script>
 setTimeout(function() {
diff --git a/third_party/blink/web_tests/platform/linux/images/rendering-broken-block-flow-images-expected.png b/third_party/blink/web_tests/platform/linux/images/rendering-broken-block-flow-images-expected.png
index 87ed1dd..485be0f 100644
--- a/third_party/blink/web_tests/platform/linux/images/rendering-broken-block-flow-images-expected.png
+++ b/third_party/blink/web_tests/platform/linux/images/rendering-broken-block-flow-images-expected.png
Binary files differ
diff --git a/third_party/blink/web_tests/platform/linux/virtual/exotic-color-space/images/rendering-broken-block-flow-images-expected.png b/third_party/blink/web_tests/platform/linux/virtual/exotic-color-space/images/rendering-broken-block-flow-images-expected.png
index 8326e8da6..42fa90e 100644
--- a/third_party/blink/web_tests/platform/linux/virtual/exotic-color-space/images/rendering-broken-block-flow-images-expected.png
+++ b/third_party/blink/web_tests/platform/linux/virtual/exotic-color-space/images/rendering-broken-block-flow-images-expected.png
Binary files differ
diff --git a/third_party/blink/web_tests/platform/linux/virtual/gpu-rasterization/images/rendering-broken-block-flow-images-expected.png b/third_party/blink/web_tests/platform/linux/virtual/gpu-rasterization/images/rendering-broken-block-flow-images-expected.png
index 0e7bcef..dc32f49 100644
--- a/third_party/blink/web_tests/platform/linux/virtual/gpu-rasterization/images/rendering-broken-block-flow-images-expected.png
+++ b/third_party/blink/web_tests/platform/linux/virtual/gpu-rasterization/images/rendering-broken-block-flow-images-expected.png
Binary files differ
diff --git a/third_party/blink/web_tests/platform/win/images/rendering-broken-block-flow-images-expected.png b/third_party/blink/web_tests/platform/win/images/rendering-broken-block-flow-images-expected.png
index c051414a..3818f8bb 100644
--- a/third_party/blink/web_tests/platform/win/images/rendering-broken-block-flow-images-expected.png
+++ b/third_party/blink/web_tests/platform/win/images/rendering-broken-block-flow-images-expected.png
Binary files differ
diff --git a/third_party/blink/web_tests/platform/win/virtual/exotic-color-space/images/rendering-broken-block-flow-images-expected.png b/third_party/blink/web_tests/platform/win/virtual/exotic-color-space/images/rendering-broken-block-flow-images-expected.png
index c7fee50..51ac7d9e 100644
--- a/third_party/blink/web_tests/platform/win/virtual/exotic-color-space/images/rendering-broken-block-flow-images-expected.png
+++ b/third_party/blink/web_tests/platform/win/virtual/exotic-color-space/images/rendering-broken-block-flow-images-expected.png
Binary files differ