sw/qa/extras/ooxmlexport/ooxmlexport17.cxx   |   53 +++++++++++++++++++++++++++
 sw/source/filter/ww8/docxattributeoutput.cxx |   12 ++++++
 2 files changed, 65 insertions(+)

New commits:
commit 116338b8af38a20e21c1248cc077cfe1f0cc07c0
Author:     Miklos Vajna <vmik...@collabora.com>
AuthorDate: Tue May 10 10:41:21 2022 +0200
Commit:     Miklos Vajna <vmik...@collabora.com>
CommitDate: Wed May 11 16:43:58 2022 +0200

    sw content controls, drop-down: add DOCX export
    
    Map the list items to an XML markup like:
    
          <w:sdt>
            <w:sdtPr>
              <w:dropDownList>
                <w:listItem w:displayText="red" w:value="R"/>
                <w:listItem w:displayText="green" w:value="G"/>
                <w:listItem w:displayText="blue" w:value="B"/>
              </w:dropDownList>
            </w:sdtPr>
            ...
          </w:sdt>
    
    (cherry picked from commit 6de2fc2a705d3f356cac898201a98d7db6102d1a)
    
    Conflicts:
            sw/qa/extras/ooxmlexport/ooxmlexport17.cxx
    
    Change-Id: I483009603f1138e4bd5871bfd8c760a3de739ba1
    Reviewed-on: https://gerrit.libreoffice.org/c/core/+/134147
    Tested-by: Jenkins CollaboraOffice <jenkinscollaboraoff...@gmail.com>
    Reviewed-by: Miklos Vajna <vmik...@collabora.com>

diff --git a/sw/qa/extras/ooxmlexport/ooxmlexport17.cxx 
b/sw/qa/extras/ooxmlexport/ooxmlexport17.cxx
index 38a17bca714b..1c4c99597c21 100644
--- a/sw/qa/extras/ooxmlexport/ooxmlexport17.cxx
+++ b/sw/qa/extras/ooxmlexport/ooxmlexport17.cxx
@@ -22,6 +22,7 @@
 #include <comphelper/configuration.hxx>
 #include <comphelper/scopeguard.hxx>
 #include <officecfg/Office/Common.hxx>
+#include <comphelper/propertyvalue.hxx>
 
 #include <swmodeltestbase.hxx>
 #include <unotxdoc.hxx>
@@ -182,6 +183,58 @@ CPPUNIT_TEST_FIXTURE(Test, 
testCheckboxContentControlExport)
     assertXPath(pXmlDoc, "//w:sdt/w:sdtPr/w14:checkbox/w14:uncheckedState", 
"val", "2610");
 }
 
+CPPUNIT_TEST_FIXTURE(Test, testDropdownContentControlExport)
+{
+    // Given a document with a checkbox content control around a text portion:
+    mxComponent = loadFromDesktop("private:factory/swriter");
+    uno::Reference<lang::XMultiServiceFactory> xMSF(mxComponent, 
uno::UNO_QUERY);
+    uno::Reference<text::XTextDocument> xTextDocument(mxComponent, 
uno::UNO_QUERY);
+    uno::Reference<text::XText> xText = xTextDocument->getText();
+    uno::Reference<text::XTextCursor> xCursor = xText->createTextCursor();
+    xText->insertString(xCursor, "choose an item", /*bAbsorb=*/false);
+    xCursor->gotoStart(/*bExpand=*/false);
+    xCursor->gotoEnd(/*bExpand=*/true);
+    uno::Reference<text::XTextContent> xContentControl(
+        xMSF->createInstance("com.sun.star.text.ContentControl"), 
uno::UNO_QUERY);
+    uno::Reference<beans::XPropertySet> xContentControlProps(xContentControl, 
uno::UNO_QUERY);
+    {
+        uno::Sequence<beans::PropertyValues> aListItems = {
+            {
+                comphelper::makePropertyValue("DisplayText", 
uno::Any(OUString("red"))),
+                comphelper::makePropertyValue("Value", 
uno::Any(OUString("R"))),
+            },
+            {
+                comphelper::makePropertyValue("DisplayText", 
uno::Any(OUString("green"))),
+                comphelper::makePropertyValue("Value", 
uno::Any(OUString("G"))),
+            },
+            {
+                comphelper::makePropertyValue("DisplayText", 
uno::Any(OUString("blue"))),
+                comphelper::makePropertyValue("Value", 
uno::Any(OUString("B"))),
+            },
+        };
+        xContentControlProps->setPropertyValue("ListItems", 
uno::Any(aListItems));
+    }
+    xText->insertTextContent(xCursor, xContentControl, /*bAbsorb=*/true);
+
+    // When exporting to DOCX:
+    save("Office Open XML Text", maTempFile);
+    mbExported = true;
+
+    // Then make sure the expected markup is used:
+    xmlDocUniquePtr pXmlDoc = parseExport("word/document.xml");
+    // Without the fix in place, this test would have failed with:
+    // - Expected: 1
+    // - Actual  : 0
+    // - XPath '//w:sdt/w:sdtPr/w:dropDownList/w:listItem[1]' number of nodes 
is incorrect
+    // i.e. the list items were lost on export.
+    assertXPath(pXmlDoc, "//w:sdt/w:sdtPr/w:dropDownList/w:listItem[1]", 
"displayText", "red");
+    assertXPath(pXmlDoc, "//w:sdt/w:sdtPr/w:dropDownList/w:listItem[1]", 
"value", "R");
+    assertXPath(pXmlDoc, "//w:sdt/w:sdtPr/w:dropDownList/w:listItem[2]", 
"displayText", "green");
+    assertXPath(pXmlDoc, "//w:sdt/w:sdtPr/w:dropDownList/w:listItem[2]", 
"value", "G");
+    assertXPath(pXmlDoc, "//w:sdt/w:sdtPr/w:dropDownList/w:listItem[3]", 
"displayText", "blue");
+    assertXPath(pXmlDoc, "//w:sdt/w:sdtPr/w:dropDownList/w:listItem[3]", 
"value", "B");
+}
+
 DECLARE_OOXMLEXPORT_TEST(testTdf137466, "tdf137466.docx")
 {
     xmlDocUniquePtr pXmlDoc = parseExport("word/document.xml");
diff --git a/sw/source/filter/ww8/docxattributeoutput.cxx 
b/sw/source/filter/ww8/docxattributeoutput.cxx
index 2fd7a442c77d..939f3cffbbf2 100644
--- a/sw/source/filter/ww8/docxattributeoutput.cxx
+++ b/sw/source/filter/ww8/docxattributeoutput.cxx
@@ -2363,6 +2363,18 @@ void DocxAttributeOutput::WriteContentControlStart()
         m_pSerializer->endElementNS(XML_w14, XML_checkbox);
     }
 
+    if (m_pContentControl->HasListItems())
+    {
+        m_pSerializer->startElementNS(XML_w, XML_dropDownList);
+        for (const auto& rItem : m_pContentControl->GetListItems())
+        {
+            m_pSerializer->singleElementNS(XML_w, XML_listItem,
+                    FSNS(XML_w, XML_displayText), rItem.m_aDisplayText,
+                    FSNS(XML_w, XML_value), rItem.m_aValue);
+        }
+        m_pSerializer->endElementNS(XML_w, XML_dropDownList);
+    }
+
     m_pSerializer->endElementNS(XML_w, XML_sdtPr);
     m_pSerializer->startElementNS(XML_w, XML_sdtContent);
     m_pContentControl = nullptr;

Reply via email to