sw/qa/uibase/shells/shells.cxx      |   13 ++++++++----
 sw/source/uibase/shells/textfld.cxx |   39 +++++++++++++++++++++++++++++++++---
 2 files changed, 45 insertions(+), 7 deletions(-)

New commits:
commit 1c2ef850db29beb369dcc89a58fc73416ecd9c5c
Author:     Miklos Vajna <vmik...@collabora.com>
AuthorDate: Wed Nov 16 12:10:30 2022 +0100
Commit:     Miklos Vajna <vmik...@collabora.com>
CommitDate: Wed Nov 16 13:11:54 2022 +0100

    sw, .uno:TextFormField command: accept HTML in the FieldResult parameter
    
    - improve the calculation of the SwPaM that decides the start/end of the
      fieldmark: with HTML import the length of the content is not the
      length of the input string anymore
    
    - use SwTranslateHelper::PasteHTMLToPaM() to accept formatted content
      inside a single paragraph
    
    - avoid unwanted last empty paragraph from the HTML import
    
    - this builds on top of commit 6870c0c3385bf5d19e9c80bf973fca255ae38c08
      (sw: add new FieldCode parameter for the .uno:TextFormField command,
      2022-11-15), which introduced plain text support for FieldResult
    
    Change-Id: I8332f6ee532ab1e844a4b94f22b1f72d2406eb3a
    Reviewed-on: https://gerrit.libreoffice.org/c/core/+/142756
    Reviewed-by: Miklos Vajna <vmik...@collabora.com>
    Tested-by: Jenkins

diff --git a/sw/qa/uibase/shells/shells.cxx b/sw/qa/uibase/shells/shells.cxx
index 803c41657e89..4361920b3e87 100644
--- a/sw/qa/uibase/shells/shells.cxx
+++ b/sw/qa/uibase/shells/shells.cxx
@@ -278,11 +278,10 @@ CPPUNIT_TEST_FIXTURE(SwUibaseShellsTest, 
testInsertTextFormField)
 
     // When inserting an ODF_UNHANDLED fieldmark:
     OUString aExpectedCommand("ADDIN ZOTERO_BIBL foo bar");
-    OUString aExpectedResult("(Abrikosov, n.d.)");
     uno::Sequence<css::beans::PropertyValue> aArgs = {
         comphelper::makePropertyValue("FieldType", 
uno::Any(OUString(ODF_UNHANDLED))),
         comphelper::makePropertyValue("FieldCommand", 
uno::Any(aExpectedCommand)),
-        comphelper::makePropertyValue("FieldResult", 
uno::Any(aExpectedResult)),
+        comphelper::makePropertyValue("FieldResult", 
uno::Any(OUString("<p>aaa</p><p>bbb</p>"))),
     };
     dispatchCommand(mxComponent, ".uno:TextFormField", aArgs);
 
@@ -305,8 +304,14 @@ CPPUNIT_TEST_FIXTURE(SwUibaseShellsTest, 
testInsertTextFormField)
     it->second >>= aActualCommand;
     CPPUNIT_ASSERT_EQUAL(aExpectedCommand, aActualCommand);
 
-    OUString aActualResult = pFieldmark->GetContent();
-    CPPUNIT_ASSERT_EQUAL(aExpectedResult, aActualResult);
+    SwPaM aPam(pFieldmark->GetMarkStart(), pFieldmark->GetMarkEnd());
+    // Ignore the leading field start + sep.
+    aPam.GetMark()->SetContent(aPam.GetMark()->GetContentIndex() + 2);
+    // Ignore the trailing field end.
+    aPam.GetPoint()->SetContent(aPam.GetPoint()->GetContentIndex() - 1);
+    CPPUNIT_ASSERT(aPam.HasMark());
+    OUString aActualResult = aPam.GetText();
+    CPPUNIT_ASSERT_EQUAL(OUString("aaa\nbbb"), aActualResult);
 }
 
 CPPUNIT_PLUGIN_IMPLEMENT();
diff --git a/sw/source/uibase/shells/textfld.cxx 
b/sw/source/uibase/shells/textfld.cxx
index 85dffcd7affc..762705c90b66 100644
--- a/sw/source/uibase/shells/textfld.cxx
+++ b/sw/source/uibase/shells/textfld.cxx
@@ -63,6 +63,7 @@
 #include <IMark.hxx>
 #include <officecfg/Office/Compatibility.hxx>
 #include <ndtxt.hxx>
+#include <translatehelper.hxx>
 
 
 using namespace nsSwDocInfoSubType;
@@ -703,6 +704,9 @@ 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)
@@ -717,12 +721,40 @@ FIELD_INSERT:
                     aFieldResult = pFieldResult->GetValue();
                 }
 
-                bool bSuccess = 
rSh.GetDoc()->getIDocumentContentOperations().InsertString(*pCursorPos, 
aFieldResult);
+                // Split node to remember where the start position is.
+                bool bSuccess = 
rSh.GetDoc()->getIDocumentContentOperations().SplitNode(
+                    *pCursorPos->GetPoint(), false);
                 if(bSuccess)
                 {
+                    SwPaM aFieldPam(*pCursorPos->GetPoint());
+                    aFieldPam.Move(fnMoveBackward, GoInContent);
+                    if (pFieldResult)
+                    {
+                        // Paste HTML content.
+                        SwTranslateHelper::PasteHTMLToPaM(rSh, pCursorPos, 
aFieldResult.toUtf8(),
+                                                          true);
+                        if (pCursorPos->GetPoint()->GetContentIndex() == 0)
+                        {
+                            // The paste created a last empty text node, 
remove it.
+                            SwPaM aPam(*pCursorPos->GetPoint());
+                            aPam.SetMark();
+                            aPam.Move(fnMoveBackward, GoInContent);
+                            
rSh.GetDoc()->getIDocumentContentOperations().DeleteAndJoin(aPam);
+                        }
+                    }
+                    else
+                    {
+                        // Insert default placeholder.
+                        
rSh.GetDoc()->getIDocumentContentOperations().InsertString(*pCursorPos,
+                                                                               
    aFieldResult);
+                    }
+                    // Undo the above SplitNode().
+                    aFieldPam.SetMark();
+                    aFieldPam.Move(fnMoveForward, GoInContent);
+                    
rSh.GetDoc()->getIDocumentContentOperations().DeleteAndJoin(aFieldPam);
+                    *aFieldPam.GetMark() = *pCursorPos->GetPoint();
+
                     IDocumentMarkAccess* pMarksAccess = 
rSh.GetDoc()->getIDocumentMarkAccess();
-                    SwPaM aFieldPam(pCursorPos->GetPoint()->GetNode(), 
pCursorPos->GetPoint()->GetContentIndex() - aFieldResult.getLength(),
-                                    pCursorPos->GetPoint()->GetNode(), 
pCursorPos->GetPoint()->GetContentIndex());
                     sw::mark::IFieldmark* pFieldmark = 
pMarksAccess->makeFieldBookmark(
                         aFieldPam, OUString(), aFieldType, aFieldPam.Start());
                     if (pFieldmark && !aFieldCode.isEmpty())
@@ -733,6 +765,7 @@ FIELD_INSERT:
                 }
             }
 
+            rSh.EndAction();
             
rSh.GetDoc()->GetIDocumentUndoRedo().EndUndo(SwUndoId::INSERT_FORM_FIELD, 
nullptr);
             rSh.GetView().GetViewFrame()->GetBindings().Invalidate( SID_UNDO );
         }

Reply via email to