sw/qa/extras/uiwriter/uiwriter2.cxx |   88 ++++++++++++++++++++++++++++++++++++
 sw/source/core/undo/unins.cxx       |    5 +-
 2 files changed, 91 insertions(+), 2 deletions(-)

New commits:
commit 4f3677eff9d177489e836490d0deaf580c9e8458
Author:     Michael Stahl <[email protected]>
AuthorDate: Thu Jun 9 18:58:06 2022 +0200
Commit:     Thorsten Behrens <[email protected]>
CommitDate: Tue Jun 21 23:40:53 2022 +0200

    tdf#140007 sw: fix SwUndoReplace
    
    (regression from commit d6b0e84b236b78f4b21bd16e46dda3fa0876096d)
    
    Reviewed-on: https://gerrit.libreoffice.org/c/core/+/135585
    Tested-by: Jenkins
    Reviewed-by: Michael Stahl <[email protected]>
    (cherry picked from commit 45613274794636ba98d0e978fe872511297d275d)
    Reviewed-on: https://gerrit.libreoffice.org/c/core/+/135549
    Reviewed-by: Xisco Fauli <[email protected]>
    (cherry picked from commit 7df50ecd9dea623058dc7bf9095fd13d9bb49860)
    
    Change-Id: I1facf1584a349d1d087438f4e6fd3a63a80c6f7e
    Reviewed-on: https://gerrit.libreoffice.org/c/core/+/136219
    Tested-by: Thorsten Behrens <[email protected]>
    Reviewed-by: Thorsten Behrens <[email protected]>

diff --git a/sw/qa/extras/uiwriter/uiwriter2.cxx 
b/sw/qa/extras/uiwriter/uiwriter2.cxx
index bdea12f0370a..81c4c3e31c33 100644
--- a/sw/qa/extras/uiwriter/uiwriter2.cxx
+++ b/sw/qa/extras/uiwriter/uiwriter2.cxx
@@ -820,6 +820,94 @@ CPPUNIT_TEST_FIXTURE(SwUiWriterTest2, testTdf131912)
     CPPUNIT_ASSERT_EQUAL(OUString("foo"), pWrtShell->GetCursor()->GetText());
 }
 
+CPPUNIT_TEST_FIXTURE(SwUiWriterTest2, testTdf140007)
+{
+    typedef sal_uLong SwNodeOffset;
+    SwDoc* const pDoc = createDoc();
+    SwWrtShell* const pWrtShell = pDoc->GetDocShell()->GetWrtShell();
+
+    pWrtShell->Insert("foo");
+    pWrtShell->SplitNode();
+    pWrtShell->Insert("bar");
+    pWrtShell->SplitNode();
+    pWrtShell->Insert("baz");
+    CPPUNIT_ASSERT_EQUAL(sal_uLong(13), pDoc->GetNodes().Count());
+    CPPUNIT_ASSERT_EQUAL(OUString("foo"),
+                         
pDoc->GetNodes()[SwNodeOffset(9)]->GetTextNode()->GetText());
+    CPPUNIT_ASSERT_EQUAL(OUString("bar"),
+                         
pDoc->GetNodes()[SwNodeOffset(10)]->GetTextNode()->GetText());
+    CPPUNIT_ASSERT_EQUAL(OUString("baz"),
+                         
pDoc->GetNodes()[SwNodeOffset(11)]->GetTextNode()->GetText());
+
+    pWrtShell->SttEndDoc(true);
+    pWrtShell->EndPara(false);
+    pWrtShell->Right(CRSR_SKIP_CHARS, /*bSelect=*/true, 1, 
/*bBasicCall=*/false);
+    pWrtShell->Replace(" ", true);
+    CPPUNIT_ASSERT_EQUAL(SwNodeOffset(12), pDoc->GetNodes().Count());
+    CPPUNIT_ASSERT_EQUAL(OUString("foo bar"),
+                         
pDoc->GetNodes()[SwNodeOffset(9)]->GetTextNode()->GetText());
+    CPPUNIT_ASSERT_EQUAL(OUString("baz"),
+                         
pDoc->GetNodes()[SwNodeOffset(10)]->GetTextNode()->GetText());
+
+    pWrtShell->SttEndDoc(true);
+    pWrtShell->EndPara(false);
+    pWrtShell->Right(CRSR_SKIP_CHARS, /*bSelect=*/true, 1, 
/*bBasicCall=*/false);
+    pWrtShell->Replace(" ", true);
+    CPPUNIT_ASSERT_EQUAL(OUString("foo bar baz"),
+                         
pDoc->GetNodes()[SwNodeOffset(9)]->GetTextNode()->GetText());
+    CPPUNIT_ASSERT_EQUAL(SwNodeOffset(11), pDoc->GetNodes().Count());
+
+    pWrtShell->Undo();
+
+    CPPUNIT_ASSERT_EQUAL(SwNodeOffset(12), pDoc->GetNodes().Count());
+    CPPUNIT_ASSERT_EQUAL(OUString("foo bar"),
+                         
pDoc->GetNodes()[SwNodeOffset(9)]->GetTextNode()->GetText());
+    CPPUNIT_ASSERT_EQUAL(OUString("baz"),
+                         
pDoc->GetNodes()[SwNodeOffset(10)]->GetTextNode()->GetText());
+
+    pWrtShell->Undo();
+
+    CPPUNIT_ASSERT_EQUAL(SwNodeOffset(13), pDoc->GetNodes().Count());
+    CPPUNIT_ASSERT_EQUAL(OUString("foo"),
+                         
pDoc->GetNodes()[SwNodeOffset(9)]->GetTextNode()->GetText());
+    CPPUNIT_ASSERT_EQUAL(OUString("bar"),
+                         
pDoc->GetNodes()[SwNodeOffset(10)]->GetTextNode()->GetText());
+    CPPUNIT_ASSERT_EQUAL(OUString("baz"),
+                         
pDoc->GetNodes()[SwNodeOffset(11)]->GetTextNode()->GetText());
+
+    pWrtShell->Redo();
+
+    CPPUNIT_ASSERT_EQUAL(SwNodeOffset(12), pDoc->GetNodes().Count());
+    CPPUNIT_ASSERT_EQUAL(OUString("foo bar"),
+                         
pDoc->GetNodes()[SwNodeOffset(9)]->GetTextNode()->GetText());
+    CPPUNIT_ASSERT_EQUAL(OUString("baz"),
+                         
pDoc->GetNodes()[SwNodeOffset(10)]->GetTextNode()->GetText());
+
+    pWrtShell->Redo();
+
+    CPPUNIT_ASSERT_EQUAL(OUString("foo bar baz"),
+                         
pDoc->GetNodes()[SwNodeOffset(9)]->GetTextNode()->GetText());
+    CPPUNIT_ASSERT_EQUAL(SwNodeOffset(11), pDoc->GetNodes().Count());
+
+    pWrtShell->Undo();
+
+    CPPUNIT_ASSERT_EQUAL(SwNodeOffset(12), pDoc->GetNodes().Count());
+    CPPUNIT_ASSERT_EQUAL(OUString("foo bar"),
+                         
pDoc->GetNodes()[SwNodeOffset(9)]->GetTextNode()->GetText());
+    CPPUNIT_ASSERT_EQUAL(OUString("baz"),
+                         
pDoc->GetNodes()[SwNodeOffset(10)]->GetTextNode()->GetText());
+
+    pWrtShell->Undo();
+
+    CPPUNIT_ASSERT_EQUAL(SwNodeOffset(13), pDoc->GetNodes().Count());
+    CPPUNIT_ASSERT_EQUAL(OUString("foo"),
+                         
pDoc->GetNodes()[SwNodeOffset(9)]->GetTextNode()->GetText());
+    CPPUNIT_ASSERT_EQUAL(OUString("bar"),
+                         
pDoc->GetNodes()[SwNodeOffset(10)]->GetTextNode()->GetText());
+    CPPUNIT_ASSERT_EQUAL(OUString("baz"),
+                         
pDoc->GetNodes()[SwNodeOffset(11)]->GetTextNode()->GetText());
+}
+
 CPPUNIT_TEST_FIXTURE(SwUiWriterTest2, testTdf54819)
 {
     load(DATA_DIRECTORY, "tdf54819.fodt");
diff --git a/sw/source/core/undo/unins.cxx b/sw/source/core/undo/unins.cxx
index 6135466bf73d..f1f7f48188aa 100644
--- a/sw/source/core/undo/unins.cxx
+++ b/sw/source/core/undo/unins.cxx
@@ -689,7 +689,8 @@ void SwUndoReplace::Impl::UndoImpl(::sw::UndoRedoContext & 
rContext)
 
     if( m_bSplitNext )
     {
-        SwPosition aPos(*pNd, pNd->Len());
+        assert(m_nSttCnt + m_sOld.getLength() <= pNd->Len());
+        SwPosition aPos(*pNd, m_nSttCnt + m_sOld.getLength());
         pDoc->getIDocumentContentOperations().SplitNode( aPos, false );
         pNd->RestoreMetadata(m_pMetadataUndoEnd);
         pNd = pDoc->GetNodes()[ m_nSttNd - m_nOffset ]->GetTextNode();
@@ -723,7 +724,7 @@ void SwUndoReplace::Impl::UndoImpl(::sw::UndoRedoContext & 
rContext)
     }
 
     rPam.GetPoint()->nNode = m_nSttNd;
-    rPam.GetPoint()->nContent = m_nSttCnt;
+    
rPam.GetPoint()->nContent.Assign(rPam.GetPoint()->nNode.GetNode().GetTextNode(),
 m_nSttCnt);
 }
 
 void SwUndoReplace::Impl::RedoImpl(::sw::UndoRedoContext & rContext)

Reply via email to