Skip to content

Commit 760ff51

Browse files
authored
Fix toPlainText where <ol start='n'> tags appear (#5044)
1 parent 7f65075 commit 760ff51

File tree

2 files changed

+31
-2
lines changed
  • libraries/matrixui/src

2 files changed

+31
-2
lines changed

libraries/matrixui/src/main/kotlin/io/element/android/libraries/matrix/ui/messages/ToPlainText.kt

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,10 +57,16 @@ private class PlainTextNodeVisitor : NodeVisitor {
5757
if (node is TextNode && node.text().isNotBlank()) {
5858
builder.append(node.text())
5959
} else if (node is Element && node.tagName() == "li") {
60-
val index = node.elementSiblingIndex()
60+
val index = node.elementSiblingIndex() + 1
6161
val isOrdered = node.parent()?.nodeName()?.lowercase() == "ol"
6262
if (isOrdered) {
63-
builder.append("${index + 1}. ")
63+
val startIndex = node.parent()?.attr("start")?.toIntOrNull()
64+
val actualIndex = if (startIndex != null) {
65+
startIndex + index - 1
66+
} else {
67+
index
68+
}
69+
builder.append("$actualIndex. ")
6470
} else {
6571
builder.append("")
6672
}

libraries/matrixui/src/test/kotlin/io/element/android/libraries/matrix/ui/messages/ToPlainTextTest.kt

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,29 @@ class ToPlainTextTest {
9696
)
9797
}
9898

99+
@Test
100+
fun `TextMessageType toPlainText - respects the ol start attr if present`() {
101+
val messageType = TextMessageType(
102+
body = "1. First item\n2. Second item\n",
103+
formatted = FormattedBody(
104+
format = MessageFormat.HTML,
105+
body = """
106+
<ol start='11'>
107+
<li>First item.</li>
108+
<li>Second item.</li>
109+
</ol>
110+
<br />
111+
""".trimIndent()
112+
)
113+
)
114+
assertThat(messageType.toPlainText(permalinkParser = FakePermalinkParser())).isEqualTo(
115+
"""
116+
11. First item.
117+
12. Second item.
118+
""".trimIndent()
119+
)
120+
}
121+
99122
@Test
100123
fun `TextMessageType toPlainText - returns the markdown body if the formatted one cannot be parsed`() {
101124
val messageType = TextMessageType(

0 commit comments

Comments
 (0)