sw/qa/uibase/shells/shells.cxx      |   40 ++++++++++++++++++++++++++++++++++++
 sw/source/uibase/shells/textfld.cxx |   16 ++++++++++----
 2 files changed, 52 insertions(+), 4 deletions(-)

New commits:
commit de0db79ed42b3df9e648b1b7cf7cae37f0601b23
Author:     Miklos Vajna <[email protected]>
AuthorDate: Tue Jan 17 11:38:14 2023 +0100
Commit:     Miklos Vajna <[email protected]>
CommitDate: Wed Jan 18 14:12:05 2023 +0000

    sw: .uno:TextFormField: handle Endnote as a value for the Wrapper parameter
    
    This is similar to the fieldmark-in-footnote case, but here we need to
    make sure that the layout is calculated before the cursor attempts to
    jump to the endnote, otherwise the fieldmark will be inserted before the
    endnote anchor, not in the endnote body.
    
    The move of StartAction() / EndAction() calls can be done
    unconditionally, it's not a problem for the existing footnote case.
    
    (cherry picked from commit 43d80906c8693ca27c5b3077fbaa259df4004924)
    
    Change-Id: I772d27937456978ca6aa01148eff3163d8877208
    Reviewed-on: https://gerrit.libreoffice.org/c/core/+/145672
    Tested-by: Jenkins CollaboraOffice <[email protected]>
    Reviewed-by: Justin Luth <[email protected]>

diff --git a/sw/qa/uibase/shells/shells.cxx b/sw/qa/uibase/shells/shells.cxx
index f4fcbd8eeb7e..a41bd2299b5f 100644
--- a/sw/qa/uibase/shells/shells.cxx
+++ b/sw/qa/uibase/shells/shells.cxx
@@ -40,6 +40,7 @@
 #include <bookmark.hxx>
 #include <ndtxt.hxx>
 #include <ftnidx.hxx>
+#include <txtftn.hxx>
 
 constexpr OUStringLiteral DATA_DIRECTORY = u"/sw/qa/uibase/shells/data/";
 
@@ -890,6 +891,45 @@ CPPUNIT_TEST_FIXTURE(SwUibaseShellsTest, 
testInsertTextFormFieldFootnote)
     CPPUNIT_ASSERT_EQUAL(static_cast<size_t>(1), rFootnotes.size());
 }
 
+CPPUNIT_TEST_FIXTURE(SwUibaseShellsTest, testInsertTextFormFieldEndnote)
+{
+    // Given an empty document:
+    SwDoc* pDoc = createSwDoc();
+
+    // When inserting an ODF_UNHANDLED fieldmark inside an endnote:
+    uno::Sequence<css::beans::PropertyValue> aArgs = {
+        comphelper::makePropertyValue("FieldType", 
uno::Any(OUString(ODF_UNHANDLED))),
+        comphelper::makePropertyValue("FieldCommand",
+                                      uno::Any(OUString("ADDIN ZOTERO_BIBL foo 
bar"))),
+        comphelper::makePropertyValue("FieldResult", 
uno::Any(OUString("result"))),
+        comphelper::makePropertyValue("Wrapper", 
uno::Any(OUString("Endnote"))),
+    };
+    dispatchCommand(mxComponent, ".uno:TextFormField", aArgs);
+
+    // Then make sure that the endnote is created:
+    SwFootnoteIdxs& rFootnotes = pDoc->GetFootnoteIdxs();
+    // Without the accompanying fix in place, this test would have failed with:
+    // - Expected: 1
+    // - Actual  : 0
+    // i.e. no endnote was inserted.
+    CPPUNIT_ASSERT_EQUAL(static_cast<size_t>(1), rFootnotes.size());
+    SwTextFootnote* pEndnote = rFootnotes[0];
+    const SwFormatFootnote& rFormatEndnote = pEndnote->GetFootnote();
+    CPPUNIT_ASSERT(rFormatEndnote.IsEndNote());
+    // Also check that the endnote body contains the fieldmark:
+    SwWrtShell* pWrtShell = pDoc->GetDocShell()->GetWrtShell();
+    pWrtShell->SttEndDoc(/*bStt=*/true);
+    pWrtShell->GotoFootnoteText();
+    pWrtShell->EndOfSection(/*bSelect=*/true);
+    SwCursor* pCursor = pWrtShell->GetCursor();
+    OUString aActual = pCursor->GetText();
+    static sal_Unicode const aForbidden[]
+        = { CH_TXT_ATR_FIELDSTART, CH_TXT_ATR_FIELDSEP, CH_TXT_ATR_FIELDEND, 0 
};
+    aActual = comphelper::string::removeAny(aActual, aForbidden);
+    // Then this was empty: the fieldmark was inserted before the note anchor, 
not in the note body.
+    CPPUNIT_ASSERT_EQUAL(OUString("result"), aActual);
+}
+
 CPPUNIT_PLUGIN_IMPLEMENT();
 
 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
diff --git a/sw/source/uibase/shells/textfld.cxx 
b/sw/source/uibase/shells/textfld.cxx
index cd9d1f61ebf6..bce713647c62 100644
--- a/sw/source/uibase/shells/textfld.cxx
+++ b/sw/source/uibase/shells/textfld.cxx
@@ -722,9 +722,6 @@ FIELD_INSERT:
             }
 
             
rSh.GetDoc()->GetIDocumentUndoRedo().StartUndo(SwUndoId::INSERT_FORM_FIELD, 
nullptr);
-            // Don't update the layout after inserting content and before 
deleting temporary
-            // text nodes.
-            rSh.StartAction();
 
             SwPaM* pCursorPos = rSh.GetCursor();
             if(pCursorPos)
@@ -749,8 +746,19 @@ FIELD_INSERT:
                     {
                         rSh.InsertFootnote(OUString());
                     }
+                    else if (aWrapper == "Endnote")
+                    {
+                        // It's important that there is no Start/EndAction() 
around this, so the
+                        // inner EndAction() triggers a layout update and the 
cursor can jump to the
+                        // created SwFootnoteFrame.
+                        rSh.InsertFootnote(OUString(), /*bEndNote=*/true);
+                    }
                 }
 
+                // Don't update the layout after inserting content and before 
deleting temporary
+                // text nodes.
+                rSh.StartAction();
+
                 // Split node to remember where the start position is.
                 bool bSuccess = 
rSh.GetDoc()->getIDocumentContentOperations().SplitNode(
                     *pCursorPos->GetPoint(), false);
@@ -793,9 +801,9 @@ FIELD_INSERT:
                             std::pair<OUString, uno::Any>(ODF_CODE_PARAM, 
uno::Any(aFieldCode)));
                     }
                 }
+                rSh.EndAction();
             }
 
-            rSh.EndAction();
             
rSh.GetDoc()->GetIDocumentUndoRedo().EndUndo(SwUndoId::INSERT_FORM_FIELD, 
nullptr);
             rSh.GetView().GetViewFrame()->GetBindings().Invalidate( SID_UNDO );
         }

Reply via email to