Bug 1265708 - Pre: Implement StringUtils.stripRef r=sebastian
☠☠ backed out by 5cdc84e272d5 ☠ ☠
authorAndrzej Hunt <ahunt@mozilla.com>
Fri, 03 Jun 2016 14:04:39 -0700
changeset 339384 974090a524da56fad3b8045b19461e9ae6817529
parent 339383 f685593ac3cc4ea3b8d6545e42eb7d225095161a
child 339385 047961bdb4adf91179be3b8a983cffc0d8e9debb
push id6249
push userjlund@mozilla.com
push dateMon, 01 Aug 2016 13:59:36 +0000
treeherdermozilla-beta@bad9d4f5bf7e [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewerssebastian
bugs1265708
milestone49.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 1265708 - Pre: Implement StringUtils.stripRef r=sebastian MozReview-Commit-ID: AO25iiUMXLE
mobile/android/base/java/org/mozilla/gecko/util/StringUtils.java
mobile/android/base/moz.build
mobile/android/tests/background/junit4/src/org/mozilla/gecko/util/TestStringUtils.java
--- a/mobile/android/base/java/org/mozilla/gecko/util/StringUtils.java
+++ b/mobile/android/base/java/org/mozilla/gecko/util/StringUtils.java
@@ -57,16 +57,36 @@ public class StringUtils {
         // Otherwise, if a dot or a colon is found, we assume this is a URL
         if (dot > -1 || colon > -1) {
             return false;
         }
         // Otherwise, text is ambiguous, and we keep its status unchanged
         return wasSearchQuery;
     }
 
+    /**
+     * Strip the ref from a URL, if present
+     *
+     * @return The base URL, without the ref. The original String is returned if it has no ref,
+     *         of if the input is malformed.
+     */
+    public static String stripRef(final String inputURL) {
+        if (inputURL == null) {
+            return null;
+        }
+
+        final int refIndex = inputURL.indexOf('#');
+
+        if (refIndex >= 0) {
+            return inputURL.substring(0, refIndex);
+        }
+
+        return inputURL;
+    }
+
     public static class UrlFlags {
         public static final int NONE = 0;
         public static final int STRIP_HTTPS = 1;
     }
 
     public static String stripScheme(String url) {
         return stripScheme(url, UrlFlags.NONE);
     }
--- a/mobile/android/base/moz.build
+++ b/mobile/android/base/moz.build
@@ -126,16 +126,17 @@ gujar.sources += ['java/org/mozilla/geck
     'util/NetworkUtils.java',
     'util/NonEvictingLruCache.java',
     'util/PrefUtils.java',
     'util/ProxySelector.java',
     'util/RawResource.java',
     'util/StringUtils.java',
     'util/ThreadUtils.java',
     'util/UIAsyncTask.java',
+    'util/URLUtils.java',
     'util/UUIDUtil.java',
     'util/WeakReferenceHandler.java',
     'util/WebActivityMapper.java',
     'util/WindowUtils.java',
 ]]
 gujar.extra_jars = [
     CONFIG['ANDROID_SUPPORT_ANNOTATIONS_JAR_LIB'],
     CONFIG['ANDROID_SUPPORT_V4_AAR_LIB'],
--- 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
@@ -6,16 +6,18 @@
 package org.mozilla.gecko.util;
 
 import junit.framework.Assert;
 
 import org.junit.Test;
 import org.junit.runner.RunWith;
 import org.mozilla.gecko.background.testhelpers.TestRunner;
 
+import static org.junit.Assert.assertEquals;
+
 @RunWith(TestRunner.class)
 public class TestStringUtils {
     @Test
     public void testIsHttpOrHttps() {
         // No value
         Assert.assertFalse(StringUtils.isHttpOrHttps(null));
         Assert.assertFalse(StringUtils.isHttpOrHttps(""));
 
@@ -35,9 +37,20 @@ public class TestStringUtils {
         Assert.assertFalse(StringUtils.isHttpOrHttps("ftp://people.mozilla.org"));
         Assert.assertFalse(StringUtils.isHttpOrHttps("javascript:window.google.com"));
         Assert.assertFalse(StringUtils.isHttpOrHttps("tel://1234567890"));
 
         // No scheme
         Assert.assertFalse(StringUtils.isHttpOrHttps("google.com"));
         Assert.assertFalse(StringUtils.isHttpOrHttps("git@github.com:mozilla/gecko-dev.git"));
     }
+
+    @Test
+    public void testStripRef() {
+        assertEquals(StringUtils.stripRef(null), null);
+        assertEquals(StringUtils.stripRef(""), "");
+
+        assertEquals(StringUtils.stripRef("??AAABBBCCC"), "??AAABBBCCC");
+        assertEquals(StringUtils.stripRef("https://mozilla.org"), "https://mozilla.org");
+        assertEquals(StringUtils.stripRef("https://mozilla.org#BBBB"), "https://mozilla.org");
+        assertEquals(StringUtils.stripRef("https://mozilla.org/#BBBB"), "https://mozilla.org/");
+    }
 }