From ae40ecb0a78c8a77afce27cd2036ef780acc6e58 Mon Sep 17 00:00:00 2001 From: Miklos Vajna Date: Thu, 22 Sep 2022 08:36:51 +0200 Subject: sw content controls, combo box: add ODT filter Map the ComboBox UNO property to: on export, and do the opposite on import. (cherry picked from commit 21d93d8d2ffd9c5d5cfe9064590b35e0727295c9) Change-Id: I33c162ace15025c8031eb678ba5a43ac085c4b6c Reviewed-on: https://gerrit.libreoffice.org/c/core/+/140430 Tested-by: Jenkins CollaboraOffice Reviewed-by: Miklos Vajna --- .../OpenDocument-v1.3+libreoffice-schema.rng | 6 ++ xmloff/qa/unit/data/content-control-combo-box.fodt | 8 +++ xmloff/qa/unit/text.cxx | 66 ++++++++++++++++++++++ xmloff/source/text/txtparae.cxx | 9 +++ xmloff/source/text/xmlcontentcontrolcontext.cxx | 13 +++++ xmloff/source/text/xmlcontentcontrolcontext.hxx | 1 + 6 files changed, 103 insertions(+) create mode 100644 xmloff/qa/unit/data/content-control-combo-box.fodt diff --git a/schema/libreoffice/OpenDocument-v1.3+libreoffice-schema.rng b/schema/libreoffice/OpenDocument-v1.3+libreoffice-schema.rng index ec496da3e194..039750e5d5a8 100644 --- a/schema/libreoffice/OpenDocument-v1.3+libreoffice-schema.rng +++ b/schema/libreoffice/OpenDocument-v1.3+libreoffice-schema.rng @@ -2922,6 +2922,12 @@ xmlns:loext="urn:org:documentfoundation:names:experimental:office:xmlns:loext:1. + + + + + + diff --git a/xmloff/qa/unit/data/content-control-combo-box.fodt b/xmloff/qa/unit/data/content-control-combo-box.fodt new file mode 100644 index 000000000000..03f6911fb7dd --- /dev/null +++ b/xmloff/qa/unit/data/content-control-combo-box.fodt @@ -0,0 +1,8 @@ + + + + + choose a color + + + diff --git a/xmloff/qa/unit/text.cxx b/xmloff/qa/unit/text.cxx index 94d7da5e30a9..388106702217 100644 --- a/xmloff/qa/unit/text.cxx +++ b/xmloff/qa/unit/text.cxx @@ -795,6 +795,72 @@ CPPUNIT_TEST_FIXTURE(XmloffStyleTest, testPlainTextContentControlImport) CPPUNIT_ASSERT(bPlainText); } +CPPUNIT_TEST_FIXTURE(XmloffStyleTest, testComboBoxContentControlExport) +{ + // Given a document with a combo box content control around a text portion: + getComponent() = loadFromDesktop("private:factory/swriter"); + uno::Reference xMSF(getComponent(), uno::UNO_QUERY); + uno::Reference xTextDocument(getComponent(), uno::UNO_QUERY); + uno::Reference xText = xTextDocument->getText(); + uno::Reference xCursor = xText->createTextCursor(); + xText->insertString(xCursor, "test", /*bAbsorb=*/false); + xCursor->gotoStart(/*bExpand=*/false); + xCursor->gotoEnd(/*bExpand=*/true); + uno::Reference xContentControl( + xMSF->createInstance("com.sun.star.text.ContentControl"), uno::UNO_QUERY); + uno::Reference xContentControlProps(xContentControl, uno::UNO_QUERY); + xContentControlProps->setPropertyValue("ComboBox", uno::Any(true)); + xText->insertTextContent(xCursor, xContentControl, /*bAbsorb=*/true); + + // When exporting to ODT: + uno::Reference xStorable(getComponent(), uno::UNO_QUERY); + uno::Sequence aStoreProps = comphelper::InitPropertySequence({ + { "FilterName", uno::Any(OUString("writer8")) }, + }); + utl::TempFile aTempFile; + aTempFile.EnableKillingFile(); + xStorable->storeToURL(aTempFile.GetURL(), aStoreProps); + validate(aTempFile.GetFileName(), test::ODF); + + // Then make sure the expected markup is used: + std::unique_ptr pStream = parseExportStream(aTempFile, "content.xml"); + xmlDocUniquePtr pXmlDoc = parseXmlStream(pStream.get()); + // Without the accompanying fix in place, this test would have failed with: + // - XPath '//loext:content-control' no attribute 'combobox' exist + // i.e. the combo box content control was turned into a drop-down one on export. + assertXPath(pXmlDoc, "//loext:content-control", "combobox", "true"); +} + +CPPUNIT_TEST_FIXTURE(XmloffStyleTest, testComboBoxContentControlImport) +{ + // Given an ODF document with a plain-text content control: + OUString aURL = m_directories.getURLFromSrc(DATA_DIRECTORY) + "content-control-combo-box.fodt"; + + // When loading that document: + getComponent() = loadFromDesktop(aURL); + + // Then make sure that the content control is not lost on import: + uno::Reference xTextDocument(getComponent(), uno::UNO_QUERY); + uno::Reference xParagraphsAccess(xTextDocument->getText(), + uno::UNO_QUERY); + uno::Reference xParagraphs = xParagraphsAccess->createEnumeration(); + uno::Reference xParagraph(xParagraphs->nextElement(), + uno::UNO_QUERY); + uno::Reference xPortions = xParagraph->createEnumeration(); + uno::Reference xTextPortion(xPortions->nextElement(), uno::UNO_QUERY); + OUString aPortionType; + xTextPortion->getPropertyValue("TextPortionType") >>= aPortionType; + CPPUNIT_ASSERT_EQUAL(OUString("ContentControl"), aPortionType); + uno::Reference xContentControl; + xTextPortion->getPropertyValue("ContentControl") >>= xContentControl; + uno::Reference xContentControlProps(xContentControl, uno::UNO_QUERY); + bool bComboBox{}; + xContentControlProps->getPropertyValue("ComboBox") >>= bComboBox; + // Without the accompanying fix in place, this test would have failed, the import result was a + // drop-down content control (not a combo box one). + CPPUNIT_ASSERT(bComboBox); +} + CPPUNIT_TEST_FIXTURE(XmloffStyleTest, testDropdownContentControlAutostyleExport) { // Given a document with a dropdown content control, and formatting that forms an autostyle in diff --git a/xmloff/source/text/txtparae.cxx b/xmloff/source/text/txtparae.cxx index 9f73f02a2790..97896efdc6ae 100644 --- a/xmloff/source/text/txtparae.cxx +++ b/xmloff/source/text/txtparae.cxx @@ -3955,6 +3955,15 @@ void XMLTextParagraphExport::ExportContentControl( sax::Converter::convertBool(aBuffer, bPlainText); GetExport().AddAttribute(XML_NAMESPACE_LO_EXT, XML_PLAIN_TEXT, aBuffer.makeStringAndClear()); } + + bool bComboBox = false; + xPropertySet->getPropertyValue("ComboBox") >>= bComboBox; + if (bComboBox) + { + OUStringBuffer aBuffer; + sax::Converter::convertBool(aBuffer, bComboBox); + GetExport().AddAttribute(XML_NAMESPACE_LO_EXT, XML_COMBOBOX, aBuffer.makeStringAndClear()); + } } SvXMLElementExport aElem(GetExport(), bExport, XML_NAMESPACE_LO_EXT, XML_CONTENT_CONTROL, false, diff --git a/xmloff/source/text/xmlcontentcontrolcontext.cxx b/xmloff/source/text/xmlcontentcontrolcontext.cxx index bd829af032bf..36580bab3a02 100644 --- a/xmloff/source/text/xmlcontentcontrolcontext.cxx +++ b/xmloff/source/text/xmlcontentcontrolcontext.cxx @@ -125,6 +125,14 @@ void XMLContentControlContext::startFastElement( } break; } + case XML_ELEMENT(LO_EXT, XML_COMBOBOX): + { + if (sax::Converter::convertBool(bTmp, rIter.toView())) + { + m_bComboBox = bTmp; + } + break; + } default: XMLOFF_WARN_UNKNOWN("xmloff", rIter); } @@ -215,6 +223,11 @@ void XMLContentControlContext::endFastElement(sal_Int32) { xPropertySet->setPropertyValue("PlainText", uno::Any(m_bPlainText)); } + + if (m_bComboBox) + { + xPropertySet->setPropertyValue("ComboBox", uno::Any(m_bComboBox)); + } } css::uno::Reference diff --git a/xmloff/source/text/xmlcontentcontrolcontext.hxx b/xmloff/source/text/xmlcontentcontrolcontext.hxx index 59c852419b6c..e507b02e85cd 100644 --- a/xmloff/source/text/xmlcontentcontrolcontext.hxx +++ b/xmloff/source/text/xmlcontentcontrolcontext.hxx @@ -49,6 +49,7 @@ class XMLContentControlContext : public SvXMLImportContext OUString m_aDateLanguage; OUString m_aCurrentDate; bool m_bPlainText = false; + bool m_bComboBox = false; public: XMLContentControlContext(SvXMLImport& rImport, sal_Int32 nElement, XMLHints_Impl& rHints, -- cgit