Bug 1388112 - Force capitalisation from autocorrection result. r?janH
MozReview-Commit-ID: 2wCgIpStQ52
--- a/mobile/android/base/java/org/mozilla/gecko/toolbar/ToolbarEditText.java
+++ b/mobile/android/base/java/org/mozilla/gecko/toolbar/ToolbarEditText.java
@@ -301,30 +301,36 @@ public class ToolbarEditText extends Cus
mAutoCompleteResult = "";
return;
}
final Editable text = getText();
final int textLength = text.length();
final int resultLength = result.length();
final int autoCompleteStart = text.getSpanStart(AUTOCOMPLETE_SPAN);
+ final int pathStart = StringUtils.pathStartIndex(getNonAutocompleteText(text));
mAutoCompleteResult = result;
if (autoCompleteStart > -1) {
// Autocomplete text already exists; we should replace existing autocomplete text.
// If the result and the current text don't have the same prefixes,
// the result is stale and we should wait for the another result to come in.
final String userText = text.toString().substring(0, autoCompleteStart);
if (!StringUtils.caseInsensitiveStartsWith(result, userText)) {
return;
}
beginSettingAutocomplete();
+ // Should we force capitalisation from result
+ if (pathStart != -1) {
+ text.replace(pathStart, autoCompleteStart, result, pathStart, autoCompleteStart);
+ }
+
// Replace the existing autocomplete text with new one.
// replace() preserves the autocomplete spans that we set before.
text.replace(autoCompleteStart, textLength, result, autoCompleteStart, resultLength);
// Reshow the cursor if there is no longer any autocomplete text.
if (autoCompleteStart == resultLength) {
setCursorVisible(true);
}
@@ -336,16 +342,23 @@ public class ToolbarEditText extends Cus
// If the result prefix doesn't match the current text,
// the result is stale and we should wait for the another result to come in.
if (resultLength <= textLength ||
!StringUtils.caseInsensitiveStartsWith(result, text.toString())) {
return;
}
+ // Now that we know the result and the usertext are the same case-insensitively,
+ // we should check whether their path parts match case-sensitively
+ if (pathStart != -1 &&
+ !TextUtils.regionMatches(result, pathStart, text, pathStart, textLength - pathStart)) {
+ return;
+ }
+
final Object[] spans = text.getSpans(textLength, textLength, Object.class);
final int[] spanStarts = new int[spans.length];
final int[] spanEnds = new int[spans.length];
final int[] spanFlags = new int[spans.length];
// Save selection/composing span bounds so we can restore them later.
for (int i = 0; i < spans.length; i++) {
final Object span = spans[i];
--- a/mobile/android/geckoview/src/main/java/org/mozilla/gecko/util/StringUtils.java
+++ b/mobile/android/geckoview/src/main/java/org/mozilla/gecko/util/StringUtils.java
@@ -238,16 +238,27 @@ public class StringUtils {
* @throws UnsupportedOperationException if this isn't a hierarchical URI
*
* @return a set of decoded names
*/
public static Set<String> getQueryParameterNames(Uri uri) {
return uri.getQueryParameterNames();
}
+ /**
+ * @return the index of the path segment of URLs
+ */
+ public static int pathStartIndex(String text) {
+ if (text.contains("://")) {
+ return text.indexOf('/', text.indexOf("://") + 3);
+ } else {
+ return text.indexOf('/');
+ }
+ }
+
public static String safeSubstring(@NonNull final String str, final int start, final int end) {
return str.substring(
Math.max(0, start),
Math.min(end, str.length()));
}
/**
* Check if this might be a RTL (right-to-left) text by looking at the first character.
--- a/mobile/android/tests/background/junit4/src/org/mozilla/gecko/util/TestStringUtils.java
+++ b/mobile/android/tests/background/junit4/src/org/mozilla/gecko/util/TestStringUtils.java
@@ -135,9 +135,21 @@ public class TestStringUtils {
// test ambiguous
String ambiguous = "~!@#$%^&*()_+`34567890-=qwertyuiop[]\\QWERTYUIOP{}|asdfghjkl;'ASDFGHJKL:\"ZXCVBNM<>?zxcvbnm,./";
ambiguous = ambiguous.replace(" ","").replace(".","").replace(":","");
assertTrue(StringUtils.isSearchQuery(ambiguous,true));
assertFalse(StringUtils.isSearchQuery(ambiguous,false));
}
+ @Test
+ public void testPathStartIndex(){
+ // Tests without protocol
+ assertTrue(StringUtils.pathStartIndex("mozilla.org") == -1);
+ assertTrue(StringUtils.pathStartIndex("mozilla.org/en-US") == 11);
+
+ // Tests with protocol
+ assertTrue(StringUtils.pathStartIndex("https://mozilla.org") == -1);
+ assertTrue(StringUtils.pathStartIndex("https://mozilla.org/") == 19);
+ assertTrue(StringUtils.pathStartIndex("https://mozilla.org/en-US") == 19);
+
+ }
}