From 44abfea789b8677a741530c44a0cd35e5276ddf3 Mon Sep 17 00:00:00 2001 From: Peter Varga Date: Mon, 19 Nov 2018 13:43:54 +0100 Subject: Fix IME with negative start position Change-Id: I64a39975788cc40c5e0595c1a0c5db9000889bfd Reviewed-by: Allan Sandfeld Jensen --- src/core/render_widget_host_view_qt.cpp | 11 ++---- .../widgets/qwebengineview/tst_qwebengineview.cpp | 42 ++++++++++++++++++++++ 2 files changed, 45 insertions(+), 8 deletions(-) diff --git a/src/core/render_widget_host_view_qt.cpp b/src/core/render_widget_host_view_qt.cpp index af7471759..267460bdc 100644 --- a/src/core/render_widget_host_view_qt.cpp +++ b/src/core/render_widget_host_view_qt.cpp @@ -1316,14 +1316,9 @@ void RenderWidgetHostViewQt::handleInputMethodEvent(QInputMethodEvent *ev) if (replacementLength > 0) { - int start = ev->replacementStart(); - - if (start >= 0) - replacementRange = gfx::Range(start, start + replacementLength); - else if (m_surroundingText.length() + start >= 0) { - start = m_surroundingText.length() + start; - replacementRange = gfx::Range(start, start + replacementLength); - } + int replacementStart = ev->replacementStart() < 0 ? m_cursorPosition + ev->replacementStart() : ev->replacementStart(); + if (replacementStart >= 0 && replacementStart < m_surroundingText.length()) + replacementRange = gfx::Range(replacementStart, replacementStart + replacementLength); } // There are so-far two known cases, when an empty QInputMethodEvent is received. diff --git a/tests/auto/widgets/qwebengineview/tst_qwebengineview.cpp b/tests/auto/widgets/qwebengineview/tst_qwebengineview.cpp index a0f8f11d2..a6487d19a 100644 --- a/tests/auto/widgets/qwebengineview/tst_qwebengineview.cpp +++ b/tests/auto/widgets/qwebengineview/tst_qwebengineview.cpp @@ -2292,6 +2292,48 @@ void tst_QWebEngineView::imeComposition() QTRY_COMPARE(view.focusProxy()->inputMethodQuery(Qt::ImAnchorPosition).toInt(), 15); QTRY_COMPARE(view.focusProxy()->inputMethodQuery(Qt::ImCurrentSelection).toString(), QString("")); QCOMPARE(selectionChangedSpy.count(), 2); + selectionChangedSpy.clear(); + + + // 5. Mimic behavior of QtVirtualKeyboard with enabled text prediction. + evaluateJavaScriptSync(view.page(), "document.getElementById('input1').value='QtWebEngine';"); + QTRY_COMPARE(evaluateJavaScriptSync(view.page(), "document.getElementById('input1').value").toString(), QString("QtWebEngine")); + + // Move cursor into position. + QTest::keyClick(view.focusProxy(), Qt::Key_Home); + for (int j = 0; j < 2; ++j) + QTest::keyClick(view.focusProxy(), Qt::Key_Right); + QTRY_COMPARE(view.focusProxy()->inputMethodQuery(Qt::ImCursorPosition).toInt(), 2); + + // Turn text into composition by using negative start position. + { + int replaceFrom = -1 * view.focusProxy()->inputMethodQuery(Qt::ImCursorPosition).toInt(); + int replaceLength = view.focusProxy()->inputMethodQuery(Qt::ImSurroundingText).toString().size(); + + QList attributes; + QInputMethodEvent event("QtWebEngine", attributes); + event.setCommitString(QString(), replaceFrom, replaceLength); + QApplication::sendEvent(view.focusProxy(), &event); + } + QTRY_COMPARE(view.focusProxy()->inputMethodQuery(Qt::ImSurroundingText).toString(), QString("")); + QCOMPARE(view.focusProxy()->inputMethodQuery(Qt::ImCursorPosition).toInt(), 11); + QCOMPARE(view.focusProxy()->inputMethodQuery(Qt::ImAnchorPosition).toInt(), 11); + QCOMPARE(view.focusProxy()->inputMethodQuery(Qt::ImCurrentSelection).toString(), QString("")); + QCOMPARE(evaluateJavaScriptSync(view.page(), "document.getElementById('input1').value").toString(), QString("QtWebEngine")); + + // Commit. + { + QList attributes; + QInputMethodEvent event(QString(), attributes); + event.setCommitString("QtWebEngine", 0, 0); + QApplication::sendEvent(view.focusProxy(), &event); + } + QTRY_COMPARE(view.focusProxy()->inputMethodQuery(Qt::ImSurroundingText).toString(), QString("QtWebEngine")); + QCOMPARE(view.focusProxy()->inputMethodQuery(Qt::ImCursorPosition).toInt(), 11); + QCOMPARE(view.focusProxy()->inputMethodQuery(Qt::ImAnchorPosition).toInt(), 11); + QCOMPARE(view.focusProxy()->inputMethodQuery(Qt::ImCurrentSelection).toString(), QString("")); + QCOMPARE(evaluateJavaScriptSync(view.page(), "document.getElementById('input1').value").toString(), QString("QtWebEngine")); + QCOMPARE(selectionChangedSpy.count(), 0); } void tst_QWebEngineView::newlineInTextarea() -- cgit v1.2.3