sw/qa/extras/uiwriter/uiwriter9.cxx |   29 +++++++++++++++++++++++++++++
 sw/source/core/undo/unins.cxx       |   12 ++++++++----
 2 files changed, 37 insertions(+), 4 deletions(-)

New commits:
commit 258c34bddb0a1da09a3bb0ccccf2bd673be377b1
Author:     Mike Kaganski <mike.kagan...@collabora.com>
AuthorDate: Wed Jun 5 10:43:07 2024 +0500
Commit:     Adolfo Jayme Barrientos <fit...@ubuntu.com>
CommitDate: Thu Jun 6 04:25:29 2024 +0200

    tdf#144752: UNDO/REDO: make sure to select the replaced content
    
    In the specific case of the bug, the non-empty selection means that
    the spell check won't skip the word with the cursor. But this also
    establishes consistency with other Undo/Redo cases, which generally
    select the text.
    
    Change-Id: Ib7c1c911908dc59947e610d735907be7a363ca87
    Reviewed-on: https://gerrit.libreoffice.org/c/core/+/168423
    Reviewed-by: Mike Kaganski <mike.kagan...@collabora.com>
    Tested-by: Jenkins
    (cherry picked from commit 31f46cec1406bb39453f5d909da1321980b0e405)
    Reviewed-on: https://gerrit.libreoffice.org/c/core/+/168433
    Reviewed-by: Adolfo Jayme Barrientos <fit...@ubuntu.com>

diff --git a/sw/qa/extras/uiwriter/uiwriter9.cxx 
b/sw/qa/extras/uiwriter/uiwriter9.cxx
index 2389aa63714a..96ad4b05638f 100644
--- a/sw/qa/extras/uiwriter/uiwriter9.cxx
+++ b/sw/qa/extras/uiwriter/uiwriter9.cxx
@@ -256,6 +256,35 @@ CPPUNIT_TEST_FIXTURE(SwUiWriterTest9, testTdf161172)
     CPPUNIT_ASSERT_EQUAL(sal_Int16(0), getProperty<sal_Int16>(para, 
u"NumberingLevel"_ustr));
 }
 
+CPPUNIT_TEST_FIXTURE(SwUiWriterTest9, testTdf144752)
+{
+    // Undoing/redoing a replacement must select the new text
+    createSwDoc();
+    SwXTextDocument* pDoc = dynamic_cast<SwXTextDocument*>(mxComponent.get());
+    CPPUNIT_ASSERT(pDoc);
+    SwWrtShell* pWrtShell = pDoc->GetDocShell()->GetWrtShell();
+    CPPUNIT_ASSERT(pWrtShell);
+
+    emulateTyping(*pDoc, u"Some Text");
+    CPPUNIT_ASSERT(!pWrtShell->HasSelection());
+    // Select "Text", and replace with "Word"
+    pWrtShell->Left(SwCursorSkipMode::Chars, /*bSelect*/ true, 4, 
/*bBasicCall*/ false);
+    pWrtShell->Replace(u"Word"_ustr, false);
+    pWrtShell->EndOfSection();
+    CPPUNIT_ASSERT(!pWrtShell->HasSelection());
+
+    // Undo and check, that the "Text" is selected
+    dispatchCommand(mxComponent, ".uno:Undo", {});
+    // Without the fix, this would fail
+    CPPUNIT_ASSERT(pWrtShell->HasSelection());
+    CPPUNIT_ASSERT_EQUAL(u"Text"_ustr, pWrtShell->GetSelText());
+
+    // Redo and check, that the "Word" is selected
+    dispatchCommand(mxComponent, ".uno:Redo", {});
+    CPPUNIT_ASSERT(pWrtShell->HasSelection());
+    CPPUNIT_ASSERT_EQUAL(u"Word"_ustr, pWrtShell->GetSelText());
+}
+
 } // end of anonymous namespace
 CPPUNIT_PLUGIN_IMPLEMENT();
 
diff --git a/sw/source/core/undo/unins.cxx b/sw/source/core/undo/unins.cxx
index bf6c2877b2d4..ff6dbeb74c4f 100644
--- a/sw/source/core/undo/unins.cxx
+++ b/sw/source/core/undo/unins.cxx
@@ -672,7 +672,10 @@ void SwUndoReplace::Impl::UndoImpl(::sw::UndoRedoContext & 
rContext)
             rPam.GetPoint()->Assign( m_nEndNd - m_nOffset, m_nEndCnt );
             pDoc->getIDocumentContentOperations().DeleteAndJoin(rPam);
         }
-        rPam.DeleteMark();
+        if (*rPam.GetMark() == *rPam.GetPoint())
+            rPam.DeleteMark();
+        else
+            rPam.Normalize(false);
         pNd = pDoc->GetNodes()[ m_nSttNd - m_nOffset ]->GetTextNode();
         OSL_ENSURE( pNd, "Dude, where's my TextNode?" );
     }
@@ -712,8 +715,6 @@ void SwUndoReplace::Impl::UndoImpl(::sw::UndoRedoContext & 
rContext)
             }
         }
     }
-
-    rPam.GetPoint()->Assign( m_nSttNd, m_nSttCnt );
 }
 
 void SwUndoReplace::Impl::RedoImpl(::sw::UndoRedoContext & rContext)
@@ -751,7 +752,10 @@ void SwUndoReplace::Impl::RedoImpl(::sw::UndoRedoContext & 
rContext)
     }
 
     rDoc.getIDocumentContentOperations().ReplaceRange( rPam, m_sIns, m_bRegExp 
);
-    rPam.DeleteMark();
+    if (*rPam.GetMark() == *rPam.GetPoint())
+        rPam.DeleteMark();
+    else
+        rPam.Normalize(false);
 }
 
 void SwUndoReplace::Impl::SetEnd(SwPaM const& rPam)

Reply via email to