sw/qa/extras/ww8export/ww8export3.cxx | 3 ++- sw/source/filter/ww8/ww8par5.cxx | 23 ++++++++++++++++++++--- 2 files changed, 22 insertions(+), 4 deletions(-)
New commits: commit f5c2085e70c40370a790868d4683133a41e6599d Author: Justin Luth <justin_l...@sil.org> AuthorDate: Fri Mar 11 14:55:00 2022 +0200 Commit: Miklos Vajna <vmik...@collabora.com> CommitDate: Thu Mar 24 13:20:38 2022 +0100 tdf#147861 ww8import: solve TODO: not fixed-field if equal Do not mark the field as "fixed" if the displayed string matches the internal variable. This allows changing the variable within LO and having the field update to reflect that change, which is the way that these fields are supposed to work (although in MS Word they only update manually via F9 which is why some needed to be fixed in the first place). Change-Id: Id359cbf0b48e63bddab3e45871326988467d7ddb Reviewed-on: https://gerrit.libreoffice.org/c/core/+/131414 Tested-by: Jenkins Reviewed-by: Justin Luth <jl...@mail.com> Reviewed-by: Miklos Vajna <vmik...@collabora.com> diff --git a/sw/qa/extras/ww8export/ww8export3.cxx b/sw/qa/extras/ww8export/ww8export3.cxx index bce5fdfbe2af..cf26c2d2447f 100644 --- a/sw/qa/extras/ww8export/ww8export3.cxx +++ b/sw/qa/extras/ww8export/ww8export3.cxx @@ -104,7 +104,8 @@ DECLARE_WW8EXPORT_TEST(testTdf147861_customField, "tdf147861_customField.doc") { // These should each be specific values, not a shared DocProperty getParagraph(1, "CustomEditedTitle"); // edited - getParagraph(2, " INSERT Custom Title here"); // matches current DocProperty + // A couple of \x0\x0 at the end of the import variable thwart an equality comparison + CPPUNIT_ASSERT(getParagraph(2)->getString().startsWith(" INSERT Custom Title here")); getParagraph(3, "My Title"); // edited // Verify that these are fields, and not just plain text diff --git a/sw/source/filter/ww8/ww8par5.cxx b/sw/source/filter/ww8/ww8par5.cxx index 66b08888416b..a78fa37f2e45 100644 --- a/sw/source/filter/ww8/ww8par5.cxx +++ b/sw/source/filter/ww8/ww8par5.cxx @@ -1650,13 +1650,30 @@ eF_ResT SwWW8ImplReader::Read_F_DocInfo( WW8FieldDesc* pF, OUString& rStr ) // In other words, Word lets the field to be out of sync with the controlling variable. // Marking as FIXEDFLD solves the automatic replacement problem, but of course prevents // Writer from making any changes, even on an F9 refresh. - // TODO: If the field already matches the DocProperty, no need to mark as fixed. // TODO: Extend LO to allow a linked field that doesn't automatically update. + IDocumentContentOperations& rIDCO(m_rDoc.getIDocumentContentOperations()); const auto pType(static_cast<SwDocInfoFieldType*>( m_rDoc.getIDocumentFieldsAccess().GetSysFieldType(SwFieldIds::DocInfo))); const OUString sDisplayed = GetFieldResult(pF); - SwDocInfoField aField(pType, DI_CUSTOM | DI_SUB_FIXED | nReg, aDocProperty, sDisplayed); - m_rDoc.getIDocumentContentOperations().InsertPoolItem(*m_pPaM, SwFormatField(aField)); + SwDocInfoField aField(pType, DI_CUSTOM | nReg, aDocProperty); + + // If text already matches the DocProperty var, then safe to treat as refreshable field. + OUString sVariable = aField.ExpandField(/*bCache=*/false, nullptr); + if (sDisplayed.getLength() != sVariable.getLength()) + { + sal_Int32 nLen = sVariable.indexOf('\x0'); + if (nLen >= 0) + sVariable = sVariable.copy(0, nLen); + } + if (sDisplayed == sVariable) + rIDCO.InsertPoolItem(*m_pPaM, SwFormatField(aField)); + else + { + // They don't match, so use a fixed field to prevent LO from altering the contents. + SwDocInfoField aFixedField(pType, DI_CUSTOM | DI_SUB_FIXED | nReg, aDocProperty, + sDisplayed); + rIDCO.InsertPoolItem(*m_pPaM, SwFormatField(aFixedField)); + } return eF_ResT::OK; }