diff --git a/sw/qa/extras/ooxmlexport/ooxmlexport14.cxx b/sw/qa/extras/ooxmlexport/ooxmlexport14.cxx index 3f0b7fb53d54..7bfcb49cd7d3 100644 --- a/sw/qa/extras/ooxmlexport/ooxmlexport14.cxx +++ b/sw/qa/extras/ooxmlexport/ooxmlexport14.cxx @@ -893,7 +893,18 @@ CPPUNIT_TEST_FIXTURE(Test, testUserField) xField->getTextFieldMaster()->setPropertyValue(u"Content"_ustr, uno::Any(u"bar"_ustr)); uno::Reference xDocument(mxComponent, uno::UNO_QUERY); uno::Reference xText = xDocument->getText(); - xText->insertTextContent(xText->createTextCursor(), xField, /*bAbsorb=*/false); + uno::Reference xCursor = xText->createTextCursor(); + xText->insertTextContent(xCursor, xField, /*bAbsorb=*/false); + + // Also add an unnamed field + uno::Reference xUnnamedField( + xFactory->createInstance(u"com.sun.star.text.TextField.User"_ustr), uno::UNO_QUERY); + uno::Reference 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); @@ -908,8 +919,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]")); } diff --git a/sw/source/filter/ww8/docxexport.cxx b/sw/source/filter/ww8/docxexport.cxx index a9427a4c9fd6..14c59e0d5424 100644 --- a/sw/source/filter/ww8/docxexport.cxx +++ b/sw/source/filter/ww8/docxexport.cxx @@ -1033,9 +1033,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; }