1
0
Fork 0

Switched to AC string extensions (#6258)

* Switched to AC string extensions

* Clean up imports
master
Daphne Liu 2019-11-22 23:15:51 -08:00 committed by Tiger Oakes
parent 77334b6108
commit 4cfbaf2dc4
4 changed files with 7 additions and 48 deletions

View File

@ -4,40 +4,21 @@
package org.mozilla.fenix.ext
import android.content.Context
import android.net.Uri
import android.util.Patterns
import android.webkit.URLUtil
import androidx.core.net.toUri
import kotlinx.coroutines.runBlocking
import mozilla.components.lib.publicsuffixlist.PublicSuffixList
import mozilla.components.lib.publicsuffixlist.ext.urlToTrimmedHost
import mozilla.components.support.ktx.android.net.hostWithoutCommonPrefixes
import java.net.IDN
import java.net.MalformedURLException
import java.net.URL
import java.util.Locale
const val FILE_PREFIX = "file://"
const val MAX_VALID_PORT = 65_535
/**
* Replaces the keys with the values with the map provided.
*/
fun String.replace(pairs: Map<String, String>): String {
var result = this
pairs.forEach { (l, r) -> result = result.replace(l, r) }
return result
}
/**
* Tries to parse and get host part if this [String] is valid URL.
* Otherwise returns the string.
*/
fun String.tryGetHostFromUrl(): String = try {
URL(this).host
} catch (e: MalformedURLException) {
this
}
/**
* Shortens URLs to be more user friendly.
*
@ -110,15 +91,8 @@ private fun Uri.isIpv6(): Boolean {
/**
* Trim a host's prefix and suffix
*/
fun String.urlToTrimmedHost(publicSuffixList: PublicSuffixList): String {
return try {
val host = toUri().hostWithoutCommonPrefixes ?: return this
runBlocking {
publicSuffixList.stripPublicSuffix(host).await()
}
} catch (e: MalformedURLException) {
this
}
fun String.urlToTrimmedHost(context: Context): String = runBlocking {
urlToTrimmedHost(context.components.publicSuffixList).await()
}
/**

View File

@ -6,8 +6,8 @@ package org.mozilla.fenix.library.history
import androidx.paging.ItemKeyedDataSource
import mozilla.components.concept.storage.VisitInfo
import mozilla.components.support.ktx.kotlin.tryGetHostFromUrl
import org.mozilla.fenix.components.history.PagedHistoryProvider
import org.mozilla.fenix.ext.tryGetHostFromUrl
class HistoryDataSource(
private val historyProvider: PagedHistoryProvider

View File

@ -7,7 +7,7 @@ package org.mozilla.fenix.trackingprotection
import mozilla.components.concept.engine.EngineSession.TrackingProtectionPolicy.TrackingCategory
import mozilla.components.concept.engine.content.blocking.Tracker
import mozilla.components.concept.engine.content.blocking.TrackerLog
import org.mozilla.fenix.ext.tryGetHostFromUrl
import mozilla.components.support.ktx.kotlin.tryGetHostFromUrl
import org.mozilla.fenix.trackingprotection.TrackingProtectionCategory.CROSS_SITE_TRACKING_COOKIES
import org.mozilla.fenix.trackingprotection.TrackingProtectionCategory.CRYPTOMINERS
import org.mozilla.fenix.trackingprotection.TrackingProtectionCategory.FINGERPRINTERS

View File

@ -24,25 +24,10 @@ class StringTest {
private val publicSuffixList = testContext.components.publicSuffixList
@Test
fun replace() {
val chars = mapOf("Mozilla Corporation" to "moco", "Mozilla Foundation" to "mofo")
val sentence = "Mozilla Corporation and Mozilla Foundation are committed to the future of the internet"
val new = sentence.replace(chars)
assertEquals(new, "moco and mofo are committed to the future of the internet")
}
@Test
fun `Try Get Host From Url`() {
val urlTest = "http://www.example.com:1080/docs/resource1.html"
val new = urlTest.tryGetHostFromUrl()
assertEquals(new, "www.example.com")
}
@Test
fun `Url To Trimmed Host`() {
val urlTest = "http://www.example.com:1080/docs/resource1.html"
val new = urlTest.urlToTrimmedHost(testContext.components.publicSuffixList)
val new = urlTest.urlToTrimmedHost(testContext)
assertEquals(new, "example")
}