tdf#39828 sw: fix table formula calculation at track changes

Tracked deleted cell content, e.g. removed digits
of the numbers were used in calculation instead of
the actual table content. (For example, if the cell
content changed from "1" to "2", tracked cell
text is a crossed out "1" and a plain "2", and
Writer calculated with "12" instead of "2".)

Change-Id: Iecc91585cd37be3647d85f97ba1716a5fc836242
Reviewed-on: https://gerrit.libreoffice.org/c/core/+/122393
Tested-by: László Németh <nemeth@numbertext.org>
Reviewed-by: László Németh <nemeth@numbertext.org>
diff --git a/sw/qa/extras/uiwriter/data/tdf39828.fodt b/sw/qa/extras/uiwriter/data/tdf39828.fodt
new file mode 100644
index 0000000..b32ca53
--- /dev/null
+++ b/sw/qa/extras/uiwriter/data/tdf39828.fodt
@@ -0,0 +1,57 @@
<?xml version="1.0" encoding="UTF-8"?>

<office:document 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:dom="http://www.w3.org/2001/xml-events" xmlns:form="urn:oasis:names:tc:opendocument:xmlns:form:1.0" xmlns:xforms="http://www.w3.org/2002/xforms" xmlns:field="urn:openoffice:names:experimental:ooo-ms-interop:xmlns:field:1.0" xmlns:loext="urn:org:documentfoundation:names:experimental:office:xmlns:loext:1.0" xmlns:script="urn:oasis:names:tc:opendocument:xmlns:script:1.0" xmlns:drawooo="http://openoffice.org/2010/draw" xmlns:math="http://www.w3.org/1998/Math/MathML" xmlns:tableooo="http://openoffice.org/2009/table" xmlns:of="urn:oasis:names:tc:opendocument:xmlns:of:1.2" xmlns:oooc="http://openoffice.org/2004/calc" xmlns:ooow="http://openoffice.org/2004/writer" xmlns:calcext="urn:org:documentfoundation:names:experimental:calc:xmlns:calcext:1.0" xmlns:config="urn:oasis:names:tc:opendocument:xmlns:config:1.0" xmlns:office="urn:oasis:names:tc:opendocument:xmlns:office:1.0" xmlns:xlink="http://www.w3.org/1999/xlink" xmlns:fo="urn:oasis:names:tc:opendocument:xmlns:xsl-fo-compatible:1.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:ooo="http://openoffice.org/2004/office" xmlns:chart="urn:oasis:names:tc:opendocument:xmlns:chart:1.0" xmlns:meta="urn:oasis:names:tc:opendocument:xmlns:meta:1.0" xmlns:style="urn:oasis:names:tc:opendocument:xmlns:style:1.0" xmlns:text="urn:oasis:names:tc:opendocument:xmlns:text:1.0" xmlns:rpt="http://openoffice.org/2005/report" xmlns:draw="urn:oasis:names:tc:opendocument:xmlns:drawing:1.0" xmlns:officeooo="http://openoffice.org/2009/office" xmlns:dr3d="urn:oasis:names:tc:opendocument:xmlns:dr3d:1.0" xmlns:svg="urn:oasis:names:tc:opendocument:xmlns:svg-compatible:1.0" xmlns:table="urn:oasis:names:tc:opendocument:xmlns:table:1.0" xmlns:number="urn:oasis:names:tc:opendocument:xmlns:datastyle:1.0" office:version="1.3" office:mimetype="application/vnd.oasis.opendocument.text">
 <office:body>
  <office:text>
   <text:tracked-changes text:track-changes="false">
    <text:changed-region xml:id="ct94678778467312" text:id="ct94678778467312">
     <text:deletion>
      <office:change-info>
       <dc:creator>xxx4</dc:creator>
       <dc:date>2021-09-21T14:41:20</dc:date>
      </office:change-info>
     </text:deletion>
    </text:changed-region>
    <text:changed-region xml:id="ct94678778469008" text:id="ct94678778469008">
     <text:insertion>
      <office:change-info>
       <dc:creator>xxx4</dc:creator>
       <dc:date>2021-09-21T14:41:20</dc:date>
      </office:change-info>
     </text:insertion>
    </text:changed-region>
   </text:tracked-changes>
   <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:table-row table:style-name="TableLine94678778446544">
     <table:table-cell table:style-name="TableBox94678778448640" office:value-type="string">
      <text:p text:style-name="Standard"><text:change-start text:change-id="ct94678778467312"/>1<text:change-end text:change-id="ct94678778467312"/><text:change-start text:change-id="ct94678778469008"/><text:span text:style-name="T1">2</text:span><text:change-end text:change-id="ct94678778469008"/></text:p>
     </table:table-cell>
    </table:table-row>
    <table:table-row table:style-name="TableLine94678778490512">
     <table:table-cell table:style-name="TableBox94678778448640" office:value-type="string">
      <text:p text:style-name="Standard">2</text:p>
     </table:table-cell>
    </table:table-row>
    <table:table-row table:style-name="TableLine94678778493872">
     <table:table-cell table:style-name="TableBox94678778448640" office:value-type="string">
      <text:p text:style-name="Standard"><text:table-formula text:formula="ooow: &lt;A1&gt;+&lt;A2&gt;" style:data-style-name="N0">4</text:table-formula></text:p>
     </table:table-cell>
    </table:table-row>
    <table:table-row table:style-name="TableLine94678778453968">
     <table:table-cell table:style-name="TableBox94678778448640" office:value-type="string">
      <text:p text:style-name="Standard"><text:table-formula text:formula="ooow:SUM(&lt;A1:A3&gt;)" style:data-style-name="N0">8</text:table-formula></text:p>
     </table:table-cell>
    </table:table-row>
   </table:table>
   <text:p text:style-name="Standard"/>
  </office:text>
 </office:body>
</office:document>
diff --git a/sw/qa/extras/uiwriter/uiwriter3.cxx b/sw/qa/extras/uiwriter/uiwriter3.cxx
index 19ebe1e..cc9f2f3 100644
--- a/sw/qa/extras/uiwriter/uiwriter3.cxx
+++ b/sw/qa/extras/uiwriter/uiwriter3.cxx
@@ -2089,6 +2089,40 @@ CPPUNIT_TEST_FIXTURE(SwUiWriterTest3, testTdf87199)
    CPPUNIT_ASSERT(xCellA1->getString().endsWith("test1"));
}

CPPUNIT_TEST_FIXTURE(SwUiWriterTest3, testTdf39828)
{
    SwDoc* pDoc = createSwDoc(DATA_DIRECTORY, "tdf39828.fodt");
    SwWrtShell* pWrtShell = pDoc->GetDocShell()->GetWrtShell();

    pWrtShell->GotoNextTOXBase();

    // show changes
    pDoc->getIDocumentRedlineAccess().SetRedlineFlags(RedlineFlags::ShowDelete
                                                      | RedlineFlags::ShowInsert);
    CPPUNIT_ASSERT_MESSAGE("redlining should be off",
                           !pDoc->getIDocumentRedlineAccess().IsRedlineOn());
    CPPUNIT_ASSERT_MESSAGE(
        "redlines should be visible",
        IDocumentRedlineAccess::IsShowChanges(pDoc->getIDocumentRedlineAccess().GetRedlineFlags()));

    uno::Reference<text::XTextTablesSupplier> xTextTablesSupplier(mxComponent, uno::UNO_QUERY);
    uno::Reference<container::XIndexAccess> xIndexAccess(xTextTablesSupplier->getTextTables(),
                                                         uno::UNO_QUERY);
    uno::Reference<text::XTextTable> xTextTable(xIndexAccess->getByIndex(0), uno::UNO_QUERY);

    CPPUNIT_ASSERT_EQUAL(sal_Int32(4), xTextTable->getRows()->getCount());

    uno::Reference<text::XTextRange> xCellA1(xTextTable->getCellByName("A1"), uno::UNO_QUERY);
    // deleted "1", inserted "2"
    CPPUNIT_ASSERT_EQUAL(OUString("12"), xCellA1->getString());
    uno::Reference<text::XTextRange> xCellA3(xTextTable->getCellByName("A3"), uno::UNO_QUERY);
    // This was 14 (bad sum: 2 + A1, where A1 was 12 instead of the correct 2)
    CPPUNIT_ASSERT_EQUAL(OUString("4"), xCellA3->getString());
    uno::Reference<text::XTextRange> xCellA4(xTextTable->getCellByName("A4"), uno::UNO_QUERY);
    // This was 28 (bad sum: 2 + A1 + A3, where A1 was 12 and A3 was 14)
    CPPUNIT_ASSERT_EQUAL(OUString("8"), xCellA4->getString());
}

CPPUNIT_TEST_FIXTURE(SwUiWriterTest3, testTdf132603)
{
    createSwDoc();
diff --git a/sw/source/core/fields/cellfml.cxx b/sw/source/core/fields/cellfml.cxx
index 4357f26..9babd54 100644
--- a/sw/source/core/fields/cellfml.cxx
+++ b/sw/source/core/fields/cellfml.cxx
@@ -151,6 +151,15 @@ double SwTableBox::GetValue( SwTableCalcPara& rCalcPara ) const

        sal_Int32 nSttPos = 0;
        OUString sText = pTextNd->GetText();

        // use text of the tracked changes
        if ( sText != pTextNd->GetRedlineText() &&
            sText.getLength() > 0 &&
            sText[0] != CH_TXTATR_BREAKWORD && sText[0] != CH_TXTATR_INWORD )
        {
            sText = pTextNd->GetRedlineText();
        }

        while ( nSttPos < sText.getLength() && ( sText[nSttPos]==' ' || sText[nSttPos]=='\t' ) )
            ++nSttPos;