Bug 814886 - Correctly check selection bounds on Android; r=cpeterson
authorJim Chen <nchen@mozilla.com>
Tue, 27 Nov 2012 10:20:01 -0500
changeset 114230 12ab14c79a08ffe4cf2010e582bc5f87782c0164
parent 114229 26172ff887ae4ab578e6b468062d4e2d4476b9cc
child 114231 7d18627ba3d2ca4c2ad6ac3ecf5c231d7fdd58cb
push id23910
push userryanvm@gmail.com
push dateWed, 28 Nov 2012 00:19:51 +0000
treeherdermozilla-central@3c3a8eed0578 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewerscpeterson
bugs814886
milestone20.0a1
first release with
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
last release without
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
Bug 814886 - Correctly check selection bounds on Android; r=cpeterson
mobile/android/base/GeckoEditable.java
--- a/mobile/android/base/GeckoEditable.java
+++ b/mobile/android/base/GeckoEditable.java
@@ -122,17 +122,19 @@ final class GeckoEditable
             final Action action = new Action(TYPE_REPLACE_TEXT);
             action.mSequence = text;
             action.mStart = start;
             action.mEnd = end;
             return action;
         }
 
         static Action newSetSelection(int start, int end) {
-            if (start < 0 || start > end) {
+            // start == -1 when the start offset should remain the same
+            // end == -1 when the end offset should remain the same
+            if (start < -1 || end < -1) {
                 throw new IllegalArgumentException("invalid selection offsets");
             }
             final Action action = new Action(TYPE_SET_SELECTION);
             action.mStart = start;
             action.mEnd = end;
             return action;
         }
 
@@ -428,18 +430,22 @@ final class GeckoEditable
             // GeckoEditableListener methods should all be called from the Gecko thread
             GeckoApp.assertOnGeckoThread();
         }
         final Action action = mActionQueue.peek();
 
         switch (action.mType) {
         case Action.TYPE_SET_SELECTION:
             final int len = mText.length();
-            final int selStart = Math.min(action.mStart, len);
-            final int selEnd = Math.min(action.mEnd, len);
+            final int curStart = Selection.getSelectionStart(mText);
+            final int curEnd = Selection.getSelectionEnd(mText);
+            // start == -1 when the start offset should remain the same
+            // end == -1 when the end offset should remain the same
+            final int selStart = Math.min(action.mStart < 0 ? curStart : action.mStart, len);
+            final int selEnd = Math.min(action.mEnd < 0 ? curEnd : action.mEnd, len);
 
             if (selStart < action.mStart || selEnd < action.mEnd) {
                 Log.w(LOGTAG, "IME sync error: selection out of bounds");
             }
             Selection.setSelection(mText, selStart, selEnd);
             geckoPostToUI(new Runnable() {
                 public void run() {
                     mActionQueue.syncWithGecko();
@@ -515,17 +521,17 @@ final class GeckoEditable
     }
 
     @Override
     public void onSelectionChange(final int start, final int end) {
         if (DEBUG) {
             // GeckoEditableListener methods should all be called from the Gecko thread
             GeckoApp.assertOnGeckoThread();
         }
-        if (start < 0 || start > end || end > mText.length()) {
+        if (start < 0 || start > mText.length() || end < 0 || end > mText.length()) {
             throw new IllegalArgumentException("invalid selection notification range");
         }
         final int seqnoWhenPosted = ++mGeckoUpdateSeqno;
 
         geckoPostToUI(new Runnable() {
             public void run() {
                 mActionQueue.syncWithGecko();
                 /* check to see there has not been another action that potentially changed the