From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
From: Vladimir Levin <vmpstr@chromium.org>
Date: Sat, 9 Jan 2021 01:26:39 +0000
Subject: content-visibility: Don't adjust position of a locked hittest result
 node.

This patch ensures that if we have a hittest result that has a locked
node, we don't try to recurse into its subtree. This can happen when we
do a PositionWithAffinity check.

R=chrishtr@chromium.org

(cherry picked from commit 8483cf6944e38203c3b247163c54cfa105e89c56)

Bug: 1162131
Change-Id: I357bd7032c6c2b6c9405bf26c49a36bda22d6a0d
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2611453
Reviewed-by: Chris Harrelson <chrishtr@chromium.org>
Commit-Queue: vmpstr <vmpstr@chromium.org>
Cr-Original-Commit-Position: refs/heads/master@{#840727}
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2618603
Reviewed-by: Xianzhu Wang <wangxianzhu@chromium.org>
Reviewed-by: Krishna Govind <govind@chromium.org>
Cr-Commit-Position: refs/branch-heads/4324@{#1566}
Cr-Branched-From: c73b5a651d37a6c4d0b8e3262cc4015a5579c6c8-refs/heads/master@{#827102}

diff --git a/third_party/blink/renderer/core/layout/hit_test_result.cc b/third_party/blink/renderer/core/layout/hit_test_result.cc
index 1359aa6bdd532028d8280b093a4381a70e4a7577..156f768e1f522f9b01c6633de2abb670b32c5c7d 100644
--- a/third_party/blink/renderer/core/layout/hit_test_result.cc
+++ b/third_party/blink/renderer/core/layout/hit_test_result.cc
@@ -21,12 +21,14 @@
 
 #include "third_party/blink/renderer/core/layout/hit_test_result.h"
 
+#include "third_party/blink/renderer/core/display_lock/display_lock_utilities.h"
 #include "third_party/blink/renderer/core/dom/flat_tree_traversal.h"
 #include "third_party/blink/renderer/core/dom/pseudo_element.h"
 #include "third_party/blink/renderer/core/dom/shadow_root.h"
 #include "third_party/blink/renderer/core/editing/editing_utilities.h"
 #include "third_party/blink/renderer/core/editing/frame_selection.h"
 #include "third_party/blink/renderer/core/editing/position_with_affinity.h"
+#include "third_party/blink/renderer/core/editing/text_affinity.h"
 #include "third_party/blink/renderer/core/editing/visible_units.h"
 #include "third_party/blink/renderer/core/frame/local_frame.h"
 #include "third_party/blink/renderer/core/frame/visual_viewport.h"
@@ -144,6 +146,19 @@ PositionWithAffinity HitTestResult::GetPosition() const {
   LayoutObject* layout_object = GetLayoutObject();
   if (!layout_object)
     return PositionWithAffinity();
+
+  // We should never have a layout object that is within a locked subtree.
+  CHECK(!DisplayLockUtilities::NearestLockedExclusiveAncestor(*layout_object));
+
+  // If the layout object is blocked by display lock, we return the beginning of
+  // the node as the position. This is because we don't paint contents of the
+  // element. Furthermore, any caret adjustments below can access layout-dirty
+  // state in the subtree of this object.
+  if (layout_object->ChildPaintBlockedByDisplayLock()) {
+    return PositionWithAffinity(Position(*inner_node_, 0),
+                                TextAffinity::kDefault);
+  }
+
   if (inner_possibly_pseudo_node_->IsPseudoElement() &&
       inner_possibly_pseudo_node_->GetPseudoId() == kPseudoIdBefore) {
     return PositionWithAffinity(MostForwardCaretPosition(
diff --git a/third_party/blink/web_tests/external/wpt/css/css-contain/content-visibility/content-visibility-080.html b/third_party/blink/web_tests/external/wpt/css/css-contain/content-visibility/content-visibility-080.html
new file mode 100644
index 0000000000000000000000000000000000000000..d3cea5fb83767ddfc236850097387644e0f74c8e
--- /dev/null
+++ b/third_party/blink/web_tests/external/wpt/css/css-contain/content-visibility/content-visibility-080.html
@@ -0,0 +1,31 @@
+<!doctype HTML>
+<html id=html>
+<meta charset="utf8">
+<title>Content Visibility: caret position with html hidden</title>
+<link rel="author" title="Vladimir Levin" href="mailto:vmpstr@chromium.org">
+<link rel="help" href="https://drafts.csswg.org/css-contain/#content-visibility">
+<meta name="assert" content="caretRangeFromPoint works even if html has content-visibility hidden">
+
+<script src="/resources/testharness.js"></script>
+<script src="/resources/testharnessreport.js"></script>
+
+<meter></meter>
+<iframe></iframe>
+<style>
+* {
+  all: initial;
+  content-visibility: hidden;
+}
+</style>
+
+<script>
+test(() => {
+  const range = document.caretRangeFromPoint();
+  assert_not_equals(range, null, "range exists");
+  assert_equals(range.startContainer, html, "startContainer is html");
+  assert_equals(range.startOffset, 0, "startOffset is zero");
+  assert_equals(range.endContainer, html, "endContainer is html");
+  assert_equals(range.endOffset, 0, "endOffset is zero");
+}, "Caret range from point");
+</script>
+</html>
