Applies new UrlDetector to the RichTextViewer

This commit is contained in:
Vitor Pamplona
2026-03-07 16:38:25 -05:00
parent 030921c963
commit e3f99051db
12 changed files with 178 additions and 73 deletions
@@ -220,6 +220,15 @@ class DomainNameReader(
// done = true
lastWasAscii = isAscii
} else if (index == 0) {
println("First Char: $curr ${curr in UrlDetector.CANNOT_BEGIN_URLS_WITH}")
if (curr in UrlDetector.CANNOT_BEGIN_URLS_WITH) {
newStart = index + 1
currentLabelLength = 0
topLevelLength = 0
numeric = true
dots = 0
}
}
index++
}
@@ -650,7 +650,9 @@ class UrlDetector(
if (state == ReadEndState.ValidUrl && buffer.isNotEmpty()) {
// Add the url to the list of good urls.
if (buffer.isNotEmpty()) {
urlList.add(currentUrlMarker.createUrl(buffer.toString()))
var url = buffer.toString()
if (url.lastOrNull() in CANNOT_END_URLS_WITH) url = url.dropLast(1)
urlList.add(currentUrlMarker.createUrl(url))
}
}
@@ -669,7 +671,7 @@ class UrlDetector(
}
companion object {
private val VALID_SCHEMES_NO_SLASHES: List<String> =
val VALID_SCHEMES_NO_SLASHES: List<String> =
listOf(
"http:",
"https:",
@@ -681,9 +683,42 @@ class UrlDetector(
"blossom:",
)
private val VALID_SCHEMES =
val VALID_SCHEMES =
VALID_SCHEMES_NO_SLASHES.map {
"$it//"
}
val CANNOT_BEGIN_URLS_WITH =
setOf(
',',
'.',
';',
'?',
'!',
')',
'}',
'(',
'{',
'\u3002',
'\uFF0E',
'\uFF61',
)
val CANNOT_END_URLS_WITH =
setOf(
',',
'.',
';',
'?',
'!',
':',
')',
'}',
'(',
'{',
'\u3002',
'\uFF0E',
'\uFF61',
)
}
}
@@ -58,7 +58,7 @@ class UriDetectionTest {
runTest(
"the url google.com is a lot better then www.google.com.",
"google.com",
"www.google.com.",
"www.google.com",
)
}
@@ -76,7 +76,7 @@ class UriDetectionTest {
"this is an international domain: http://\u043F\u0440\u0438\u043c\u0435\u0440.\u0438\u0441\u043f\u044b" +
"\u0442\u0430\u043d\u0438\u0435 so is this: \u4e94\u7926\u767c\u5c55.\u4e2d\u570b.",
"http://\u043F\u0440\u0438\u043c\u0435\u0440.\u0438\u0441\u043f\u044b\u0442\u0430\u043d\u0438\u0435",
"\u4e94\u7926\u767c\u5c55.\u4e2d\u570b.",
"\u4e94\u7926\u767c\u5c55.\u4e2d\u570b",
)
}
@@ -215,7 +215,7 @@ class UriDetectionTest {
"www.www",
"yahoo.com",
"yahoo.com.br",
"hello.hello.",
"hello.hello",
"hello.com",
)
}
@@ -395,7 +395,7 @@ class UriDetectionTest {
runTest("%2ewtfismyip")
runTest("wtfismyip%2e")
runTest("wtfismyip%2ecom%2e", "wtfismyip%2ecom%2e")
runTest("wtfismyip%2ecom.", "wtfismyip%2ecom.")
runTest("wtfismyip%2ecom.", "wtfismyip%2ecom")
runTest("%2ewtfismyip%2ecom", "wtfismyip%2ecom")
}
@@ -662,7 +662,7 @@ class UriDetectionTest {
runTest("hello:password@go12//", "hello:password@go12//")
runTest("hello:password@go12", "hello:password@go12")
runTest("hello:password@go12 lala", "hello:password@go12")
runTest("hello.com..", "hello.com.")
runTest("hello.com..", "hello.com")
runTest("a/")
runTest("4/5")
runTest("concerns/worries")
@@ -694,7 +694,7 @@ class UriDetectionTest {
@Test
fun testNostr() {
runTest("Check this post nostr:somethingsomething . I think it is really cool", "nostr:somethingsomething")
runTest("Check this post nostr:npub1048qg5p6kfnpth2l98kq3dffg097tutm4npsz2exygx25ge2k9xqf5x3nf . I think it is really cool", "nostr:npub1048qg5p6kfnpth2l98kq3dffg097tutm4npsz2exygx25ge2k9xqf5x3nf")
}
@Test
@@ -704,7 +704,7 @@ class UriDetectionTest {
@Test
fun testNostrSlashes() {
runTest("Check this post nostr://somethingsomething . I think it is really cool", "nostr://somethingsomething")
runTest("Check this post nostr://npub1048qg5p6kfnpth2l98kq3dffg097tutm4npsz2exygx25ge2k9xqf5x3nf . I think it is really cool", "nostr://npub1048qg5p6kfnpth2l98kq3dffg097tutm4npsz2exygx25ge2k9xqf5x3nf")
}
@Test
@@ -742,6 +742,45 @@ class UriDetectionTest {
runTest("ほtest.comほ", "test.com")
}
@Test
fun testBeginsAndEndsWithPunctuation() {
UrlDetector.CANNOT_END_URLS_WITH.forEach { punctuation ->
runTest("${punctuation}http://test.com?s=dd", "http://test.com?s=dd")
runTest("http://test.com?s=dd$punctuation", "http://test.com?s=dd")
runTest("${punctuation}http://test.com?s=dd$punctuation", "http://test.com?s=dd")
runTest("${punctuation}http://test.com/", "http://test.com/")
runTest("http://test.com/.", "http://test.com/")
runTest("${punctuation}http://test.com/$punctuation", "http://test.com/")
runTest("${punctuation}http://test.com", "http://test.com")
runTest("http://test.com$punctuation", "http://test.com")
runTest("${punctuation}http://test.com$punctuation", "http://test.com")
runTest("${punctuation}test.com", "test.com")
runTest("test.com$punctuation", "test.com")
runTest("${punctuation}test.com$punctuation", "test.com")
}
UrlDetector.CANNOT_BEGIN_URLS_WITH.forEach { punctuation ->
runTest("${punctuation}http://test.com?s=dd", "http://test.com?s=dd")
runTest("http://test.com?s=dd$punctuation", "http://test.com?s=dd")
runTest("${punctuation}http://test.com?s=dd$punctuation", "http://test.com?s=dd")
runTest("${punctuation}http://test.com/", "http://test.com/")
runTest("http://test.com/.", "http://test.com/")
runTest("${punctuation}http://test.com/$punctuation", "http://test.com/")
runTest("${punctuation}http://test.com", "http://test.com")
runTest("http://test.com$punctuation", "http://test.com")
runTest("${punctuation}http://test.com$punctuation", "http://test.com")
runTest("${punctuation}test.com", "test.com")
runTest("test.com$punctuation", "test.com")
runTest("${punctuation}test.com$punctuation", "test.com")
}
}
private fun runTest(
text: String,
vararg expected: String?,