From 0ddd9f9ff45f61013ea18763eca4c68aedce6caa Mon Sep 17 00:00:00 2001 From: Mike Kaganski Date: Mon, 11 May 2015 02:31:39 +1000 Subject: tdf#70318: don't forget to clean up second fake paragraph RTF insert is made into an empty paragraph. To do that, two splits are made before the insert, but only one is reverted afterwards. This patch removes the second. Also fixes a memory leak from unreleased heap object The corresponding unit test is corrected. It was incorrect because \par doesn't begin new paragraph; it only ends paragraph. If a RTF is ended with \par } then no newline is added to its end. The old unit test only worked because of the bug fixed by this patch. Correct way of inserting new paragraph in the end of a RTF is \par \par} Change-Id: I63d50a940d7960beb35f7d774c833ed8499acbef --- sw/source/filter/rtf/swparrtf.cxx | 25 ++++++++++++++++++++++++- 1 file changed, 24 insertions(+), 1 deletion(-) (limited to 'sw/source/filter/rtf/swparrtf.cxx') diff --git a/sw/source/filter/rtf/swparrtf.cxx b/sw/source/filter/rtf/swparrtf.cxx index 2730561bdfa2..987fba83fefb 100644 --- a/sw/source/filter/rtf/swparrtf.cxx +++ b/sw/source/filter/rtf/swparrtf.cxx @@ -50,7 +50,7 @@ sal_uLong SwRTFReader::Read(SwDoc& rDoc, const OUString& /*rBaseURL*/, SwPaM& rP // Step 1: XTextRange will be updated when content is inserted, so we know // the end position. const uno::Reference xInsertPosition = SwXTextRange::CreateXTextRange(rDoc, *rPam.GetPoint(), 0); - SwNodeIndex* pSttNdIdx = new SwNodeIndex(rDoc.GetNodes()); + std::shared_ptr pSttNdIdx(new SwNodeIndex(rDoc.GetNodes())); const SwPosition* pPos = rPam.GetPoint(); // Step 2: Split once and remember the node that has been split. @@ -59,6 +59,8 @@ sal_uLong SwRTFReader::Read(SwDoc& rDoc, const OUString& /*rBaseURL*/, SwPaM& rP // Step 3: Split again. rDoc.getIDocumentContentOperations().SplitNode(*pPos, false); + std::shared_ptr pSttNdIdx2(new SwNodeIndex(rDoc.GetNodes())); + *pSttNdIdx2 = pPos->nNode.GetIndex(); // Step 4: Insert all content into the new node rPam.Move(fnMoveBackward); @@ -126,6 +128,27 @@ sal_uLong SwRTFReader::Read(SwDoc& rDoc, const OUString& /*rBaseURL*/, SwPaM& rP } } + if (pSttNdIdx2->GetIndex()) + { + // If we are in insert mode, join the split node that is after + // the new content with the last new node. Or in other words: + // Revert the second split node. + SwTxtNode* pTxtNode = pSttNdIdx2->GetNode().GetTxtNode(); + SwNodeIndex aPrevIdx(*pSttNdIdx2); + if (pTxtNode && pTxtNode->CanJoinPrev(&aPrevIdx) && pSttNdIdx2->GetIndex() - 1 == aPrevIdx.GetIndex()) + { + // If the last new node isn't empty, convert the node's text + // attributes into hints. Otherwise, set the new node's + // paragraph style at the next (empty) node. + SwTxtNode* pDelNd = aPrevIdx.GetNode().GetTxtNode(); + if (pTxtNode->GetTxt().getLength()) + pDelNd->FmtToTxtAttr(pTxtNode); + else + pTxtNode->ChgFmtColl(pDelNd->GetTxtColl()); + pTxtNode->JoinPrev(); + } + } + return ret; } -- cgit