-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
iOS: Improve HTML text parsing and add iOS tests (#418)
### 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
1 parent
9e70a00
commit 036ecf5
Showing
4 changed files
with
100 additions
and
30 deletions.
There are no files selected for viewing
File renamed without changes.
This file was deleted.
Oops, something went wrong.
37 changes: 37 additions & 0 deletions
37
...e/trip-planner/ui/src/iosMain/kotlin/xyz/ksharma/krail/trip/planner/ui/alerts/HtmlText.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,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(" ", " ") | ||
.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 | ||
} | ||
} |
63 changes: 63 additions & 0 deletions
63
...ip-planner/ui/src/iosTest/kotlin/xyz/ksharma/krail/trip/planner/ui/alerts/HtmlTextTest.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,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 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> World" | ||
val expected = "Hello\n Link World" | ||
assertEquals(expected, replaceHtmlTags(input)) | ||
} | ||
} |