-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add default annotationProviders and test
- Loading branch information
Showing
9 changed files
with
207 additions
and
15 deletions.
There are no files selected for viewing
30 changes: 30 additions & 0 deletions
30
...linktext/src/androidMain/kotlin/sh/calvin/autolinktext/MatchAnnotationProvider.android.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
package sh.calvin.autolinktext | ||
|
||
import android.telephony.PhoneNumberUtils | ||
import androidx.core.net.toUri | ||
import java.net.URL | ||
|
||
internal actual fun getMatchAnnotationProviderDefaults() = | ||
object : MatchAnnotationProviderDefaultsInterface { | ||
override val WebUrl: MatchAnnotationProvider<*> | ||
get() = { | ||
val url = it.matchedText.toUri().let { | ||
if (it.scheme == null) | ||
URL("https://${it}") | ||
else | ||
it | ||
} | ||
url.toString() | ||
} | ||
|
||
override val EmailAddress: MatchAnnotationProvider<*> | ||
get() = { | ||
"mailto:${it.matchedText}" | ||
} | ||
|
||
override val PhoneNumber: MatchAnnotationProvider<*> | ||
get() = { | ||
val phone = PhoneNumberUtils.normalizeNumber(it.matchedText) | ||
"tel:${phone}" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,6 @@ | ||
package sh.calvin.autolinktext | ||
|
||
import androidx.compose.ui.text.ExperimentalTextApi | ||
import org.junit.runner.RunWith | ||
import org.robolectric.RobolectricTestRunner | ||
import org.robolectric.RuntimeEnvironment | ||
|
@@ -104,4 +105,36 @@ class AndroidTextRuleTest { | |
assertEquals("604-555-7890", text.slice(matches[5])) | ||
assertEquals(rules[2], matches[5].rule) | ||
} | ||
|
||
@OptIn(NotForAndroid::class, ExperimentalTextApi::class) | ||
@Test | ||
fun testAnnotateString() { | ||
val context = RuntimeEnvironment.getApplication() | ||
val contextData = AndroidContextData(context) | ||
|
||
val text = "Visit https://www.google.com\n" + | ||
"Visit www.google.com\n" + | ||
"Email [email protected]\n" + | ||
"Call 6045557890\n" + | ||
"Call +1 (604) 555-7890\n" + | ||
"Call 604-555-7890\n" | ||
val rules = listOf( | ||
TextRuleDefaults.webUrl(contextData), | ||
TextRuleDefaults.emailAddress(contextData), | ||
TextRuleDefaults.phoneNumber(contextData), | ||
) | ||
val matches = rules.getAllMatches(text) | ||
val annotatedString = matches.annotateString(text) | ||
|
||
fun getUrlAtMatch(index: Int) = annotatedString.getUrlAnnotations( | ||
matches[index].start, matches[index].endExclusive | ||
).first().item.url | ||
|
||
assertEquals("https://www.google.com", getUrlAtMatch(0)) | ||
assertEquals("https://www.google.com", getUrlAtMatch(1)) | ||
assertEquals("mailto:[email protected]", getUrlAtMatch(2)) | ||
assertEquals("tel:6045557890", getUrlAtMatch(3)) | ||
assertEquals("tel:+16045557890", getUrlAtMatch(4)) | ||
assertEquals("tel:6045557890", getUrlAtMatch(5)) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,6 +4,7 @@ import kotlin.test.Test | |
import kotlin.test.assertEquals | ||
import kotlin.test.assertNotEquals | ||
import kotlin.test.assertNull | ||
import androidx.compose.ui.text.ExperimentalTextApi | ||
|
||
class TextRuleTest { | ||
@Test | ||
|
@@ -200,27 +201,39 @@ class TextRuleTest { | |
assertEquals(rules[1], matches[2].rule) | ||
assertEquals("6045557890", text.slice(matches[3])) | ||
assertEquals(rules[2], matches[3].rule) | ||
assertEquals("+1 (604) 555-7890", text.slice(matches[4])) | ||
assertEqualsOneOf(listOf("+1 (604) 555-7890", "(604) 555-7890"), text.slice(matches[4])) | ||
assertEquals(rules[2], matches[4].rule) | ||
assertEquals("604-555-7890", text.slice(matches[5])) | ||
assertEquals(rules[2], matches[5].rule) | ||
} | ||
|
||
@OptIn(NotForAndroid::class) | ||
@OptIn(NotForAndroid::class, ExperimentalTextApi::class) | ||
@IgnoreAndroid | ||
@Test | ||
fun textWithAnnotations() { | ||
val text = "read our privacy policy" | ||
fun testAnnotateString() { | ||
val text = "Visit https://www.google.com\n" + | ||
"Visit www.google.com\n" + | ||
"Email [email protected]\n" + | ||
"Call 6045557890\n" + | ||
"Call +1 (604) 555-7890\n" + | ||
"Call 604-555-7890\n" | ||
val rules = listOf( | ||
TextRule( | ||
textMatcher = TextMatcher.StringMatcher("privacy policy"), | ||
annotationProvider = { "https://example.com/privacy" }, | ||
), | ||
TextRuleDefaults.webUrl(NullContextData), | ||
TextRuleDefaults.emailAddress(NullContextData), | ||
TextRuleDefaults.phoneNumber(NullContextData), | ||
) | ||
val matches = rules.getAllMatches(text) | ||
|
||
assertEquals(1, matches.size) | ||
assertEquals("[email protected]", text.slice(matches[0])) | ||
assertEquals(rules[1], matches[0].rule) | ||
val annotatedString = matches.annotateString(text) | ||
|
||
fun getUrlAtMatch(index: Int) = annotatedString.getUrlAnnotations( | ||
matches[index].start, matches[index].endExclusive | ||
).first().item.url | ||
|
||
assertEquals("https://www.google.com", getUrlAtMatch(0)) | ||
assertEqualsOneOf(listOf("https://www.google.com", "http://www.google.com"), getUrlAtMatch(1)) | ||
assertEquals("mailto:[email protected]", getUrlAtMatch(2)) | ||
assertEquals("tel:6045557890", getUrlAtMatch(3)) | ||
assertEquals("tel:+16045557890", getUrlAtMatch(4)) | ||
assertEquals("tel:6045557890", getUrlAtMatch(5)) | ||
} | ||
} |
7 changes: 7 additions & 0 deletions
7
autolinktext/src/commonTest/kotlin/sh/calvin/autolinktext/helpers.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
package sh.calvin.autolinktext | ||
|
||
import kotlin.test.assertTrue | ||
|
||
fun assertEqualsOneOf(expected: List<String>, actual: String) { | ||
assertTrue(expected.contains(actual), "Expected one of $expected, but was $actual") | ||
} |
38 changes: 38 additions & 0 deletions
38
autolinktext/src/iosMain/kotlin/sh/calvin/autolinktext/MatchAnnotationProvider.ios.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
package sh.calvin.autolinktext | ||
|
||
import platform.Foundation.NSURL | ||
import platform.Foundation.NSURLComponents | ||
|
||
object IosMatchAnnotationProviderDefaults : MatchAnnotationProviderDefaultsInterface { | ||
override val WebUrl: MatchAnnotationProvider<*> | ||
get() = { | ||
val url = when (val data = it.data) { | ||
is NSURL -> data | ||
else -> NSURLComponents(it.matchedText).apply { | ||
scheme = scheme ?: "https" | ||
}.URL | ||
} | ||
url?.absoluteString | ||
} | ||
|
||
override val EmailAddress: MatchAnnotationProvider<*> | ||
get() = { | ||
val url = when (val data = it.data) { | ||
is NSURL -> data | ||
else -> NSURL(string = "mailto:${it.matchedText}") | ||
} | ||
url.absoluteString | ||
} | ||
|
||
override val PhoneNumber: MatchAnnotationProvider<*> | ||
get() = { | ||
val url = when (val data = it.data) { | ||
is NSURL -> data | ||
else -> NSURL(string = "tel:${normalizePhoneNumber(it.matchedText)}") | ||
} | ||
url.absoluteString | ||
} | ||
} | ||
|
||
internal actual fun getMatchAnnotationProviderDefaults(): MatchAnnotationProviderDefaultsInterface = | ||
IosMatchAnnotationProviderDefaults |
28 changes: 28 additions & 0 deletions
28
autolinktext/src/jvmMain/kotlin/sh/calvin/autolinktext/MatchAnnotationProvider.jvm.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
package sh.calvin.autolinktext | ||
|
||
import java.net.URI | ||
|
||
internal actual fun getMatchAnnotationProviderDefaults() = | ||
object : MatchAnnotationProviderDefaultsInterface { | ||
override val WebUrl: MatchAnnotationProvider<*> | ||
get() = { result -> | ||
val url = URI(result.matchedText).let { | ||
if (it.scheme == null || it.scheme.isEmpty()) { | ||
URI("https://${result.matchedText}") | ||
} else { | ||
it | ||
} | ||
} | ||
url.toString() | ||
} | ||
|
||
override val EmailAddress: MatchAnnotationProvider<*> | ||
get() = { | ||
"mailto:${it.matchedText}" | ||
} | ||
|
||
override val PhoneNumber: MatchAnnotationProvider<*> | ||
get() = { | ||
"tel:${normalizePhoneNumber(it.matchedText)}" | ||
} | ||
} |
25 changes: 25 additions & 0 deletions
25
autolinktext/src/wasmJsMain/kotlin/sh/calvin/autolinktext/MatchAnnotationProvider.wasmJs.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
package sh.calvin.autolinktext | ||
|
||
internal actual fun getMatchAnnotationProviderDefaults() = | ||
object : MatchAnnotationProviderDefaultsInterface { | ||
override val WebUrl: MatchAnnotationProvider<*> | ||
get() = { | ||
var url = it.matchedText | ||
val protocolRegex = Regex("^\\S+://.+$") | ||
val hasProtocol = protocolRegex.matches(url) | ||
if (!hasProtocol) { | ||
url = "https://$url" | ||
} | ||
url | ||
} | ||
|
||
override val EmailAddress: MatchAnnotationProvider<*> | ||
get() = { | ||
"mailto:${it.matchedText}" | ||
} | ||
|
||
override val PhoneNumber: MatchAnnotationProvider<*> | ||
get() = { | ||
"tel:${normalizePhoneNumber(it.matchedText)}" | ||
} | ||
} |