From 098a2536d08a18981032273b55e287bd231dc58e Mon Sep 17 00:00:00 2001 From: Justin Luth Date: Fri, 15 Mar 2024 20:01:39 -0400 Subject: tdf#147583 sw find: fix backwards search for emptyPara/endOfPara A comment added 10 years ago thought that it had never worked. The very last paragraph end cannot be selected manually, and so of course it can't work in find either. It didn't work earlier either. Everything goes ballistic if searching inside comments, but that would be handled elsewhere anyway I guess. It didn't work earlier either. Keeping the search "inside the selection" didn't work earlier either. That should be fixed now. make CppunitTest_sw_uiwriter7 \ CPPUNIT_TEST_NAME=testTdf147583_backwardSearch Change-Id: I48a72084d277b8c270255de9296a2743162937cf Reviewed-on: https://gerrit.libreoffice.org/c/core/+/164892 Tested-by: Jenkins Reviewed-by: Justin Luth Reviewed-by: Miklos Vajna Reviewed-on: https://gerrit.libreoffice.org/c/core/+/164982 Tested-by: Jenkins CollaboraOffice (cherry picked from commit 2f5b9916baaf0017ece84bf867a9e9acfe86d61a) --- .../uiwriter/data/tdf147583_backwardSearch.odt | Bin 0 -> 28013 bytes sw/qa/extras/uiwriter/uiwriter7.cxx | 31 ++++++++++ sw/source/core/crsr/findtxt.cxx | 67 +++++++++++---------- 3 files changed, 66 insertions(+), 32 deletions(-) create mode 100644 sw/qa/extras/uiwriter/data/tdf147583_backwardSearch.odt diff --git a/sw/qa/extras/uiwriter/data/tdf147583_backwardSearch.odt b/sw/qa/extras/uiwriter/data/tdf147583_backwardSearch.odt new file mode 100644 index 000000000000..9bfde3bfaa6b Binary files /dev/null and b/sw/qa/extras/uiwriter/data/tdf147583_backwardSearch.odt differ diff --git a/sw/qa/extras/uiwriter/uiwriter7.cxx b/sw/qa/extras/uiwriter/uiwriter7.cxx index 92b6732f2778..dbae48e33798 100644 --- a/sw/qa/extras/uiwriter/uiwriter7.cxx +++ b/sw/qa/extras/uiwriter/uiwriter7.cxx @@ -357,6 +357,37 @@ CPPUNIT_TEST_FIXTURE(SwUiWriterTest7, testTextSearch) pCursor->GetPointNode().GetTextNode()->GetText()); } +CPPUNIT_TEST_FIXTURE(SwUiWriterTest7, testTdf147583_backwardSearch) +{ + createSwDoc("tdf147583_backwardSearch.odt"); + uno::Reference xSearch(mxComponent, uno::UNO_QUERY); + uno::Reference xSearchDes = xSearch->createSearchDescriptor(); + uno::Reference xProp(xSearchDes, uno::UNO_QUERY); + + uno::Reference xIndex; + const sal_Int32 nParas = getParagraphs(); + + //specifying the search attributes + uno::Reference xPropSet(xSearchDes, uno::UNO_QUERY_THROW); + xSearchDes->setPropertyValue("SearchRegularExpression", uno::Any(true)); // regex + xSearchDes->setSearchString("$"); // the end of the paragraph pilcrow marker + + // xSearchDes->setPropertyValue("SearchBackwards", uno::Any(false)); + // xIndex.set(xSearch->findAll(xSearchDes), uno::UNO_SET_THROW); + // // all paragraphs (including the unselected last one) should be found + // CPPUNIT_ASSERT_EQUAL(nParas, xIndex->getCount()); + + xSearchDes->setPropertyValue("SearchBackwards", uno::Any(true)); + xIndex.set(xSearch->findAll(xSearchDes), uno::UNO_SET_THROW); + // all paragraphs (except the troublesome last one) are found + CPPUNIT_ASSERT_EQUAL(nParas - 1, xIndex->getCount()); + + xSearchDes->setSearchString("^$"); // empty paragraphs + xIndex.set(xSearch->findAll(xSearchDes), uno::UNO_SET_THROW); + // should actually be 10 (including the empty para with the comment marker, and the last para) + CPPUNIT_ASSERT_EQUAL(sal_Int32(8), xIndex->getCount()); +} + CPPUNIT_TEST_FIXTURE(SwUiWriterTest7, testTdf69282) { createSwDoc(); diff --git a/sw/source/core/crsr/findtxt.cxx b/sw/source/core/crsr/findtxt.cxx index 26805b281adc..e87e4d6ff5c1 100644 --- a/sw/source/core/crsr/findtxt.cxx +++ b/sw/source/core/crsr/findtxt.cxx @@ -730,7 +730,6 @@ bool DoSearch(SwPaM & rSearchPam, SwRootFrame const*const pLayout, SwPaM* pPam) { bool bFound = false; - SwPosition& rPtPos = *pPam->GetPoint(); OUString sCleanStr; std::vector aFltArr; LanguageType eLastLang = LANGUAGE_SYSTEM; @@ -872,39 +871,43 @@ bool DoSearch(SwPaM & rSearchPam, if ( bFound ) return true; - else if ((bChkEmptyPara && !nStart.GetAnyIndex() && !nTextLen.GetAnyIndex()) - || bChkParaEnd) + + if (!bChkEmptyPara && !bChkParaEnd) + return false; + + if (bChkEmptyPara && bSrchForward && nTextLen.GetAnyIndex()) + return false; // the length is not zero - there is content here + + // move to the end (or start) of the paragraph + *rSearchPam.GetPoint() = *pPam->GetPoint(); + if (pLayout) { - *rSearchPam.GetPoint() = *pPam->GetPoint(); - if (pLayout) - { - *rSearchPam.GetPoint() = pFrame->MapViewToModelPos( - bChkParaEnd ? nTextLen.GetFrameIndex() : TextFrameIndex(0)); - } - else - { - rSearchPam.GetPoint()->SetContent( bChkParaEnd ? nTextLen.GetModelIndex() : 0 ); - } - rSearchPam.SetMark(); - const SwNode *const pSttNd = bSrchForward - ? &rSearchPam.GetPoint()->GetNode() // end of the frame - : &rPtPos.GetNode(); // keep the bug as-is for now... - /* FIXME: this condition does not work for !bSrchForward backward - * search, it probably never did. (pSttNd != &rNdIdx.GetNode()) - * is never true in this case. */ - if( (bSrchForward || pSttNd != &rPtPos.GetNode()) && - rSearchPam.Move(fnMoveForward, GoInContent) && - (!bSrchForward || pSttNd != &rSearchPam.GetPoint()->GetNode()) && - SwNodeOffset(1) == abs(rSearchPam.GetPoint()->GetNodeIndex() - - rSearchPam.GetMark()->GetNodeIndex())) - { - // if backward search, switch point and mark - if( !bSrchForward ) - rSearchPam.Exchange(); - return true; - } + *rSearchPam.GetPoint() = pFrame->MapViewToModelPos( + bSrchForward ? nTextLen.GetFrameIndex() : TextFrameIndex(0)); } - return bFound; + else + { + rSearchPam.GetPoint()->SetContent(bSrchForward ? nTextLen.GetModelIndex() : 0); + } + rSearchPam.SetMark(); + + if (!rSearchPam.Move(fnMove, GoInContent)) + return false; // at start or end of the document + + // selection must not be outside of the search area + if (!pPam->ContainsPosition(*rSearchPam.GetPoint())) + return false; + + if (SwNodeOffset(1) == abs(rSearchPam.GetPoint()->GetNodeIndex() - + rSearchPam.GetMark()->GetNodeIndex())) + { + if (bChkEmptyPara && !bSrchForward && rSearchPam.GetPoint()->GetContentIndex()) + return false; // the length is not zero - there is content here + + return true; + } + + return false; } namespace { -- cgit