sw/qa/extras/ooxmlexport/data/tdf163894_from_top.docx |binary
 sw/qa/extras/ooxmlexport/ooxmlexport14.cxx            |   27 +++++++++
 sw/source/core/fields/reffld.cxx                      |   50 ++++++++++++++----
 3 files changed, 67 insertions(+), 10 deletions(-)

New commits:
commit c29c1fd9c2c079147cf8c3d07e7bed0ea9434a06
Author:     László Németh <[email protected]>
AuthorDate: Sat Feb 21 00:49:57 2026 +0100
Commit:     László Németh <[email protected]>
CommitDate: Sat Feb 21 10:43:47 2026 +0100

    tdf#163894 sw DOCX: fix char style-ref at multi-page paragraphs
    
    Fix page -> previous pages -> next pages search order for
    pages with paragraphs which overlaps adjacent pages.
    
    Follow-up to commit da88bd54eba4bb3c5a588c211d0233a8eef77ffb
    "tdf#163894 sw DOCX: (p)refer char style-ref on the actual page",
    commit fce6492756e4657008ed1ce734a08929aa19c264
    "tdf#163894 sw DOCX: add character styles to style-ref window",
    commit 955f0f9b5e7f1d4ba42eb314478cc6924b4b63a7
    "tdf#163894 sw DOCX: fix style-ref with character style",
    commit d4fdafa103bfea94a279d7069ddc50ba92f67d01 "tdf#160402
    writerfilter,sw: STYLEREF field can refer to character style"
    and commit 32c588dd1164aa2fc4c8120ddb74bd510cc082f9
    "tdf#86790: Add support for a word-style styleref".
    
    Change-Id: I8faf292a699c58e0c38b4e59c021e36c795b5f07
    Reviewed-on: https://gerrit.libreoffice.org/c/core/+/199911
    Tested-by: Jenkins
    Reviewed-by: László Németh <[email protected]>

diff --git a/sw/qa/extras/ooxmlexport/data/tdf163894_from_top.docx 
b/sw/qa/extras/ooxmlexport/data/tdf163894_from_top.docx
new file mode 100644
index 000000000000..8c8ac402ffd8
Binary files /dev/null and 
b/sw/qa/extras/ooxmlexport/data/tdf163894_from_top.docx differ
diff --git a/sw/qa/extras/ooxmlexport/ooxmlexport14.cxx 
b/sw/qa/extras/ooxmlexport/ooxmlexport14.cxx
index 36d7d3a1209f..846e2dca8d13 100644
--- a/sw/qa/extras/ooxmlexport/ooxmlexport14.cxx
+++ b/sw/qa/extras/ooxmlexport/ooxmlexport14.cxx
@@ -889,6 +889,33 @@ CPPUNIT_TEST_FIXTURE(Test, testTdf32363)
                 "expand", u"Hidden text with the referred character style");
 }
 
+CPPUNIT_TEST_FIXTURE(Test, 
testTdf163894_from_top_to_beginning_of_the_documentMarguerite)
+{
+    createSwDoc("tdf163894_from_top.docx");
+    save(TestFilter::DOCX);
+
+    xmlDocUniquePtr pLayout = parseLayoutDump();
+    assertXPath(pLayout, 
"/root/page[1]/header/txt[1]/SwParaPortion/SwLineLayout/SwFieldPortion[1]",
+                "expand", u"handbooks");
+    assertXPath(pLayout, 
"/root/page[1]/header/txt[1]/SwParaPortion/SwLineLayout/SwFieldPortion[2]",
+                "expand", u"infuriating");
+
+    // This was "initializes"
+    assertXPath(pLayout, 
"/root/page[2]/header/txt[1]/SwParaPortion/SwLineLayout/SwFieldPortion[1]",
+                "expand", u"infuriating");
+    assertXPath(pLayout, 
"/root/page[2]/header/txt[1]/SwParaPortion/SwLineLayout/SwFieldPortion[2]",
+                "expand", u"infuriating");
+
+    assertXPath(pLayout, 
"/root/page[3]/header/txt[1]/SwParaPortion/SwLineLayout/SwFieldPortion[1]",
+                "expand", u"initializes");
+    assertXPath(pLayout, 
"/root/page[3]/header/txt[1]/SwParaPortion/SwLineLayout/SwFieldPortion[2]",
+                "expand", u"Marguerite");
+    assertXPath(pLayout, 
"/root/page[4]/header/txt[1]/SwParaPortion/SwLineLayout/SwFieldPortion[1]",
+                "expand", u"maroon");
+    assertXPath(pLayout, 
"/root/page[4]/header/txt[1]/SwParaPortion/SwLineLayout/SwFieldPortion[2]",
+                "expand", u"modicum");
+}
+
 CPPUNIT_TEST_FIXTURE(Test, testTdf161643)
 {
     createSwDoc("fdo76163.docx");
diff --git a/sw/source/core/fields/reffld.cxx b/sw/source/core/fields/reffld.cxx
index f05c8edd93c3..8eec1c781ec4 100644
--- a/sw/source/core/fields/reffld.cxx
+++ b/sw/source/core/fields/reffld.cxx
@@ -1392,6 +1392,14 @@ namespace
                     {
                         if ( !bHasHint )
                         {
+                            // looking for the hints on the actual page, but 
the first
+                            // matching hint is after the end of the actual 
page
+                            if ( bHasEndOffset &&
+                                      TextFrameIndex(pHint->GetStart()) > 
nEndOffset )
+                            {
+                                return nullptr;
+                            }
+
                             *pStart = pHint->GetStart();
                             bHasHint = true;
                         }
@@ -1447,15 +1455,17 @@ namespace
                 SwNode* pCurrent = rNodes[nCurrent];
                 SwTextNode* pFound = SearchForStyleAnchor(pSelf, pCurrent, 
rStyleName, pStart, pEnd, nPageStartOffset,
                                 nNodeStart == nNodeEnd ? nPageEndOffset : 
TextFrameIndex(0), bCaseSensitive);
-
                 if (pFound)
                     return pFound;
+
+                nCurrent++;
             }
 
             for (; nCurrent <= nNodeEnd; ++nCurrent)
             {
                 SwNode* pCurrent = rNodes[nCurrent];
-                SwTextNode* pFound = SearchForStyleAnchor(pSelf, pCurrent, 
rStyleName, pStart, pEnd, TextFrameIndex(0), TextFrameIndex(0), bCaseSensitive);
+                SwTextNode* pFound = SearchForStyleAnchor(pSelf, pCurrent, 
rStyleName, pStart, pEnd, TextFrameIndex(0),
+                                nCurrent == nNodeEnd ? nPageEndOffset : 
TextFrameIndex(0), bCaseSensitive);
                 if (pFound)
                     return pFound;
             }
@@ -1468,7 +1478,10 @@ namespace
             if ( nPageEndOffset != TextFrameIndex(0) )
             {
                 SwNode* pCurrent = rNodes[nCurrent];
-                SwTextNode* pFound = SearchForStyleAnchor(pSelf, pCurrent, 
rStyleName, pStart, pEnd, nPageStartOffset, nPageEndOffset, bCaseSensitive, 
bSearchBackward);
+                SwTextNode* pFound = SearchForStyleAnchor(pSelf, pCurrent, 
rStyleName, pStart, pEnd,
+                                nNodeStart == nNodeEnd ? nPageStartOffset : 
TextFrameIndex(0),
+                                nPageEndOffset, bCaseSensitive, 
bSearchBackward);
+
                 if (pFound)
                     return pFound;
                 // continue with the last but one paragraph on the page
@@ -1479,7 +1492,10 @@ namespace
             for (; nCurrent >= nNodeStart; --nCurrent)
             {
                 SwNode* pCurrent = rNodes[nCurrent];
-                SwTextNode* pFound = SearchForStyleAnchor(pSelf, pCurrent, 
rStyleName, pStart, pEnd, TextFrameIndex(0), TextFrameIndex(0), bCaseSensitive, 
bSearchBackward);
+                SwTextNode* pFound = SearchForStyleAnchor(pSelf, pCurrent, 
rStyleName, pStart, pEnd,
+                                nNodeStart == nCurrent ? nPageStartOffset : 
TextFrameIndex(0),
+                                TextFrameIndex(0), bCaseSensitive, 
bSearchBackward);
+
                 if (pFound)
                     return pFound;
             }
@@ -1795,12 +1811,18 @@ SwTextNode* 
SwGetRefFieldType::FindAnchorRefStyleMarginal(SwDoc* pDoc,
         return pTextNd;
 
     // 2. Search up from the top of the page
-    pTextNd = SearchForStyleAnchor(pSelf, nodes, SwNodeOffset(0), nPageStart - 
1, /*bBackwards*/true, styleName, pStart, pEnd, nPageStartOffset, 
nPageEndOffset);
+    if ( nPageStartOffset != TextFrameIndex(0) )
+        pTextNd = SearchForStyleAnchor(pSelf, nodes, SwNodeOffset(0), 
nPageStart, /*bBackwards*/true, styleName, pStart, pEnd, TextFrameIndex(0), 
nPageStartOffset);
+    else
+        pTextNd = SearchForStyleAnchor(pSelf, nodes, SwNodeOffset(0), 
nPageStart - 1, /*bBackwards*/true, styleName, pStart, pEnd, nPageStartOffset, 
nPageEndOffset);
     if (pTextNd)
         return pTextNd;
 
     // 3. Search down from the bottom of the page
-    pTextNd = SearchForStyleAnchor(pSelf, nodes, nPageEnd + 1, nodes.Count() - 
1, /*bBackwards*/false, styleName, pStart, pEnd, nPageStartOffset, 
nPageEndOffset);
+    if ( nPageEndOffset != TextFrameIndex(0) )
+        pTextNd = SearchForStyleAnchor(pSelf, nodes, nPageEnd, nodes.Count() - 
1, /*bBackwards*/false, styleName, pStart, pEnd, nPageEndOffset);
+    else
+        pTextNd = SearchForStyleAnchor(pSelf, nodes, nPageEnd + 1, 
nodes.Count() - 1, /*bBackwards*/false, styleName, pStart, pEnd);
     if (pTextNd)
         return pTextNd;
 
@@ -1812,13 +1834,21 @@ SwTextNode* 
SwGetRefFieldType::FindAnchorRefStyleMarginal(SwDoc* pDoc,
     if (pTextNd)
         return pTextNd;
 
-    pTextNd = SearchForStyleAnchor(pSelf, nodes, SwNodeOffset(0), nPageStart - 
1, /*bBackwards*/true, styleName, pStart, pEnd, nPageStartOffset, 
nPageEndOffset,
-                                   false /* bCaseSensitive */);
+    if ( nPageStartOffset != TextFrameIndex(0) )
+        pTextNd = SearchForStyleAnchor(pSelf, nodes, SwNodeOffset(0), 
nPageStart - 1, /*bBackwards*/true, styleName, pStart, pEnd,
+                                   TextFrameIndex(0), nPageStartOffset, false 
/* bCaseSensitive */);
+    else
+        pTextNd = SearchForStyleAnchor(pSelf, nodes, SwNodeOffset(0), 
nPageStart - 1, /*bBackwards*/true, styleName, pStart, pEnd,
+                                   nPageStartOffset, nPageEndOffset, false /* 
bCaseSensitive */);
     if (pTextNd)
         return pTextNd;
 
-    pTextNd = SearchForStyleAnchor(pSelf, nodes, nPageEnd + 1, nodes.Count() - 
1, /*bBackwards*/false, styleName, pStart, pEnd, nPageStartOffset, 
nPageEndOffset,
-                                   false /* bCaseSensitive */);
+    if ( nPageEndOffset != TextFrameIndex(0) )
+        pTextNd = SearchForStyleAnchor(pSelf, nodes, nPageEnd, nodes.Count() - 
1, /*bBackwards*/false, styleName, pStart, pEnd,
+                                   nPageEndOffset, TextFrameIndex(0), false /* 
bCaseSensitive */);
+    else
+        pTextNd = SearchForStyleAnchor(pSelf, nodes, nPageEnd + 1, 
nodes.Count() - 1, /*bBackwards*/false, styleName, pStart, pEnd,
+                                   TextFrameIndex(0), TextFrameIndex(0), false 
/* bCaseSensitive */);
     return pTextNd;
 }
 

Reply via email to