sw/qa/extras/uiwriter/data/variable-field-table-row-split-header.fodt |  270 
++++++++++
 sw/qa/extras/uiwriter/uiwriter3.cxx                                   |   73 ++
 sw/source/core/doc/DocumentFieldsManager.cxx                          |   25 
 sw/source/core/doc/docfld.cxx                                         |   45 +
 sw/source/core/docnode/node2lay.cxx                                   |   77 ++
 sw/source/core/fields/expfld.cxx                                      |    3 
 sw/source/core/inc/docfld.hxx                                         |   11 
 sw/source/core/inc/node2lay.hxx                                       |    6 
 8 files changed, 477 insertions(+), 33 deletions(-)

New commits:
commit 9dc6e2c9062725ef1f9d7e321cae5f4dbe8ca749
Author:     Michael Stahl <michael.st...@allotropia.de>
AuthorDate: Tue Apr 5 18:27:35 2022 +0200
Commit:     Michael Stahl <michael.st...@allotropia.de>
CommitDate: Wed Apr 6 19:51:19 2022 +0200

    sw: fix expansion of SetGetExpField in headers with split table rows
    
    The problem is that a get field in a header on page N may calculate and
    show values that do not take into account a set field on page N-1.
    
    This happens if a table row with multiple columns is split across the
    pages: SwGetExpField::ChangeExpansion() calls GetBodyTextNode(), which
    returns the first node in the first column that is on page N, but in the
    SwNodes array this node is *before* any node in columns 2..M, any of
    which may be on page N-1 in the layout.
    
    So try to fix this by adding a page number to SetGetExpField and using
    that as the highest priority in operator<().
    
    This is a bit risky because some of the places that create
    SetGetExpField don't have a frame to get the page number from; try to
    adapt all that call into MakeFieldList(), while leaving unrelated ones
    such as in MakeSetList() unchanged.
    
    Change-Id: Ied2a897ad34f0faf1ef3d50baad07b23fafd49bd
    Reviewed-on: https://gerrit.libreoffice.org/c/core/+/132641
    Tested-by: Jenkins
    Reviewed-by: Michael Stahl <michael.st...@allotropia.de>

diff --git 
a/sw/qa/extras/uiwriter/data/variable-field-table-row-split-header.fodt 
b/sw/qa/extras/uiwriter/data/variable-field-table-row-split-header.fodt
new file mode 100644
index 000000000000..9d3668b6b2ee
--- /dev/null
+++ b/sw/qa/extras/uiwriter/data/variable-field-table-row-split-header.fodt
@@ -0,0 +1,270 @@
+<?xml version='1.0' encoding='UTF-8'?>
+<office:document xmlns:officeooo="http://openoffice.org/2009/office"; 
xmlns:css3t="http://www.w3.org/TR/css3-text/"; 
xmlns:grddl="http://www.w3.org/2003/g/data-view#"; 
xmlns:xhtml="http://www.w3.org/1999/xhtml"; 
xmlns:formx="urn:openoffice:names:experimental:ooxml-odf-interop:xmlns:form:1.0"
 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"; 
xmlns:xsd="http://www.w3.org/2001/XMLSchema"; 
xmlns:chart="urn:oasis:names:tc:opendocument:xmlns:chart:1.0" 
xmlns:svg="urn:oasis:names:tc:opendocument:xmlns:svg-compatible:1.0" 
xmlns:text="urn:oasis:names:tc:opendocument:xmlns:text:1.0" 
xmlns:oooc="http://openoffice.org/2004/calc"; 
xmlns:style="urn:oasis:names:tc:opendocument:xmlns:style:1.0" 
xmlns:ooow="http://openoffice.org/2004/writer"; 
xmlns:meta="urn:oasis:names:tc:opendocument:xmlns:meta:1.0" 
xmlns:dc="http://purl.org/dc/elements/1.1/"; 
xmlns:rpt="http://openoffice.org/2005/report"; 
xmlns:draw="urn:oasis:names:tc:opendocument:xmlns:drawing:1.0" 
xmlns:config="urn:oasis:names:tc:opendocument:xmlns
 :config:1.0" xmlns:xlink="http://www.w3.org/1999/xlink"; 
xmlns:fo="urn:oasis:names:tc:opendocument:xmlns:xsl-fo-compatible:1.0" 
xmlns:ooo="http://openoffice.org/2004/office"; 
xmlns:office="urn:oasis:names:tc:opendocument:xmlns:office:1.0" 
xmlns:dr3d="urn:oasis:names:tc:opendocument:xmlns:dr3d:1.0" 
xmlns:table="urn:oasis:names:tc:opendocument:xmlns:table:1.0" 
xmlns:number="urn:oasis:names:tc:opendocument:xmlns:datastyle:1.0" 
xmlns:of="urn:oasis:names:tc:opendocument:xmlns:of:1.2" 
xmlns:calcext="urn:org:documentfoundation:names:experimental:calc:xmlns:calcext:1.0"
 xmlns:tableooo="http://openoffice.org/2009/table"; 
xmlns:drawooo="http://openoffice.org/2010/draw"; 
xmlns:loext="urn:org:documentfoundation:names:experimental:office:xmlns:loext:1.0"
 xmlns:dom="http://www.w3.org/2001/xml-events"; 
xmlns:field="urn:openoffice:names:experimental:ooo-ms-interop:xmlns:field:1.0" 
xmlns:math="http://www.w3.org/1998/Math/MathML"; 
xmlns:form="urn:oasis:names:tc:opendocument:xmlns:form:1.0" xmlns:script="ur
 n:oasis:names:tc:opendocument:xmlns:script:1.0" 
xmlns:xforms="http://www.w3.org/2002/xforms"; office:version="1.3" 
office:mimetype="application/vnd.oasis.opendocument.text">
+ 
<office:meta><meta:creation-date>2022-04-06T17:07:05.947362063</meta:creation-date><dc:date>2022-04-06T17:17:45.832216066</dc:date><meta:editing-duration>PT10M42S</meta:editing-duration><meta:editing-cycles>2</meta:editing-cycles><meta:generator>LibreOfficeDev/7.4.0.0.alpha0$Linux_X86_64
 
LibreOffice_project/a5b31508f860f07ec86cda3cf992fcea2bee68ef</meta:generator><meta:document-statistic
 meta:table-count="1" meta:image-count="0" meta:object-count="0" 
meta:page-count="5" meta:paragraph-count="7" meta:word-count="7" 
meta:character-count="15" 
meta:non-whitespace-character-count="15"/></office:meta>
+ <office:font-face-decls>
+  <style:font-face style:name="Liberation Serif" svg:font-family="'Liberation 
Serif'" style:font-family-generic="roman" style:font-pitch="variable"/>
+  <style:font-face style:name="Lohit Devanagari1" svg:font-family="'Lohit 
Devanagari'" style:font-family-generic="system" style:font-pitch="variable"/>
+  <style:font-face style:name="Source Han Serif CN" svg:font-family="'Source 
Han Serif CN'" style:font-family-generic="system" style:font-pitch="variable"/>
+ </office:font-face-decls>
+ <office:styles>
+  <draw:gradient draw:name="gradient" draw:style="linear" 
draw:start-color="#000000" draw:end-color="#ffffff" draw:start-intensity="100%" 
draw:end-intensity="100%" draw:angle="0deg" draw:border="0%"/>
+  <draw:hatch draw:name="hatch" draw:style="single" draw:color="#3465a4" 
draw:distance="0.02cm" draw:rotation="0"/>
+  <style:default-style style:family="graphic">
+   <style:graphic-properties svg:stroke-color="#3465a4" 
draw:fill-color="#729fcf" fo:wrap-option="no-wrap" draw:shadow-offset-x="0.3cm" 
draw:shadow-offset-y="0.3cm" draw:start-line-spacing-horizontal="0.283cm" 
draw:start-line-spacing-vertical="0.283cm" 
draw:end-line-spacing-horizontal="0.283cm" 
draw:end-line-spacing-vertical="0.283cm" style:flow-with-text="false"/>
+   <style:paragraph-properties style:text-autospace="ideograph-alpha" 
style:line-break="strict" style:writing-mode="lr-tb" 
style:font-independent-line-spacing="false">
+    <style:tab-stops/>
+   </style:paragraph-properties>
+   <style:text-properties style:use-window-font-color="true" 
loext:opacity="0%" loext:color-lum-mod="100%" loext:color-lum-off="0%" 
style:font-name="Liberation Serif" fo:font-size="12pt" fo:language="de" 
fo:country="DE" style:letter-kerning="true" style:font-name-asian="Source Han 
Serif CN" style:font-size-asian="10.5pt" style:language-asian="zh" 
style:country-asian="CN" style:font-name-complex="Lohit Devanagari1" 
style:font-size-complex="12pt" style:language-complex="hi" 
style:country-complex="IN"/>
+  </style:default-style>
+  <style:default-style style:family="paragraph">
+   <style:paragraph-properties fo:orphans="2" fo:widows="2" 
fo:hyphenation-ladder-count="no-limit" style:text-autospace="ideograph-alpha" 
style:punctuation-wrap="hanging" style:line-break="strict" 
style:tab-stop-distance="1.251cm" style:writing-mode="page"/>
+   <style:text-properties style:use-window-font-color="true" 
loext:opacity="0%" style:font-name="Liberation Serif" fo:font-size="12pt" 
fo:language="de" fo:country="DE" style:letter-kerning="true" 
style:font-name-asian="Source Han Serif CN" style:font-size-asian="10.5pt" 
style:language-asian="zh" style:country-asian="CN" 
style:font-name-complex="Lohit Devanagari1" style:font-size-complex="12pt" 
style:language-complex="hi" style:country-complex="IN" fo:hyphenate="false" 
fo:hyphenation-remain-char-count="2" fo:hyphenation-push-char-count="2" 
loext:hyphenation-no-caps="false"/>
+  </style:default-style>
+  <style:default-style style:family="table">
+   <style:table-properties table:border-model="collapsing"/>
+  </style:default-style>
+  <style:default-style style:family="table-row">
+   <style:table-row-properties fo:keep-together="auto"/>
+  </style:default-style>
+  <style:style style:name="Standard" style:family="paragraph" 
style:class="text"/>
+  <style:style style:name="Table_20_Contents" style:display-name="Table 
Contents" style:family="paragraph" style:parent-style-name="Standard" 
style:class="extra">
+   <style:paragraph-properties fo:orphans="0" fo:widows="0" 
text:number-lines="false" text:line-number="0"/>
+  </style:style>
+  <style:style style:name="Header_20_and_20_Footer" style:display-name="Header 
and Footer" style:family="paragraph" style:parent-style-name="Standard" 
style:class="extra">
+   <style:paragraph-properties text:number-lines="false" text:line-number="0">
+    <style:tab-stops>
+     <style:tab-stop style:position="3.249cm" style:type="center"/>
+     <style:tab-stop style:position="6.5cm" style:type="right"/>
+    </style:tab-stops>
+   </style:paragraph-properties>
+  </style:style>
+  <style:style style:name="Header" style:family="paragraph" 
style:parent-style-name="Header_20_and_20_Footer" style:class="extra">
+   <style:paragraph-properties text:number-lines="false" text:line-number="0">
+    <style:tab-stops>
+     <style:tab-stop style:position="3.249cm" style:type="center"/>
+     <style:tab-stop style:position="6.5cm" style:type="right"/>
+    </style:tab-stops>
+   </style:paragraph-properties>
+  </style:style>
+  <style:style style:name="Footer" style:family="paragraph" 
style:parent-style-name="Header_20_and_20_Footer" style:class="extra">
+   <style:paragraph-properties text:number-lines="false" text:line-number="0">
+    <style:tab-stops>
+     <style:tab-stop style:position="3.249cm" style:type="center"/>
+     <style:tab-stop style:position="6.5cm" style:type="right"/>
+    </style:tab-stops>
+   </style:paragraph-properties>
+  </style:style>
+  <text:outline-style style:name="Outline">
+   <text:outline-level-style text:level="1" style:num-format="">
+    <style:list-level-properties 
text:list-level-position-and-space-mode="label-alignment">
+     <style:list-level-label-alignment text:label-followed-by="listtab"/>
+    </style:list-level-properties>
+   </text:outline-level-style>
+   <text:outline-level-style text:level="2" style:num-format="">
+    <style:list-level-properties 
text:list-level-position-and-space-mode="label-alignment">
+     <style:list-level-label-alignment text:label-followed-by="listtab"/>
+    </style:list-level-properties>
+   </text:outline-level-style>
+   <text:outline-level-style text:level="3" style:num-format="">
+    <style:list-level-properties 
text:list-level-position-and-space-mode="label-alignment">
+     <style:list-level-label-alignment text:label-followed-by="listtab"/>
+    </style:list-level-properties>
+   </text:outline-level-style>
+   <text:outline-level-style text:level="4" style:num-format="">
+    <style:list-level-properties 
text:list-level-position-and-space-mode="label-alignment">
+     <style:list-level-label-alignment text:label-followed-by="listtab"/>
+    </style:list-level-properties>
+   </text:outline-level-style>
+   <text:outline-level-style text:level="5" style:num-format="">
+    <style:list-level-properties 
text:list-level-position-and-space-mode="label-alignment">
+     <style:list-level-label-alignment text:label-followed-by="listtab"/>
+    </style:list-level-properties>
+   </text:outline-level-style>
+   <text:outline-level-style text:level="6" style:num-format="">
+    <style:list-level-properties 
text:list-level-position-and-space-mode="label-alignment">
+     <style:list-level-label-alignment text:label-followed-by="listtab"/>
+    </style:list-level-properties>
+   </text:outline-level-style>
+   <text:outline-level-style text:level="7" style:num-format="">
+    <style:list-level-properties 
text:list-level-position-and-space-mode="label-alignment">
+     <style:list-level-label-alignment text:label-followed-by="listtab"/>
+    </style:list-level-properties>
+   </text:outline-level-style>
+   <text:outline-level-style text:level="8" style:num-format="">
+    <style:list-level-properties 
text:list-level-position-and-space-mode="label-alignment">
+     <style:list-level-label-alignment text:label-followed-by="listtab"/>
+    </style:list-level-properties>
+   </text:outline-level-style>
+   <text:outline-level-style text:level="9" style:num-format="">
+    <style:list-level-properties 
text:list-level-position-and-space-mode="label-alignment">
+     <style:list-level-label-alignment text:label-followed-by="listtab"/>
+    </style:list-level-properties>
+   </text:outline-level-style>
+   <text:outline-level-style text:level="10" style:num-format="">
+    <style:list-level-properties 
text:list-level-position-and-space-mode="label-alignment">
+     <style:list-level-label-alignment text:label-followed-by="listtab"/>
+    </style:list-level-properties>
+   </text:outline-level-style>
+  </text:outline-style>
+  <text:notes-configuration text:note-class="footnote" style:num-format="1" 
text:start-value="0" text:footnotes-position="page" 
text:start-numbering-at="document"/>
+  <text:notes-configuration text:note-class="endnote" style:num-format="i" 
text:start-value="0"/>
+  <text:linenumbering-configuration text:number-lines="false" 
text:offset="0.499cm" style:num-format="1" text:number-position="left" 
text:increment="5"/>
+ </office:styles>
+ <office:automatic-styles>
+  <style:style style:name="Table1" style:family="table">
+   <style:table-properties style:width="6.5cm" table:align="margins"/>
+  </style:style>
+  <style:style style:name="Table1.A" style:family="table-column">
+   <style:table-column-properties style:column-width="3.251cm" 
style:rel-column-width="32767*"/>
+  </style:style>
+  <style:style style:name="Table1.A1" style:family="table-cell">
+   <style:table-cell-properties fo:padding="0.097cm" fo:border-left="0.5pt 
solid #000000" fo:border-right="none" fo:border-top="0.5pt solid #000000" 
fo:border-bottom="0.5pt solid #000000"/>
+  </style:style>
+  <style:style style:name="Table1.B1" style:family="table-cell">
+   <style:table-cell-properties fo:padding="0.097cm" fo:border="0.5pt solid 
#000000"/>
+  </style:style>
+  <style:style style:name="Table1.A2" style:family="table-cell">
+   <style:table-cell-properties fo:padding="0.097cm" fo:border-left="0.5pt 
solid #000000" fo:border-right="none" fo:border-top="none" 
fo:border-bottom="0.5pt solid #000000"/>
+  </style:style>
+  <style:style style:name="Table1.B2" style:family="table-cell">
+   <style:table-cell-properties fo:padding="0.097cm" fo:border-left="0.5pt 
solid #000000" fo:border-right="0.5pt solid #000000" fo:border-top="none" 
fo:border-bottom="0.5pt solid #000000"/>
+  </style:style>
+  <style:style style:name="P1" style:family="paragraph" 
style:parent-style-name="Standard">
+   <style:paragraph-properties fo:break-before="page"/>
+  </style:style>
+  <style:style style:name="P2" style:family="paragraph" 
style:parent-style-name="Table_20_Contents">
+   <style:text-properties/>
+  </style:style>
+  <style:page-layout style:name="pm1">
+   <style:page-layout-properties fo:page-width="10.5cm" 
fo:page-height="14.801cm" style:num-format="1" 
style:print-orientation="portrait" fo:margin-top="2cm" fo:margin-bottom="2cm" 
fo:margin-left="2cm" fo:margin-right="2cm" style:writing-mode="lr-tb" 
style:footnote-max-height="0cm" loext:margin-gutter="0cm">
+    <style:footnote-sep style:width="0.018cm" 
style:distance-before-sep="0.101cm" style:distance-after-sep="0.101cm" 
style:line-style="solid" style:adjustment="left" style:rel-width="25%" 
style:color="#000000"/>
+   </style:page-layout-properties>
+   <style:header-style>
+    <style:header-footer-properties fo:min-height="0cm" 
fo:margin-bottom="0.499cm" fo:background-color="transparent" draw:fill="none"/>
+   </style:header-style>
+   <style:footer-style>
+    <style:header-footer-properties fo:min-height="0cm" 
fo:margin-top="0.499cm" fo:background-color="transparent" draw:fill="none"/>
+   </style:footer-style>
+  </style:page-layout>
+  <number:number-style style:name="N0">
+   <number:number number:min-integer-digits="1"/>
+  </number:number-style>
+ </office:automatic-styles>
+ <office:master-styles>
+  <style:master-page style:name="Standard" style:page-layout-name="pm1">
+   <style:header>
+    <text:p text:style-name="Header"><text:variable-get 
text:name="CARRYOVER_VALUE" 
style:data-style-name="N0">4</text:variable-get></text:p>
+   </style:header>
+   <style:footer>
+    <text:p text:style-name="Footer"><text:variable-get 
text:name="CARRYOVER_VALUE" 
style:data-style-name="N0">4</text:variable-get></text:p>
+   </style:footer>
+  </style:master-page>
+ </office:master-styles>
+ <office:body>
+  <office:text text:use-soft-page-breaks="true">
+   <text:variable-decls>
+    <text:variable-decl office:value-type="float" text:name="CARRYOVER_VALUE"/>
+   </text:variable-decls>
+   <text:sequence-decls>
+    <text:sequence-decl text:display-outline-level="0" 
text:name="Illustration"/>
+    <text:sequence-decl text:display-outline-level="0" text:name="Table"/>
+    <text:sequence-decl text:display-outline-level="0" text:name="Text"/>
+    <text:sequence-decl text:display-outline-level="0" text:name="Drawing"/>
+    <text:sequence-decl text:display-outline-level="0" text:name="Figure"/>
+   </text:sequence-decls>
+   <table:table table:name="Table1" table:style-name="Table1">
+    <table:table-column table:style-name="Table1.A" 
table:number-columns-repeated="2"/>
+    <table:table-row table:style-name="TableLine213872096">
+     <table:table-cell table:style-name="Table1.A1" office:value-type="string">
+      <text:p text:style-name="P2"/>
+      <text:p text:style-name="P2"/>
+      <text:p text:style-name="P2"/>
+      <text:p text:style-name="P2"/>
+      <text:p text:style-name="P2"/>
+      <text:p text:style-name="P2"/>
+      <text:p text:style-name="P2"/>
+      <text:p text:style-name="P2"/>
+      <text:p text:style-name="P2"/>
+      <text:p text:style-name="P2"/>
+      <text:p text:style-name="P2"/>
+      <text:p text:style-name="P2"/>
+      <text:p text:style-name="P2"/>
+      <text:p text:style-name="P2"/>
+      <text:p text:style-name="P2"/>
+      <text:p text:style-name="P2"/>
+      <text:p text:style-name="P2"/>
+      <text:p text:style-name="P2"><text:soft-page-break/></text:p>
+      <text:p text:style-name="P2"/>
+      <text:p text:style-name="P2"/>
+     </table:table-cell>
+     <table:table-cell table:style-name="Table1.B1" office:value-type="string">
+      <text:p text:style-name="Table_20_Contents"><text:variable-get 
text:name="CARRYOVER_VALUE" 
style:data-style-name="N0">0</text:variable-get><text:variable-set 
text:name="CARRYOVER_VALUE" office:value-type="float" office:value="1" 
style:data-style-name="N0">1</text:variable-set><text:variable-get 
text:name="CARRYOVER_VALUE" 
style:data-style-name="N0">1</text:variable-get></text:p>
+     </table:table-cell>
+    </table:table-row>
+    <table:table-row table:style-name="TableLine213872096">
+     <table:table-cell table:style-name="Table1.A2" office:value-type="string">
+      <text:p text:style-name="Table_20_Contents"/>
+      <text:p text:style-name="Table_20_Contents"/>
+      <text:p text:style-name="Table_20_Contents"/>
+      <text:p text:style-name="Table_20_Contents"/>
+      <text:p text:style-name="Table_20_Contents"/>
+      <text:p text:style-name="Table_20_Contents"/>
+      <text:p text:style-name="Table_20_Contents"/>
+      <text:p text:style-name="Table_20_Contents"/>
+      <text:p text:style-name="Table_20_Contents"/>
+      <text:p text:style-name="Table_20_Contents"/>
+      <text:p text:style-name="Table_20_Contents"/>
+      <text:p text:style-name="Table_20_Contents"/>
+      <text:p text:style-name="Table_20_Contents"/>
+      <text:p text:style-name="Table_20_Contents"/>
+      <text:p 
text:style-name="Table_20_Contents"><text:soft-page-break/></text:p>
+      <text:p text:style-name="Table_20_Contents"/>
+      <text:p text:style-name="Table_20_Contents"/>
+      <text:p text:style-name="Table_20_Contents"/>
+      <text:p text:style-name="Table_20_Contents"/>
+     </table:table-cell>
+     <table:table-cell table:style-name="Table1.B2" office:value-type="string">
+      <text:p text:style-name="Table_20_Contents"><text:variable-get 
text:name="CARRYOVER_VALUE" 
style:data-style-name="N0">1</text:variable-get><text:variable-set 
text:name="CARRYOVER_VALUE" office:value-type="float" office:value="2" 
style:data-style-name="N0">2</text:variable-set><text:variable-get 
text:name="CARRYOVER_VALUE" 
style:data-style-name="N0">2</text:variable-get></text:p>
+     </table:table-cell>
+    </table:table-row>
+    <table:table-row table:style-name="TableLine213872096">
+     <table:table-cell table:style-name="Table1.A2" office:value-type="string">
+      <text:p text:style-name="Table_20_Contents"/>
+      <text:p text:style-name="Table_20_Contents"/>
+      <text:p text:style-name="Table_20_Contents"/>
+      <text:p text:style-name="Table_20_Contents"/>
+      <text:p text:style-name="Table_20_Contents"/>
+      <text:p text:style-name="Table_20_Contents"/>
+      <text:p text:style-name="Table_20_Contents"/>
+      <text:p text:style-name="Table_20_Contents"/>
+      <text:p text:style-name="Table_20_Contents"/>
+      <text:p text:style-name="Table_20_Contents"/>
+      <text:p text:style-name="Table_20_Contents"/>
+      <text:p text:style-name="Table_20_Contents"/>
+      <text:p 
text:style-name="Table_20_Contents"><text:soft-page-break/></text:p>
+      <text:p text:style-name="Table_20_Contents"/>
+      <text:p text:style-name="Table_20_Contents"/>
+      <text:p text:style-name="Table_20_Contents"/>
+      <text:p text:style-name="Table_20_Contents"/>
+      <text:p text:style-name="Table_20_Contents"/>
+      <text:p text:style-name="Table_20_Contents"/>
+     </table:table-cell>
+     <table:table-cell table:style-name="Table1.B2" office:value-type="string">
+      <text:p text:style-name="Table_20_Contents"><text:variable-get 
text:name="CARRYOVER_VALUE" 
style:data-style-name="N0">2</text:variable-get><text:variable-set 
text:name="CARRYOVER_VALUE" office:value-type="float" office:value="3" 
style:data-style-name="N0">3</text:variable-set><text:variable-get 
text:name="CARRYOVER_VALUE" 
style:data-style-name="N0">3</text:variable-get></text:p>
+     </table:table-cell>
+    </table:table-row>
+    <table:table-row table:style-name="TableLine213872096">
+     <table:table-cell table:style-name="Table1.A2" office:value-type="string">
+      <text:p text:style-name="Table_20_Contents"/>
+     </table:table-cell>
+     <table:table-cell table:style-name="Table1.B2" office:value-type="string">
+      <text:p text:style-name="Table_20_Contents"><text:variable-get 
text:name="CARRYOVER_VALUE" 
style:data-style-name="N0">3</text:variable-get><text:variable-set 
text:name="CARRYOVER_VALUE" office:value-type="float" office:value="4" 
style:data-style-name="N0">4</text:variable-set><text:variable-get 
text:name="CARRYOVER_VALUE" 
style:data-style-name="N0">4</text:variable-get></text:p>
+     </table:table-cell>
+    </table:table-row>
+   </table:table>
+   <text:p text:style-name="Standard"/>
+   <text:p text:style-name="Standard"/>
+   <text:p text:style-name="P1"/>
+  </office:text>
+ </office:body>
+</office:document>
\ No newline at end of file
diff --git a/sw/qa/extras/uiwriter/uiwriter3.cxx 
b/sw/qa/extras/uiwriter/uiwriter3.cxx
index 654e08ace527..b4819567138e 100644
--- a/sw/qa/extras/uiwriter/uiwriter3.cxx
+++ b/sw/qa/extras/uiwriter/uiwriter3.cxx
@@ -154,6 +154,79 @@ CPPUNIT_TEST_FIXTURE(SwUiWriterTest3, testTdf145321)
     CPPUNIT_ASSERT_EQUAL(3, getPages());
 }
 
+CPPUNIT_TEST_FIXTURE(SwUiWriterTest3, testVariableFieldTableRowSplitHeader)
+{
+    SwDoc* const pDoc = createSwDoc(DATA_DIRECTORY, 
"variable-field-table-row-split-header.fodt");
+
+    // finish layout
+    Scheduler::ProcessEventsToIdle();
+    xmlDocUniquePtr pXmlDoc = parseLayoutDump();
+
+    // the fields in cell are: variable-get variable-set variable-get
+    assertXPath(pXmlDoc, 
"/root/page[1]/body/tab/row[1]/cell[2]/txt[1]/Special", 3);
+    assertXPath(pXmlDoc, 
"/root/page[1]/body/tab/row[1]/cell[2]/txt[1]/Special[1]", "rText", "0");
+    assertXPath(pXmlDoc, 
"/root/page[1]/body/tab/row[1]/cell[2]/txt[1]/Special[2]", "rText", "1");
+    assertXPath(pXmlDoc, 
"/root/page[1]/body/tab/row[1]/cell[2]/txt[1]/Special[3]", "rText", "1");
+    assertXPath(pXmlDoc, "/root/page[1]/header/txt[1]/Special[1]", "rText", 
"0");
+    assertXPath(pXmlDoc, "/root/page[1]/footer/txt[1]/Special[1]", "rText", 
"1");
+    // here the header had shown the wrong value
+    assertXPath(pXmlDoc, 
"/root/page[2]/body/tab/row[2]/cell[2]/txt[1]/Special[1]", "rText", "1");
+    assertXPath(pXmlDoc, 
"/root/page[2]/body/tab/row[2]/cell[2]/txt[1]/Special[2]", "rText", "2");
+    assertXPath(pXmlDoc, 
"/root/page[2]/body/tab/row[2]/cell[2]/txt[1]/Special[3]", "rText", "2");
+    assertXPath(pXmlDoc, "/root/page[2]/header/txt[1]/Special[1]", "rText", 
"1");
+    assertXPath(pXmlDoc, "/root/page[2]/footer/txt[1]/Special[1]", "rText", 
"2");
+
+    assertXPath(pXmlDoc, 
"/root/page[3]/body/tab/row[2]/cell[2]/txt[1]/Special[1]", "rText", "2");
+    assertXPath(pXmlDoc, 
"/root/page[3]/body/tab/row[2]/cell[2]/txt[1]/Special[2]", "rText", "3");
+    assertXPath(pXmlDoc, 
"/root/page[3]/body/tab/row[2]/cell[2]/txt[1]/Special[3]", "rText", "3");
+    assertXPath(pXmlDoc, "/root/page[3]/header/txt[1]/Special[1]", "rText", 
"2");
+    assertXPath(pXmlDoc, "/root/page[3]/footer/txt[1]/Special[1]", "rText", 
"3");
+
+    assertXPath(pXmlDoc, 
"/root/page[4]/body/tab/row[2]/cell[2]/txt[1]/Special[1]", "rText", "3");
+    assertXPath(pXmlDoc, 
"/root/page[4]/body/tab/row[2]/cell[2]/txt[1]/Special[2]", "rText", "4");
+    assertXPath(pXmlDoc, 
"/root/page[4]/body/tab/row[2]/cell[2]/txt[1]/Special[3]", "rText", "4");
+    assertXPath(pXmlDoc, "/root/page[4]/header/txt[1]/Special[1]", "rText", 
"3");
+    assertXPath(pXmlDoc, "/root/page[4]/footer/txt[1]/Special[1]", "rText", 
"4");
+
+    assertXPath(pXmlDoc, "/root/page[5]/header/txt[1]/Special[1]", "rText", 
"4");
+    assertXPath(pXmlDoc, "/root/page[5]/footer/txt[1]/Special[1]", "rText", 
"4");
+
+    discardDumpedLayout();
+    // update and check again
+    pDoc->getIDocumentFieldsAccess().UpdateFields(true);
+    Scheduler::ProcessEventsToIdle();
+    pXmlDoc = parseLayoutDump();
+
+    // the fields in cell are: variable-get variable-set variable-get
+    assertXPath(pXmlDoc, 
"/root/page[1]/body/tab/row[1]/cell[2]/txt[1]/Special", 3);
+    assertXPath(pXmlDoc, 
"/root/page[1]/body/tab/row[1]/cell[2]/txt[1]/Special[1]", "rText", "0");
+    assertXPath(pXmlDoc, 
"/root/page[1]/body/tab/row[1]/cell[2]/txt[1]/Special[2]", "rText", "1");
+    assertXPath(pXmlDoc, 
"/root/page[1]/body/tab/row[1]/cell[2]/txt[1]/Special[3]", "rText", "1");
+    assertXPath(pXmlDoc, "/root/page[1]/header/txt[1]/Special[1]", "rText", 
"0");
+    assertXPath(pXmlDoc, "/root/page[1]/footer/txt[1]/Special[1]", "rText", 
"1");
+    // here the header had shown the wrong value
+    assertXPath(pXmlDoc, 
"/root/page[2]/body/tab/row[2]/cell[2]/txt[1]/Special[1]", "rText", "1");
+    assertXPath(pXmlDoc, 
"/root/page[2]/body/tab/row[2]/cell[2]/txt[1]/Special[2]", "rText", "2");
+    assertXPath(pXmlDoc, 
"/root/page[2]/body/tab/row[2]/cell[2]/txt[1]/Special[3]", "rText", "2");
+    assertXPath(pXmlDoc, "/root/page[2]/header/txt[1]/Special[1]", "rText", 
"1");
+    assertXPath(pXmlDoc, "/root/page[2]/footer/txt[1]/Special[1]", "rText", 
"2");
+
+    assertXPath(pXmlDoc, 
"/root/page[3]/body/tab/row[2]/cell[2]/txt[1]/Special[1]", "rText", "2");
+    assertXPath(pXmlDoc, 
"/root/page[3]/body/tab/row[2]/cell[2]/txt[1]/Special[2]", "rText", "3");
+    assertXPath(pXmlDoc, 
"/root/page[3]/body/tab/row[2]/cell[2]/txt[1]/Special[3]", "rText", "3");
+    assertXPath(pXmlDoc, "/root/page[3]/header/txt[1]/Special[1]", "rText", 
"2");
+    assertXPath(pXmlDoc, "/root/page[3]/footer/txt[1]/Special[1]", "rText", 
"3");
+
+    assertXPath(pXmlDoc, 
"/root/page[4]/body/tab/row[2]/cell[2]/txt[1]/Special[1]", "rText", "3");
+    assertXPath(pXmlDoc, 
"/root/page[4]/body/tab/row[2]/cell[2]/txt[1]/Special[2]", "rText", "4");
+    assertXPath(pXmlDoc, 
"/root/page[4]/body/tab/row[2]/cell[2]/txt[1]/Special[3]", "rText", "4");
+    assertXPath(pXmlDoc, "/root/page[4]/header/txt[1]/Special[1]", "rText", 
"3");
+    assertXPath(pXmlDoc, "/root/page[4]/footer/txt[1]/Special[1]", "rText", 
"4");
+
+    assertXPath(pXmlDoc, "/root/page[5]/header/txt[1]/Special[1]", "rText", 
"4");
+    assertXPath(pXmlDoc, "/root/page[5]/footer/txt[1]/Special[1]", "rText", 
"4");
+}
+
 CPPUNIT_TEST_FIXTURE(SwUiWriterTest3, testTdf147126)
 {
     createSwDoc(DATA_DIRECTORY, "tdf147126.docx");
diff --git a/sw/source/core/doc/DocumentFieldsManager.cxx 
b/sw/source/core/doc/DocumentFieldsManager.cxx
index e1f3b7a0d58e..bc492046572f 100644
--- a/sw/source/core/doc/DocumentFieldsManager.cxx
+++ b/sw/source/core/doc/DocumentFieldsManager.cxx
@@ -34,6 +34,7 @@
 #include <SwUndoField.hxx>
 #include <flddat.hxx>
 #include <cntfrm.hxx>
+#include <node2lay.hxx>
 #include <section.hxx>
 #include <docufld.hxx>
 #include <calbck.hxx>
@@ -724,9 +725,12 @@ void DocumentFieldsManager::UpdateTableFields( 
SfxPoolItem* pHt )
                         {
                             SwPosition aPos( *pTableNd );
                             if( GetBodyTextNode( m_rDoc, aPos, *pFrame ) )
+                            {
                                 FieldsToCalc( *pCalc, SetGetExpField(
-                                    aPos.nNode, pFormatField->GetTextField(),
-                                    &aPos.nContent), pLayout);
+                                        aPos.nNode, 
pFormatField->GetTextField(),
+                                        &aPos.nContent, 
pFrame->GetPhyPageNum()),
+                                    pLayout);
+                            }
                             else
                                 pFrame = nullptr;
                         }
@@ -735,8 +739,11 @@ void DocumentFieldsManager::UpdateTableFields( 
SfxPoolItem* pHt )
                     {
                         // create index to determine the TextNode
                         SwNodeIndex aIdx( rTextNd );
+                        SwFrame const*const pFrame2 = 
::sw::FindNeighbourFrameForNode(rTextNd);
                         FieldsToCalc( *pCalc,
-                            SetGetExpField(aIdx, pFormatField->GetTextField()),
+                            SetGetExpField(aIdx, pFormatField->GetTextField(),
+                                nullptr,
+                                pFrame2 ? pFrame2->GetPhyPageNum() : 0),
                             pLayout);
                     }
 
@@ -796,8 +803,11 @@ void DocumentFieldsManager::UpdateTableFields( 
SfxPoolItem* pHt )
                             {
                                 SwPosition aPos( *pCNd );
                                 if( GetBodyTextNode( m_rDoc, aPos, *pFrame ) )
-                                    FieldsToCalc(*pCalc, 
SetGetExpField(aPos.nNode),
-                                            pLayout);
+                                {
+                                    FieldsToCalc(*pCalc, 
SetGetExpField(aPos.nNode,
+                                            nullptr, nullptr, 
pFrame->GetPhyPageNum()),
+                                        pLayout);
+                                }
                                 else
                                     pFrame = nullptr;
                             }
@@ -807,7 +817,10 @@ void DocumentFieldsManager::UpdateTableFields( 
SfxPoolItem* pHt )
                     {
                         // create index to determine the TextNode
                         SwNodeIndex aIdx( *pTableNd );
-                        FieldsToCalc(*pCalc, SetGetExpField(aIdx), pLayout);
+                        SwFrame const*const pFrame2 = 
::sw::FindNeighbourFrameForNode(*pTableNd);
+                        FieldsToCalc(*pCalc, SetGetExpField(aIdx, nullptr, 
nullptr,
+                                pFrame2 ? pFrame2->GetPhyPageNum() : 0),
+                            pLayout);
                     }
 
                     SwTableCalcPara aPara(*pCalc, pTableNd->GetTable(), 
pLayout);
diff --git a/sw/source/core/doc/docfld.cxx b/sw/source/core/doc/docfld.cxx
index 2a10c63debc9..652af8a188b9 100644
--- a/sw/source/core/doc/docfld.cxx
+++ b/sw/source/core/doc/docfld.cxx
@@ -33,7 +33,9 @@
 #include <IDocumentMarkAccess.hxx>
 #include <IDocumentState.hxx>
 #include <IDocumentLayoutAccess.hxx>
+#include <node2lay.hxx>
 #include <cntfrm.hxx>
+#include <pagefrm.hxx>
 #include <txtfrm.hxx>
 #include <notxtfrm.hxx>
 #include <pam.hxx>
@@ -63,7 +65,9 @@ using namespace ::com::sun::star::uno;
 SetGetExpField::SetGetExpField(
     const SwNodeIndex& rNdIdx,
     const SwTextField* pField,
-    const SwIndex* pIdx )
+    const SwIndex* pIdx,
+    sal_uInt16 const nPageNumber)
+    : m_nPageNumber(nPageNumber)
 {
     m_eSetGetExpFieldType = TEXTFIELD;
     m_CNTNT.pTextField = pField;
@@ -89,7 +93,9 @@ SetGetExpField::SetGetExpField( const SwNodeIndex& rNdIdx,
 // these always have content position 0xffffffff!
 // There is never a field on this, only up to COMPLETE_STRING possible
 SetGetExpField::SetGetExpField( const SwSectionNode& rSectNd,
-                                const SwPosition* pPos )
+                                const SwPosition* pPos,
+                                sal_uInt16 const nPageNumber)
+    : m_nPageNumber(nPageNumber)
 {
     m_eSetGetExpFieldType = SECTIONNODE;
     m_CNTNT.pSection = &rSectNd.GetSection();
@@ -107,7 +113,9 @@ SetGetExpField::SetGetExpField( const SwSectionNode& 
rSectNd,
 }
 
 SetGetExpField::SetGetExpField(::sw::mark::IBookmark const& rBookmark,
-                               SwPosition const*const pPos)
+                               SwPosition const*const pPos,
+                               sal_uInt16 const nPageNumber)
+    : m_nPageNumber(nPageNumber)
 {
     m_eSetGetExpFieldType = BOOKMARK;
     m_CNTNT.pBookmark = &rBookmark;
@@ -220,6 +228,10 @@ bool SetGetExpField::operator==( const SetGetExpField& 
rField ) const
 
 bool SetGetExpField::operator<( const SetGetExpField& rField ) const
 {
+    if (m_nPageNumber != 0 && rField.m_nPageNumber != 0 && m_nPageNumber != 
rField.m_nPageNumber)
+    {
+        return m_nPageNumber < rField.m_nPageNumber;
+    }
     if( m_nNode < rField.m_nNode || ( m_nNode == rField.m_nNode && m_nContent 
< rField.m_nContent ))
         return true;
     else if( m_nNode != rField.m_nNode || m_nContent != rField.m_nContent )
@@ -1030,8 +1042,10 @@ void SwDocUpdateField::GetBodyNode( const SwTextField& 
rTField, SwFieldIds nFiel
     // always the first! (in tab headline, header-/footer)
     Point aPt;
     std::pair<Point, bool> const tmp(aPt, false);
-    const SwContentFrame* pFrame = rTextNd.getLayoutFrame(
-        rDoc.getIDocumentLayoutAccess().GetCurrentLayout(), nullptr, &tmp);
+    // need pos to get the frame on the correct page
+    SwPosition const pos(const_cast<SwTextNode&>(rTextNd), rTField.GetStart());
+    const SwFrame* pFrame = rTextNd.getLayoutFrame(
+        rDoc.getIDocumentLayoutAccess().GetCurrentLayout(), &pos, &tmp);
 
     std::unique_ptr<SetGetExpField> pNew;
     bool bIsInBody = false;
@@ -1046,8 +1060,16 @@ void SwDocUpdateField::GetBodyNode( const SwTextField& 
rTField, SwFieldIds nFiel
         // in frames whose anchor is in redline. However, we do want to update
         // fields in hidden sections. So: In order to be updated, a field 1)
         // must have a frame, or 2) it must be in the document body.
+        if (pFrame == nullptr && bIsInBody)
+        {   // try harder to get a frame for the page number
+            pFrame = ::sw::FindNeighbourFrameForNode(rTextNd);
+            // possibly there is no layout at all, happens in mail merge
+        }
         if( (pFrame != nullptr) || bIsInBody )
-            pNew.reset(new SetGetExpField( aIdx, &rTField ));
+        {
+            pNew.reset(new SetGetExpField(aIdx, &rTField, nullptr,
+                pFrame ? pFrame->GetPhyPageNum() : 0));
+        }
     }
     else
     {
@@ -1055,7 +1077,8 @@ void SwDocUpdateField::GetBodyNode( const SwTextField& 
rTField, SwFieldIds nFiel
         SwPosition aPos( rDoc.GetNodes().GetEndOfPostIts() );
         bool const bResult = GetBodyTextNode( rDoc, aPos, *pFrame );
         OSL_ENSURE(bResult, "where is the Field");
-        pNew.reset(new SetGetExpField( aPos.nNode, &rTField, &aPos.nContent ));
+        pNew.reset(new SetGetExpField(aPos.nNode, &rTField, &aPos.nContent,
+            pFrame->GetPhyPageNum()));
     }
 
     // always set the BodyTextFlag in GetExp or DB fields
@@ -1106,13 +1129,17 @@ void SwDocUpdateField::GetBodyNodeGeneric(SwNode const& 
rNode, T const& rCond)
 
             bool const bResult = GetBodyTextNode( rDoc, aPos, *pFrame );
             OSL_ENSURE(bResult, "where is the Field");
-            pNew.reset(new SetGetExpField(rCond, &aPos));
+            pNew.reset(new SetGetExpField(rCond, &aPos, 
pFrame->GetPhyPageNum()));
 
         } while( false );
     }
 
     if( !pNew )
-        pNew.reset(new SetGetExpField(rCond));
+    {
+        // try harder to get a frame for the page number
+        SwFrame const*const pFrame = ::sw::FindNeighbourFrameForNode(rNode);
+        pNew.reset(new SetGetExpField(rCond, nullptr, pFrame ? 
pFrame->GetPhyPageNum() : 0));
+    }
 
     m_pFieldSortList->insert( std::move(pNew) );
 }
diff --git a/sw/source/core/docnode/node2lay.cxx 
b/sw/source/core/docnode/node2lay.cxx
index 591fc6541f4f..5cc15c3106b5 100644
--- a/sw/source/core/docnode/node2lay.cxx
+++ b/sw/source/core/docnode/node2lay.cxx
@@ -20,6 +20,7 @@
 #include <calbck.hxx>
 #include <node.hxx>
 #include <ndindex.hxx>
+#include <pam.hxx>
 #include <swtable.hxx>
 #include <ftnfrm.hxx>
 #include <sectfrm.hxx>
@@ -57,7 +58,7 @@ public:
     SwFrame* GetFrame( const Point* pDocPos ) const;
 };
 
-static SwNode* GoNextWithFrame(const SwNodes& rNodes, SwNodeIndex *pIdx)
+static SwNode* GoNextWithFrame(const SwNodes& rNodes, SwNodeIndex *pIdx, 
SwFlowFrame const**const ppFrame)
 {
     if( pIdx->GetIndex() >= rNodes.Count() - 1 )
         return nullptr;
@@ -67,20 +68,27 @@ static SwNode* GoNextWithFrame(const SwNodes& rNodes, 
SwNodeIndex *pIdx)
     while( aTmp < rNodes.Count()-1 )
     {
         pNd = &aTmp.GetNode();
-        bool bFound = false;
+        SwFrame const* pFound(nullptr);
         if ( pNd->IsContentNode() )
             // sw_redlinehide: assume that it's OK to find a node with the same
             // frame as the caller's one
-            bFound = SwIterator<SwFrame, SwContentNode, 
sw::IteratorMode::UnwrapMulti>(*static_cast<SwContentNode*>(pNd)).First();
+            pFound = SwIterator<SwFrame, SwContentNode, 
sw::IteratorMode::UnwrapMulti>(*static_cast<SwContentNode*>(pNd)).First();
         else if ( pNd->IsTableNode() )
-            bFound = 
SwIterator<SwFrame,SwFormat>(*static_cast<SwTableNode*>(pNd)->GetTable().GetFrameFormat()).First()
 ;
+            pFound = 
SwIterator<SwFrame,SwFormat>(*static_cast<SwTableNode*>(pNd)->GetTable().GetFrameFormat()).First()
 ;
         else if( pNd->IsEndNode() && 
!pNd->StartOfSectionNode()->IsSectionNode() )
         {
             pNd = nullptr;
             break;
         }
-        if ( bFound )
-                break;
+        if (pFound != nullptr)
+        {
+            if (ppFrame)
+            {
+                *ppFrame = SwFlowFrame::CastFlowFrame(pFound);
+                assert(*ppFrame);
+            }
+            break;
+        }
         ++aTmp;
     }
 
@@ -91,7 +99,7 @@ static SwNode* GoNextWithFrame(const SwNodes& rNodes, 
SwNodeIndex *pIdx)
     return pNd;
 }
 
-static SwNode* GoPreviousWithFrame(SwNodeIndex *pIdx)
+static SwNode* GoPreviousWithFrame(SwNodeIndex *pIdx, SwFlowFrame const**const 
ppFrame)
 {
     if( !pIdx->GetIndex() )
         return nullptr;
@@ -101,20 +109,27 @@ static SwNode* GoPreviousWithFrame(SwNodeIndex *pIdx)
     while( aTmp.GetIndex() )
     {
         pNd = &aTmp.GetNode();
-        bool bFound = false;
+        SwFrame const* pFound(nullptr);
         if ( pNd->IsContentNode() )
             // sw_redlinehide: assume that it's OK to find a node with the same
             // frame as the caller's one
-            bFound = SwIterator<SwFrame, SwContentNode, 
sw::IteratorMode::UnwrapMulti>(*static_cast<SwContentNode*>(pNd)).First();
+            pFound = SwIterator<SwFrame, SwContentNode, 
sw::IteratorMode::UnwrapMulti>(*static_cast<SwContentNode*>(pNd)).First();
         else if ( pNd->IsTableNode() )
-            bFound = 
SwIterator<SwFrame,SwFormat>(*static_cast<SwTableNode*>(pNd)->GetTable().GetFrameFormat()).First();
+            pFound = 
SwIterator<SwFrame,SwFormat>(*static_cast<SwTableNode*>(pNd)->GetTable().GetFrameFormat()).First();
         else if( pNd->IsStartNode() && !pNd->IsSectionNode() )
         {
             pNd = nullptr;
             break;
         }
-        if ( bFound )
-                break;
+        if (pFound != nullptr)
+        {
+            if (ppFrame)
+            {
+                *ppFrame = SwFlowFrame::CastFlowFrame(pFound);
+                assert(*ppFrame);
+            }
+            break;
+        }
         --aTmp;
     }
 
@@ -125,6 +140,40 @@ static SwNode* GoPreviousWithFrame(SwNodeIndex *pIdx)
     return pNd;
 }
 
+namespace sw {
+
+SwFrame const* FindNeighbourFrameForNode(SwNode const& rNode)
+{
+    SwNodeIndex idx(rNode);
+    SwFlowFrame const* pFlow(nullptr);
+    if (SwNode *const pNode = GoPreviousWithFrame(&idx, &pFlow))
+    {
+        if (::CheckNodesRange(rNode, idx, true))
+        {
+            while (pFlow->HasFollow())
+            {   // try to get the one on the current page
+                pFlow = pFlow->GetFollow();
+            }
+            return &pFlow->GetFrame();
+        }
+    }
+    idx = rNode;
+    if (SwNode *const pNode = GoNextWithFrame(idx.GetNodes(), &idx, &pFlow))
+    {
+        if (::CheckNodesRange(rNode, idx, true))
+        {
+            while (pFlow->IsFollow())
+            {   // try to get the one on the current page
+                pFlow = pFlow->GetPrecede();
+            }
+            return &pFlow->GetFrame();
+        }
+    }
+    return nullptr;
+}
+
+}
+
 /**
  * The main purpose of this ctor is to find the right sw::BroadcastingModify 
to iterate over.
  *
@@ -145,7 +194,7 @@ SwNode2LayImpl::SwNode2LayImpl( const SwNode& rNode, 
SwNodeOffset nIdx, bool bSe
         if( !bSearch && rNode.GetIndex() < mnIndex )
         {
             SwNodeIndex aTmp( *rNode.EndOfSectionNode(), +1 );
-            pNd = GoPreviousWithFrame( &aTmp );
+            pNd = GoPreviousWithFrame(&aTmp, nullptr);
             if( pNd && rNode.GetIndex() > pNd->GetIndex() )
                 pNd = nullptr; // Do not go over the limits
             mbMaster = false;
@@ -153,7 +202,7 @@ SwNode2LayImpl::SwNode2LayImpl( const SwNode& rNode, 
SwNodeOffset nIdx, bool bSe
         else
         {
             SwNodeIndex aTmp( rNode, -1 );
-            pNd = GoNextWithFrame( rNode.GetNodes(), &aTmp );
+            pNd = GoNextWithFrame(rNode.GetNodes(), &aTmp, nullptr);
             mbMaster = true;
             if( !bSearch && pNd && rNode.EndOfSectionIndex() < pNd->GetIndex() 
)
                 pNd = nullptr; // Do not go over the limits
diff --git a/sw/source/core/fields/expfld.cxx b/sw/source/core/fields/expfld.cxx
index 6c5f45f3716d..6bf92cc47f0b 100644
--- a/sw/source/core/fields/expfld.cxx
+++ b/sw/source/core/fields/expfld.cxx
@@ -375,7 +375,8 @@ void SwGetExpField::ChangeExpansion( const SwFrame& rFrame, 
const SwTextField& r
 
     SwRootFrame const& rLayout(*rFrame.getRootFrame());
     OUString & rExpand(rLayout.IsHideRedlines() ? m_sExpandRLHidden : 
m_sExpand);
-    SetGetExpField aEndField( aPos.nNode, &rField, &aPos.nContent );
+    // here a page number is needed to sort correctly
+    SetGetExpField aEndField(aPos.nNode, &rField, &aPos.nContent, 
rFrame.GetPhyPageNum());
     if(GetSubType() & nsSwGetSetExpType::GSE_STRING)
     {
         SwHashTable<HashStr> aHashTable(0);
diff --git a/sw/source/core/inc/docfld.hxx b/sw/source/core/inc/docfld.hxx
index 70d6e033dc0d..117ee0408e34 100644
--- a/sw/source/core/inc/docfld.hxx
+++ b/sw/source/core/inc/docfld.hxx
@@ -44,6 +44,8 @@ enum class SwFieldIds : sal_uInt16;
 // Update expression fields
 class SetGetExpField
 {
+    // TODO: in case of multiple layouts, storing this only once isn't going 
to work (although already a problem for cached field value)
+    sal_uInt16 m_nPageNumber = 0;
     SwNodeOffset m_nNode;
     union {
         const SwTextField* pTextField;
@@ -64,15 +66,18 @@ class SetGetExpField
 
 public:
     SetGetExpField( const SwNodeIndex& rNdIdx, const SwTextField* pField = 
nullptr,
-                    const SwIndex* pIdx = nullptr );
+                    const SwIndex* pIdx = nullptr,
+                    sal_uInt16 nPageNumber = 0);
 
     SetGetExpField( const SwNodeIndex& rNdIdx, const SwTextINetFormat& rINet );
 
     SetGetExpField( const SwSectionNode& rSectNode,
-                    const SwPosition* pPos = nullptr  );
+                    const SwPosition* pPos = nullptr,
+                    sal_uInt16 nPageNumber = 0);
 
     SetGetExpField( ::sw::mark::IBookmark const& rBookmark,
-                    SwPosition const* pPos = nullptr );
+                    SwPosition const* pPos = nullptr,
+                    sal_uInt16 nPageNumber = 0);
 
     SetGetExpField( const SwTableBox& rTableBox  );
 
diff --git a/sw/source/core/inc/node2lay.hxx b/sw/source/core/inc/node2lay.hxx
index e0334c156435..0ba712f19727 100644
--- a/sw/source/core/inc/node2lay.hxx
+++ b/sw/source/core/inc/node2lay.hxx
@@ -79,6 +79,12 @@ public:
     void RestoreUpperFrames(SwNodes& rNds, SwNodeOffset nStt, SwNodeOffset 
nEnd);
 };
 
+namespace sw
+{
+SwFrame const* FindNeighbourFrameForNode(SwNode const& rNode);
+
+} // namespace sw
+
 #endif
 
 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Reply via email to