From c13103179b9c1a8472d5d7b215075f25afaec252 Mon Sep 17 00:00:00 2001 From: Michael Stahl Date: Thu, 4 Feb 2021 12:36:41 +0100 Subject: sw: fix copying and deleting of table in section via API This is a follow-up to 7ab349296dac79dad3fec09f60348efcbb9ea17e. The first problem was that tables that contain protected cells refused to be deleted, which caused an infinite loop in SwXTextRange::DeleteAndInsert(). This also affected SwXTextTable::dispose(), and DDE tables. Fix this by forcibly deleting even protected cells from UNO APIs; protection is an UI feature. The second problem was that pasting the table SwFEShell::Paste() would set up aCpyPam so that it selects from the start node of the table to the last text node in the table, excluding the table cell and table end nodes, which caused: DocumentContentOperationsManager.cxx:3548: An insufficient number of nodes were copied! Fix this by setting up aCpyPam over the entire body section of the clipboard document. Change-Id: I8c6caee5e75260dff79f106efc11f9669feddc0c Reviewed-on: https://gerrit.libreoffice.org/c/core/+/110411 Tested-by: Jenkins Reviewed-by: Michael Stahl (cherry picked from commit c598e0accf24e149520a22a8c062249f147844c7) --- sw/inc/doc.hxx | 8 +- .../data/tdf134252_onlytable_protected.fodt | 87 ++++++++++++++++++++++ sw/qa/extras/unowriter/unowriter.cxx | 59 +++++++++++++++ sw/source/core/docnode/ndtbl.cxx | 18 +++-- sw/source/core/frmedt/fecopy.cxx | 20 ++--- sw/source/core/frmedt/fetab.cxx | 3 +- sw/source/core/unocore/unoobj2.cxx | 2 +- sw/source/core/unocore/unotbl.cxx | 2 +- 8 files changed, 175 insertions(+), 24 deletions(-) create mode 100644 sw/qa/extras/unowriter/data/tdf134252_onlytable_protected.fodt diff --git a/sw/inc/doc.hxx b/sw/inc/doc.hxx index b80e1157a7c9..7a2096f303ad 100644 --- a/sw/inc/doc.hxx +++ b/sw/inc/doc.hxx @@ -21,6 +21,7 @@ // SwDoc interfaces #include +#include #include #include "swdllapi.h" #include "swtypes.hxx" @@ -1189,7 +1190,8 @@ public: sal_uInt16 nCnt = 1, bool bBehind = true ); // Delete Columns/Rows in table. - bool DeleteRowCol( const SwSelBoxes& rBoxes, bool bColumn = false ); + enum class RowColMode { DeleteRow = 0, DeleteColumn = 1, DeleteProtected = 2 }; + bool DeleteRowCol(const SwSelBoxes& rBoxes, RowColMode eMode = RowColMode::DeleteRow); void DeleteRow( const SwCursor& rCursor ); void DeleteCol( const SwCursor& rCursor ); @@ -1658,6 +1660,10 @@ private: }; +namespace o3tl { + template<> struct typed_flags : is_typed_flags {}; +} + // This method is called in Dtor of SwDoc and deletes cache of ContourObjects. void ClrContourCache(); diff --git a/sw/qa/extras/unowriter/data/tdf134252_onlytable_protected.fodt b/sw/qa/extras/unowriter/data/tdf134252_onlytable_protected.fodt new file mode 100644 index 000000000000..f588fdba0515 --- /dev/null +++ b/sw/qa/extras/unowriter/data/tdf134252_onlytable_protected.fodt @@ -0,0 +1,87 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + baz + + + + + foo + + + diff --git a/sw/qa/extras/unowriter/unowriter.cxx b/sw/qa/extras/unowriter/unowriter.cxx index fd3fe08a5a13..48c3ed7ba90d 100644 --- a/sw/qa/extras/unowriter/unowriter.cxx +++ b/sw/qa/extras/unowriter/unowriter.cxx @@ -481,6 +481,65 @@ CPPUNIT_TEST_FIXTURE(SwUnoWriter, testSectionAnchorCopyTableAtEnd) xCursor->getString()); } +CPPUNIT_TEST_FIXTURE(SwUnoWriter, testSectionAnchorCopyTable) +{ + // this contains a section that ends with a table (plus another section) + load(DATA_DIRECTORY, "tdf134252_onlytable_protected.fodt"); + + uno::Reference const xTextTablesSupplier(mxComponent, + uno::UNO_QUERY); + uno::Reference const xTables(xTextTablesSupplier->getTextTables(), + uno::UNO_QUERY); + CPPUNIT_ASSERT_EQUAL(sal_Int32(1), xTables->getCount()); + + uno::Reference const xTextSectionsSupplier(mxComponent, + uno::UNO_QUERY); + uno::Reference const xSections( + xTextSectionsSupplier->getTextSections(), uno::UNO_QUERY); + + CPPUNIT_ASSERT_EQUAL(sal_Int32(1), xSections->getCount()); + + uno::Reference const xSection(xSections->getByIndex(0), uno::UNO_QUERY); + uno::Reference const xAnchor(xSection->getAnchor()); + CPPUNIT_ASSERT_EQUAL(OUString("baz" SAL_NEWLINE_STRING), xAnchor->getString()); + + // copy the content of the section to a clipboard document + uno::Reference const xTS( + uno::Reference(mxComponent, uno::UNO_QUERY_THROW)->getCurrentController(), + uno::UNO_QUERY); + uno::Reference const xTTS(xTS, uno::UNO_QUERY); + uno::Reference const xTransferable( + xTTS->getTransferableForTextRange(xAnchor)); + + // check this doesn't throw + CPPUNIT_ASSERT(xAnchor->getStart().is()); + CPPUNIT_ASSERT(xAnchor->getEnd().is()); + + // replace section content + xAnchor->setString("quux"); + + // table in section was deleted, but not section itself + CPPUNIT_ASSERT_EQUAL(sal_Int32(0), xTables->getCount()); + CPPUNIT_ASSERT_EQUAL(sal_Int32(1), xSections->getCount()); + CPPUNIT_ASSERT_EQUAL(OUString("\"" + "quux" /*SAL_NEWLINE_STRING*/ "\""), + OUString("\"" + xAnchor->getString() + "\"")); + + // now paste it + uno::Reference const xTVCS(xTS, uno::UNO_QUERY); + uno::Reference const xCursor(xTVCS->getViewCursor()); + xCursor->gotoEnd(false); + xTS->insertTransferable(xTransferable); + + // table in section was pasted, but not section itself + CPPUNIT_ASSERT_EQUAL(sal_Int32(1), xTables->getCount()); + CPPUNIT_ASSERT_EQUAL(sal_Int32(1), xSections->getCount()); + xCursor->gotoStart(true); + CPPUNIT_ASSERT_EQUAL( + OUString("quux" SAL_NEWLINE_STRING "foo" SAL_NEWLINE_STRING "baz" SAL_NEWLINE_STRING), + xCursor->getString()); +} + CPPUNIT_TEST_FIXTURE(SwUnoWriter, testTextRangeInTable) { load(DATA_DIRECTORY, "bookmarkintable.fodt"); diff --git a/sw/source/core/docnode/ndtbl.cxx b/sw/source/core/docnode/ndtbl.cxx index 15a49729ce51..8c733e5f92ab 100644 --- a/sw/source/core/docnode/ndtbl.cxx +++ b/sw/source/core/docnode/ndtbl.cxx @@ -1929,22 +1929,28 @@ void SwDoc::DeleteCol( const SwCursor& rCursor ) // Thus delete the Columns GetIDocumentUndoRedo().StartUndo(SwUndoId::COL_DELETE, nullptr); - DeleteRowCol( aBoxes, true ); + DeleteRowCol(aBoxes, SwDoc::RowColMode::DeleteColumn); GetIDocumentUndoRedo().EndUndo(SwUndoId::COL_DELETE, nullptr); } -bool SwDoc::DeleteRowCol( const SwSelBoxes& rBoxes, bool bColumn ) +bool SwDoc::DeleteRowCol(const SwSelBoxes& rBoxes, RowColMode const eMode) { - if( ::HasProtectedCells( rBoxes )) + if (!(eMode & SwDoc::RowColMode::DeleteProtected) + && ::HasProtectedCells(rBoxes)) + { return false; + } OSL_ENSURE( !rBoxes.empty(), "No valid Box list" ); SwTableNode* pTableNd = const_cast(rBoxes[0]->GetSttNd()->FindTableNode()); if( !pTableNd ) return false; - if( dynamic_cast( &pTableNd->GetTable() ) != nullptr) + if (!(eMode & SwDoc::RowColMode::DeleteProtected) + && dynamic_cast(&pTableNd->GetTable()) != nullptr) + { return false; + } ::ClearFEShellTabCols(*this, nullptr); SwSelBoxes aSelBoxes( rBoxes ); @@ -1953,7 +1959,7 @@ bool SwDoc::DeleteRowCol( const SwSelBoxes& rBoxes, bool bColumn ) long nMax = 0; if( rTable.IsNewModel() ) { - if( bColumn ) + if (eMode & SwDoc::RowColMode::DeleteColumn) rTable.ExpandColumnSelection( aSelBoxes, nMin, nMax ); else rTable.FindSuperfluousRows( aSelBoxes ); @@ -2112,7 +2118,7 @@ bool SwDoc::DeleteRowCol( const SwSelBoxes& rBoxes, bool bColumn ) if (rTable.IsNewModel()) { - if (bColumn) + if (eMode & SwDoc::RowColMode::DeleteColumn) rTable.PrepareDeleteCol( nMin, nMax ); rTable.FindSuperfluousRows( aSelBoxes ); if (pUndo) diff --git a/sw/source/core/frmedt/fecopy.cxx b/sw/source/core/frmedt/fecopy.cxx index c2470b997a93..f8bb7b36ff4b 100644 --- a/sw/source/core/frmedt/fecopy.cxx +++ b/sw/source/core/frmedt/fecopy.cxx @@ -686,25 +686,19 @@ bool SwFEShell::Paste( SwDoc* pClpDoc, bool bNestedTable ) OSL_ENSURE( pClpDoc, "no clipboard document" ); // then till end of the nodes array SwNodeIndex aIdx( pClpDoc->GetNodes().GetEndOfExtras(), 2 ); - SwPaM aCpyPam( aIdx ); //DocStart + // select content section, whatever it may contain + SwPaM aCpyPam(aIdx, SwNodeIndex(pClpDoc->GetNodes().GetEndOfContent(), -1)); + if (SwContentNode *const pAtEnd = aCpyPam.GetNode(true).GetContentNode()) + { + pAtEnd->MakeEndIndex(&aCpyPam.GetPoint()->nContent); + } // If there are table formulas in the area, then display the table first // so that the table formula can calculate a new value first // (individual boxes in the area are retrieved via the layout) SwFieldType* pTableFieldTyp = GetDoc()->getIDocumentFieldsAccess().GetSysFieldType( SwFieldIds::Table ); - SwTableNode *const pSrcNd = aCpyPam.GetNode().GetTableNode(); - if( !pSrcNd ) // table node ? - { // don't skip !! - SwContentNode* pCNd = aCpyPam.GetNode().GetContentNode(); - if( pCNd ) - aCpyPam.GetPoint()->nContent.Assign( pCNd, 0 ); - else if( !aCpyPam.Move( fnMoveForward, GoInNode )) - aCpyPam.Move( fnMoveBackward, GoInNode ); - } - - aCpyPam.SetMark(); - aCpyPam.Move( fnMoveForward, GoInDoc ); + SwTableNode *const pSrcNd = aCpyPam.GetNode(false).GetTableNode(); bool bRet = true; StartAllAction(); diff --git a/sw/source/core/frmedt/fetab.cxx b/sw/source/core/frmedt/fetab.cxx index a6cd65a55d15..f60c49fb5b6b 100644 --- a/sw/source/core/frmedt/fetab.cxx +++ b/sw/source/core/frmedt/fetab.cxx @@ -296,9 +296,8 @@ bool SwFEShell::DeleteCol() // then delete the column StartUndo(SwUndoId::COL_DELETE); - bRet = GetDoc()->DeleteRowCol( aBoxes, true ); + bRet = GetDoc()->DeleteRowCol(aBoxes, SwDoc::RowColMode::DeleteColumn); EndUndo(SwUndoId::COL_DELETE); - } else bRet = false; diff --git a/sw/source/core/unocore/unoobj2.cxx b/sw/source/core/unocore/unoobj2.cxx index 8e17f700c4d9..e7673186713c 100644 --- a/sw/source/core/unocore/unoobj2.cxx +++ b/sw/source/core/unocore/unoobj2.cxx @@ -829,7 +829,7 @@ static void DeleteTable(SwDoc & rDoc, SwTable& rTable) } // note: if the table is the content in the section, this will create // a new text node - that's desirable here - rDoc.DeleteRowCol(aSelBoxes); + rDoc.DeleteRowCol(aSelBoxes, SwDoc::RowColMode::DeleteProtected); } void SwXTextRange::DeleteAndInsert( diff --git a/sw/source/core/unocore/unotbl.cxx b/sw/source/core/unocore/unotbl.cxx index e2e5e123871e..b80268283d38 100644 --- a/sw/source/core/unocore/unotbl.cxx +++ b/sw/source/core/unocore/unotbl.cxx @@ -2227,7 +2227,7 @@ void SwXTextTable::dispose() SwSelBoxes aSelBoxes; for(auto& rBox : pTable->GetTabSortBoxes() ) aSelBoxes.insert(rBox); - pFormat->GetDoc()->DeleteRowCol(aSelBoxes); + pFormat->GetDoc()->DeleteRowCol(aSelBoxes, SwDoc::RowColMode::DeleteProtected); } void SAL_CALL SwXTextTable::addEventListener( -- cgit