sw/qa/extras/ooxmlexport/ooxmlexport14.cxx |   20 +++++++++++++++++---
 sw/source/filter/ww8/docxexport.cxx        |    4 ++--
 2 files changed, 19 insertions(+), 5 deletions(-)

New commits:
commit 3717e354ce73bb299395b9a4854f01513aa99945
Author:     Aron Budea <[email protected]>
AuthorDate: Sat Oct 18 00:41:23 2025 +1030
Commit:     Thorsten Behrens <[email protected]>
CommitDate: Wed Nov 12 03:40:49 2025 +0100

    tdf#168921 sw: don't export user fields with empty names to DOCX
    
    Otherwise Word will complain.
    
    Started from ae5f469d3893de73850ccc369dbf426a4acd8f15.
    
    Change-Id: I223bcea33645e02972bc189eef0b913fc19b3b48
    Reviewed-on: https://gerrit.libreoffice.org/c/core/+/192583
    Reviewed-by: Aron Budea <[email protected]>
    Tested-by: Jenkins
    (cherry picked from commit bd01fe073f0ef0eae54f6e9a6d4ecce68171d1c9)
    Reviewed-on: https://gerrit.libreoffice.org/c/core/+/192691
    Reviewed-by: Adolfo Jayme Barrientos <[email protected]>
    (cherry picked from commit 2c0f2c55ab43f39e54e8df9164665dd7544d8658)
    Reviewed-on: https://gerrit.libreoffice.org/c/core/+/193776
    Tested-by: Thorsten Behrens <[email protected]>
    Reviewed-by: Thorsten Behrens <[email protected]>

diff --git a/sw/qa/extras/ooxmlexport/ooxmlexport14.cxx 
b/sw/qa/extras/ooxmlexport/ooxmlexport14.cxx
index 4b0fbdd13716..0f43154eef03 100644
--- a/sw/qa/extras/ooxmlexport/ooxmlexport14.cxx
+++ b/sw/qa/extras/ooxmlexport/ooxmlexport14.cxx
@@ -898,7 +898,18 @@ CPPUNIT_TEST_FIXTURE(Test, testUserField)
     xField->getTextFieldMaster()->setPropertyValue("Content", 
uno::Any(OUString("bar")));
     uno::Reference<text::XTextDocument> xDocument(mxComponent, uno::UNO_QUERY);
     uno::Reference<text::XText> xText = xDocument->getText();
-    xText->insertTextContent(xText->createTextCursor(), xField, 
/*bAbsorb=*/false);
+    uno::Reference<text::XTextCursor> xCursor = xText->createTextCursor();
+    xText->insertTextContent(xCursor, xField, /*bAbsorb=*/false);
+
+    // Also add an unnamed field
+    uno::Reference<text::XDependentTextField> xUnnamedField(
+        xFactory->createInstance(u"com.sun.star.text.TextField.User"_ustr), 
uno::UNO_QUERY);
+    uno::Reference<beans::XPropertySet> xUnnamedMaster(
+        xFactory->createInstance(u"com.sun.star.text.FieldMaster.User"_ustr), 
uno::UNO_QUERY);
+    xUnnamedMaster->setPropertyValue(u"Name"_ustr, uno::Any(u""_ustr));
+    xUnnamedField->attachTextFieldMaster(xUnnamedMaster);
+    xUnnamedField->getTextFieldMaster()->setPropertyValue(u"Content"_ustr, 
uno::Any(u""_ustr));
+    xText->insertTextContent(xCursor, xUnnamedField, /*bAbsorb=*/false);
 
     // Export to docx.
     save("Office Open XML Text");
@@ -913,8 +924,11 @@ CPPUNIT_TEST_FIXTURE(Test, testUserField)
     // Make sure that not only the variables, but also their values are 
written.
     pXmlDoc = parseExport("word/settings.xml");
     CPPUNIT_ASSERT(pXmlDoc);
-    assertXPath(pXmlDoc, "//w:docVars/w:docVar"_ostr, "name"_ostr, "foo");
-    assertXPath(pXmlDoc, "//w:docVars/w:docVar"_ostr, "val"_ostr, "bar");
+    assertXPath(pXmlDoc, "//w:docVars/w:docVar[1]"_ostr, "name", "foo");
+    assertXPath(pXmlDoc, "//w:docVars/w:docVar[1]"_ostr, "val", "bar");
+    // Except the field with empty name, which mustn't get written, Word can't 
import it
+    CPPUNIT_ASSERT_EQUAL_MESSAGE("Variable with empty name shouldn't be 
exported", 0,
+                                 countXPathNodes(pXmlDoc, 
"//w:docVars/w:docVar[2]"));
 }
 
 CPPUNIT_TEST_FIXTURE(Test, testHighlightEdit_numbering)
diff --git a/sw/source/filter/ww8/docxexport.cxx 
b/sw/source/filter/ww8/docxexport.cxx
index e7c1972ce9c0..ee7ee12ddab8 100644
--- a/sw/source/filter/ww8/docxexport.cxx
+++ b/sw/source/filter/ww8/docxexport.cxx
@@ -971,9 +971,9 @@ void DocxExport::WriteDocVars(const 
sax_fastparser::FSHelperPtr& pFS)
     constexpr OUString aPrefix(u"com.sun.star.text.fieldmaster.User."_ustr);
     for (const auto& rMasterName : std::as_const(aMasterNames))
     {
-        if (!rMasterName.startsWith(aPrefix))
+        if (!rMasterName.startsWith(aPrefix) || rMasterName.getLength() == 
aPrefix.getLength())
         {
-            // Not a user field.
+            // Not a user field or user field has empty name.
             continue;
         }
 

Reply via email to