From 7df324b4561a6551812bdfa5b72ba1c937148c41 Mon Sep 17 00:00:00 2001 From: Szymon Kłos Date: Tue, 7 Sep 2021 12:54:56 +0200 Subject: lok: IME: preserve formatting when inserting at the end of paragraph MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit There was a problem with inserting characters at the end of paragraph in online. When user set text to bolded, inserted few characters, then switched bold off. When trying to put another non-bolded character - the result was still bolded. It is caused by IME mechanics which gathers all elements for composition and at the end deletes these characters and inserts again but using document interface to keep undo records. During that operation formatting has been lost. This change does the same in reversed order so we first put new string correctly formatted at the end. Then we remove old leftovers. In result formatting is correct. Change-Id: Ieb498afdbe42c0080c30ea3db994cedc98e89847 Signed-off-by: Szymon Kłos Reviewed-on: https://gerrit.libreoffice.org/c/core/+/121774 Tested-by: Jenkins CollaboraOffice Reviewed-on: https://gerrit.libreoffice.org/c/core/+/122477 Reviewed-on: https://gerrit.libreoffice.org/c/core/+/123100 Tested-by: Jenkins --- sw/qa/extras/tiledrendering/tiledrendering.cxx | 109 +++++++++++++++++++++++++ sw/source/core/doc/extinput.cxx | 13 ++- 2 files changed, 121 insertions(+), 1 deletion(-) diff --git a/sw/qa/extras/tiledrendering/tiledrendering.cxx b/sw/qa/extras/tiledrendering/tiledrendering.cxx index ed2ba675644f..e1ebae14d421 100644 --- a/sw/qa/extras/tiledrendering/tiledrendering.cxx +++ b/sw/qa/extras/tiledrendering/tiledrendering.cxx @@ -32,6 +32,7 @@ #include #include #include +#include #include #include #include @@ -132,6 +133,7 @@ public: void testTdf115088(); void testRedlineField(); void testIMESupport(); + void testIMEFormattingAtEndOfParagraph(); void testSplitNodeRedlineCallback(); void testDeleteNodeRedlineCallback(); void testVisCursorInvalidation(); @@ -212,6 +214,7 @@ public: CPPUNIT_TEST(testTdf115088); CPPUNIT_TEST(testRedlineField); CPPUNIT_TEST(testIMESupport); + CPPUNIT_TEST(testIMEFormattingAtEndOfParagraph); CPPUNIT_TEST(testSplitNodeRedlineCallback); CPPUNIT_TEST(testDeleteNodeRedlineCallback); CPPUNIT_TEST(testVisCursorInvalidation); @@ -2219,6 +2222,112 @@ void SwTiledRenderingTest::testIMESupport() CPPUNIT_ASSERT_EQUAL(OUString(aInputs[aInputs.size() - 1] + "Aaa bbb."), pShellCursor->GetPoint()->nNode.GetNode().GetTextNode()->GetText()); } +void SwTiledRenderingTest::testIMEFormattingAtEndOfParagraph() +{ + comphelper::LibreOfficeKit::setActive(); + SwXTextDocument* pXTextDocument = createDoc("dummy.fodt"); + VclPtr pDocWindow = pXTextDocument->getDocWindow(); + + SwView* pView = dynamic_cast(SfxViewShell::Current()); + assert(pView); + SwWrtShell* pWrtShell = pView->GetWrtShellPtr(); + + // delete all characters + + for (int i = 0; i < 9; i++) + { + pXTextDocument->postKeyEvent(LOK_KEYEVENT_KEYINPUT, 0, KEY_DELETE); + pXTextDocument->postKeyEvent(LOK_KEYEVENT_KEYUP, 0, KEY_DELETE); + } + + Scheduler::ProcessEventsToIdle(); + + pDocWindow->PostExtTextInputEvent(VclEventId::ExtTextInput, "a"); + pDocWindow->PostExtTextInputEvent(VclEventId::EndExtTextInput, ""); + + // status: "a" + + comphelper::dispatchCommand(".uno:Bold", uno::Sequence()); + Scheduler::ProcessEventsToIdle(); + + pDocWindow->PostExtTextInputEvent(VclEventId::ExtTextInput, "b"); + pDocWindow->PostExtTextInputEvent(VclEventId::EndExtTextInput, ""); + + pXTextDocument->postKeyEvent(LOK_KEYEVENT_KEYINPUT, 0, KEY_RETURN); + pXTextDocument->postKeyEvent(LOK_KEYEVENT_KEYUP, 0, KEY_RETURN); + Scheduler::ProcessEventsToIdle(); + + // status: "ab\n" + + pDocWindow->PostExtTextInputEvent(VclEventId::ExtTextInput, "a"); + pDocWindow->PostExtTextInputEvent(VclEventId::EndExtTextInput, ""); + + std::unique_ptr pItem; + pView->GetViewFrame()->GetBindings().QueryState(SID_ATTR_CHAR_WEIGHT, pItem); + auto pWeightItem = dynamic_cast(pItem.get()); + CPPUNIT_ASSERT(pWeightItem); + + CPPUNIT_ASSERT_EQUAL(FontWeight::WEIGHT_BOLD, pWeightItem->GetWeight()); + + pXTextDocument->postKeyEvent(LOK_KEYEVENT_KEYINPUT, 0, KEY_RETURN); + pXTextDocument->postKeyEvent(LOK_KEYEVENT_KEYUP, 0, KEY_RETURN); + Scheduler::ProcessEventsToIdle(); + + // status: "ab\n + // a\n" + + comphelper::dispatchCommand(".uno:Bold", uno::Sequence()); + Scheduler::ProcessEventsToIdle(); + + pDocWindow->PostExtTextInputEvent(VclEventId::ExtTextInput, "b"); + pDocWindow->PostExtTextInputEvent(VclEventId::EndExtTextInput, ""); + + pView->GetViewFrame()->GetBindings().QueryState(SID_ATTR_CHAR_WEIGHT, pItem); + auto pWeightItem2 = dynamic_cast(pItem.get()); + CPPUNIT_ASSERT(pWeightItem2); + + CPPUNIT_ASSERT_EQUAL(FontWeight::WEIGHT_NORMAL, pWeightItem2->GetWeight()); + + // status: "ab\n + // a\n" + // b" + + comphelper::dispatchCommand(".uno:Bold", uno::Sequence()); + Scheduler::ProcessEventsToIdle(); + + pDocWindow->PostExtTextInputEvent(VclEventId::ExtTextInput, "a"); + pDocWindow->PostExtTextInputEvent(VclEventId::EndExtTextInput, ""); + + pView->GetViewFrame()->GetBindings().QueryState(SID_ATTR_CHAR_WEIGHT, pItem); + auto pWeightItem3 = dynamic_cast(pItem.get()); + CPPUNIT_ASSERT(pWeightItem3); + + CPPUNIT_ASSERT_EQUAL(FontWeight::WEIGHT_BOLD, pWeightItem3->GetWeight()); + + comphelper::dispatchCommand(".uno:Bold", uno::Sequence()); + Scheduler::ProcessEventsToIdle(); + + pDocWindow->PostExtTextInputEvent(VclEventId::ExtTextInput, "b"); + pDocWindow->PostExtTextInputEvent(VclEventId::EndExtTextInput, ""); + + pView->GetViewFrame()->GetBindings().QueryState(SID_ATTR_CHAR_WEIGHT, pItem); + auto pWeightItem4 = dynamic_cast(pItem.get()); + CPPUNIT_ASSERT(pWeightItem4); + + CPPUNIT_ASSERT_EQUAL(FontWeight::WEIGHT_NORMAL, pWeightItem4->GetWeight()); + + // status: "ab\n + // a\n" + // bab" + + // the cursor should be at position 3nd + SwShellCursor* pShellCursor = pWrtShell->getShellCursor(false); + CPPUNIT_ASSERT_EQUAL(static_cast(3), pShellCursor->GetPoint()->nContent.GetIndex()); + + // check the content + CPPUNIT_ASSERT_EQUAL(OUString("bab"), pShellCursor->GetPoint()->nNode.GetNode().GetTextNode()->GetText()); +} + void SwTiledRenderingTest::testSplitNodeRedlineCallback() { // Load a document. diff --git a/sw/source/core/doc/extinput.cxx b/sw/source/core/doc/extinput.cxx index 9ddcf0a935b8..5790126b1810 100644 --- a/sw/source/core/doc/extinput.cxx +++ b/sw/source/core/doc/extinput.cxx @@ -102,12 +102,23 @@ SwExtTextInput::~SwExtTextInput() } else { - pTNd->EraseText( rIdx, nEndCnt - nSttCnt ); + // we need to keep correct formatting + // ie. when we erase first, then we will lost information about format + // so: + // 0. initial status: xxxx | OLD + // 1. insert new content using Doc interface at the end so we will use the same formatting + // status: xxxx | OLD | NEW + // 2. erase old content which is placed at "start" position and before recently inserted text + // status: xxxx | NEW + + sal_Int32 nLenghtOfOldString = nEndCnt - nSttCnt; if( m_bInsText ) { rDoc.getIDocumentContentOperations().InsertString( *this, sText ); } + + pTNd->EraseText( rIdx, nLenghtOfOldString ); } rDoc.GetIDocumentUndoRedo().DoGroupUndo(bKeepGroupUndo); if (m_eInputLanguage == LANGUAGE_DONTKNOW) -- cgit