fix(deps): update wysiwyg to v2.41.0 (#5921)
* fix(deps): update wysiwyg to v2.41.0 * Reuse already parsed document instead of parsing it again * Fix `toPlainText` representation with formatting spans --------- Co-authored-by: renovate[bot] <29139614+renovate[bot]@users.noreply.github.com> Co-authored-by: Jorge Martín <jorgem@element.io>
This commit is contained in:
commit
a43c66f56d
8 changed files with 71 additions and 49 deletions
|
|
@ -12,7 +12,7 @@ import io.element.android.libraries.matrix.api.permalink.PermalinkData
|
|||
import io.element.android.libraries.matrix.api.permalink.PermalinkParser
|
||||
import io.element.android.libraries.matrix.api.timeline.item.event.FormattedBody
|
||||
import io.element.android.libraries.matrix.api.timeline.item.event.MessageFormat
|
||||
import org.jsoup.Jsoup
|
||||
import io.element.android.wysiwyg.utils.HtmlToDomParser
|
||||
import org.jsoup.nodes.Document
|
||||
|
||||
/**
|
||||
|
|
@ -34,9 +34,9 @@ fun FormattedBody.toHtmlDocument(
|
|||
?.trimEnd()
|
||||
?.let { formattedBody ->
|
||||
val dom = if (prefix != null) {
|
||||
Jsoup.parse("$prefix $formattedBody")
|
||||
HtmlToDomParser.document("$prefix $formattedBody")
|
||||
} else {
|
||||
Jsoup.parse(formattedBody)
|
||||
HtmlToDomParser.document(formattedBody)
|
||||
}
|
||||
|
||||
// Prepend `@` to mentions
|
||||
|
|
|
|||
|
|
@ -55,8 +55,15 @@ private class PlainTextNodeVisitor : NodeVisitor {
|
|||
private val builder = StringBuilder()
|
||||
|
||||
override fun head(node: Node, depth: Int) {
|
||||
if (node is TextNode && node.text().isNotBlank()) {
|
||||
builder.append(node.text())
|
||||
if (node is TextNode) {
|
||||
// If the text node is blank, only add a single whitespace char if there wasn't already one
|
||||
if (node.text().isBlank()) {
|
||||
if (builder.lastOrNull()?.isWhitespace() == false) {
|
||||
builder.append(" ")
|
||||
}
|
||||
} else {
|
||||
builder.append(node.text())
|
||||
}
|
||||
} else if (node is Element && node.tagName() == "li") {
|
||||
val index = node.elementSiblingIndex() + 1
|
||||
val isOrdered = node.parent()?.nodeName()?.lowercase() == "ol"
|
||||
|
|
|
|||
|
|
@ -45,7 +45,7 @@ class ToPlainTextTest {
|
|||
val formattedBody = FormattedBody(
|
||||
format = MessageFormat.HTML,
|
||||
body = """
|
||||
Hello world
|
||||
Hello <strong>formatted</strong> <em>world</em>
|
||||
<ul><li>This is an unordered list.</li></ul>
|
||||
<ol><li>This is an ordered list.</li></ol>
|
||||
<br />
|
||||
|
|
@ -53,7 +53,7 @@ class ToPlainTextTest {
|
|||
)
|
||||
assertThat(formattedBody.toPlainText(permalinkParser = FakePermalinkParser())).isEqualTo(
|
||||
"""
|
||||
Hello world
|
||||
Hello formatted world
|
||||
• This is an unordered list.
|
||||
1. This is an ordered list.
|
||||
""".trimIndent()
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue