tdf#123757 DOCX import: separate different style tables

to avoid of losing table layout, color etc. during
their prohibited combination into one table: table
style of the second table overwrote the table style
of the first table, or without table style, the
second table was overwritten by the table style of
the first table.

Change-Id: I0a2663d41f7d019e7b329198944a300260025b65
Reviewed-on: https://gerrit.libreoffice.org/c/core/+/89416
Tested-by: Jenkins
Tested-by: László Németh <nemeth@numbertext.org>
Reviewed-by: László Németh <nemeth@numbertext.org>
diff --git a/sw/qa/extras/ooxmlexport/data/tdf123757.docx b/sw/qa/extras/ooxmlexport/data/tdf123757.docx
new file mode 100644
index 0000000..6cfc821
--- /dev/null
+++ b/sw/qa/extras/ooxmlexport/data/tdf123757.docx
Binary files differ
diff --git a/sw/qa/extras/ooxmlexport/ooxmlexport14.cxx b/sw/qa/extras/ooxmlexport/ooxmlexport14.cxx
index 9a35092..ae8c0a8 100644
--- a/sw/qa/extras/ooxmlexport/ooxmlexport14.cxx
+++ b/sw/qa/extras/ooxmlexport/ooxmlexport14.cxx
@@ -544,6 +544,13 @@ DECLARE_OOXMLEXPORT_EXPORTONLY_TEST(testTdf128290, "tdf128290.odt")
    assertXPath(pXml, "/w:document/w:body/w:tbl/w:tblPr/w:tblLayout", "type", "fixed");
}

DECLARE_OOXMLEXPORT_EXPORTONLY_TEST(testTdf123757, "tdf123757.docx")
{
    xmlDocPtr pXml = parseExport("word/document.xml");
    CPPUNIT_ASSERT(pXml);
    assertXPath(pXml, "/w:document/w:body/w:tbl", 2);
}

DECLARE_OOXMLEXPORT_TEST(testContSectBreakHeaderFooter, "cont-sect-break-header-footer.docx")
{
    // Load a document with a continuous section break on page 2.
diff --git a/writerfilter/source/dmapper/DomainMapperTableManager.cxx b/writerfilter/source/dmapper/DomainMapperTableManager.cxx
index 0701dc1..18ddcd9 100644
--- a/writerfilter/source/dmapper/DomainMapperTableManager.cxx
+++ b/writerfilter/source/dmapper/DomainMapperTableManager.cxx
@@ -238,9 +238,8 @@ bool DomainMapperTableManager::sprm(Sprm & rSprm)
            break;
            case NS_ooxml::LN_CT_TblPrBase_tblStyle: //table style name
            {
                m_sTableStyleName = pValue->getString();
                TablePropertyMapPtr pPropMap( new TablePropertyMap );
                pPropMap->Insert( META_PROP_TABLE_STYLE_NAME, uno::makeAny( m_sTableStyleName ));
                pPropMap->Insert( META_PROP_TABLE_STYLE_NAME, uno::makeAny( pValue->getString() ));
                insertTableProps(pPropMap);
            }
            break;
@@ -444,6 +443,8 @@ void DomainMapperTableManager::startLevel( )
    m_aGridSpans.push_back( pNewSpans );
    m_aCellWidths.push_back( pNewCellWidths );
    m_aTablePositions.push_back( pNewPositionHandler );
    // empty name will be replaced by the table style name, if it exists
    m_aTableStyleNames.push_back( OUString() );

    TablePositionHandlerPtr pTmpPosition;
    TablePropertyMapPtr pTmpProperties( new TablePropertyMap( ) );
@@ -500,6 +501,7 @@ void DomainMapperTableManager::endLevel( )
    // Pop back the table position after endLevel as it's used
    // in the endTable method called in endLevel.
    m_aTablePositions.pop_back();
    m_aTableStyleNames.pop_back();
}

void DomainMapperTableManager::endOfCellAction()
@@ -521,7 +523,7 @@ void DomainMapperTableManager::endOfRowAction()
    TagLogger::getInstance().startElement("endOfRowAction");
#endif

    // Compare the table position with the previous ones. We may need to split
    // Compare the table position and style with the previous ones. We may need to split
    // into two tables if those are different. We surely don't want to do anything
    // if we don't have any row yet.
    TablePositionHandlerPtr pTmpPosition = m_aTmpPosition.back();
@@ -529,7 +531,13 @@ void DomainMapperTableManager::endOfRowAction()
    TablePositionHandlerPtr pCurrentPosition = m_aTablePositions.back();
    bool bSamePosition = ( pTmpPosition == pCurrentPosition ) ||
                         ( pTmpPosition && pCurrentPosition && *pTmpPosition == *pCurrentPosition );
    if ( !bSamePosition && m_nRow > 0 )
    bool bIsSetTableStyle = pTablePropMap->isSet(META_PROP_TABLE_STYLE_NAME);
    OUString sTableStyleName;
    bool bSameTableStyle = ( !bIsSetTableStyle && m_aTableStyleNames.back().isEmpty()) ||
            ( bIsSetTableStyle &&
              (pTablePropMap->getProperty(META_PROP_TABLE_STYLE_NAME)->second >>= sTableStyleName) &&
              sTableStyleName == m_aTableStyleNames.back() );
    if ( (!bSamePosition || !bSameTableStyle) && m_nRow > 0 )
    {
        // Save the grid infos to have them survive the end/start level
        IntVectorPtr pTmpTableGrid = m_aTableGrid.back();
@@ -556,6 +564,13 @@ void DomainMapperTableManager::endOfRowAction()
        m_nCell.push_back(nTmpCell);
        m_aGridBefore.push_back(nTmpGridBefore);
    }
    // save table style in the first row for comparison
    if ( m_nRow == 0 && pTablePropMap->isSet(META_PROP_TABLE_STYLE_NAME) )
    {
        pTablePropMap->getProperty(META_PROP_TABLE_STYLE_NAME)->second >>= sTableStyleName;
        m_aTableStyleNames.pop_back();
        m_aTableStyleNames.push_back( sTableStyleName );
    }

    // Push the tmp position now that we compared it
    m_aTablePositions.pop_back();
@@ -772,7 +787,6 @@ void DomainMapperTableManager::endOfRowAction()
void DomainMapperTableManager::clearData()
{
    m_nRow = m_nHeaderRepeat = m_nTableWidth = m_nLayoutType = 0;
    m_sTableStyleName.clear();
}

}
diff --git a/writerfilter/source/dmapper/DomainMapperTableManager.hxx b/writerfilter/source/dmapper/DomainMapperTableManager.hxx
index 67c4993..edf24a3 100644
--- a/writerfilter/source/dmapper/DomainMapperTableManager.hxx
+++ b/writerfilter/source/dmapper/DomainMapperTableManager.hxx
@@ -46,7 +46,7 @@ class DomainMapperTableManager : public TableManager
    sal_Int32       m_nTableWidth; //might be set directly or has to be calculated from the column positions
    /// Are we in a shape (text append stack is not empty) or in the body document?
    bool m_bIsInShape;
    OUString m_sTableStyleName;
    std::vector< OUString > m_aTableStyleNames;
    /// Grab-bag of table look attributes for preserving.
    comphelper::SequenceAsHashMap m_aTableLook;
    std::vector< TablePositionHandlerPtr > m_aTablePositions;