tdf#103480 sc: fix lost hyperlink at merging an empty cell with its cell

Merging a cell containing a hyperlink with an empty cell
resulted the loss of the hyperlink, i.e. only the text
content was kept.

Change-Id: I5148ce55157e3ad7926f5b5a82a682b837a784ed
Reviewed-on: https://gerrit.libreoffice.org/c/core/+/154520
Reviewed-by: László Németh <nemeth@numbertext.org>
Tested-by: László Németh <nemeth@numbertext.org>
diff --git a/sc/qa/unit/ucalc_copypaste.cxx b/sc/qa/unit/ucalc_copypaste.cxx
index bad57d8..575cfb9 100644
--- a/sc/qa/unit/ucalc_copypaste.cxx
+++ b/sc/qa/unit/ucalc_copypaste.cxx
@@ -10752,6 +10752,23 @@ CPPUNIT_TEST_FIXTURE(TestCopyPaste, testUndoBackgroundColor)
    m_pDoc->DeleteTab(0);
}

CPPUNIT_TEST_FIXTURE(TestCopyPaste, testMergedHyperlink)
{
    m_pDoc->InsertTab(0, "Table1");
    m_pDoc->InitDrawLayer(m_xDocShell.get());

    ScFieldEditEngine& pEE = m_pDoc->GetEditEngine();
    pEE.SetTextCurrentDefaults("https://libreoffice.org/");
    m_pDoc->SetEditText(ScAddress(1, 0, 0), pEE.CreateTextObject()); // B1

    m_pDoc->DoMergeContents(0, 0, 1, 0, 0); // A1:B1

    CPPUNIT_ASSERT_EQUAL(CELLTYPE_EDIT, m_pDoc->GetCellType(ScAddress(0, 0, 0))); // A1
    const EditTextObject* pEditObj = m_pDoc->GetEditText(ScAddress(0, 0, 0)); // A1
    CPPUNIT_ASSERT(pEditObj);
    CPPUNIT_ASSERT_EQUAL(OUString("https://libreoffice.org/"), pEditObj->GetText(0));
}

CPPUNIT_PLUGIN_IMPLEMENT();

/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
diff --git a/sc/source/core/data/documen3.cxx b/sc/source/core/data/documen3.cxx
index 73cf516..b52deed 100644
--- a/sc/source/core/data/documen3.cxx
+++ b/sc/source/core/data/documen3.cxx
@@ -2030,6 +2030,7 @@ void ScDocument::DoMergeContents( SCCOL nStartCol, SCROW nStartRow,
    OUString aCellStr;
    SCCOL nCol;
    SCROW nRow;
    ScCellValue aCell;
    for (nRow=nStartRow; nRow<=nEndRow; nRow++)
        for (nCol=nStartCol; nCol<=nEndCol; nCol++)
        {
@@ -2039,12 +2040,18 @@ void ScDocument::DoMergeContents( SCCOL nStartCol, SCROW nStartRow,
                if (!aTotal.isEmpty())
                    aTotal.append(' ');
                aTotal.append(aCellStr);
                ScAddress aPos(nCol, nRow, nTab);
                if ((GetCellType(aPos) == CELLTYPE_EDIT) && aCell.isEmpty())
                    aCell = ScRefCellValue(*this, aPos);
            }
            if (nCol != nStartCol || nRow != nStartRow)
                SetString(nCol,nRow,nTab,"");
        }

    SetString(nStartCol,nStartRow,nTab,aTotal.makeStringAndClear());
    if (aCell.isEmpty() || !GetString(nStartCol, nStartRow, nTab).isEmpty())
        SetString(nStartCol, nStartRow, nTab, aTotal.makeStringAndClear());
    else
        aCell.release(*this, ScAddress(nStartCol, nStartRow, nTab));
}

void ScDocument::DoEmptyBlock( SCCOL nStartCol, SCROW nStartRow,