sw/source/core/crsr/bookmrk.cxx  |   27 +++++++++++++++++++--------
 sw/source/core/txtnode/ndtxt.cxx |    7 +++++--
 2 files changed, 24 insertions(+), 10 deletions(-)

New commits:
commit a80aef24826a7c3787fe6f66ea77cbf9699d6060
Author:     Michael Stahl <michael.st...@cib.de>
AuthorDate: Tue Nov 12 14:25:15 2019 +0100
Commit:     Thorsten Behrens <thorsten.behr...@cib.de>
CommitDate: Wed Nov 13 01:25:26 2019 +0100

    sw: avoid partial expansion of bookmark over new fieldmark
    
    The comment that was added with commit
    71f13a801608538f56a0cb2bce07a03faa5856f6 "#i29942# SwTxtNode::Update for
    bookmarks fixed. Bookmarks no longer grow on either side."
    suggests not to expand bookmarks in any case but in case the start and
    end position was the same, the start position was still moved so the
    mark expanded anyway.
    
    Unfortunately meanwhile commit c91024891ff10c2ae01e11a28a9aecca2f36b6c3
    "tdf#96479 workaround bookmark end pos handling when inserting text
    into a text node." added a requirement that bookmarks with start==end
    *are* expanded.
    
    For the case where a new field is inserted, shortcut the insertion
    to do only one InsertString call so that any expansion of bookmarks will
    expand it over the entire field, not just its CH_TXT_ATR_FIELDSTART.
    
    Change-Id: If2608c9cf34a217330156e7ea566dcf528890c45
    Reviewed-on: https://gerrit.libreoffice.org/82521
    Tested-by: Jenkins
    Reviewed-by: Thorsten Behrens <thorsten.behr...@cib.de>

diff --git a/sw/source/core/crsr/bookmrk.cxx b/sw/source/core/crsr/bookmrk.cxx
index 7e514b7e18da..a16713dc295d 100644
--- a/sw/source/core/crsr/bookmrk.cxx
+++ b/sw/source/core/crsr/bookmrk.cxx
@@ -158,24 +158,35 @@ namespace
         SwPosition const*const pSepPos)
     {
         io_pDoc->GetIDocumentUndoRedo().StartUndo(SwUndoId::UI_REPLACE, 
nullptr);
+        OUString startChar(aStartMark);
+        if (aEndMark != CH_TXT_ATR_FORMELEMENT
+            && pField->GetMarkStart() == pField->GetMarkEnd())
+        {
+            // do only 1 InsertString call - to expand existing bookmarks at 
the
+            // position over the whole field instead of just aStartMark
+            startChar += OUStringChar(CH_TXT_ATR_FIELDSEP) + 
OUStringChar(aEndMark);
+        }
 
         SwPosition start = pField->GetMarkStart();
         if (aEndMark != CH_TXT_ATR_FORMELEMENT)
         {
             SwPaM aStartPaM(start);
-            io_pDoc->getIDocumentContentOperations().InsertString(aStartPaM, 
OUString(aStartMark));
-            --start.nContent; // restore, it was moved by InsertString
+            io_pDoc->getIDocumentContentOperations().InsertString(aStartPaM, 
startChar);
+            start.nContent -= startChar.getLength(); // restore, it was moved 
by InsertString
             // do not manipulate via reference directly but call 
SetMarkStartPos
             // which works even if start and end pos were the same
             pField->SetMarkStartPos( start );
             SwPosition& rEnd = pField->GetMarkEnd(); // note: retrieve after
             // setting start, because if start==end it can go stale, see 
SetMarkPos()
             assert(pSepPos == nullptr || (start < *pSepPos && *pSepPos <= 
rEnd));
-            *aStartPaM.GetPoint() = pSepPos ? *pSepPos : rEnd;
-            io_pDoc->getIDocumentContentOperations().InsertString(aStartPaM, 
OUString(CH_TXT_ATR_FIELDSEP));
-            if (!pSepPos || rEnd < *pSepPos)
-            {   // rEnd is not moved automatically if it's same as insert pos
-                ++rEnd.nContent;
+            if (startChar.getLength() == 1)
+            {
+                *aStartPaM.GetPoint() = pSepPos ? *pSepPos : rEnd;
+                
io_pDoc->getIDocumentContentOperations().InsertString(aStartPaM, 
OUString(CH_TXT_ATR_FIELDSEP));
+                if (!pSepPos || rEnd < *pSepPos)
+                {   // rEnd is not moved automatically if it's same as insert 
pos
+                    ++rEnd.nContent;
+                }
             }
             assert(pSepPos == nullptr || (start < *pSepPos && *pSepPos <= 
rEnd));
         }
@@ -185,7 +196,7 @@ namespace
         }
 
         SwPosition& rEnd = pField->GetMarkEnd();
-        if (aEndMark)
+        if (aEndMark && startChar.getLength() == 1)
         {
             SwPaM aEndPaM(rEnd);
             io_pDoc->getIDocumentContentOperations().InsertString(aEndPaM, 
OUString(aEndMark));
diff --git a/sw/source/core/txtnode/ndtxt.cxx b/sw/source/core/txtnode/ndtxt.cxx
index e39432ca13a4..6adb0a93dcf6 100644
--- a/sw/source/core/txtnode/ndtxt.cxx
+++ b/sw/source/core/txtnode/ndtxt.cxx
@@ -1386,8 +1386,9 @@ void SwTextNode::Update(
             }
         }
 
-        // Bookmarks must never grow to either side, when editing (directly) 
to the left or right (#i29942#)!
-        // And a bookmark with same start and end must remain to the left of 
the inserted text (used in XML import).
+        // Bookmarks must never grow to either side, when editing (directly)
+        // to the left or right (i#29942)! Exception: if the bookmark has
+        // 2 positions and start == end, then expand it (tdf#96479)
         {
             bool bAtLeastOneBookmarkMoved = false;
             bool bAtLeastOneExpandedBookmarkAtInsertionPosition = false;
@@ -1412,6 +1413,8 @@ void SwTextNode::Update(
                     {   // don't switch to iterating aTmpIdxReg!
                         next = rEndIdx.GetNext();
                     }
+                    // tdf#96479: if start == end, ignore the other position
+                    // so it is moved!
                     rEndIdx.Assign( &aTmpIdxReg, rEndIdx.GetIndex() );
                     bAtLeastOneBookmarkMoved = true;
                 }
_______________________________________________
Libreoffice-commits mailing list
libreoffice-comm...@lists.freedesktop.org
https://lists.freedesktop.org/mailman/listinfo/libreoffice-commits

Reply via email to