Skip to content

Commit

Permalink
iOS: Improve HTML text parsing and add iOS tests (#418)
Browse files Browse the repository at this point in the history
### TL;DR
Enhanced HTML text handling for iOS platform and added comprehensive unit tests.

### What changed?
- Added error handling to iOS HTML text processing with `runCatching`
- Improved HTML tag replacement by adding support for `</ul>` and `<li>` tags
- Added trim functionality to remove unnecessary whitespace
- Created unit tests to verify HTML tag replacement functionality

### Why make this change?
To improve the reliability of HTML text processing on iOS and ensure consistent text display across different HTML formats. The addition of unit tests provides confidence in the HTML parsing functionality and helps prevent regressions.
  • Loading branch information
ksharma-xyz authored Dec 3, 2024
1 parent 9e70a00 commit 036ecf5
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 036ecf5

Please sign in to comment.