tdf#125522: Mail merge: Hidden text frames are not completely removed

Change-Id: I67550b325a0ac177ffbd52718234c2a0edf1154c
Reviewed-on: https://gerrit.libreoffice.org/73033
Tested-by: Jenkins
Reviewed-by: Thorsten Behrens <Thorsten.Behrens@CIB.de>
diff --git a/sw/qa/extras/mailmerge/data/tdf125522.odt b/sw/qa/extras/mailmerge/data/tdf125522.odt
new file mode 100644
index 0000000..265d8fc
--- /dev/null
+++ b/sw/qa/extras/mailmerge/data/tdf125522.odt
Binary files differ
diff --git a/sw/qa/extras/mailmerge/mailmerge.cxx b/sw/qa/extras/mailmerge/mailmerge.cxx
index d4ef29e..7e6e209 100644
--- a/sw/qa/extras/mailmerge/mailmerge.cxx
+++ b/sw/qa/extras/mailmerge/mailmerge.cxx
@@ -933,6 +933,30 @@ DECLARE_SHELL_MAILMERGE_TEST(testTdf62364, "tdf62364.odt", "10-testing-addresses
    }
}

DECLARE_SHELL_MAILMERGE_TEST(tdf125522_shell, "tdf125522.odt", "10-testing-addresses.ods", "testing-addresses")
{
    // prepare unit test and run
    executeMailMerge();

    // reset currently opened layout of the original template,
    // and create the layout of the document with 10 mails inside
    dumpMMLayout();

    // there should be no any text frame in output
    SwXTextDocument* pTextDoc = dynamic_cast<SwXTextDocument *>(mxMMComponent.get());
    CPPUNIT_ASSERT(pTextDoc);

    const auto & rNodes = pTextDoc->GetDocShell()->GetDoc()->GetNodes();
    for (sal_uLong nodeIndex = 0; nodeIndex<rNodes.Count(); nodeIndex++)
    {
        const SwNodePtr aNode = rNodes[nodeIndex];
        if (aNode->StartOfSectionNode())
        {
            CPPUNIT_ASSERT(!aNode->StartOfSectionNode()->GetFlyFormat());
        }
    }
}

DECLARE_SHELL_MAILMERGE_TEST(testTd78611_shell, "tdf78611.odt", "10-testing-addresses.ods", "testing-addresses")
{
    // prepare unit test and run
diff --git a/sw/source/core/doc/doc.cxx b/sw/source/core/doc/doc.cxx
index 2da307c..7828fba 100644
--- a/sw/source/core/doc/doc.cxx
+++ b/sw/source/core/doc/doc.cxx
@@ -1438,12 +1438,30 @@ bool SwDoc::RemoveInvisibleContent()
        if ( pTextNd )
        {
            bool bRemoved = false;
            SwPaM aPam(*pTextNd, 0, *pTextNd, pTextNd->GetText().getLength());
            if ( pTextNd->HasHiddenCharAttribute( true ) )
            {
                bRemoved = true;
                bRet = true;
                RemoveOrDeleteContents(pTextNd, getIDocumentContentOperations());

                if (2 == pTextNd->EndOfSectionIndex() - pTextNd->StartOfSectionIndex())
                {
                    SwFrameFormat *const pFormat = pTextNd->StartOfSectionNode()->GetFlyFormat();
                    if (nullptr != pFormat)
                    {
                        // remove hidden text frame
                        getIDocumentLayoutAccess().DelLayoutFormat(pFormat);
                    }
                    else
                    {
                        // default, remove hidden paragraph
                        RemoveOrDeleteContents(pTextNd, getIDocumentContentOperations());
                    }
                }
                else
                {
                    // default, remove hidden paragraph
                    RemoveOrDeleteContents(pTextNd, getIDocumentContentOperations());
                }
            }
            else if ( pTextNd->HasHiddenCharAttribute( false ) )
            {
@@ -1455,7 +1473,11 @@ bool SwDoc::RemoveInvisibleContent()
            // Footnotes/Frames may have been removed, therefore we have
            // to reset n:
            if ( bRemoved )
                n = aPam.GetPoint()->nNode.GetIndex();
            {
                // [n] has to be inside [0 .. GetNodes().Count()] range
                if (n > GetNodes().Count())
                    n = GetNodes().Count();
            }
        }
    }