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

New commits:
commit 45613274794636ba98d0e978fe872511297d275d
Author:     Michael Stahl <michael.st...@allotropia.de>
AuthorDate: Thu Jun 9 18:58:06 2022 +0200
Commit:     Michael Stahl <michael.st...@allotropia.de>
CommitDate: Fri Jun 10 14:59:17 2022 +0200

    tdf#140007 sw: fix SwUndoReplace
    
    (regression from commit d6b0e84b236b78f4b21bd16e46dda3fa0876096d)
    
    Change-Id: I1facf1584a349d1d087438f4e6fd3a63a80c6f7e
    Reviewed-on: https://gerrit.libreoffice.org/c/core/+/135585
    Tested-by: Jenkins
    Reviewed-by: Michael Stahl <michael.st...@allotropia.de>

diff --git a/sw/qa/extras/uiwriter/uiwriter2.cxx 
b/sw/qa/extras/uiwriter/uiwriter2.cxx
index 8e844e424578..8aedaf38ef7a 100644
--- a/sw/qa/extras/uiwriter/uiwriter2.cxx
+++ b/sw/qa/extras/uiwriter/uiwriter2.cxx
@@ -944,6 +944,93 @@ CPPUNIT_TEST_FIXTURE(SwUiWriterTest2, testTdf131912)
     CPPUNIT_ASSERT_EQUAL(OUString("foo"), pWrtShell->GetCursor()->GetText());
 }
 
+CPPUNIT_TEST_FIXTURE(SwUiWriterTest2, testTdf140007)
+{
+    SwDoc* const pDoc = createSwDoc();
+    SwWrtShell* const pWrtShell = pDoc->GetDocShell()->GetWrtShell();
+
+    pWrtShell->Insert("foo");
+    pWrtShell->SplitNode();
+    pWrtShell->Insert("bar");
+    pWrtShell->SplitNode();
+    pWrtShell->Insert("baz");
+    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->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, testTdf39721)
 {
 // FIXME: disabled on Windows because of a not reproducible problem (not 
related to the patch)
diff --git a/sw/source/core/undo/unins.cxx b/sw/source/core/undo/unins.cxx
index a47fbcc9ed9f..5c4c8c9474ad 100644
--- a/sw/source/core/undo/unins.cxx
+++ b/sw/source/core/undo/unins.cxx
@@ -686,7 +686,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();
@@ -720,7 +721,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