sw/qa/core/layout/calcmove.cxx                      |   19 +++++++++++++++++++
 sw/qa/core/layout/data/ignore-top-margin-table.docx |binary
 sw/source/core/layout/calcmove.cxx                  |    6 +++---
 3 files changed, 22 insertions(+), 3 deletions(-)

New commits:
commit 07c93a65d2c8579adb100b7ae0c312e1872a1f37
Author:     Miklos Vajna <vmik...@collabora.com>
AuthorDate: Tue May 14 13:49:51 2024 +0200
Commit:     Caolán McNamara <caolan.mcnam...@collabora.com>
CommitDate: Wed May 15 13:45:49 2024 +0200

    tdf#160952 sw: ignore top margin only at page top, not e.g. table top
    
    The bugdoc has a table at the top of the 2nd page and we ignored the top
    margin inside the table cell (for the first paragraph), while this
    doesn't happen in Word.
    
    As mentioned at
    
<https://gerrit.libreoffice.org/c/core/+/167221/3#message-c03abf8e8e3cd20d49006058e6b3eb130585ff8f>,
    the old code assumed "top of the page" for all frames not having a
    previous frame, while that code was only tested with text frames
    directly in the body frame of a page frame.
    
    Fix the problem by limiting this "collapse upper spacing" behavior to
    frames directly in body frames. This keeps the old bugdoc working, but
    is meant to restore the old, wanted behavior in other cases like e.g. in
    table cells.
    
    If later it's discovered that upper spacing collapsing is wanted in
    other contexts, those are best added on a case by case basis.
    
    (cherry picked from commit 6025ac371bd5cd07c0af550d78db323ad394173b)
    
    Change-Id: Ieb93facd8b2e7f6412fd20873c10ce6c8b775619
    Reviewed-on: https://gerrit.libreoffice.org/c/core/+/167690
    Tested-by: Jenkins CollaboraOffice <jenkinscollaboraoff...@gmail.com>
    Tested-by: Caolán McNamara <caolan.mcnam...@collabora.com>
    Reviewed-by: Caolán McNamara <caolan.mcnam...@collabora.com>

diff --git a/sw/qa/core/layout/calcmove.cxx b/sw/qa/core/layout/calcmove.cxx
index 3e4deec52ae8..a44dc1256b83 100644
--- a/sw/qa/core/layout/calcmove.cxx
+++ b/sw/qa/core/layout/calcmove.cxx
@@ -39,6 +39,25 @@ CPPUNIT_TEST_FIXTURE(Test, testIgnoreTopMargin)
     // i.e. the top margin in the first para of a non-first page wasn't 
ignored, like in Word.
     CPPUNIT_ASSERT_EQUAL(static_cast<sal_Int32>(0), nParaTopMargin);
 }
+
+CPPUNIT_TEST_FIXTURE(Test, testIgnoreTopMarginTable)
+{
+    // Given a DOCX (>= Word 2013) file, with 2 pages:
+    // When loading that document:
+    createSwDoc("ignore-top-margin-table.docx");
+
+    // Then make sure that the paragraph on the 2nd page in B1 has a top 
margin:
+    xmlDocUniquePtr pXmlDoc = parseLayoutDump();
+    sal_Int32 nParaTopMargin
+        = getXPath(pXmlDoc, 
"/root/page[2]/body/tab/row/cell[2]/txt/infos/prtBounds"_ostr,
+                   "top"_ostr)
+              .toInt32();
+    // Without the accompanying fix in place, this test would have failed with:
+    // - Expected: 2000
+    // - Actual  : 0
+    // i.e. the top margin in B1's first paragraph was ignored, but not in 
Word.
+    CPPUNIT_ASSERT_EQUAL(static_cast<sal_Int32>(2000), nParaTopMargin);
+}
 }
 
 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
diff --git a/sw/qa/core/layout/data/ignore-top-margin-table.docx 
b/sw/qa/core/layout/data/ignore-top-margin-table.docx
new file mode 100644
index 000000000000..c82f6d63c13f
Binary files /dev/null and 
b/sw/qa/core/layout/data/ignore-top-margin-table.docx differ
diff --git a/sw/source/core/layout/calcmove.cxx 
b/sw/source/core/layout/calcmove.cxx
index 081472b98ee3..63c774c25cd6 100644
--- a/sw/source/core/layout/calcmove.cxx
+++ b/sw/source/core/layout/calcmove.cxx
@@ -1092,9 +1092,9 @@ bool SwFrame::IsCollapseUpper() const
         return false;
     }
 
-    // Word >= 2013 style: when we're at the top of the page, but not on the 
first page, then ignore
-    // the upper margin for paragraphs.
-    if (GetPrev())
+    // Word >= 2013 style: when we're at the top of the page's body, but not 
on the first page, then
+    // ignore the upper margin for paragraphs.
+    if (GetPrev() || !GetUpper() || !GetUpper()->IsBodyFrame())
     {
         return false;
     }

Reply via email to