Skip to content

Commit f99b38e

Browse files
jmartinespbmarty
andauthored
Android: improve handling of line break chars in text nodes (#67)
Co-authored-by: Benoit Marty <[email protected]>
1 parent df2c5b3 commit f99b38e

File tree

2 files changed

+27
-2
lines changed

2 files changed

+27
-2
lines changed

platforms/android/library/src/main/java/io/element/android/wysiwyg/utils/HtmlToSpansParser.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -388,7 +388,7 @@ internal class HtmlToSpansParser(
388388
} else if ((stripLeading && !reachedNonWhite) || lastWasWhite) {
389389
i += Character.charCount(c)
390390
continue
391-
} else if (c == '\n'.code && i == text.length - 1) {
391+
} else if (c == '\n'.code && (i == text.length - 1 || !reachedNonWhite)) {
392392
// Do nothing, this is probably just an HTML formatting line break
393393
} else {
394394
result.append(' ')

platforms/android/library/src/test/kotlin/io/element/android/wysiwyg/utils/HtmlToSpansParserTest.kt

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@
99
package io.element.android.wysiwyg.utils
1010

1111
import android.text.Spanned
12-
import io.element.android.wysiwyg.display.TextDisplay
1312
import io.element.android.wysiwyg.display.MentionDisplayHandler
13+
import io.element.android.wysiwyg.display.TextDisplay
1414
import io.element.android.wysiwyg.test.fakes.createFakeStyleConfig
1515
import io.element.android.wysiwyg.test.utils.dumpSpans
1616
import io.element.android.wysiwyg.view.spans.OrderedListSpan
@@ -270,6 +270,31 @@ class HtmlToSpansParserTest {
270270
)
271271
}
272272

273+
@Test
274+
fun testLeadingLineBreakCharsInHtmlTextAreIgnored() {
275+
val html = "<p>First Line</p>\n<p>Line after Empty Line<br />\nThird Line</p>\n"
276+
val spanned = convertHtml(
277+
html = html,
278+
isEditor = false,
279+
)
280+
assertThat(
281+
spanned.toString(), equalTo("First Line\n\nLine after Empty Line\nThird Line")
282+
)
283+
}
284+
285+
@Test
286+
fun testLineBreakCharsInMiddleOrEndOfHtmlTextAreConvertedToWhitespace() {
287+
val html = "<p>First Line\n</p><p>Line after Empty Line<br />Third\n\n\n\nWith more</p>"
288+
val spanned = convertHtml(
289+
html = html,
290+
isEditor = false,
291+
)
292+
assertThat(
293+
spanned.toString(),
294+
equalTo("First Line\n\nLine after Empty Line\nThird With more")
295+
)
296+
}
297+
273298
private fun convertHtml(
274299
html: String,
275300
isEditor: Boolean = true,

0 commit comments

Comments
 (0)