sw: find & replace: fix soft hyphen cleaning

If there is a formatted soft hyphen after the search string, then
replace skipped the replacement, even if there was a match.

Do the same for comments & footnotes, though the primary reported
problem was just soft hyphens.

(The same happened when manually doing find & replace using the Ctrl-H
dialog.)

Change-Id: I8437e84dea99ceef98d515beef5893cf954e674f
Reviewed-on: https://gerrit.libreoffice.org/c/core/+/99236
Reviewed-by: Miklos Vajna <vmiklos@collabora.com>
Tested-by: Jenkins
diff --git a/sw/CppunitTest_sw_core_crsr.mk b/sw/CppunitTest_sw_core_crsr.mk
new file mode 100644
index 0000000..895b3ff
--- /dev/null
+++ b/sw/CppunitTest_sw_core_crsr.mk
@@ -0,0 +1,73 @@
# -*- Mode: makefile-gmake; tab-width: 4; indent-tabs-mode: t -*-
#*************************************************************************
#
# This file is part of the LibreOffice project.
#
# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this
# file, You can obtain one at http://mozilla.org/MPL/2.0/.
#
#*************************************************************************

$(eval $(call gb_CppunitTest_CppunitTest,sw_core_crsr))

$(eval $(call gb_CppunitTest_use_common_precompiled_header,sw_core_crsr))

$(eval $(call gb_CppunitTest_add_exception_objects,sw_core_crsr, \
    sw/qa/core/crsr/crsr \
))

$(eval $(call gb_CppunitTest_use_libraries,sw_core_crsr, \
    comphelper \
    cppu \
    cppuhelper \
    sal \
    sfx \
    svxcore \
    sw \
    test \
    unotest \
    utl \
    vcl \
    svt \
    tl \
    svl \
))

$(eval $(call gb_CppunitTest_use_externals,sw_core_crsr,\
    boost_headers \
    libxml2 \
))

$(eval $(call gb_CppunitTest_set_include,sw_core_crsr,\
    -I$(SRCDIR)/sw/inc \
    -I$(SRCDIR)/sw/source/core/inc \
    -I$(SRCDIR)/sw/source/uibase/inc \
    -I$(SRCDIR)/sw/qa/inc \
    $$(INCLUDE) \
))

$(eval $(call gb_CppunitTest_use_api,sw_core_crsr,\
	udkapi \
	offapi \
	oovbaapi \
))

$(eval $(call gb_CppunitTest_use_ure,sw_core_crsr))
$(eval $(call gb_CppunitTest_use_vcl,sw_core_crsr))

$(eval $(call gb_CppunitTest_use_rdb,sw_core_crsr,services))

$(eval $(call gb_CppunitTest_use_custom_headers,sw_core_crsr,\
    officecfg/registry \
))

$(eval $(call gb_CppunitTest_use_configuration,sw_core_crsr))

$(eval $(call gb_CppunitTest_use_uiconfigs,sw_core_crsr, \
    modules/swriter \
))

$(eval $(call gb_CppunitTest_use_more_fonts,sw_core_crsr))

# vim: set noet sw=4 ts=4:
diff --git a/sw/Module_sw.mk b/sw/Module_sw.mk
index 9ffbfa4..731dc4e 100644
--- a/sw/Module_sw.mk
+++ b/sw/Module_sw.mk
@@ -116,6 +116,7 @@ $(eval $(call gb_Module_add_slowcheck_targets,sw,\
    CppunitTest_sw_core_txtnode \
    CppunitTest_sw_core_objectpositioning \
    CppunitTest_sw_core_unocore \
    CppunitTest_sw_core_crsr \
))

ifneq ($(DISABLE_GUI),TRUE)
diff --git a/sw/qa/core/crsr/crsr.cxx b/sw/qa/core/crsr/crsr.cxx
new file mode 100644
index 0000000..9345fdd
--- /dev/null
+++ b/sw/qa/core/crsr/crsr.cxx
@@ -0,0 +1,77 @@
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/*
 * This file is part of the LibreOffice project.
 *
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
 */

#include <swmodeltestbase.hxx>

#include <com/sun/star/awt/FontWeight.hpp>
#include <com/sun/star/text/ControlCharacter.hpp>
#include <com/sun/star/view/XLineCursor.hpp>

#include <comphelper/propertysequence.hxx>
#include <svl/srchitem.hxx>

/// Covers sw/source/core/crsr/ fixes.
class SwCoreCrsrTest : public SwModelTestBase
{
};

CPPUNIT_TEST_FIXTURE(SwCoreCrsrTest, testFindReplace)
{
    loadURL("private:factory/swriter", nullptr);

    // Given: a document with two "foo" in it, the second followed by a formatted soft hyphen.
    uno::Reference<text::XTextDocument> xTextDocument(mxComponent, uno::UNO_QUERY);
    uno::Reference<text::XText> xText = xTextDocument->getText();
    // Create a document which has 2 lines: first line has foo in it, second line has the same, but
    // followed by a formatted soft hyphen.
    xText->insertString(xText->getEnd(), "foo xxx", /*bAbsorb=*/false);
    xText->insertControlCharacter(xText->getEnd(), text::ControlCharacter::PARAGRAPH_BREAK,
                                  /*bAbsorb=*/false);
    xText->insertString(xText->getEnd(), OUString(u"foo xxx \xad after"), /*bAbsorb=*/false);
    uno::Reference<frame::XModel> xModel(mxComponent, uno::UNO_QUERY);
    uno::Reference<text::XTextViewCursorSupplier> xTextViewCursorSupplier(
        xModel->getCurrentController(), uno::UNO_QUERY);
    uno::Reference<text::XTextViewCursor> xViewCursor = xTextViewCursorSupplier->getViewCursor();
    xViewCursor->gotoEnd(/*bExpand=*/false);
    xViewCursor->goLeft(/*nCount=*/6, /*bExpand=*/false);
    xViewCursor->goLeft(/*nCount=*/1, /*bExpand=*/true);
    uno::Reference<beans::XPropertySet> xViewCursorProps(xViewCursor, uno::UNO_QUERY);
    xViewCursorProps->setPropertyValue("CharWeight", uno::makeAny(awt::FontWeight::BOLD));
    xViewCursor->gotoStart(/*bExpand=*/false);

    // When: doing search & replace 3 times.
    uno::Sequence<beans::PropertyValue> aArgs(comphelper::InitPropertySequence({
        { "SearchItem.SearchString", uno::makeAny(OUString("foo")) },
        { "SearchItem.ReplaceString", uno::makeAny(OUString("bar")) },
        { "SearchItem.Command", uno::makeAny(static_cast<sal_Int16>(SvxSearchCmd::REPLACE)) },
    }));
    // Find the first foo.
    dispatchCommand(mxComponent, ".uno:ExecuteSearch", aArgs);
    // Replace the first foo.
    dispatchCommand(mxComponent, ".uno:ExecuteSearch", aArgs);
    // Replace the second foo.
    dispatchCommand(mxComponent, ".uno:ExecuteSearch", aArgs);

    // Then: the second "foo" should be replaced as well.
    xViewCursor->gotoEnd(/*bExpand=*/false);
    uno::Reference<view::XLineCursor> xLineCursor(xViewCursor, uno::UNO_QUERY);
    xLineCursor->gotoStartOfLine(/*bExpand=*/true);
    OUString aActual = xViewCursor->getString();
    CPPUNIT_ASSERT_GREATEREQUAL(static_cast<sal_Int32>(3), aActual.getLength());
    OUString aActualStart = aActual.copy(0, 3);
    // Without the accompanying fix in place, this test would have failed with:
    // - Expected: bar
    // - Actual  : foo
    // i.e. the foo on the second line was not replaced.
    CPPUNIT_ASSERT_EQUAL(OUString("bar"), aActualStart);
}

CPPUNIT_PLUGIN_IMPLEMENT();

/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
diff --git a/sw/source/core/crsr/findtxt.cxx b/sw/source/core/crsr/findtxt.cxx
index 526fe6a..b7c4afb 100644
--- a/sw/source/core/crsr/findtxt.cxx
+++ b/sw/source/core/crsr/findtxt.cxx
@@ -293,7 +293,10 @@ lcl_CleanStr(const SwTextNode& rNd,
                        if ( bEmpty && nStart == nCurrent )
                        {
                            rArr.push_back( nCurrent );
                            --rEnd.GetAnyIndex();
                            if (rEnd.GetAnyIndex() > nCurrent.GetAnyIndex())
                            {
                                --rEnd.GetAnyIndex();
                            }
                            buf.remove(nCurrent.GetAnyIndex(), 1);
                        }
                        else
@@ -309,7 +312,10 @@ lcl_CleanStr(const SwTextNode& rNd,
                        if( bRemoveCommentAnchors )
                        {
                            rArr.push_back( nCurrent );
                            --rEnd.GetAnyIndex();
                            if (rEnd.GetAnyIndex() > nCurrent.GetAnyIndex())
                            {
                                --rEnd.GetAnyIndex();
                            }
                            buf.remove( nCurrent.GetAnyIndex(), 1 );
                        }
                    }
@@ -325,7 +331,14 @@ lcl_CleanStr(const SwTextNode& rNd,
        if ( bNewSoftHyphen )
        {
            rArr.push_back( nCurrent );
            --rEnd.GetAnyIndex();

            // If the soft hyphen to be removed is past the end of the range we're searching in,
            // don't adjust the end.
            if (rEnd.GetAnyIndex() > nCurrent.GetAnyIndex())
            {
                --rEnd.GetAnyIndex();
            }

            buf.remove(nCurrent.GetAnyIndex(), 1);
            ++nSoftHyphen.GetAnyIndex();
        }