Skip to content

Commit

Permalink
Fix '?' prefix being unescaped on Android target
Browse files Browse the repository at this point in the history
  • Loading branch information
dewantawsif committed Sep 28, 2024
1 parent cec8905 commit 438db81
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -57,20 +57,16 @@ internal fun String.removeAndroidMirroringFormat(): String {
.replace("""\@""", "@")
}

private val androidLinkingCharacters = setOf('@', '?')

internal fun String.convertXmlStringToAndroidLocalization(): String {
// Android resources should comply with requirements:
// https://developer.android.com/guide/topics/resources/string-resource#escaping_quotes
return StringEscapeUtils
.unescapeXml(this)
.replace("\n", "\\n")
.let { StringEscapeUtils.escapeXml11(it) }
.let {
if (it.getOrNull(0) == '@') {
replaceFirst("@", """\@""")
} else {
it
}
}
.replaceFirstChar { if (it in androidLinkingCharacters) "\\$it" else "$it" }
.replace(""", "\\"")
.replace("'", "\\'")
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -124,10 +124,18 @@ class XmlStringsToPlatformTest {
}

@Test
fun stringLikeAndroidLinkOnStringAndroidTest() {
fun stringLikeAndroidResourceLinkOnStringAndroidTest() {
assertEquals(
expected = """\@same text""",
actual = """@same text""".convertXmlStringToAndroidLocalization()
)
}

@Test
fun stringLikeAndroidAttributeLinkOnStringAndroidTest() {
assertEquals(
expected = """\?same text""",
actual = """?same text""".convertXmlStringToAndroidLocalization()
)
}
}

0 comments on commit 438db81

Please sign in to comment.