tdf#168921 sw: don't export user fields with empty names to DOCX

Otherwise Word will complain.

Started from ae5f469d38.

Change-Id: I223bcea33645e02972bc189eef0b913fc19b3b48
Reviewed-on: https://gerrit.libreoffice.org/c/core/+/192583
Reviewed-by: Aron Budea <aron.budea@collabora.com>
Tested-by: Jenkins
This commit is contained in:
Aron Budea
2025-10-18 13:17:56 +02:00
parent f5b8ec6b3f
commit bd01fe073f
2 changed files with 19 additions and 5 deletions
+17 -3
View File
@@ -893,7 +893,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);
@@ -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]"));
}
+2 -2
View File
@@ -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;
}