v2 tdf#134951 docxexport: don't lose postponed comment

This patch basically reverts and re-writes the original fix.

Prior to mstahl's 7.4 commit f261fae4af72b1328bc3e93fc52a9a24ce0975b3
   tdf#143039 tdf#143106 sw: DOCX export: don't export duplicate flys
ooxmlexport5's comment would only last for one or two round-trips,
and then be lost after that. It kept moving around into things
like the fly itself, or the footnote.
With mstahl's commit, it was lost immediately (same as Word 2016),
and so the ooxmlexport5 unit test that broke was commented out.

Change-Id: Ia7e881143f1243a3ab81bb94c02b57189f3730f0
Reviewed-on: https://gerrit.libreoffice.org/c/core/+/128703
Tested-by: Jenkins
Reviewed-by: Michael Stahl <michael.stahl@allotropia.de>
Reviewed-by: Justin Luth <jluth@mail.com>
diff --git a/sw/qa/extras/ooxmlexport/ooxmlexport5.cxx b/sw/qa/extras/ooxmlexport/ooxmlexport5.cxx
index 327e7e8..8decd30 100644
--- a/sw/qa/extras/ooxmlexport/ooxmlexport5.cxx
+++ b/sw/qa/extras/ooxmlexport/ooxmlexport5.cxx
@@ -130,18 +130,15 @@ CPPUNIT_TEST_FIXTURE(Test, testNoDuplicateAttributeExport)

CPPUNIT_TEST_FIXTURE(Test, testfdo79008)
{
    loadAndSave("fdo79008.docx");
    loadAndReload("fdo79008.docx");
    /* File crashing while saving in LO.
     * Check if document.xml file is created after fix
     */
    parseExport("word/document.xml");

#if 0
    // TODO: jluth says this test case needs more work
    // tdf#134951: there is only one comment
    xmlDocUniquePtr pXmlSettings = parseExport("word/comments.xml");
    assertXPath(pXmlSettings, "/w:comments/w:comment", 1);
#endif

    // Read-only is set, but it is not enforced, so it should be off...
    SwXTextDocument* pTextDoc = dynamic_cast<SwXTextDocument *>(mxComponent.get());
diff --git a/sw/source/filter/ww8/wrtw8nds.cxx b/sw/source/filter/ww8/wrtw8nds.cxx
index f092b8f..6a84f77 100644
--- a/sw/source/filter/ww8/wrtw8nds.cxx
+++ b/sw/source/filter/ww8/wrtw8nds.cxx
@@ -394,7 +394,7 @@ sal_Int32 SwWW8AttrIter::SearchNext( sal_Int32 nStartPos )
    return nMinPos;
}

void SwWW8AttrIter::OutAttr(sal_Int32 nSwPos, bool bWriteCombChars, bool bPostponeSingleUse)
void SwWW8AttrIter::OutAttr(sal_Int32 nSwPos, bool bWriteCombChars)
{
    m_rExport.AttrOutput().RTLAndCJKState( mbCharIsRTL, GetScript() );

@@ -458,13 +458,6 @@ void SwWW8AttrIter::OutAttr(sal_Int32 nSwPos, bool bWriteCombChars, bool bPostpo
                        nWhichId = aIter.NextWhich();
                    }
                }
                else if (bPostponeSingleUse &&
                         (nWhich == RES_TXTATR_FTN || nWhich == RES_TXTATR_ANNOTATION || nWhich == RES_TXTATR_FIELD))
                {
                    // Do not duplicate these multiple times when the character run is split.
                    // Skip this time - it will be attempted later.
                    // ?? also RES_TXTATR_REFMARK: RES_TXTATR_TOXMARK: RES_TXTATR_META: RES_TXTATR_METAFIELD: ??
                }
                else
                    aRangeItems[nWhich] = (&(pHt->GetAttr()));
            }
@@ -2388,6 +2381,7 @@ void MSWordExportBase::OutputTextNode( SwTextNode& rNode )

            const SwRedlineData* pRedlineData = aAttrIter.GetRunLevelRedline( nCurrentPos );
            bool bPostponeWritingText    = false ;
            bool bStartedPostponedRunProperties = false;
            OUString aSavedSnippet ;

            sal_Int32 nNextAttr = GetNextPos( &aAttrIter, rNode, nCurrentPos );
@@ -2655,8 +2649,9 @@ void MSWordExportBase::OutputTextNode( SwTextNode& rNode )
            {
                // Output the character attributes
                // #i51277# do this before writing flys at end of paragraph
                bStartedPostponedRunProperties = true;
                AttrOutput().StartRunProperties();
                aAttrIter.OutAttr(nCurrentPos, false, bPostponeWritingText);
                aAttrIter.OutAttr(nCurrentPos, false);
                AttrOutput().EndRunProperties( pRedlineData );
            }

@@ -2758,29 +2753,28 @@ void MSWordExportBase::OutputTextNode( SwTextNode& rNode )

            AttrOutput().WritePostitFieldReference();

            if (bPostponeWritingText
                && (FLY_PROCESSED == nStateOfFlyFrame || FLY_NONE == nStateOfFlyFrame))
            if (bPostponeWritingText)
            {
                AttrOutput().EndRun(&rNode, nCurrentPos, nNextAttr == nEnd);
                //write the postponed text run
                AttrOutput().StartRun( pRedlineData, nCurrentPos, bSingleEmptyRun );
                AttrOutput().SetAnchorIsLinkedToNode( false );
                AttrOutput().ResetFlyProcessingFlag();
                if (0 != nEnd)
                if (FLY_PROCESSED == nStateOfFlyFrame || FLY_NONE == nStateOfFlyFrame)
                {
                    AttrOutput().EndRun(&rNode, nCurrentPos, /*bLastRun=*/false);

                    AttrOutput().StartRun( pRedlineData, nCurrentPos, bSingleEmptyRun );
                    AttrOutput().SetAnchorIsLinkedToNode( false );
                    AttrOutput().ResetFlyProcessingFlag();
                }
                if (0 != nEnd && !bStartedPostponedRunProperties)
                {
                    AttrOutput().StartRunProperties();
                    aAttrIter.OutAttr( nCurrentPos, false );
                    AttrOutput().EndRunProperties( pRedlineData );

                    // OutAttr may have introduced new comments, so write them out now
                    AttrOutput().WritePostitFieldReference();
                }
                AttrOutput().RunText( aSavedSnippet, eChrSet );
                AttrOutput().EndRun(&rNode, nCurrentPos, nNextAttr == nEnd);
            }
            else if( bPostponeWritingText && !aSavedSnippet.isEmpty() )
            {
                //write the postponed text run
                AttrOutput().RunText( aSavedSnippet, eChrSet );
                AttrOutput().EndRun(&rNode, nCurrentPos, nNextAttr == nEnd);
            }
            else
                AttrOutput().EndRun(&rNode, nCurrentPos, nNextAttr == nEnd);

diff --git a/sw/source/filter/ww8/wrtww8.hxx b/sw/source/filter/ww8/wrtww8.hxx
index bd960da..4df6d5c 100644
--- a/sw/source/filter/ww8/wrtww8.hxx
+++ b/sw/source/filter/ww8/wrtww8.hxx
@@ -1542,7 +1542,7 @@ public:

    void NextPos() { if ( nCurrentSwPos < SAL_MAX_INT32 ) nCurrentSwPos = SearchNext( nCurrentSwPos + 1 ); }

    void OutAttr(sal_Int32 nSwPos, bool bWriteCombinedChars, bool bPostponeSingleUse = false);
    void OutAttr(sal_Int32 nSwPos, bool bWriteCombinedChars);
    virtual const SfxPoolItem* HasTextItem( sal_uInt16 nWhich ) const override;
    virtual const SfxPoolItem& GetItem( sal_uInt16 nWhich ) const override;
    int OutAttrWithRange(const SwTextNode& rNode, sal_Int32 nPos);