tdf#132514 DOCX import: fix lost table style with footer

Table paragraphs collected for table style processing
were mixed when both body text and footer contain tables,
i.e. clearing paragraph vector at processing the first table
resulted missing paragraph vector and table style processing
for the other one.

Now tables in footer, also nested tables collect their
paragraphs in separated table paragraph vectors.

Regression from commit 6c5da2cd7af5c2d90e4d8e9635ba8c9989c87923
(tdf#119054 DOCX: fix not table style based bottom margin).

Change-Id: Ib8568d8379cfb7da869120cdc7fe12895252d661
Reviewed-on: https://gerrit.libreoffice.org/c/core/+/93415
Tested-by: Jenkins
Reviewed-by: László Németh <nemeth@numbertext.org>
diff --git a/sw/qa/extras/ooxmlexport/data/tdf132514.docx b/sw/qa/extras/ooxmlexport/data/tdf132514.docx
new file mode 100644
index 0000000..d32f841
--- /dev/null
+++ b/sw/qa/extras/ooxmlexport/data/tdf132514.docx
Binary files differ
diff --git a/sw/qa/extras/ooxmlexport/ooxmlexport6.cxx b/sw/qa/extras/ooxmlexport/ooxmlexport6.cxx
index 9b41605..e262303 100644
--- a/sw/qa/extras/ooxmlexport/ooxmlexport6.cxx
+++ b/sw/qa/extras/ooxmlexport/ooxmlexport6.cxx
@@ -474,6 +474,14 @@ DECLARE_OOXMLEXPORT_EXPORTONLY_TEST(testTdf131258, "tdf131258.docx")
    assertXPath(pXmlDoc, "/w:document/w:body/w:tbl/w:tr[1]/w:tc[1]/w:p/w:pPr/w:spacing", "after", "0");
}

DECLARE_OOXMLEXPORT_EXPORTONLY_TEST(testTdf132514, "tdf132514.docx")
{
    xmlDocPtr pXmlDoc = parseExport();
    // Keep table style setting, when the footer also contain a table
    assertXPath(pXmlDoc, "/w:document/w:body/w:tbl/w:tr[1]/w:tc[2]/w:p[2]/w:pPr/w:spacing", "before", "0");
    assertXPath(pXmlDoc, "/w:document/w:body/w:tbl/w:tr[1]/w:tc[2]/w:p[2]/w:pPr/w:spacing", "after", "0");
}

DECLARE_OOXMLEXPORT_EXPORTONLY_TEST(testFdo69636, "fdo69636.docx")
{
    /*
diff --git a/writerfilter/source/dmapper/DomainMapperTableHandler.cxx b/writerfilter/source/dmapper/DomainMapperTableHandler.cxx
index 0c73bb6..a3548cd 100644
--- a/writerfilter/source/dmapper/DomainMapperTableHandler.cxx
+++ b/writerfilter/source/dmapper/DomainMapperTableHandler.cxx
@@ -1252,6 +1252,7 @@ void DomainMapperTableHandler::endTable(unsigned int nestedTableLevel, bool bTab

            if ( !aAllTableParaProperties.empty() )
            {
                TableParagraphVectorPtr pTableParagraphs = m_rDMapper_Impl.getTableManager().getCurrentParagraphs();
                for (size_t nRow = 0; nRow < m_aTableRanges.size(); ++nRow)
                {
                    for (size_t nCell = 0; nCell < m_aTableRanges[nRow].size(); ++nCell)
@@ -1263,8 +1264,8 @@ void DomainMapperTableHandler::endTable(unsigned int nestedTableLevel, bool bTab
                        uno::Reference<text::XTextRangeCompare> xTextRangeCompare(rStartPara->getText(), uno::UNO_QUERY);
                        bool bApply = false;
                        // search paragraphs of the cell
                        std::vector<TableParagraph>::iterator aIt = m_rDMapper_Impl.m_aParagraphsToEndTable.begin();
                        while ( aIt != m_rDMapper_Impl.m_aParagraphsToEndTable.end() ) try
                        std::vector<TableParagraph>::iterator aIt = pTableParagraphs->begin();
                        while ( aIt != pTableParagraphs->end() ) try
                        {
                            if (!bApply && xTextRangeCompare->compareRegionStarts(rStartPara, aIt->m_rStartParagraph) == 0)
                                bApply = true;
@@ -1273,7 +1274,7 @@ void DomainMapperTableHandler::endTable(unsigned int nestedTableLevel, bool bTab
                                bool bEndOfApply = (xTextRangeCompare->compareRegionEnds(rEndPara, aIt->m_rEndParagraph) == 0);
                                ApplyParagraphPropertiesFromTableStyle(*aIt, aAllTableParaProperties, aCellProperties[nRow][nCell]);
                                // erase processed paragraph from list of pending paragraphs
                                aIt = m_rDMapper_Impl.m_aParagraphsToEndTable.erase(aIt);
                                aIt = pTableParagraphs->erase(aIt);
                                if (bEndOfApply)
                                    break;
                            }
@@ -1435,8 +1436,6 @@ void DomainMapperTableHandler::endTable(unsigned int nestedTableLevel, bool bTab
    m_aCellProperties.clear();
    m_aRowProperties.clear();
    m_bHadFootOrEndnote = false;
    if (nestedTableLevel <= 1 && m_rDMapper_Impl.m_bConvertedTable)
        m_rDMapper_Impl.m_aParagraphsToEndTable.clear();

#ifdef DBG_UTIL
    TagLogger::getInstance().endElement();
diff --git a/writerfilter/source/dmapper/DomainMapperTableHandler.hxx b/writerfilter/source/dmapper/DomainMapperTableHandler.hxx
index 142b781..545f609 100644
--- a/writerfilter/source/dmapper/DomainMapperTableHandler.hxx
+++ b/writerfilter/source/dmapper/DomainMapperTableHandler.hxx
@@ -55,15 +55,6 @@ struct HorizontallyMergedCell
    }
};

/// Information about a paragraph to be finished after a table end.
struct TableParagraph
{
    css::uno::Reference<css::text::XTextRange> m_rStartParagraph;
    css::uno::Reference<css::text::XTextRange> m_rEndParagraph;
    PropertyMapPtr m_pPropertyMap;
    css::uno::Reference<css::beans::XPropertySet> m_rPropertySet;
};

/// Class to handle events generated by TableManager::resolveCurrentTable().
class DomainMapperTableHandler final : public virtual SvRefBase
{
diff --git a/writerfilter/source/dmapper/DomainMapperTableManager.cxx b/writerfilter/source/dmapper/DomainMapperTableManager.cxx
index a1bb10e..9e7cf46 100644
--- a/writerfilter/source/dmapper/DomainMapperTableManager.cxx
+++ b/writerfilter/source/dmapper/DomainMapperTableManager.cxx
@@ -50,6 +50,7 @@ DomainMapperTableManager::DomainMapperTableManager() :
    m_bPushCurrentWidth(false),
    m_bTableSizeTypeInserted(false),
    m_nLayoutType(0),
    m_aParagraphsToEndTable(),
    m_pTablePropsHandler(new TablePropertiesHandler())
{
    m_pTablePropsHandler->SetTableManager( this );
@@ -424,6 +425,11 @@ TablePositionHandler* DomainMapperTableManager::getCurrentTableRealPosition()
        return nullptr;
}

TableParagraphVectorPtr DomainMapperTableManager::getCurrentParagraphs( )
{
    return m_aParagraphsToEndTable.top( );
}

void DomainMapperTableManager::setIsInShape(bool bIsInShape)
{
    m_bIsInShape = bIsInShape;
@@ -440,6 +446,12 @@ void DomainMapperTableManager::startLevel( )
        oCurrentWidth = m_aCellWidths.back()->back();
        m_aCellWidths.back()->pop_back();
    }
    std::optional<TableParagraph> oParagraph;
    if (getTableDepthDifference() > 0 && !m_aParagraphsToEndTable.empty() && !m_aParagraphsToEndTable.top()->empty())
    {
        oParagraph = m_aParagraphsToEndTable.top()->back();
        m_aParagraphsToEndTable.top()->pop_back();
    }

    IntVectorPtr pNewGrid = std::make_shared<vector<sal_Int32>>();
    IntVectorPtr pNewSpans = std::make_shared<vector<sal_Int32>>();
@@ -460,10 +472,14 @@ void DomainMapperTableManager::startLevel( )
    m_aGridBefore.push_back( 0 );
    m_nTableWidth = 0;
    m_nLayoutType = 0;
    TableParagraphVectorPtr pNewParagraphs = std::make_shared<vector<TableParagraph>>();
    m_aParagraphsToEndTable.push( pNewParagraphs );

    // And push it back to the right level.
    if (oCurrentWidth)
        m_aCellWidths.back()->push_back(*oCurrentWidth);
    if (oParagraph)
        m_aParagraphsToEndTable.top()->push_back(*oParagraph);
}

void DomainMapperTableManager::endLevel( )
@@ -508,6 +524,7 @@ void DomainMapperTableManager::endLevel( )
    // in the endTable method called in endLevel.
    m_aTablePositions.pop_back();
    m_aTableStyleNames.pop_back();
    m_aParagraphsToEndTable.pop();
}

void DomainMapperTableManager::endOfCellAction()
diff --git a/writerfilter/source/dmapper/DomainMapperTableManager.hxx b/writerfilter/source/dmapper/DomainMapperTableManager.hxx
index 0801e9d..cb298e1 100644
--- a/writerfilter/source/dmapper/DomainMapperTableManager.hxx
+++ b/writerfilter/source/dmapper/DomainMapperTableManager.hxx
@@ -63,6 +63,8 @@ class DomainMapperTableManager : public TableManager
    bool m_bTableSizeTypeInserted;
    /// Table layout algorithm, IOW if we should consider fixed column width or not.
    sal_uInt32 m_nLayoutType;
    /// Collected table paragraphs for table style handling
    std::stack< TableParagraphVectorPtr > m_aParagraphsToEndTable;

    std::unique_ptr<TablePropertiesHandler> m_pTablePropsHandler;
    PropertyMapPtr            m_pStyleProps;
@@ -93,6 +95,7 @@ public:
    IntVectorPtr const & getCurrentSpans( );
    IntVectorPtr const & getCurrentCellWidths( );
    sal_uInt32 getCurrentGridBefore( );
    TableParagraphVectorPtr getCurrentParagraphs( );

    /// Turn the attributes collected so far in m_aTableLook into a property and clear the container.
    void finishTableLook();
diff --git a/writerfilter/source/dmapper/DomainMapper_Impl.cxx b/writerfilter/source/dmapper/DomainMapper_Impl.cxx
index 4ef7b844..f385565 100644
--- a/writerfilter/source/dmapper/DomainMapper_Impl.cxx
+++ b/writerfilter/source/dmapper/DomainMapper_Impl.cxx
@@ -1786,7 +1786,7 @@ void DomainMapper_Impl::finishParagraph( const PropertyMapPtr& pPropertyMap, con
                    if (m_nTableDepth > 0)
                    {
                        TableParagraph aPending{xParaCursor, xCur, pParaContext, xParaProps};
                        m_aParagraphsToEndTable.push_back(aPending);
                        getTableManager().getCurrentParagraphs()->push_back(aPending);
                    }

                    // hidden empty paragraph with a not hidden shape, set as not hidden
diff --git a/writerfilter/source/dmapper/DomainMapper_Impl.hxx b/writerfilter/source/dmapper/DomainMapper_Impl.hxx
index 33a4eb5..ddce870 100644
--- a/writerfilter/source/dmapper/DomainMapper_Impl.hxx
+++ b/writerfilter/source/dmapper/DomainMapper_Impl.hxx
@@ -1058,9 +1058,6 @@ public:
    bool m_bIsActualParagraphFramed;
    std::vector<css::uno::Any> aFramedRedlines;

    /// Table paragraph properties may need style update based on table style
    std::vector<TableParagraph> m_aParagraphsToEndTable;

private:
    void PushPageHeaderFooter(bool bHeader, SectionPropertyMap::PageType eType);
    // Start a new index section; if needed, finish current paragraph
diff --git a/writerfilter/source/dmapper/PropertyMap.hxx b/writerfilter/source/dmapper/PropertyMap.hxx
index 6bfc1fe..f3b7f828b 100644
--- a/writerfilter/source/dmapper/PropertyMap.hxx
+++ b/writerfilter/source/dmapper/PropertyMap.hxx
@@ -592,6 +592,17 @@ public:

typedef tools::SvRef< TablePropertyMap > TablePropertyMapPtr;

/// Information about a paragraph to be finished after a table end.
struct TableParagraph
{
    css::uno::Reference<css::text::XTextRange> m_rStartParagraph;
    css::uno::Reference<css::text::XTextRange> m_rEndParagraph;
    PropertyMapPtr m_pPropertyMap;
    css::uno::Reference<css::beans::XPropertySet> m_rPropertySet;
};

typedef std::shared_ptr< std::vector<TableParagraph> > TableParagraphVectorPtr;

} // namespace dmapper
} // namespace writerfilter