Fix element with RTL direction gets scrolled when its height is set to 0

overflow_rect_ may be set to empty in later stages, so using it to
determine whether the first layout is completed can lead to incorrect
judgments. And the condition is not necessary at all any more. This CL
removes it.

Bug: 40064904
Change-Id: I2b7d30a30fbaf8c31259ba9c6b32de64b372a203
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/6606077
Reviewed-by: Stefan Zager <szager@chromium.org>
Commit-Queue: Perry <perryuwang@gmail.com>
Cr-Commit-Position: refs/heads/main@{#1468493}
diff --git a/third_party/blink/renderer/core/paint/paint_layer_scrollable_area.cc b/third_party/blink/renderer/core/paint/paint_layer_scrollable_area.cc
index a5d01b49..d150319 100644
--- a/third_party/blink/renderer/core/paint/paint_layer_scrollable_area.cc
+++ b/third_party/blink/renderer/core/paint/paint_layer_scrollable_area.cc
@@ -928,10 +928,6 @@
 }
 
 void PaintLayerScrollableArea::UpdateScrollOrigin() {
-  // This should do nothing prior to first layout; the if-clause will catch
-  // that.
-  if (overflow_rect_.IsEmpty())
-    return;
   PhysicalRect scrollable_overflow = overflow_rect_;
   scrollable_overflow.Move(-PhysicalOffset(GetLayoutBox()->BorderLeft(),
                                            GetLayoutBox()->BorderTop()));
@@ -1047,6 +1043,7 @@
   if (scrollbars_will_change) {
     SetHasHorizontalScrollbar(needs_horizontal_scrollbar);
     SetHasVerticalScrollbar(needs_vertical_scrollbar);
+    UpdateScrollOrigin();
 
     // If we change scrollbars on the layout viewport, the visual viewport
     // needs to update paint properties to account for the correct
@@ -1251,6 +1248,7 @@
                               needs_vertical_scrollbar);
     SetHasHorizontalScrollbar(needs_horizontal_scrollbar);
     SetHasVerticalScrollbar(needs_vertical_scrollbar);
+    UpdateScrollOrigin();
   }
 
   // Recalculate the snap container data since the scrolling behaviour for this
@@ -1814,7 +1812,6 @@
     scrollbar_manager_.SetHasVerticalScrollbar(false);
   }
   UpdateScrollCornerStyle();
-  UpdateScrollOrigin();
 
   // Force an update since we know the scrollbars have changed things.
   if (GetLayoutBox()->GetDocument().HasDraggableRegions()) {
@@ -1840,8 +1837,6 @@
 
   scrollbar_manager_.SetHasHorizontalScrollbar(has_scrollbar);
 
-  UpdateScrollOrigin();
-
   // Destroying or creating one bar can cause our scrollbar corner to come and
   // go. We need to update the opposite scrollbar's style.
   if (HasHorizontalScrollbar())
@@ -1875,8 +1870,6 @@
 
   scrollbar_manager_.SetHasVerticalScrollbar(has_scrollbar);
 
-  UpdateScrollOrigin();
-
   // Destroying or creating one bar can cause our scrollbar corner to come and
   // go. We need to update the opposite scrollbar's style.
   if (HasHorizontalScrollbar())
diff --git a/third_party/blink/web_tests/external/wpt/css/css-overflow/overflow-rtl-scroll-left.html b/third_party/blink/web_tests/external/wpt/css/css-overflow/overflow-rtl-scroll-left.html
new file mode 100644
index 0000000..0c8377375
--- /dev/null
+++ b/third_party/blink/web_tests/external/wpt/css/css-overflow/overflow-rtl-scroll-left.html
@@ -0,0 +1,49 @@
+<!doctype html>
+<meta charset="utf-8">
+<title>overflow: rtl scroll left should return 0 when overflow size is empty</title>
+<link rel="author" href="mailto:perryuwang@gmail.com">
+<link rel="help" href="https://issues.chromium.org/issues/40064904">
+<script src="/css/css-transitions/support/helper.js"></script>
+<script src="/resources/testharness.js"></script>
+<script src="/resources/testharnessreport.js"></script>
+<style>
+  #rtl-parent {
+    direction: rtl;
+    overflow: auto;
+    width: 300px;
+    height: 200px;
+  }
+  #rtl-child {
+    width: 500px;
+    height: 200px;
+  }
+</style>
+
+<div id="rtl-parent">
+  <div id="rtl-child"></div>
+</div>
+
+<script>
+promise_test(async () => {
+  const parent = document.getElementById('rtl-parent');
+  const child = document.getElementById('rtl-child');
+
+  await waitForAnimationFrames(5);
+  assert_equals(parent.offsetWidth, 300);
+  assert_equals(parent.offsetHeight, 200);
+  assert_equals(child.offsetWidth, 500);
+  assert_equals(child.offsetHeight, 200);
+
+  assert_equals(parent.scrollWidth, 500);
+  assert_equals(parent.scrollHeight, 200);
+  assert_equals(parent.scrollLeft, 0);
+
+  child.style.height = '0px';
+  parent.style.height = '0px';
+
+  await waitForAnimationFrames(5);
+  assert_equals(parent.offsetHeight, 0);
+  assert_equals(parent.scrollHeight, 0);
+  assert_equals(parent.scrollLeft, 0);
+}, 'rtl scroll left should be 0 when overflow size is empty');
+</script>