Fix caret moving to start of edit on click after the text.

It now moves to the end of the edit text instead.

In a multiline edit, clicking after the end of a line in the middle
of the text moves the caret to the beginning of the next line. This
is a known issue to be addressed later.

Bug: chromium:832293
Change-Id: I55ca1f7eebf935fbb7d9526f115489bdfbd8ba1c
Reviewed-on: https://pdfium-review.googlesource.com/31012
Reviewed-by: dsinclair <dsinclair@chromium.org>
Commit-Queue: Henrique Nakashima <hnakashima@chromium.org>
diff --git a/DEPS b/DEPS
index 476f404..c4783cf 100644
--- a/DEPS
+++ b/DEPS
@@ -28,7 +28,7 @@
   'jinja2_revision': 'd34383206fa42d52faa10bb9931d6d538f3a57e0',
   'jpeg_turbo_revision': '7260e4d8b8e1e40b17f03fafdf1cd83296900f76',
   'markupsafe_revision': '8f45f5cfa0009d2a70589bcda0349b8cb2b72783',
-  'pdfium_tests_revision': 'b86c0ec9ee2aa3ba5e113a0e88a5c3292eb0fe2f',
+  'pdfium_tests_revision': '9b7ff5b879ce578f4f186ad546f45fc9fb592943',
   'skia_revision': 'af7700265b74123d8ad3de6dde0c21545453140b',
   'tools_memory_revision': '427f10475e1a8d72424c29d00bf689122b738e5d',
   'trace_event_revision': '0e9a47d74970bee1bbfc063c47215406f8918699',
diff --git a/xfa/fde/cfde_texteditengine.cpp b/xfa/fde/cfde_texteditengine.cpp
index 39396ad..ac66fd7 100644
--- a/xfa/fde/cfde_texteditengine.cpp
+++ b/xfa/fde/cfde_texteditengine.cpp
@@ -899,12 +899,19 @@
 
   size_t start_it_idx = start_it->nStart;
   for (; start_it <= end_it; ++start_it) {
-    if (!start_it->rtPiece.Contains(point))
+    // We need to handle clicking off the end of the line. Using Contains()
+    // would constrain the detection to the text on the line.
+    bool piece_contains_point_vertically =
+        (point.y >= start_it->rtPiece.top &&
+         point.y < start_it->rtPiece.bottom());
+    if (!piece_contains_point_vertically)
       continue;
 
     std::vector<CFX_RectF> rects = GetCharRects(*start_it);
     for (size_t i = 0; i < rects.size(); ++i) {
-      if (!rects[i].Contains(point))
+      bool character_contains_point_horizontally =
+          (point.x >= rects[i].left && point.x < rects[i].right());
+      if (!character_contains_point_horizontally)
         continue;
       size_t pos = start_it->nStart + i;
       if (pos >= text_length_)
@@ -920,6 +927,13 @@
       // TODO(dsinclair): Old code had a before flag set based on bidi?
       return pos;
     }
+
+    // Point is not within the horizontal range of any characters, it's
+    // afterwards. Return the position after the the last character.
+    // The last line has nCount equal to the number of characters + 1 (sentinel
+    // character maybe?). Restrict to the text_length_ to account for that.
+    return std::min(static_cast<size_t>(start_it->nStart + start_it->nCount),
+                    text_length_);
   }
 
   if (start_it == text_piece_info_.end())
diff --git a/xfa/fde/cfde_texteditengine_unittest.cpp b/xfa/fde/cfde_texteditengine_unittest.cpp
index 522e61a..e279f8e 100644
--- a/xfa/fde/cfde_texteditengine_unittest.cpp
+++ b/xfa/fde/cfde_texteditengine_unittest.cpp
@@ -415,6 +415,18 @@
   engine()->Insert(0, L"Hello World");
   EXPECT_EQ(0U, engine()->GetIndexForPoint({0.0f, 0.0f}));
   EXPECT_EQ(11U, engine()->GetIndexForPoint({999999.0f, 9999999.0f}));
+  EXPECT_EQ(11U, engine()->GetIndexForPoint({999999.0f, 0.0f}));
+  EXPECT_EQ(1U, engine()->GetIndexForPoint({10.0f, 5.0f}));
+}
+
+TEST_F(CFDE_TextEditEngineTest, GetIndexForPointMultiline) {
+  engine()->SetFontSize(10.0f);
+  engine()->Insert(0,
+                   L"A text long enough to span multiple lines and test "
+                   L"getting indexes on multi-line edits.");
+  EXPECT_EQ(0U, engine()->GetIndexForPoint({0.0f, 0.0f}));
+  EXPECT_EQ(87U, engine()->GetIndexForPoint({999999.0f, 9999999.0f}));
+  EXPECT_EQ(12U, engine()->GetIndexForPoint({999999.0f, 0.0f}));
   EXPECT_EQ(1U, engine()->GetIndexForPoint({10.0f, 5.0f}));
 }