sw/inc/reffld.hxx                            |    1 
 sw/source/core/doc/DocumentFieldsManager.cxx |    5 ++++
 sw/source/core/fields/reffld.cxx             |   29 ++++++++++++++++++++++++++-
 3 files changed, 34 insertions(+), 1 deletion(-)

New commits:
commit 15972993ff6e106a02954125269612179e1f33aa
Author:     Skyler Grey <skyler.g...@collabora.com>
AuthorDate: Mon Oct 23 16:17:17 2023 +0000
Commit:     Caolán McNamara <caolan.mcnam...@collabora.com>
CommitDate: Mon Oct 30 20:08:56 2023 +0100

    Fix incorrect marginal STYLEREF content in docx
    
    STYLEREF fields were previously not importing with the correct content
    when loading a docx document. This is because they were not updating
    when the document had finished loading- only partway through, and where
    a STYLEREF field is in relation to everything else in the document can
    change its content.
    
    This commit fixes that issue by adding STYLEREF fields to be refreshed
    whenever other fields that can change based on pages (e.g. page number)
    fields are updated. I suspect this could lead to double updates in some
    cases where both reference and page fields are being updated. I consider
    this a relatively minor issue in comparison to incorrect field content
    when specific documents are loaded, but a followup could be made
    improving this.
    
    This commit also fixes a minor typo in reffld.cxx where m_sText is
    always the filtered text when updating fields, even if we are updating
    m_sTextRLHidden instead.
    
    Change-Id: Iecd3e83a6bd3f8c2c6adba5c7eba9ee55b773510
    Reviewed-on: https://gerrit.libreoffice.org/c/core/+/158450
    Tested-by: Jenkins
    Reviewed-by: Caolán McNamara <caolan.mcnam...@collabora.com>

diff --git a/sw/inc/reffld.hxx b/sw/inc/reffld.hxx
index 293b913c406b..3c1e3c63b5e6 100644
--- a/sw/inc/reffld.hxx
+++ b/sw/inc/reffld.hxx
@@ -100,6 +100,7 @@ public:
                                         SwRootFrame const* pLayout = nullptr,
                                         SwTextNode* pSelf = nullptr, SwFrame* 
pFrame = nullptr);
     void UpdateGetReferences();
+    void UpdateStyleReferences();
 };
 
 class SW_DLLPUBLIC SwGetRefField final : public SwField
diff --git a/sw/source/core/doc/DocumentFieldsManager.cxx 
b/sw/source/core/doc/DocumentFieldsManager.cxx
index c8703e7f06ef..a7be3bb96394 100644
--- a/sw/source/core/doc/DocumentFieldsManager.cxx
+++ b/sw/source/core/doc/DocumentFieldsManager.cxx
@@ -1292,6 +1292,11 @@ void DocumentFieldsManager::UpdatePageFields(const 
SwTwips nDocPos)
         case SwFieldIds::DocStat:
             pFieldType->CallSwClientNotify(sw::LegacyModifyHint(nullptr, 
nullptr));
             break;
+        case SwFieldIds::GetRef:
+            
static_cast<SwGetRefFieldType*>(pFieldType)->UpdateStyleReferences();
+            // Style references can vary across different pages (e.g. in 
header/footer)
+            // so they must be updated when page fields are
+            break;
         default: break;
         }
     }
diff --git a/sw/source/core/fields/reffld.cxx b/sw/source/core/fields/reffld.cxx
index 638baf0a5474..ee7791a68753 100644
--- a/sw/source/core/fields/reffld.cxx
+++ b/sw/source/core/fields/reffld.cxx
@@ -706,7 +706,7 @@ void SwGetRefField::UpdateField(const SwTextField* 
pFieldTextAttr, SwFrame* pFra
                         rText = pTextNd->GetExpandText(pLayout, nStart, nEnd - 
nStart, false, false,
                                                        false, ExpandMode(0));
                 }
-                FilterText(m_sText, GetLanguage(), m_sSetReferenceLanguage);
+                FilterText(rText, GetLanguage(), m_sSetReferenceLanguage);
             }
         }
         break;
@@ -1167,6 +1167,33 @@ void SwGetRefFieldType::UpdateGetReferences()
     CallSwClientNotify(sw::LegacyModifyHint(nullptr, nullptr));
 }
 
+void SwGetRefFieldType::UpdateStyleReferences()
+{
+    std::vector<SwFormatField*> vFields;
+    GatherFields(vFields, false);
+    bool bModified = false;
+    for(auto pFormatField: vFields)
+    {
+        // update only the GetRef fields which are also STYLEREF fields
+        SwGetRefField* pGRef = 
static_cast<SwGetRefField*>(pFormatField->GetField());
+
+        if (pGRef->GetSubType() != REF_STYLE) continue;
+
+        const SwTextField* pTField;
+        if(!pGRef->GetLanguage() &&
+            nullptr != (pTField = pFormatField->GetTextField()) &&
+            pTField->GetpTextNode())
+        {
+            
pGRef->SetLanguage(pTField->GetpTextNode()->GetLang(pTField->GetStart()));
+        }
+
+        pGRef->UpdateField(pFormatField->GetTextField(), nullptr);
+        bModified = true;
+    }
+    if (bModified)
+        CallSwClientNotify(sw::LegacyModifyHint(nullptr, nullptr));
+}
+
 void SwGetRefFieldType::SwClientNotify(const SwModify&, const SfxHint& rHint)
 {
     if (rHint.GetId() != SfxHintId::SwLegacyModify)

Reply via email to