sw/qa/extras/ooxmlexport/ooxmlexport17.cxx | 53 +++++++++++++++++++++++++++ sw/source/filter/ww8/docxattributeoutput.cxx | 12 ++++++ 2 files changed, 65 insertions(+)
New commits: commit 6de2fc2a705d3f356cac898201a98d7db6102d1a Author: Miklos Vajna <vmik...@collabora.com> AuthorDate: Tue May 10 10:41:21 2022 +0200 Commit: Miklos Vajna <vmik...@collabora.com> CommitDate: Tue May 10 13:18:51 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> Change-Id: I483009603f1138e4bd5871bfd8c760a3de739ba1 Reviewed-on: https://gerrit.libreoffice.org/c/core/+/134104 Reviewed-by: Miklos Vajna <vmik...@collabora.com> Tested-by: Jenkins diff --git a/sw/qa/extras/ooxmlexport/ooxmlexport17.cxx b/sw/qa/extras/ooxmlexport/ooxmlexport17.cxx index c7bdf83550f6..5c911887c29d 100644 --- a/sw/qa/extras/ooxmlexport/ooxmlexport17.cxx +++ b/sw/qa/extras/ooxmlexport/ooxmlexport17.cxx @@ -19,6 +19,7 @@ #include <comphelper/scopeguard.hxx> #include <officecfg/Office/Common.hxx> #include <o3tl/string_view.hxx> +#include <comphelper/propertyvalue.hxx> #include <queue> #include <swmodeltestbase.hxx> @@ -279,6 +280,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"); +} + CPPUNIT_TEST_FIXTURE(Test, testTdf148494) { loadAndSave("tdf148494.docx"); diff --git a/sw/source/filter/ww8/docxattributeoutput.cxx b/sw/source/filter/ww8/docxattributeoutput.cxx index 0e5e3607c7cd..6f111b645c2d 100644 --- a/sw/source/filter/ww8/docxattributeoutput.cxx +++ b/sw/source/filter/ww8/docxattributeoutput.cxx @@ -2355,6 +2355,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;