sw/qa/uibase/fldui/fldui.cxx      |   24 ++++++++++++++++++++++++
 sw/source/uibase/fldui/fldmgr.cxx |    7 +++++--
 2 files changed, 29 insertions(+), 2 deletions(-)

New commits:
commit 7bd39200c990f93b5b03b18fd863994ad8cfe4a2
Author:     Miklos Vajna <vmik...@collabora.com>
AuthorDate: Tue Oct 3 20:12:09 2023 +0200
Commit:     Xisco Fauli <xiscofa...@libreoffice.org>
CommitDate: Wed Oct 4 09:27:49 2023 +0200

    tdf#157394 sw: fix inserting reference mark with existing selection
    
    Type "myword" in Writer, select it, Insert -> Cross-reference, set Name
    to "myname", click Insert. The body text now had "myword" twice, instead
    of creating a reference mark on the existing "myword" word.
    
    This went wrong in commit 16075474819696f920979969474aa8300f4af530 (sw,
    field insert: handle the Content param for refmarks and accept HTML
    there, 2022-12-21), because it assumed that in case the uno comand is
    dispatched with some reference text, then that has to be inserted at the
    cursor position and only then the refmark can be created on that range.
    It was not expected that the current Writer selection would show up as
    refmark text.
    
    Fix the problem by taking the refmark text from the uno command
    parameter only in case we don't have selection, which restores the old
    behavior on manual insert of a refmark and keeps the new uno command
    parameter working.
    
    Note that contrary to the bug title, inserting cross-references did
    work, but the trouble was the insert of the duplicated word.
    
    (cherry picked from commit 4c5f51a7ac4c0f7043ead2b3b48e71c33e16f992)
    
    Change-Id: I1a8b002c2a6f196975a18e01f0e8c457cfb416bf
    Reviewed-on: https://gerrit.libreoffice.org/c/core/+/157551
    Tested-by: Jenkins
    Reviewed-by: Xisco Fauli <xiscofa...@libreoffice.org>

diff --git a/sw/qa/uibase/fldui/fldui.cxx b/sw/qa/uibase/fldui/fldui.cxx
index 79a53c842a46..78b2fcedd42a 100644
--- a/sw/qa/uibase/fldui/fldui.cxx
+++ b/sw/qa/uibase/fldui/fldui.cxx
@@ -120,6 +120,30 @@ CPPUNIT_TEST_FIXTURE(Test, testInsertRefmark)
     CPPUNIT_ASSERT_EQUAL(static_cast<size_t>(1), aAttrs.size());
     CPPUNIT_ASSERT_EQUAL(OUString("aaabbbccc"), pTextNode->GetText());
 }
+
+CPPUNIT_TEST_FIXTURE(Test, testInsertRefmarkSelection)
+{
+    // Given a document with a single selected word:
+    createSwDoc();
+    SwDoc* pDoc = getSwDoc();
+    SwWrtShell* pWrtShell = pDoc->GetDocShell()->GetWrtShell();
+    pWrtShell->Insert2("myword");
+    pWrtShell->SelAll();
+
+    // When inserting a refmark:
+    SwFieldMgr aMgr(pWrtShell);
+    SwInsertField_Data aData(SwFieldTypesEnum::SetRef, /*nSubType=*/0, 
"myname", "myword",
+                             /*nFormatId=*/0);
+    aMgr.InsertField(aData);
+
+    // Then make sure the document still just contains that word only once:
+    SwTextNode* pTextNode = 
pWrtShell->GetCursor()->GetPointNode().GetTextNode();
+    // Without the accompanying fix in place, this test would have failed with:
+    // - Expected: myword
+    // - Actual  : mywordmyword
+    // i.e. the content of the selection was duplicated.
+    CPPUNIT_ASSERT_EQUAL(OUString("myword"), pTextNode->GetText());
+}
 }
 
 CPPUNIT_PLUGIN_IMPLEMENT();
diff --git a/sw/source/uibase/fldui/fldmgr.cxx 
b/sw/source/uibase/fldui/fldmgr.cxx
index efc5f916f178..143c981a6131 100644
--- a/sw/source/uibase/fldui/fldmgr.cxx
+++ b/sw/source/uibase/fldui/fldmgr.cxx
@@ -1074,7 +1074,10 @@ bool SwFieldMgr::InsertField(
                 const OUString& rRefmarkText = rData.m_sPar2;
                 SwPaM* pCursorPos = pCurShell->GetCursor();
                 pCurShell->StartAction();
-                if (!rRefmarkText.isEmpty())
+                bool bHadMark = pCursorPos->HasMark();
+                // If we have no selection and the refmark text is provided, 
then the text is
+                // expected to be HTML.
+                if (!bHadMark && !rRefmarkText.isEmpty())
                 {
                     // Split node to remember where the start position is.
                     bool bSuccess = 
pCurShell->GetDoc()->getIDocumentContentOperations().SplitNode(
@@ -1100,7 +1103,7 @@ bool SwFieldMgr::InsertField(
 
                 pCurShell->SetAttrItem( SwFormatRefMark( rData.m_sPar1 ) );
 
-                if (!rRefmarkText.isEmpty())
+                if (!bHadMark && !rRefmarkText.isEmpty())
                 {
                     pCursorPos->DeleteMark();
                 }

Reply via email to