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>
Signed-off-by: Xisco Fauli <xiscofauli@libreoffice.org>
Reviewed-on: https://gerrit.libreoffice.org/c/core/+/151164
Tested-by: Jenkins
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/ooxmlexport17.cxx b/sw/qa/extras/ooxmlexport/ooxmlexport17.cxx
index 2f412a7d..9bde036 100644
--- a/sw/qa/extras/ooxmlexport/ooxmlexport17.cxx
+++ b/sw/qa/extras/ooxmlexport/ooxmlexport17.cxx
@@ -1202,6 +1202,62 @@ DECLARE_OOXMLEXPORT_TEST(testTdf148273_sectionBulletFormatLeak, "tdf148273_secti
    CPPUNIT_ASSERT_EQUAL(false, aValue.hasValue());
}

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();

        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());
        }
    }
}

DECLARE_OOXMLEXPORT_TEST(testTdf149089, "tdf149089.docx")
{
    uno::Reference<container::XNameAccess> xPageStyles = getStyles("PageStyles");
diff --git a/writerfilter/source/dmapper/SdtHelper.cxx b/writerfilter/source/dmapper/SdtHelper.cxx
index f71af13..37a4f33 100644
--- a/writerfilter/source/dmapper/SdtHelper.cxx
+++ b/writerfilter/source/dmapper/SdtHelper.cxx
@@ -381,9 +381,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);