diff --git a/java/src/com/android/inputmethod/latin/RecapitalizeStatus.java b/java/src/com/android/inputmethod/latin/RecapitalizeStatus.java
index 8a704ab42cf0e82cdb8ee8cdc4fc13d07276c16f..b9d7dcf7877922ed13d18a6a4efb92c5a2dead45 100644
--- a/java/src/com/android/inputmethod/latin/RecapitalizeStatus.java
+++ b/java/src/com/android/inputmethod/latin/RecapitalizeStatus.java
@@ -163,7 +163,10 @@ public class RecapitalizeStatus {
             final int codePoint = mStringBefore.codePointBefore(nonWhitespaceEnd);
             if (!Character.isWhitespace(codePoint)) break;
         }
-        if (0 != nonWhitespaceStart || len != nonWhitespaceEnd) {
+        // If nonWhitespaceStart >= nonWhitespaceEnd, that means the selection contained only
+        // whitespace, so we leave it as is.
+        if ((0 != nonWhitespaceStart || len != nonWhitespaceEnd)
+                && nonWhitespaceStart < nonWhitespaceEnd) {
             mCursorEndAfter = mCursorStartBefore + nonWhitespaceEnd;
             mCursorStartBefore = mCursorStartAfter = mCursorStartBefore + nonWhitespaceStart;
             mStringAfter = mStringBefore =