tdf#131722 DOCX import: fix lost first character in date selector

Fix another date selector control bug, date picker first character
goes out of the control. The first character of the date separator
in the first row of the table is out of control. The fix takes into
account the placement of the dummy paragraph in the first row of
the table, which caused the date separator to be corrupted.

Thanks to Gabor Kelemen for pointing out this error to me.

Follow-up to commit e898f95bfab16ddd9b04e516293cb6eb7e0a3847
"tdf#138093 DOCX import: fix broken date selector control in table".

Change-Id: I91d272b786a3d3dc047334c2a4a039f987c94ce0
Reviewed-on: https://gerrit.libreoffice.org/c/core/+/151087
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/tdf131722.docx b/sw/qa/extras/ooxmlexport/data/tdf131722.docx
new file mode 100644
index 0000000..8b72ad4
--- /dev/null
+++ b/sw/qa/extras/ooxmlexport/data/tdf131722.docx
Binary files differ
diff --git a/sw/qa/extras/ooxmlexport/ooxmlexport19.cxx b/sw/qa/extras/ooxmlexport/ooxmlexport19.cxx
index 729fd09..059d652 100644
--- a/sw/qa/extras/ooxmlexport/ooxmlexport19.cxx
+++ b/sw/qa/extras/ooxmlexport/ooxmlexport19.cxx
@@ -949,6 +949,63 @@ DECLARE_OOXMLEXPORT_TEST(testTdf138093, "tdf138093.docx")
    }
}

DECLARE_OOXMLEXPORT_TEST(testTdf131722, "tdf131722.docx")
{
    if (isExported())
    {
        xmlDocUniquePtr pXmlDoc = parseExport("word/document.xml");
        assertXPath(pXmlDoc, "//w:sdt", 4);
        uno::Reference<text::XTextTablesSupplier> xTablesSupplier(mxComponent, uno::UNO_QUERY);
        uno::Reference<container::XIndexAccess> xTables(xTablesSupplier->getTextTables(),
                                                        uno::UNO_QUERY);
        uno::Reference<text::XTextTable> xTable(xTables->getByIndex(0), uno::UNO_QUERY);
        uno::Reference<table::XCell> xCell = xTable->getCellByName("A1");
        uno::Reference<container::XEnumerationAccess> xParagraphsAccess(xCell, uno::UNO_QUERY);
        uno::Reference<container::XEnumeration> xParagraphs
            = xParagraphsAccess->createEnumeration();
        uno::Reference<container::XEnumerationAccess> xParagraph(xParagraphs->nextElement(),
                                                                 uno::UNO_QUERY);
        uno::Reference<container::XEnumeration> xPortions = xParagraph->createEnumeration();
        uno::Reference<beans::XPropertySet> xTextPortion(xPortions->nextElement(), uno::UNO_QUERY);

        OUString aPortionType;
        xTextPortion->getPropertyValue("TextPortionType") >>= aPortionType;
        CPPUNIT_ASSERT_EQUAL(OUString("ContentControl"), aPortionType);

        uno::Reference<text::XTextContent> xContentControl;
        xTextPortion->getPropertyValue("ContentControl") >>= xContentControl;
        uno::Reference<beans::XPropertySet> xContentControlProps(xContentControl, uno::UNO_QUERY);
        bool bDate{};
        xContentControlProps->getPropertyValue("Date") >>= bDate;
        CPPUNIT_ASSERT(bDate);
        uno::Reference<container::XEnumerationAccess> xContentControlEnumAccess(xContentControl,
                                                                                uno::UNO_QUERY);
        uno::Reference<container::XEnumeration> xContentControlEnum
            = xContentControlEnumAccess->createEnumeration();
        uno::Reference<text::XTextRange> xTextPortionRange(xContentControlEnum->nextElement(),
                                                           uno::UNO_QUERY);
        CPPUNIT_ASSERT_EQUAL(OUString("Enter a date here!"), xTextPortionRange->getString());
    }
    else
    {
        SwXTextDocument* pTextDoc = dynamic_cast<SwXTextDocument*>(mxComponent.get());
        CPPUNIT_ASSERT(pTextDoc);
        SwDoc* pDoc = pTextDoc->GetDocShell()->GetDoc();
        IDocumentMarkAccess* pMarkAccess = pDoc->getIDocumentMarkAccess();
        CPPUNIT_ASSERT_EQUAL(sal_Int32(2), pMarkAccess->getFieldmarksCount());

        for (auto aIter = pMarkAccess->getFieldmarksBegin();
             aIter != pMarkAccess->getFieldmarksEnd(); ++aIter)
        {
            ::sw::mark::IDateFieldmark* pFieldmark
                = dynamic_cast<::sw::mark::IDateFieldmark*>(*aIter);
            CPPUNIT_ASSERT(pFieldmark);
            CPPUNIT_ASSERT_EQUAL(OUString(ODF_FORMDATE), pFieldmark->GetFieldname());
            CPPUNIT_ASSERT_EQUAL(OUString("Enter a date here!"), pFieldmark->GetContent());
        }
    }
}

CPPUNIT_PLUGIN_IMPLEMENT();

/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
diff --git a/writerfilter/source/dmapper/SdtHelper.cxx b/writerfilter/source/dmapper/SdtHelper.cxx
index f9cedcb..4f94c46 100644
--- a/writerfilter/source/dmapper/SdtHelper.cxx
+++ b/writerfilter/source/dmapper/SdtHelper.cxx
@@ -382,9 +382,10 @@ void SdtHelper::createDateContentControl()
    {
        xCrsr->gotoRange(m_xDateFieldStartRange, false);
        // tdf#138093: Date selector reset, if placed inside table
        // Modified to XOR relationship
        // Modified to XOR relationship and adding dummy paragraph conditions
        bool bIsInTable = (m_rDM_Impl.hasTableManager() && m_rDM_Impl.getTableManager().isInTable())
                          != (m_rDM_Impl.m_nTableDepth > 0);
                              != (m_rDM_Impl.m_nTableDepth > 0)
                          && m_rDM_Impl.GetIsDummyParaAddedForTableInSection();
        if (bIsInTable)
            xCrsr->goRight(1, false);
        xCrsr->gotoEnd(true);