Skip to content

Commit da182de

Browse files
George Mountjreck
authored andcommitted
DO NOT MERGE Fixed spell check failing to change word.
Bug 5387838 On WebView.rebuildWebTextView, setTextAndKeepSelection was being called, erasing the selections. Changed it so that when text is replaced with the exact same value, no replace is done. Also, on the Google search, when a spelling change was made, the final character was placed improperly. When a single character is added, the javascript events for the character are sent. When multiple characters are changed, the entire value is replaced with no javascript key events sent. Change-Id: I791eeb3c96354cfe3cbfda7e8d05c81fcdeb152f
1 parent 2cf1cf0 commit da182de

File tree

1 file changed

+34
-19
lines changed

1 file changed

+34
-19
lines changed

core/java/android/webkit/WebTextView.java

Lines changed: 34 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -607,23 +607,31 @@ protected void onTextChanged(CharSequence s,int start,int before,int count){
607607
// character or the existing selection, so it will not get cleared
608608
// above.
609609
mGotDelete = false;
610+
// Prefer sending javascript events, so when adding one character,
611+
// don't replace the unchanged text.
612+
if (count > 1 && before == count - 1) {
613+
String replaceButOne = s.subSequence(start,
614+
start + before).toString();
615+
String replacedString = getText().subSequence(start,
616+
start + before).toString();
617+
if (replaceButOne.equals(replacedString)) {
618+
// we're just adding one character
619+
start += before;
620+
before = 0;
621+
count = 1;
622+
}
623+
}
610624
// Find the last character being replaced. If it can be represented by
611-
// events, we will pass them to native (after replacing the beginning
612-
// of the changed text), so we can see javascript events.
613-
// Otherwise, replace the text being changed (including the last
614-
// character) in the textfield.
615-
TextUtils.getChars(s, start + count - 1, start + count, mCharacter, 0);
616-
KeyCharacterMap kmap = KeyCharacterMap.load(KeyCharacterMap.VIRTUAL_KEYBOARD);
617-
KeyEvent[] events = kmap.getEvents(mCharacter);
618-
boolean cannotUseKeyEvents = null == events;
619-
int charactersFromKeyEvents = cannotUseKeyEvents ? 0 : 1;
620-
if (count > 1 || cannotUseKeyEvents) {
621-
String replace = s.subSequence(start,
622-
start + count - charactersFromKeyEvents).toString();
623-
mWebView.replaceTextfieldText(start, start + before, replace,
624-
start + count - charactersFromKeyEvents,
625-
start + count - charactersFromKeyEvents);
626-
} else {
625+
// events, we will pass them to native so we can see javascript events.
626+
// Otherwise, replace the text being changed in the textfield.
627+
KeyEvent[] events = null;
628+
if (count == 1) {
629+
TextUtils.getChars(s, start + count - 1, start + count, mCharacter, 0);
630+
KeyCharacterMap kmap = KeyCharacterMap.load(KeyCharacterMap.VIRTUAL_KEYBOARD);
631+
events = kmap.getEvents(mCharacter);
632+
}
633+
boolean useKeyEvents = (events != null);
634+
if (useKeyEvents) {
627635
// This corrects the selection which may have been affected by the
628636
// trackball or auto-correct.
629637
if (DebugFlags.WEB_TEXT_VIEW) {
@@ -633,8 +641,6 @@ protected void onTextChanged(CharSequence s,int start,int before,int count){
633641
if (!mInSetTextAndKeepSelection) {
634642
mWebView.setSelection(start, start + before);
635643
}
636-
}
637-
if (!cannotUseKeyEvents) {
638644
int length = events.length;
639645
for (int i = 0; i < length; i++) {
640646
// We never send modifier keys to native code so don't send them
@@ -643,6 +649,12 @@ protected void onTextChanged(CharSequence s,int start,int before,int count){
643649
sendDomEvent(events[i]);
644650
}
645651
}
652+
} else {
653+
String replace = s.subSequence(start,
654+
start + count).toString();
655+
mWebView.replaceTextfieldText(start, start + before, replace,
656+
start + count,
657+
start + count);
646658
}
647659
updateCachedTextfield();
648660
}
@@ -966,8 +978,11 @@ private void setMaxLength(int maxLength) {
966978
* @param text The new text to place in the textfield.
967979
*/
968980
/* package */ void setTextAndKeepSelection(String text) {
969-
mPreChange = text.toString();
970981
Editable edit = getText();
982+
mPreChange = text;
983+
if (edit.toString().equals(text)) {
984+
return;
985+
}
971986
int selStart = Selection.getSelectionStart(edit);
972987
int selEnd = Selection.getSelectionEnd(edit);
973988
mInSetTextAndKeepSelection = true;

0 commit comments

Comments
 (0)