Skip to content

Commit

Permalink
Ensure only the protocol scheme is lowercase
Browse files Browse the repository at this point in the history
  • Loading branch information
JcMinarro committed Jul 4, 2024
1 parent 79be7b2 commit 4388f62
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ private fun AnnotatedString.Builder.linkify(

val linkText = requireNotNull(matcher.group(0)!!)

val url = linkText.fixPrefix(schemes)
val url = linkText.ensureLowercaseScheme(schemes)

addStringAnnotation(
tag = tag,
Expand All @@ -148,15 +148,16 @@ private fun AnnotatedString.Builder.linkify(
}
}

private fun String.fixPrefix(schemes: List<String>): String =
lowercase(Locale.getDefault())
.let {
if (schemes.none { scheme -> it.startsWith(scheme) }) {
schemes[0] + it
} else {
it
}
internal fun String.ensureLowercaseScheme(schemes: List<String>): String =
schemes.fold(this) { acc, scheme ->
acc.replace(scheme, scheme.lowercase(), ignoreCase = true)
}.let { url ->
if (schemes.none { url.startsWith(it) }) {
schemes[0].lowercase() + url
} else {
url
}
}

private val URL_SCHEMES = listOf("http://", "https://")
private val EMAIL_SCHEMES = listOf("mailto:")
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package io.getstream.chat.android.compose.ui.util

import org.amshove.kluent.`should be equal to`
import org.junit.jupiter.params.ParameterizedTest
import org.junit.jupiter.params.provider.Arguments
import org.junit.jupiter.params.provider.MethodSource

internal class TextUtilsKtTest {

@ParameterizedTest
@MethodSource("urlArguments")
fun `Verify that only the scheme should be lowercase`(
url: String,
schemes: List<String>,
expectedResult: String
) {
url.ensureLowercaseScheme(schemes) `should be equal to` expectedResult
}

companion object {

@JvmStatic
fun urlArguments() = listOf(
Arguments.of(
"http://www.getstream.io",
listOf("https://", "http://"),
"http://www.getstream.io",
),
Arguments.of(
"https://www.getstream.io",
listOf("https://", "http://"),
"https://www.getstream.io",
),
Arguments.of(
"HTTPS://www.getstream.io",
listOf("https://", "http://"),
"https://www.getstream.io",
),
Arguments.of(
"HTtPS://www.getstream.io",
listOf("https://", "http://"),
"https://www.getstream.io",
),
Arguments.of(
"HTtPS://www.getstream.io/SomePath",
listOf("https://", "http://"),
"https://www.getstream.io/SomePath",
),
Arguments.of(
"www.getstream.io/SomePath",
listOf("https://", "http://"),
"https://www.getstream.io/SomePath",
),
)
}
}

0 comments on commit 4388f62

Please sign in to comment.