Skip to content
This repository was archived by the owner on Sep 27, 2024. It is now read-only.

Commit 6f982ac

Browse files
authored
Merge pull request #1019 from matrix-org/android/use-jsoup-for-html-parsing-fixes-line-breaks
Use Jsoup instead of TagSoup for HTML parsing.
2 parents f407db4 + 5cfb891 commit 6f982ac

File tree

8 files changed

+296
-452
lines changed

8 files changed

+296
-452
lines changed

platforms/android/example-compose/src/main/java/io/element/wysiwyg/compose/MainActivity.kt

+3
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,9 @@ class MainActivity : ComponentActivity() {
8181
var linkDialogAction by remember { mutableStateOf<LinkAction?>(null) }
8282
val coroutineScope = rememberCoroutineScope()
8383

84+
LaunchedEffect(state.messageHtml) {
85+
Timber.d("Message HTML: '${state.messageHtml}'")
86+
}
8487
val htmlText = htmlConverter.fromHtmlToSpans(state.messageHtml)
8588

8689
linkDialogAction?.let { linkAction ->

platforms/android/gradle/libs.versions.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ google-material = { module="com.google.android.material:material", version.ref="
5757

5858
# Misc
5959
timber = { module="com.jakewharton.timber:timber", version.ref="timber" }
60-
tagsoup = { module="org.ccil.cowan.tagsoup:tagsoup", version.ref="tagsoup" }
60+
jsoup = "org.jsoup:jsoup:1.18.1"
6161
molecule-runtime = { module = "app.cash.molecule:molecule-runtime", version.ref = "molecule" }
6262

6363
# Test

platforms/android/library/build.gradle

+2-2
Original file line numberDiff line numberDiff line change
@@ -90,8 +90,8 @@ dependencies {
9090

9191
implementation libs.timber
9292

93-
// XML Parsing
94-
api libs.tagsoup
93+
// HTML Parsing
94+
api libs.jsoup
9595

9696
implementation libs.androidx.core
9797
implementation libs.androidx.appcompat

platforms/android/library/src/androidTest/java/io/element/android/wysiwyg/inputhandlers/InterceptInputConnectionIntegrationTest.kt

+18-17
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,8 @@ class InterceptInputConnectionIntegrationTest {
2626
private val viewModel = EditorViewModel(
2727
provideComposer = { newComposerModel() },
2828
).also {
29-
it.htmlConverter = HtmlConverter.Factory.create(context = app,
29+
it.htmlConverter = HtmlConverter.Factory.create(
30+
context = app,
3031
styleConfig = styleConfig,
3132
mentionDisplayHandler = null,
3233
)
@@ -57,7 +58,7 @@ class InterceptInputConnectionIntegrationTest {
5758
textView.text.dumpSpans(), equalTo(
5859
listOf(
5960
"hello: android.widget.TextView.ChangeWatcher (0-5) fl=#6553618",
60-
"hello: android.text.style.StyleSpan (0-5) fl=#33",
61+
"hello: android.text.style.StyleSpan (0-5) fl=#17",
6162
"hello: android.text.method.TextKeyListener (0-5) fl=#18",
6263
"hello: android.text.style.UnderlineSpan (0-5) fl=#289",
6364
"hello: android.view.inputmethod.ComposingText (0-5) fl=#289",
@@ -80,7 +81,7 @@ class InterceptInputConnectionIntegrationTest {
8081
assertThat(
8182
textView.text.dumpSpans(), equalTo(
8283
baseEditedSpans.toMutableList().apply {
83-
add(1, "world: android.text.style.StyleSpan (0-5) fl=#33")
84+
add(1, "world: android.text.style.StyleSpan (0-5) fl=#17")
8485
}
8586
)
8687
)
@@ -97,7 +98,7 @@ class InterceptInputConnectionIntegrationTest {
9798
assertThat(
9899
textView.text.dumpSpans(), equalTo(
99100
baseEditedSpans.toMutableList().apply {
100-
add(1, "world: android.text.style.UnderlineSpan (0-5) fl=#33")
101+
add(1, "world: android.text.style.UnderlineSpan (0-5) fl=#17")
101102
}
102103
)
103104
)
@@ -114,7 +115,7 @@ class InterceptInputConnectionIntegrationTest {
114115
assertThat(
115116
textView.text.dumpSpans(), equalTo(
116117
baseEditedSpans.toMutableList().apply {
117-
add(1, "world: android.text.style.StrikethroughSpan (0-5) fl=#33")
118+
add(1, "world: android.text.style.StrikethroughSpan (0-5) fl=#17")
118119
}
119120
)
120121
)
@@ -131,7 +132,7 @@ class InterceptInputConnectionIntegrationTest {
131132
assertThat(
132133
textView.text.dumpSpans(), equalTo(
133134
baseEditedSpans.toMutableList().apply {
134-
add(1, "world: io.element.android.wysiwyg.view.spans.InlineCodeSpan (0-5) fl=#33")
135+
add(1, "world: io.element.android.wysiwyg.view.spans.InlineCodeSpan (0-5) fl=#17")
135136
}
136137
)
137138
)
@@ -147,7 +148,7 @@ class InterceptInputConnectionIntegrationTest {
147148
textView.text.dumpSpans(), equalTo(
148149
listOf(
149150
"hello: android.widget.TextView.ChangeWatcher (0-5) fl=#6553618",
150-
"hello: io.element.android.wysiwyg.view.spans.OrderedListSpan (0-5) fl=#34",
151+
"hello: io.element.android.wysiwyg.view.spans.OrderedListSpan (0-5) fl=#17",
151152
"hello: android.text.style.UnderlineSpan (0-5) fl=#289",
152153
"hello: android.view.inputmethod.ComposingText (0-5) fl=#289",
153154
"hello: android.text.method.TextKeyListener (0-5) fl=#18",
@@ -173,7 +174,7 @@ class InterceptInputConnectionIntegrationTest {
173174
textView.text.dumpSpans().joinToString(",\n"), equalTo(
174175
"""
175176
hello: android.widget.TextView.ChangeWatcher (0-5) fl=#6553618,
176-
hello: io.element.android.wysiwyg.view.spans.UnorderedListSpan (0-5) fl=#34,
177+
hello: io.element.android.wysiwyg.view.spans.UnorderedListSpan (0-5) fl=#17,
177178
hello: android.text.style.UnderlineSpan (0-5) fl=#289,
178179
hello: android.view.inputmethod.ComposingText (0-5) fl=#289,
179180
hello: android.text.method.TextKeyListener (0-5) fl=#18,
@@ -195,7 +196,7 @@ class InterceptInputConnectionIntegrationTest {
195196
textView.text.dumpSpans().joinToString(",\n"), equalTo(
196197
"""
197198
hello: android.widget.TextView.ChangeWatcher (0-5) fl=#6553618,
198-
hello: io.element.android.wysiwyg.view.spans.UnorderedListSpan (0-5) fl=#34,
199+
hello: io.element.android.wysiwyg.view.spans.UnorderedListSpan (0-5) fl=#17,
199200
hello: android.text.style.UnderlineSpan (0-5) fl=#289,
200201
hello: android.view.inputmethod.ComposingText (0-5) fl=#289,
201202
hello: android.text.method.TextKeyListener (0-5) fl=#18,
@@ -221,7 +222,7 @@ class InterceptInputConnectionIntegrationTest {
221222
textView.text.dumpSpans().joinToString(",\n"), equalTo(
222223
"""
223224
hello: android.widget.TextView.ChangeWatcher (0-5) fl=#6553618,
224-
hello: io.element.android.wysiwyg.view.spans.OrderedListSpan (0-5) fl=#34,
225+
hello: io.element.android.wysiwyg.view.spans.OrderedListSpan (0-5) fl=#17,
225226
hello: android.text.style.UnderlineSpan (0-5) fl=#289,
226227
hello: android.view.inputmethod.ComposingText (0-5) fl=#289,
227228
hello: android.text.method.TextKeyListener (0-5) fl=#18,
@@ -244,7 +245,7 @@ class InterceptInputConnectionIntegrationTest {
244245
textView.text.dumpSpans(), equalTo(
245246
listOf(
246247
"😋😋: android.widget.TextView.ChangeWatcher (0-4) fl=#6553618",
247-
"😋😋: io.element.android.wysiwyg.view.spans.OrderedListSpan (0-4) fl=#34",
248+
"😋😋: io.element.android.wysiwyg.view.spans.OrderedListSpan (0-4) fl=#17",
248249
"😋😋: android.text.style.UnderlineSpan (0-4) fl=#289",
249250
"😋😋: android.view.inputmethod.ComposingText (0-4) fl=#289",
250251
"😋😋: android.text.method.TextKeyListener (0-4) fl=#18",
@@ -270,7 +271,7 @@ class InterceptInputConnectionIntegrationTest {
270271
textView.text.dumpSpans(), equalTo(
271272
listOf(
272273
"hello: android.widget.TextView.ChangeWatcher (0-5) fl=#6553618",
273-
"hello: io.element.android.wysiwyg.view.spans.CodeBlockSpan (0-5) fl=#33",
274+
"hello: io.element.android.wysiwyg.view.spans.CodeBlockSpan (0-5) fl=#17",
274275
"hello: android.text.style.UnderlineSpan (0-5) fl=#289",
275276
"hello: android.view.inputmethod.ComposingText (0-5) fl=#289",
276277
"hello: android.text.method.TextKeyListener (0-5) fl=#18",
@@ -317,7 +318,7 @@ class InterceptInputConnectionIntegrationTest {
317318
textView.text.dumpSpans(), equalTo(
318319
listOf(
319320
"Test\n$NBSP: android.widget.TextView.ChangeWatcher (0-6) fl=#6553618",
320-
"Test\n$NBSP: io.element.android.wysiwyg.view.spans.CodeBlockSpan (0-6) fl=#33",
321+
"Test\n$NBSP: io.element.android.wysiwyg.view.spans.CodeBlockSpan (0-6) fl=#17",
321322
"$NBSP: io.element.android.wysiwyg.view.spans.ExtraCharacterSpan (5-6) fl=#17",
322323
"Test\n$NBSP: android.text.method.TextKeyListener (0-6) fl=#18",
323324
"Test\n$NBSP: android.widget.Editor.SpanController (0-6) fl=#18",
@@ -335,7 +336,7 @@ class InterceptInputConnectionIntegrationTest {
335336
textView.text.dumpSpans(), equalTo(
336337
listOf(
337338
"Test\n$NBSP: android.widget.TextView.ChangeWatcher (0-6) fl=#6553618",
338-
"Test: io.element.android.wysiwyg.view.spans.CodeBlockSpan (0-4) fl=#33",
339+
"Test: io.element.android.wysiwyg.view.spans.CodeBlockSpan (0-4) fl=#17",
339340
"$NBSP: io.element.android.wysiwyg.view.spans.ExtraCharacterSpan (5-6) fl=#17",
340341
"Test\n$NBSP: android.text.method.TextKeyListener (0-6) fl=#18",
341342
"Test\n$NBSP: android.widget.Editor.SpanController (0-6) fl=#18",
@@ -360,7 +361,7 @@ class InterceptInputConnectionIntegrationTest {
360361
textView.text.dumpSpans().joinToString(",\n"), equalTo(
361362
"""
362363
hello: android.widget.TextView.ChangeWatcher (0-5) fl=#6553618,
363-
hello: io.element.android.wysiwyg.view.spans.QuoteSpan (0-5) fl=#33,
364+
hello: io.element.android.wysiwyg.view.spans.QuoteSpan (0-5) fl=#65553,
364365
hello: android.text.style.UnderlineSpan (0-5) fl=#289,
365366
hello: android.view.inputmethod.ComposingText (0-5) fl=#289,
366367
hello: android.text.method.TextKeyListener (0-5) fl=#18,
@@ -407,7 +408,7 @@ class InterceptInputConnectionIntegrationTest {
407408
textView.text.dumpSpans(), equalTo(
408409
listOf(
409410
"Test\n$NBSP: android.widget.TextView.ChangeWatcher (0-6) fl=#6553618",
410-
"Test\n$NBSP: io.element.android.wysiwyg.view.spans.QuoteSpan (0-6) fl=#33",
411+
"Test\n$NBSP: io.element.android.wysiwyg.view.spans.QuoteSpan (0-6) fl=#65553",
411412
"$NBSP: io.element.android.wysiwyg.view.spans.ExtraCharacterSpan (5-6) fl=#17",
412413
"Test\n$NBSP: android.text.method.TextKeyListener (0-6) fl=#18",
413414
"Test\n$NBSP: android.widget.Editor.SpanController (0-6) fl=#18",
@@ -425,7 +426,7 @@ class InterceptInputConnectionIntegrationTest {
425426
textView.text.dumpSpans(), equalTo(
426427
listOf(
427428
"Test\n$NBSP: android.widget.TextView.ChangeWatcher (0-6) fl=#6553618",
428-
"Test: io.element.android.wysiwyg.view.spans.QuoteSpan (0-4) fl=#33",
429+
"Test: io.element.android.wysiwyg.view.spans.QuoteSpan (0-4) fl=#65553",
429430
"$NBSP: io.element.android.wysiwyg.view.spans.ExtraCharacterSpan (5-6) fl=#17",
430431
"Test\n$NBSP: android.text.method.TextKeyListener (0-6) fl=#18",
431432
"Test\n$NBSP: android.widget.Editor.SpanController (0-6) fl=#18",

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

+4-5
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package io.element.android.wysiwyg
22

33
import android.text.Editable
44
import android.text.TextWatcher
5+
import io.element.android.wysiwyg.utils.LoggingConfig
56
import timber.log.Timber
67
import java.util.concurrent.atomic.AtomicBoolean
78

@@ -14,8 +15,6 @@ internal class EditorTextWatcher: TextWatcher {
1415

1516
val isInEditorChange get() = updateIsFromEditor.get()
1617

17-
var enableDebugLogs = false
18-
1918
private var beforeText: CharSequence? = null
2019

2120
/**
@@ -53,7 +52,7 @@ internal class EditorTextWatcher: TextWatcher {
5352
}
5453

5554
override fun beforeTextChanged(s: CharSequence?, start: Int, count: Int, after: Int) {
56-
if (enableDebugLogs) {
55+
if (LoggingConfig.enableDebugLogs) {
5756
Timber.v("beforeTextChanged | text: \"$s\", start: $start, count: $count, after: $after")
5857
}
5958
if (!updateIsFromEditor.get()) {
@@ -62,7 +61,7 @@ internal class EditorTextWatcher: TextWatcher {
6261
}
6362

6463
override fun onTextChanged(s: CharSequence?, start: Int, before: Int, count: Int) {
65-
if (enableDebugLogs) {
64+
if (LoggingConfig.enableDebugLogs) {
6665
Timber.v("onTextChanged | text: \"$s\", start: $start, before: $before, count: $count")
6766
}
6867
if (!updateIsFromEditor.get()) {
@@ -72,7 +71,7 @@ internal class EditorTextWatcher: TextWatcher {
7271
}
7372

7473
override fun afterTextChanged(s: Editable?) {
75-
if (enableDebugLogs) {
74+
if (LoggingConfig.enableDebugLogs) {
7675
Timber.v("afterTextChanged")
7776
}
7877
if (!updateIsFromEditor.get()) {

0 commit comments

Comments
 (0)