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

New commits:
commit 821c754593447eb744ada53957e42a728512884d
Author:     Aron Budea <[email protected]>
AuthorDate: Sat Oct 18 00:41:23 2025 +1030
Commit:     Aron Budea <[email protected]>
CommitDate: Sat Oct 18 16:49:28 2025 +0200

    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/+/192642
    Tested-by: Jenkins CollaboraOffice <[email protected]>

diff --git a/sw/qa/extras/ooxmlexport/ooxmlexport14.cxx 
b/sw/qa/extras/ooxmlexport/ooxmlexport14.cxx
index cc6cce590287..7169fab4fe2c 100644
--- a/sw/qa/extras/ooxmlexport/ooxmlexport14.cxx
+++ b/sw/qa/extras/ooxmlexport/ooxmlexport14.cxx
@@ -889,7 +889,18 @@ CPPUNIT_TEST_FIXTURE(Test, testUserField)
     xField->getTextFieldMaster()->setPropertyValue(u"Content"_ustr, 
uno::Any(u"bar"_ustr));
     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(u"Office Open XML Text"_ustr);
@@ -904,8 +915,11 @@ CPPUNIT_TEST_FIXTURE(Test, testUserField)
     // Make sure that not only the variables, but also their values are 
written.
     pXmlDoc = parseExport(u"word/settings.xml"_ustr);
     CPPUNIT_ASSERT(pXmlDoc);
-    assertXPath(pXmlDoc, "//w:docVars/w:docVar", "name", u"foo");
-    assertXPath(pXmlDoc, "//w:docVars/w:docVar", "val", u"bar");
+    assertXPath(pXmlDoc, "//w:docVars/w:docVar[1]", "name", u"foo");
+    assertXPath(pXmlDoc, "//w:docVars/w:docVar[1]", "val", u"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 ffe50e1519f9..5953846cabac 100644
--- a/sw/source/filter/ww8/docxexport.cxx
+++ b/sw/source/filter/ww8/docxexport.cxx
@@ -1032,9 +1032,9 @@ void DocxExport::WriteDocVars(const 
sax_fastparser::FSHelperPtr& pFS)
     static constexpr OUString 
aPrefix(u"com.sun.star.text.fieldmaster.User."_ustr);
     for (const auto& rMasterName : 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