Fix selecting replaced text after undo

PDFium uses a stack of undo items to implement undo/redo. For text
replacement there are a variable number of undo items added to stack.
The UndoReplaceSelection undo item class was created in
https://pdfium.googlesource.com/pdfium/+/3abc96a18bf41a55958e2fe99a3ce03882c3ecb5

I believe the SelectNone calls that caused this bug were simply a
copy-paste error since many of the other undo items call that.
UndoReplaceSelection is only a sentinel item that tracks the number of
intermediate undo items, it shouldn't do any actions itself.

For ReplaceAndKeepSelection arguably when the redo action runs the
replaced text should be selected as it was originally, but in this
change it is left unselected like after redoing ReplaceSelection. I
think this makes sense because in general selection is not an undo item,
pasting over a selection is a special case that is widely implemented.
If you select text, then press undo then redo, the text will not be
selected anymore. There is no equivalent operation of "paste and then
select the pasted text" in other programs, so nothing to defer to. Also
this is an experimental API for pdfium. So overall I think it makes
sense to leave as-is instead of adding a new undo item type for this
selection.

Fixed: 445215584
Change-Id: Ife7f13aca9dbe2260f2f4a1b2ef5650099f44dd5
Reviewed-on: https://pdfium-review.googlesource.com/c/pdfium/+/135750
Commit-Queue: April Kallmeyer <ask@chromium.org>
Reviewed-by: Tom Sepez <tsepez@chromium.org>
Reviewed-by: Lei Zhang <thestig@chromium.org>
diff --git a/fpdfsdk/fpdf_formfill_embeddertest.cpp b/fpdfsdk/fpdf_formfill_embeddertest.cpp
index 6268d8a..6f33fa5 100644
--- a/fpdfsdk/fpdf_formfill_embeddertest.cpp
+++ b/fpdfsdk/fpdf_formfill_embeddertest.cpp
@@ -3210,6 +3210,19 @@
   EXPECT_EQ(FocusedFieldText(), "AB");
   EXPECT_TRUE(CanUndo());
   EXPECT_TRUE(CanRedo());
+  EXPECT_EQ(Selection(), "A");
+
+  PerformRedo();
+  EXPECT_EQ(FocusedFieldText(), "XYZB");
+  EXPECT_TRUE(CanUndo());
+  EXPECT_FALSE(CanRedo());
+  EXPECT_EQ(Selection(), "");  // The selection is not an undo item.
+
+  PerformUndo();
+  EXPECT_EQ(FocusedFieldText(), "AB");
+  EXPECT_TRUE(CanUndo());
+  EXPECT_TRUE(CanRedo());
+  EXPECT_EQ(Selection(), "A");
 
   SelectTextWithKeyboard(1, FWL_VKEY_Left, RegularFormEnd());
   EXPECT_EQ(Selection(), "B");
@@ -3272,36 +3285,43 @@
   EXPECT_EQ(FocusedFieldText(), "XYZB");
   EXPECT_TRUE(CanUndo());
   EXPECT_FALSE(CanRedo());
+  EXPECT_EQ(Selection(), "");
 
   PerformUndo();
   EXPECT_EQ(FocusedFieldText(), "AB");
   EXPECT_TRUE(CanUndo());
   EXPECT_TRUE(CanRedo());
+  EXPECT_EQ(Selection(), "A");
 
   PerformUndo();
   EXPECT_EQ(FocusedFieldText(), "A");
   EXPECT_TRUE(CanUndo());
   EXPECT_TRUE(CanRedo());
+  EXPECT_EQ(Selection(), "");
 
   PerformUndo();
   EXPECT_EQ(FocusedFieldText(), "");
   EXPECT_FALSE(CanUndo());
   EXPECT_TRUE(CanRedo());
+  EXPECT_EQ(Selection(), "");
 
   PerformRedo();
   EXPECT_EQ(FocusedFieldText(), "A");
   EXPECT_TRUE(CanUndo());
   EXPECT_TRUE(CanRedo());
+  EXPECT_EQ(Selection(), "");
 
   PerformRedo();
   EXPECT_EQ(FocusedFieldText(), "AB");
   EXPECT_TRUE(CanUndo());
   EXPECT_TRUE(CanRedo());
+  EXPECT_EQ(Selection(), "");
 
   PerformRedo();
   EXPECT_EQ(FocusedFieldText(), "XYZB");
   EXPECT_TRUE(CanUndo());
   EXPECT_FALSE(CanRedo());
+  EXPECT_EQ(Selection(), "");
 }
 
 TEST_F(FPDFFormFillTextFormEmbedderTest, ContinuouslyReplaceSelection) {
@@ -3321,6 +3341,7 @@
 
   PerformUndo();
   EXPECT_EQ(FocusedFieldText(), "");
+  EXPECT_EQ(Selection(), "");
 
   EXPECT_FALSE(CanUndo());
   EXPECT_TRUE(CanRedo());
diff --git a/fpdfsdk/pwl/cpwl_edit_impl.cpp b/fpdfsdk/pwl/cpwl_edit_impl.cpp
index 1a56f9e..a025eba 100644
--- a/fpdfsdk/pwl/cpwl_edit_impl.cpp
+++ b/fpdfsdk/pwl/cpwl_edit_impl.cpp
@@ -372,7 +372,6 @@
 CPWL_EditImpl::UndoReplaceSelection::~UndoReplaceSelection() = default;
 
 int CPWL_EditImpl::UndoReplaceSelection::Redo() {
-  edit_->SelectNone();
   if (IsEnd()) {
     return 0;
   }
@@ -382,7 +381,6 @@
 }
 
 int CPWL_EditImpl::UndoReplaceSelection::Undo() {
-  edit_->SelectNone();
   if (!IsEnd()) {
     return 0;
   }