Bug 1123514 - Correctly calculate text replacement offset; r=esawin
authorJim Chen <nchen@mozilla.com>
Sun, 06 Nov 2016 18:44:09 -0500
changeset 351409 7fa6baac8f3790a6d217c88c7187e98cf1e22e0f
parent 351408 803d5895353dd7053a80b3fcd702a17aeff7aad2
child 351410 61cf0a1a5186ae72d46d468ccb25492b1cb71734
push id6795
push userjlund@mozilla.com
push dateMon, 23 Jan 2017 14:19:46 +0000
treeherdermozilla-esr52@76101b503191 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersesawin
bugs1123514
milestone52.0a1
Bug 1123514 - Correctly calculate text replacement offset; r=esawin Fix a mistake in calculating the correct offset for one of the replacement steps, which caused the IndexOutOfBoundsException. The old code used `oldEnd` for the second text replacement without taking into account the offset change as a result of the first text replacement that was already performed. The new code correctly takes the offset, `delta`, into account.
mobile/android/geckoview/src/main/java/org/mozilla/gecko/GeckoEditable.java
--- a/mobile/android/geckoview/src/main/java/org/mozilla/gecko/GeckoEditable.java
+++ b/mobile/android/geckoview/src/main/java/org/mozilla/gecko/GeckoEditable.java
@@ -1215,23 +1215,24 @@ final class GeckoEditable extends JNIObj
 
             } else {
                 // The sequence is embedded within the changed text, so we have to perform
                 // replacement in parts. First replace part of text before the sequence.
                 mText.currentReplace(start, action.mStart, text.subSequence(0, indexInText));
 
                 // Then replace part of the text after the sequence.
                 final int actionStart = indexInText + start;
-                final int actionEnd = actionStart + action.mEnd - action.mStart;
+                final int delta = actionStart - action.mStart;
+                final int actionEnd = delta + action.mEnd;
 
                 final Spanned currentText = mText.getCurrentText();
                 final boolean resetSelStart = Selection.getSelectionStart(currentText) == actionEnd;
                 final boolean resetSelEnd = Selection.getSelectionEnd(currentText) == actionEnd;
 
-                mText.currentReplace(actionEnd, oldEnd, text.subSequence(
+                mText.currentReplace(actionEnd, delta + oldEnd, text.subSequence(
                         indexInText + action.mSequence.length(), text.length()));
 
                 // The replacement above may have shifted our selection, if the selection
                 // was at the start of the replacement range. If so, we need to reset
                 // our selection to the previous position.
                 if (resetSelStart || resetSelEnd) {
                     mText.currentSetSelection(
                             resetSelStart ? actionEnd : Selection.getSelectionStart(currentText),