include/xmloff/xmltoken.hxx                                 |    1 
 schema/libreoffice/OpenDocument-v1.3+libreoffice-schema.rng |    6 +
 sw/source/core/txtnode/thints.cxx                           |    1 
 xmloff/qa/unit/data/content-control-plain-text.fodt         |    8 +
 xmloff/qa/unit/text.cxx                                     |   66 ++++++++++++
 xmloff/source/core/xmltoken.cxx                             |    1 
 xmloff/source/text/txtparae.cxx                             |    9 +
 xmloff/source/text/xmlcontentcontrolcontext.cxx             |   13 ++
 xmloff/source/text/xmlcontentcontrolcontext.hxx             |    1 
 xmloff/source/token/tokens.txt                              |    1 
 10 files changed, 107 insertions(+)

New commits:
commit 40d39d8a30b60bd7b5775619bc92f17805d7c842
Author:     Miklos Vajna <vmik...@collabora.com>
AuthorDate: Fri Jul 22 11:50:23 2022 +0200
Commit:     Miklos Vajna <vmik...@collabora.com>
CommitDate: Wed Jul 27 08:33:36 2022 +0200

    sw: fix heap-use-after-free in SwTextNode::InsertHint()
    
    This is a problem since commit 1dce9ee7e12871ee63434499db805e806b9e9d3c
    (sw content controls, plain text: apply formatting to the entire
    contents, 2022-07-21), because I forgot to check if pAttr is still a
    valid pointer after the input field code is executed.
    
    Below code already uses this flag to make sure it's not accessing a
    dangling pointer, apply the same fix here as well.
    
    (cherry picked from commit 5d68b3878fed165bf1bdc6aa47be1df4a8265b96)
    
    Change-Id: Ifcba0bc5e3a3c0abd81ff954fb10f6880163461b
    Reviewed-on: https://gerrit.libreoffice.org/c/core/+/137473
    Tested-by: Jenkins CollaboraOffice <jenkinscollaboraoff...@gmail.com>
    Reviewed-by: Miklos Vajna <vmik...@collabora.com>

diff --git a/sw/source/core/txtnode/thints.cxx 
b/sw/source/core/txtnode/thints.cxx
index 63897088ab3c..7bb9c3a03798 100644
--- a/sw/source/core/txtnode/thints.cxx
+++ b/sw/source/core/txtnode/thints.cxx
@@ -1687,6 +1687,7 @@ bool SwTextNode::InsertHint( SwTextAttr * const pAttr, 
const SetAttrMode nMode )
         }
     }
 
+    if (bInsertHint)
     {
         // Handle the invariant that a plain text content control has the same 
character formatting
         // for all of its content.
commit 5c30dc68c7285c5386f24690981d6dd9ad952011
Author:     Miklos Vajna <vmik...@collabora.com>
AuthorDate: Fri Jul 22 08:35:10 2022 +0200
Commit:     Miklos Vajna <vmik...@collabora.com>
CommitDate: Wed Jul 27 08:33:24 2022 +0200

    sw content controls, date: add ODT filter
    
    Map the PlainText UNO property to:
    
            <loext:content-control loext:plain-text="...">
    
    on export, and do the opposite on import.
    
    (cherry picked from commit 33cf26c8b6dc6cf38edf2d22cfefbd00904d9da8)
    
    Conflicts:
            xmloff/source/token/tokens.txt
    
    Change-Id: Icec0c35b2b9fca53104e6526c98083db52df5d42
    Reviewed-on: https://gerrit.libreoffice.org/c/core/+/137472
    Tested-by: Miklos Vajna <vmik...@collabora.com>
    Reviewed-by: Miklos Vajna <vmik...@collabora.com>

diff --git a/include/xmloff/xmltoken.hxx b/include/xmloff/xmltoken.hxx
index 1b5a8eb0fd54..06ea2e2fec27 100644
--- a/include/xmloff/xmltoken.hxx
+++ b/include/xmloff/xmltoken.hxx
@@ -3495,6 +3495,7 @@ namespace xmloff::token {
         XML_PICTURE,
         XML_DATE_FORMAT,
         XML_DATE_RFC_LANGUAGE_TAG,
+        XML_PLAIN_TEXT,
 
         XML_TOKEN_END
     };
diff --git a/schema/libreoffice/OpenDocument-v1.3+libreoffice-schema.rng 
b/schema/libreoffice/OpenDocument-v1.3+libreoffice-schema.rng
index d78c60fbddd1..b2d826bae695 100644
--- a/schema/libreoffice/OpenDocument-v1.3+libreoffice-schema.rng
+++ b/schema/libreoffice/OpenDocument-v1.3+libreoffice-schema.rng
@@ -2853,6 +2853,12 @@ 
xmlns:loext="urn:org:documentfoundation:names:experimental:office:xmlns:loext:1.
           <rng:ref name="string"/>
         </rng:attribute>
       </rng:optional>
+      <rng:optional>
+        <!-- default value: false -->
+        <rng:attribute name="loext:plain-text">
+          <rng:ref name="boolean"/>
+        </rng:attribute>
+      </rng:optional>
       <rng:zeroOrMore>
         <rng:element name="loext:list-item">
           <rng:attribute name="loext:display-text">
diff --git a/xmloff/qa/unit/data/content-control-plain-text.fodt 
b/xmloff/qa/unit/data/content-control-plain-text.fodt
new file mode 100644
index 000000000000..03dbdc8a7a8e
--- /dev/null
+++ b/xmloff/qa/unit/data/content-control-plain-text.fodt
@@ -0,0 +1,8 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<office:document xmlns:text="urn:oasis:names:tc:opendocument:xmlns:text:1.0" 
xmlns:office="urn:oasis:names:tc:opendocument:xmlns:office:1.0" 
xmlns:loext="urn:org:documentfoundation:names:experimental:office:xmlns:loext:1.0"
 office:version="1.3" office:mimetype="application/vnd.oasis.opendocument.text">
+  <office:body>
+    <office:text>
+      <text:p><loext:content-control 
loext:plain-text="true">test</loext:content-control></text:p>
+    </office:text>
+  </office:body>
+</office:document>
diff --git a/xmloff/qa/unit/text.cxx b/xmloff/qa/unit/text.cxx
index 9ac816298b60..99bb0ce3ba91 100644
--- a/xmloff/qa/unit/text.cxx
+++ b/xmloff/qa/unit/text.cxx
@@ -729,6 +729,72 @@ CPPUNIT_TEST_FIXTURE(XmloffStyleTest, 
testDateContentControlImport)
     CPPUNIT_ASSERT_EQUAL(OUString("2022-05-25T00:00:00Z"), aCurrentDate);
 }
 
+CPPUNIT_TEST_FIXTURE(XmloffStyleTest, testPlainTextContentControlExport)
+{
+    // Given a document with a plain text content control around a text 
portion:
+    getComponent() = loadFromDesktop("private:factory/swriter");
+    uno::Reference<lang::XMultiServiceFactory> xMSF(getComponent(), 
uno::UNO_QUERY);
+    uno::Reference<text::XTextDocument> xTextDocument(getComponent(), 
uno::UNO_QUERY);
+    uno::Reference<text::XText> xText = xTextDocument->getText();
+    uno::Reference<text::XTextCursor> xCursor = xText->createTextCursor();
+    xText->insertString(xCursor, "test", /*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);
+    xContentControlProps->setPropertyValue("PlainText", uno::Any(true));
+    xText->insertTextContent(xCursor, xContentControl, /*bAbsorb=*/true);
+
+    // When exporting to ODT:
+    uno::Reference<frame::XStorable> xStorable(getComponent(), uno::UNO_QUERY);
+    uno::Sequence<beans::PropertyValue> 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<SvStream> 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 'plain-text' exist
+    // i.e. the plain text content control was turned into a rich text one on 
export.
+    assertXPath(pXmlDoc, "//loext:content-control", "plain-text", "true");
+}
+
+CPPUNIT_TEST_FIXTURE(XmloffStyleTest, testPlainTextContentControlImport)
+{
+    // Given an ODF document with a plain-text content control:
+    OUString aURL = m_directories.getURLFromSrc(DATA_DIRECTORY) + 
"content-control-plain-text.fodt";
+
+    // When loading that document:
+    getComponent() = loadFromDesktop(aURL);
+
+    // Then make sure that the content control is not lost on import:
+    uno::Reference<text::XTextDocument> xTextDocument(getComponent(), 
uno::UNO_QUERY);
+    uno::Reference<container::XEnumerationAccess> 
xParagraphsAccess(xTextDocument->getText(),
+                                                                    
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 bPlainText{};
+    xContentControlProps->getPropertyValue("PlainText") >>= bPlainText;
+    // Without the accompanying fix in place, this test would have failed, the 
import result was a
+    // rich text content control (not a plain text one).
+    CPPUNIT_ASSERT(bPlainText);
+}
+
 CPPUNIT_PLUGIN_IMPLEMENT();
 
 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
diff --git a/xmloff/source/core/xmltoken.cxx b/xmloff/source/core/xmltoken.cxx
index 10727ffb2928..ef29b31ce04d 100644
--- a/xmloff/source/core/xmltoken.cxx
+++ b/xmloff/source/core/xmltoken.cxx
@@ -3498,6 +3498,7 @@ namespace xmloff::token {
         TOKEN("picture", XML_PICTURE),
         TOKEN("date-format", XML_DATE_FORMAT),
         TOKEN("date-rfc-language-tag", XML_DATE_RFC_LANGUAGE_TAG),
+        TOKEN("plain-text", XML_PLAIN_TEXT),
 
 
 #if OSL_DEBUG_LEVEL > 0
diff --git a/xmloff/source/text/txtparae.cxx b/xmloff/source/text/txtparae.cxx
index 4105772445b2..09dde3a8127a 100644
--- a/xmloff/source/text/txtparae.cxx
+++ b/xmloff/source/text/txtparae.cxx
@@ -3946,6 +3946,15 @@ void XMLTextParagraphExport::ExportContentControl(
         {
             GetExport().AddAttribute(XML_NAMESPACE_LO_EXT, XML_CURRENT_DATE, 
aCurrentDate);
         }
+
+        bool bPlainText = false;
+        xPropertySet->getPropertyValue("PlainText") >>= bPlainText;
+        if (bPlainText)
+        {
+            OUStringBuffer aBuffer;
+            sax::Converter::convertBool(aBuffer, bPlainText);
+            GetExport().AddAttribute(XML_NAMESPACE_LO_EXT, XML_PLAIN_TEXT, 
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 22af458e43bc..bd829af032bf 100644
--- a/xmloff/source/text/xmlcontentcontrolcontext.cxx
+++ b/xmloff/source/text/xmlcontentcontrolcontext.cxx
@@ -117,6 +117,14 @@ void XMLContentControlContext::startFastElement(
                 m_aCurrentDate = rIter.toString();
                 break;
             }
+            case XML_ELEMENT(LO_EXT, XML_PLAIN_TEXT):
+            {
+                if (sax::Converter::convertBool(bTmp, rIter.toView()))
+                {
+                    m_bPlainText = bTmp;
+                }
+                break;
+            }
             default:
                 XMLOFF_WARN_UNKNOWN("xmloff", rIter);
         }
@@ -202,6 +210,11 @@ void XMLContentControlContext::endFastElement(sal_Int32)
     {
         xPropertySet->setPropertyValue("CurrentDate", 
uno::Any(m_aCurrentDate));
     }
+
+    if (m_bPlainText)
+    {
+        xPropertySet->setPropertyValue("PlainText", uno::Any(m_bPlainText));
+    }
 }
 
 css::uno::Reference<css::xml::sax::XFastContextHandler>
diff --git a/xmloff/source/text/xmlcontentcontrolcontext.hxx 
b/xmloff/source/text/xmlcontentcontrolcontext.hxx
index 2c3ecfb9cafb..59c852419b6c 100644
--- a/xmloff/source/text/xmlcontentcontrolcontext.hxx
+++ b/xmloff/source/text/xmlcontentcontrolcontext.hxx
@@ -48,6 +48,7 @@ class XMLContentControlContext : public SvXMLImportContext
     OUString m_aDateFormat;
     OUString m_aDateLanguage;
     OUString m_aCurrentDate;
+    bool m_bPlainText = false;
 
 public:
     XMLContentControlContext(SvXMLImport& rImport, sal_Int32 nElement, 
XMLHints_Impl& rHints,
diff --git a/xmloff/source/token/tokens.txt b/xmloff/source/token/tokens.txt
index 44d71eb8d9fe..a2c63cdb737a 100644
--- a/xmloff/source/token/tokens.txt
+++ b/xmloff/source/token/tokens.txt
@@ -3241,4 +3241,5 @@ display-text
 picture
 date-format
 date-rfc-language-tag
+plain-text
 TOKEN_END_DUMMY

Reply via email to