sw/source/core/txtnode/ndtxt.cxx |   28 ++++++++++++++++++----------
 1 file changed, 18 insertions(+), 10 deletions(-)

New commits:
commit 0c3b89da7cde9866bf3174a6276736c76efb8356
Author:     Matt K <matt...@gmail.com>
AuthorDate: Thu Aug 3 00:06:24 2023 -0500
Commit:     Mike Kaganski <mike.kagan...@collabora.com>
CommitDate: Fri Nov 10 19:05:13 2023 +0100

    tdf#62603 Fix find/replace to not extend font attributes
    
    This change modifies the logic of the core replace code
    to now not replace the 1st character and extend its
    attributes, but instead to actually replace characters
    to keep respective formatting.  Additional checks
    are made for whether the replacement was shorter or
    longer than the found text, and to handle trimming
    or appending the final portions as needed.
    
    Change-Id: I03a5645898e55ad386bacc2766af9b244a97bd21
    Reviewed-on: https://gerrit.libreoffice.org/c/core/+/155274
    Tested-by: Jenkins
    Reviewed-by: Mike Kaganski <mike.kagan...@collabora.com>

diff --git a/sw/source/core/txtnode/ndtxt.cxx b/sw/source/core/txtnode/ndtxt.cxx
index af9b5f72fc92..541e3fd5049f 100644
--- a/sw/source/core/txtnode/ndtxt.cxx
+++ b/sw/source/core/txtnode/ndtxt.cxx
@@ -3849,19 +3849,27 @@ void SwTextNode::ReplaceText( const SwContentIndex& 
rStart, const sal_Int32 nDel
     bool bOldExpFlg = IsIgnoreDontExpand();
     SetIgnoreDontExpand( true );
 
-    if (nLen && sInserted.getLength())
+    const sal_Int32 nInsLen = sInserted.getLength();
+    if (nLen && nInsLen)
     {
-        // Replace the 1st char, then delete the rest and insert.
-        // This way the attributes of the 1st char are expanded!
-        m_Text = m_Text.replaceAt(nStartPos, 1, sInserted.subView(0, 1));
+        m_Text = m_Text.replaceAt(nStartPos, nLen, sInserted);
 
-        ++const_cast<SwContentIndex&>(rStart);
-        m_Text = m_Text.replaceAt(rStart.GetIndex(), nLen - 1, u"");
-        Update(rStart, nLen - 1, UpdateMode::Negative);
+        if (nLen > nInsLen) // short insert
+        {
+            // delete up to the point that the user specified
+            const SwContentIndex aNegIdx(rStart, nInsLen);
+            Update(aNegIdx, nLen - nInsLen, UpdateMode::Negative);
+        }
+        else if (nLen < nInsLen) // long insert
+        {
+            const SwContentIndex aIdx(rStart, nLen);
+            Update(aIdx, nInsLen - nLen, UpdateMode::Replace);
+        }
 
-        std::u16string_view aTmpText( sInserted.subView(1) );
-        m_Text = m_Text.replaceAt(rStart.GetIndex(), 0, aTmpText);
-        Update(rStart, aTmpText.size(), UpdateMode::Replace);
+        for (sal_Int32 i = 0; i < nInsLen; i++)
+        {
+            ++const_cast<SwContentIndex&>(rStart);
+        }
     }
     else
     {

Reply via email to