sw/qa/extras/layout/data/three_sections.fodt |   18 ++++++
 sw/qa/extras/layout/layout2.cxx              |   42 ++++++++++++++
 sw/source/core/inc/sectfrm.hxx               |    2 
 sw/source/core/layout/frmtool.cxx            |   63 +++++++--------------
 sw/source/core/layout/layhelp.hxx            |    3 +
 sw/source/core/layout/sectfrm.cxx            |   79 ++++++++++++++-------------
 6 files changed, 128 insertions(+), 79 deletions(-)

New commits:
commit 9c8c9499339fcf4e6f92d5bdebec0c567c815d43
Author:     Mike Kaganski <mike.kagan...@collabora.com>
AuthorDate: Thu Apr 20 11:18:34 2023 +0300
Commit:     Mike Kaganski <mike.kagan...@collabora.com>
CommitDate: Tue Apr 25 11:22:46 2023 +0200

    tdf#154113: do not forget to split the outermost section frame
    
    ... when the inserted section node is not the first one.
    
    The very first frame, where InsertCnt_ puts content to, may already have
    some content after the insertion position. When an inner section ends,
    the current section needs a new frame, and the rest of content must go
    to that new frame. Previously, the new empty frame was created without
    taking the content move into account.
    
    This moves the split into the single place inside InsertCnt_, to avoid
    special processing in MakeFrames.
    
    Change-Id: I1335ebbc620af0f2b064141e8267e5bd1af0b195
    Reviewed-on: https://gerrit.libreoffice.org/c/core/+/150675
    Tested-by: Jenkins
    Reviewed-by: Mike Kaganski <mike.kagan...@collabora.com>
    Reviewed-on: https://gerrit.libreoffice.org/c/core/+/150740
    Tested-by: Jenkins CollaboraOffice <jenkinscollaboraoff...@gmail.com>

diff --git a/sw/qa/extras/layout/data/three_sections.fodt 
b/sw/qa/extras/layout/data/three_sections.fodt
new file mode 100644
index 000000000000..9233fed89085
--- /dev/null
+++ b/sw/qa/extras/layout/data/three_sections.fodt
@@ -0,0 +1,18 @@
+<?xml version="1.0" encoding="UTF-8"?>
+
+<office:document 
xmlns:office="urn:oasis:names:tc:opendocument:xmlns:office:1.0" 
xmlns:text="urn:oasis:names:tc:opendocument:xmlns:text:1.0" 
office:version="1.2" office:mimetype="application/vnd.oasis.opendocument.text">
+ <office:body>
+  <office:text>
+   <text:p>Select the text below, copy to clipboard, and paste from clipboard, 
replacing the selection.</text:p>
+   <text:section text:name="Section1">
+    <text:p>&lt;-- Start selection here. Section1</text:p>
+   </text:section>
+   <text:section text:name="Section2">
+    <text:p>Section2</text:p>
+   </text:section>
+   <text:section text:name="Section3">
+    <text:p>Section3. End selection here --&gt;</text:p>
+   </text:section>
+  </office:text>
+ </office:body>
+</office:document>
\ No newline at end of file
diff --git a/sw/qa/extras/layout/layout2.cxx b/sw/qa/extras/layout/layout2.cxx
index 9c656a6808ba..59e9d7d9f43c 100644
--- a/sw/qa/extras/layout/layout2.cxx
+++ b/sw/qa/extras/layout/layout2.cxx
@@ -19,6 +19,7 @@
 #include <i18nlangtag/languagetag.hxx>
 #include <editeng/unolingu.hxx>
 #include <svx/svdobj.hxx>
+#include <vcl/scheduler.hxx>
 
 #include <unotxdoc.hxx>
 #include <rootfrm.hxx>
@@ -1773,6 +1774,47 @@ CPPUNIT_TEST_FIXTURE(SwLayoutWriter2, 
testUserFieldTypeLanguage)
                 "1,234.56");
 }
 
+CPPUNIT_TEST_FIXTURE(SwLayoutWriter2, testTdf154113)
+{
+    createSwDoc(DATA_DIRECTORY, "three_sections.fodt");
+    Scheduler::ProcessEventsToIdle();
+
+    dispatchCommand(mxComponent, ".uno:GoToStartOfDoc", {});
+    dispatchCommand(mxComponent, ".uno:GoToNextPara", {});
+    dispatchCommand(mxComponent, ".uno:EndOfDocumentSel", {}); // to the end 
of current section!
+    dispatchCommand(mxComponent, ".uno:EndOfDocumentSel", {}); // to the end 
of the document.
+
+    uno::Reference<frame::XModel> xModel(mxComponent, uno::UNO_QUERY_THROW);
+    uno::Reference<container::XIndexAccess> 
xSelected(xModel->getCurrentSelection(),
+                                                      uno::UNO_QUERY_THROW);
+    CPPUNIT_ASSERT_EQUAL(sal_Int32(1), xSelected->getCount());
+    uno::Reference<text::XTextRange> xRange(xSelected->getByIndex(0), 
uno::UNO_QUERY_THROW);
+    CPPUNIT_ASSERT_EQUAL(OUString("<-- Start selection here. Section1" 
SAL_NEWLINE_STRING
+                                  "Section2" SAL_NEWLINE_STRING "Section3. End 
selection here -->"),
+                         xRange->getString());
+
+    dispatchCommand(mxComponent, ".uno:Cut", {});
+
+    xSelected.set(xModel->getCurrentSelection(), uno::UNO_QUERY_THROW);
+    CPPUNIT_ASSERT_EQUAL(sal_Int32(1), xSelected->getCount());
+    xRange.set(xSelected->getByIndex(0), uno::UNO_QUERY_THROW);
+    CPPUNIT_ASSERT_EQUAL(OUString(), xRange->getString());
+
+    dispatchCommand(mxComponent, ".uno:Paste", {});
+
+    xmlDocUniquePtr pXml = parseLayoutDump();
+
+    // Without the fix in place, this would fail with
+    // - Expected: 3
+    // - Actual  : 2
+    assertXPath(pXml, "/root/page/body/section", 3);
+    assertXPath(pXml, "/root/page/body/section[1]/txt/Text", "Portion",
+                "<-- Start selection here. Section1");
+    assertXPath(pXml, "/root/page/body/section[2]/txt/Text", "Portion", 
"Section2");
+    assertXPath(pXml, "/root/page/body/section[3]/txt/Text", "Portion",
+                "Section3. End selection here -->");
+}
+
 CPPUNIT_PLUGIN_IMPLEMENT();
 
 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
diff --git a/sw/source/core/inc/sectfrm.hxx b/sw/source/core/inc/sectfrm.hxx
index 09c742f8da79..e08a9b0a7137 100644
--- a/sw/source/core/inc/sectfrm.hxx
+++ b/sw/source/core/inc/sectfrm.hxx
@@ -109,7 +109,7 @@ public:
      * Splits the SectionFrame surrounding the pFrame up in two parts:
      * pFrame and the start of the 2nd part
      */
-    bool SplitSect( SwFrame* pFrame, bool bApres );
+    SwSectionFrame* SplitSect( SwFrame* pFrameStartAfter, SwFrame* 
pFramePutAfter );
     void DelEmpty( bool bRemove ); // Like Cut(), except for that Follow 
chaining is maintained
     SwFootnoteContFrame* ContainsFootnoteCont( const SwFootnoteContFrame* 
pCont = nullptr ) const;
     bool Growable() const;
diff --git a/sw/source/core/layout/frmtool.cxx 
b/sw/source/core/layout/frmtool.cxx
index 357f1f3db1a9..c1bb52fc4112 100644
--- a/sw/source/core/layout/frmtool.cxx
+++ b/sw/source/core/layout/frmtool.cxx
@@ -1727,6 +1727,9 @@ void InsertCnt_( SwLayoutFrame *pLay, SwDoc *pDoc,
                 nIndex = pNode->EndOfSectionIndex();
             else
             {
+                if (pActualSection)
+                    pActualSection->SetLastPos(pPrv);
+
                 pFrame = pNode->MakeFrame( pLay );
                 pActualSection.reset( new SwActualSection( 
pActualSection.release(),
                                                 
static_cast<SwSectionFrame*>(pFrame), pNode ) );
@@ -1881,33 +1884,30 @@ void InsertCnt_( SwLayoutFrame *pLay, SwDoc *pDoc,
                 }
 
                 // new section frame
-                pFrame = pActualSection->GetSectionNode()->MakeFrame( pLay );
-                pFrame->InsertBehind( pLay, pPrv );
-                static_cast<SwSectionFrame*>(pFrame)->Init();
-
-                // OD 12.08.2003 #i17969# - consider horizontal/vertical layout
-                // for setting position at newly inserted frame
-                lcl_SetPos( *pFrame, *pLay );
-
-                SwSectionFrame* pOuterSectionFrame = 
pActualSection->GetSectionFrame();
-
-                // a follow has to be appended to the new section frame
-                SwSectionFrame* pFollow = pOuterSectionFrame ? 
pOuterSectionFrame->GetFollow() : nullptr;
-                if ( pFollow )
+                if (SwSectionFrame* pOuterSectionFrame = 
pActualSection->GetSectionFrame())
                 {
-                    pOuterSectionFrame->SetFollow( nullptr );
-                    pOuterSectionFrame->InvalidateSize();
-                    static_cast<SwSectionFrame*>(pFrame)->SetFollow( pFollow );
-                }
+                    // Splitting moves the trailing content to the next frame
+                    pFrame = 
pOuterSectionFrame->SplitSect(pActualSection->GetLastPos(), pPrv);
 
-                // We don't want to leave empty parts back.
-                if (pOuterSectionFrame &&
-                    ! pOuterSectionFrame->IsColLocked() &&
-                    ! pOuterSectionFrame->ContainsContent() )
+                    // We don't want to leave empty parts back.
+                    if (! pOuterSectionFrame->IsColLocked() &&
+                        ! pOuterSectionFrame->ContainsContent() )
+                    {
+                        pOuterSectionFrame->DelEmpty( true );
+                        SwFrame::DestroyFrame(pOuterSectionFrame);
+                    }
+                }
+                else
                 {
-                    pOuterSectionFrame->DelEmpty( true );
-                    SwFrame::DestroyFrame(pOuterSectionFrame);
+                    pFrame = pActualSection->GetSectionNode()->MakeFrame( pLay 
);
+                    pFrame->InsertBehind( pLay, pPrv );
+                    static_cast<SwSectionFrame*>(pFrame)->Init();
+
+                    // OD 12.08.2003 #i17969# - consider horizontal/vertical 
layout
+                    // for setting position at newly inserted frame
+                    lcl_SetPos( *pFrame, *pLay );
                 }
+
                 pActualSection->SetSectionFrame( 
static_cast<SwSectionFrame*>(pFrame) );
 
                 pLay = static_cast<SwLayoutFrame*>(pFrame);
@@ -2132,20 +2132,7 @@ void MakeFrames( SwDoc *pDoc, const SwNodeIndex &rSttIdx,
             }
             else
             {
-                bool bSplit;
                 SwFrame* pPrv = bApres ? pFrame : pFrame->GetPrev();
-                // If the section frame is inserted into another one, it must 
be split.
-                if( pSct && rSttIdx.GetNode().IsSectionNode() )
-                {
-                    bSplit = pSct->SplitSect( pFrame, bApres );
-                    if( !bSplit && !bApres )
-                    {
-                        pUpper = pSct->GetUpper();
-                        pPrv = pSct->GetPrev();
-                    }
-                }
-                else
-                    bSplit = false;
 
                 ::InsertCnt_( pUpper, pDoc, rSttIdx.GetIndex(), false,
                               nEndIdx, pPrv, eMode );
@@ -2158,10 +2145,6 @@ void MakeFrames( SwDoc *pDoc, const SwNodeIndex &rSttIdx,
                         AppendAllObjs( pTable, pUpper );
                 }
 
-                // If nothing was added (e.g. a hidden section), the split 
must be reversed.
-                if( bSplit && pSct && pSct->GetNext()
-                    && pSct->GetNext()->IsSctFrame() )
-                    pSct->MergeNext( 
static_cast<SwSectionFrame*>(pSct->GetNext()) );
                 if( pFrame->IsInFly() )
                     pFrame->FindFlyFrame()->Invalidate_();
                 if( pFrame->IsInTab() )
diff --git a/sw/source/core/layout/layhelp.hxx 
b/sw/source/core/layout/layhelp.hxx
index 5df151ae7e61..d8a3da0519a5 100644
--- a/sw/source/core/layout/layhelp.hxx
+++ b/sw/source/core/layout/layhelp.hxx
@@ -85,6 +85,7 @@ class SwActualSection
 {
     SwActualSection *m_pUpper;
     SwSectionFrame    *m_pSectFrame;
+    SwFrame* m_pLastPos = nullptr; // Split it *after* this child frame
     SwSectionNode   *m_pSectNode;
 public:
     SwActualSection( SwActualSection *pUpper,
@@ -96,6 +97,8 @@ public:
     SwSectionNode   *GetSectionNode()                   { return m_pSectNode;}
     void             SetUpper(SwActualSection *p)       { m_pUpper = p; }
     SwActualSection *GetUpper()                         { return m_pUpper; }
+    void SetLastPos(SwFrame* p) { m_pLastPos = p; }
+    SwFrame* GetLastPos() const { return m_pLastPos; }
 };
 
 /// Helps during the InsertCnt_ function to create new pages.
diff --git a/sw/source/core/layout/sectfrm.cxx 
b/sw/source/core/layout/sectfrm.cxx
index 34e86e51808b..fded9b2dc36b 100644
--- a/sw/source/core/layout/sectfrm.cxx
+++ b/sw/source/core/layout/sectfrm.cxx
@@ -507,50 +507,53 @@ void SwSectionFrame::MergeNext( SwSectionFrame* pNxt )
 }
 
 /**
-|*  Divides a SectionFrame into two parts. The second one starts with the
-|*  passed frame.
+|*  Divides a SectionFrame into two parts. The content of the second one
+|*  starts after pFrameStartAfter; the created second section frame itself
+|*  is put after pFramePutAfter.
+|*  If pFrameStartAfter is nullptr, the split happens at the start.
 |*  This is required when inserting an inner section, because the MoveFwd
 |*  cannot have the desired effect within a frame or a table cell.
+|*  Splitting at the start/end makes sense, because the empty frame would
+|*  be removed after the InsertCnt_ finished.
 |*/
-bool SwSectionFrame::SplitSect( SwFrame* pFrame, bool bApres )
+SwSectionFrame* SwSectionFrame::SplitSect( SwFrame* pFrameStartAfter, SwFrame* 
pFramePutAfter )
 {
-    assert(pFrame && "SplitSect: Why?");
-    SwFrame* pOther = bApres ? pFrame->FindNext() : pFrame->FindPrev();
-    if( !pOther )
-        return false;
-    SwSectionFrame* pSect = pOther->FindSctFrame();
-    if( pSect != this )
-        return false;
+    assert(!pFrameStartAfter || pFrameStartAfter->FindSctFrame() == this);
+    SwFrame* pSav = pFrameStartAfter ? pFrameStartAfter->FindNext() : 
ContainsAny();
+    if (pSav && pSav->FindSctFrame() != this)
+        pSav = nullptr; // we are at the very end
+
     // Put the content aside
-    SwFrame* pSav = ::SaveContent( this, bApres ? pOther : pFrame );
-    OSL_ENSURE( pSav, "SplitSect: What's on?" );
-    if( pSav ) // be robust
-    {   // Create a new SctFrame, not as a Follower/master
-        SwSectionFrame* pNew = new SwSectionFrame( *pSect->GetSection(), pSect 
);
-        pNew->InsertBehind( pSect->GetUpper(), pSect );
-        pNew->Init();
-        SwRectFnSet aRectFnSet(this);
-        aRectFnSet.MakePos( *pNew, nullptr, pSect, true );
-        // OD 25.03.2003 #108339# - restore content:
-        // determine layout frame for restoring content after the 
initialization
-        // of the section frame. In the section initialization the columns are
-        // created.
-        {
-            SwLayoutFrame* pLay = pNew;
-            // Search for last layout frame, e.g. for columned sections.
-            while( pLay->Lower() && pLay->Lower()->IsLayoutFrame() )
-                pLay = static_cast<SwLayoutFrame*>(pLay->Lower());
-            ::RestoreContent( pSav, pLay, nullptr );
-        }
-        InvalidateSize_();
-        if( HasFollow() )
-        {
-            pNew->SetFollow( GetFollow() );
-            SetFollow( nullptr );
-        }
-        return true;
+    if (pSav)
+        pSav = ::SaveContent( this, pSav );
+
+    // Create a new SctFrame, not as a Follower/master
+    if (!pFramePutAfter)
+        pFramePutAfter = this;
+    SwSectionFrame* pNew = new SwSectionFrame( *GetSection(), this );
+    pNew->InsertBehind( pFramePutAfter->GetUpper(), pFramePutAfter );
+    pNew->Init();
+    SwRectFnSet aRectFnSet(this);
+    aRectFnSet.MakePos( *pNew, nullptr, pFramePutAfter, true );
+    // OD 25.03.2003 #108339# - restore content:
+    // determine layout frame for restoring content after the initialization
+    // of the section frame. In the section initialization the columns are
+    // created.
+    if (pSav)
+    {
+        SwLayoutFrame* pLay = pNew;
+        // Search for last layout frame, e.g. for columned sections.
+        while( pLay->Lower() && pLay->Lower()->IsLayoutFrame() )
+            pLay = static_cast<SwLayoutFrame*>(pLay->Lower());
+        ::RestoreContent( pSav, pLay, nullptr );
+    }
+    InvalidateSize_();
+    if( HasFollow() )
+    {
+        pNew->SetFollow( GetFollow() );
+        SetFollow( nullptr );
     }
-    return false;
+    return pNew;
 }
 
 /**

Reply via email to