Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ensure only the protocol scheme is lowercase #5314

Merged
merged 2 commits into from
Jul 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@
## stream-chat-android-compose
### 🐞 Fixed
- Fixed deleted pinned messages being highlighted as pinned. [#5315](https://github.com/GetStream/stream-chat-android/pull/5315)
- Fixed the url used when click on the link. [#5314](https://github.com/GetStream/stream-chat-android/pull/5314)

### ⬆️ Improved
- Enabled Strong Skipping Mode for Compose compiler and improved Compose performance. [#5303](https://github.com/GetStream/stream-chat-android/pull/5303)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ import androidx.compose.ui.text.font.FontStyle
import androidx.compose.ui.text.style.TextDecoration
import androidx.core.util.PatternsCompat
import io.getstream.chat.android.compose.ui.theme.ChatTheme
import java.util.Locale
import java.util.regex.Pattern

internal typealias AnnotationTag = String
Expand Down Expand Up @@ -137,7 +136,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 +147,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,72 @@
/*
* Copyright (c) 2014-2024 Stream.io Inc. All rights reserved.
*
* Licensed under the Stream License;
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://github.com/GetStream/stream-chat-android/blob/main/LICENSE
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

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",
),
)
}
}
Loading