Skip to content

Commit

Permalink
Fix: HtmlText crash - iOS
Browse files Browse the repository at this point in the history
  • Loading branch information
ksharma-xyz committed Dec 3, 2024
1 parent 5916459 commit c4f809e
Show file tree
Hide file tree
Showing 4 changed files with 100 additions and 30 deletions.
30 changes: 0 additions & 30 deletions feature/trip-planner/ui/src/iosMain/kotlin/HtmlText.kt

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package xyz.ksharma.krail.trip.planner.ui.alerts

import androidx.compose.foundation.clickable
import androidx.compose.runtime.Composable
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import xyz.ksharma.krail.taj.components.Text

@Composable
actual fun HtmlText(
text: String,
modifier: Modifier,
onClick: () -> Unit,
color: Color,
urlColor: Color,
) {
Text(text = replaceHtmlTags(text), modifier = Modifier.clickable { onClick() }, color = color)
}

// TODO - A workaround for iOS until https://issuetracker.google.com/issues/139326648 is fixed.
fun replaceHtmlTags(html: String): String {
return runCatching {
html
.replace("</div>", "\n")
.replace("</li>", "\n")
.replace("</ul>", "\n")
.replace("<li>", "")
.replace("<ul>", "")
.replace(Regex("<a href=\"[^\"]*\">"), " ")
.replace("&nbsp;", " ")
.replace(Regex("<[^>]*>"), "") // Remove all other HTML tags
.replace(Regex("\n{3,}"), "\n\n") // Replace multiple new lines with a single new line
.trim()
}.getOrElse {
html // Return the original text if an exception occurs
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
package xyz.ksharma.krail.trip.planner.ui.alerts

import kotlin.test.Test
import kotlin.test.assertEquals

class HtmlTextTest {

@Test
fun testReplaceHtmlTags_withDiv() {
val input = "Hello</div>World"
val expected = "Hello\nWorld"
assertEquals(expected, replaceHtmlTags(input))
}

@Test
fun testReplaceHtmlTags_withLi() {
val input = "Item 1</li>Item 2"
val expected = "Item 1\nItem 2"
assertEquals(expected, replaceHtmlTags(input))
}

@Test
fun testReplaceHtmlTags_withUl() {
val input = "<ul>Item 1</ul>"
val expected = "Item 1"
assertEquals(expected, replaceHtmlTags(input))
}

@Test
fun testReplaceHtmlTags_withAnchor() {
val input = "<a href=\"http://example.com\">Link</a>"
val expected = "Link"
assertEquals(expected, replaceHtmlTags(input))
}

@Test
fun testReplaceHtmlTags_withNbsp() {
val input = "Hello&nbsp;World"
val expected = "Hello World"
assertEquals(expected, replaceHtmlTags(input))
}

@Test
fun testReplaceHtmlTags_withMultipleHtmlTags() {
val input = "<div>Hello</div><ul>Item 1</ul><li>Item 2</li>"
val expected = "Hello\nItem 1\nItem 2"
assertEquals(expected, replaceHtmlTags(input))
}

@Test
fun testReplaceHtmlTags_withMultipleNewLines() {
val input = "Hello\n\n\nWorld"
val expected = "Hello\n\nWorld"
assertEquals(expected, replaceHtmlTags(input))
}

@Test
fun testReplaceHtmlTags_withComplexHtml() {
val input = "<div>Hello</div><a href=\"http://example.com\">Link</a>&nbsp;World"
val expected = "Hello\n Link World"
assertEquals(expected, replaceHtmlTags(input))
}
}

0 comments on commit c4f809e

Please sign in to comment.