Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix RichTextState.toHtml() append an additional <br> when the last line is empty #357

Merged
merged 2 commits into from
Sep 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -251,8 +251,10 @@ internal object RichTextStateHtmlParser : RichTextStateParser<String> {
val builder = StringBuilder()

var lastParagraphGroupTagName: String? = null
var isLastParagraphEmpty = false

richTextState.richParagraphList.fastForEachIndexed { index, richParagraph ->
val isParagraphEmpty = richParagraph.isEmpty()
val paragraphGroupTagName = decodeHtmlElementFromRichParagraphType(richParagraph.type)

// Close last paragraph group tag if needed
Expand All @@ -268,8 +270,15 @@ internal object RichTextStateHtmlParser : RichTextStateParser<String> {
)
builder.append("<$paragraphGroupTagName>")
// Add line break if the paragraph is empty
else if (richParagraph.isEmpty()) {
builder.append("<$BrElement>")
else if (isParagraphEmpty) {
val skipAddingBr =
isLastParagraphEmpty && richParagraph.isEmpty() && index == richTextState.richParagraphList.lastIndex

if (!skipAddingBr)
builder.append("<$BrElement>")

isLastParagraphEmpty = isParagraphEmpty

return@fastForEachIndexed
}

Expand Down Expand Up @@ -304,6 +313,8 @@ internal object RichTextStateHtmlParser : RichTextStateParser<String> {
(lastParagraphGroupTagName == "ol" || lastParagraphGroupTagName == "ul") &&
index == richTextState.richParagraphList.lastIndex
) builder.append("</$lastParagraphGroupTagName>")

isLastParagraphEmpty = isParagraphEmpty
}

return builder.toString()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import kotlin.test.Test
import kotlin.test.assertContains
import kotlin.test.assertEquals

internal class CssDecoderTest {
class CssDecoderTest {
@Test
fun testDecodeCssStyleMap() {
val map = mapOf(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import kotlin.math.roundToInt
import kotlin.test.Test
import kotlin.test.assertEquals

internal class CssEncoderTest {
class CssEncoderTest {
@Test
fun testParseCssStyle() {
val style = "font-weight: bold; color: #ff0000; font-size: 12px;"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import kotlin.test.assertEquals
import kotlin.test.assertIs
import kotlin.test.assertTrue

internal class RichTextStateHtmlParserTest {
class RichTextStateHtmlParserTest {
@Test
fun testRemoveHtmlTextExtraSpaces() {
val html = """
Expand Down Expand Up @@ -150,6 +150,26 @@ internal class RichTextStateHtmlParserTest {
assertEquals("second", richTextState.annotatedString.text)
}

@Test
fun testBrEncodeDecode() {
val html = "<p>ABC</p><br><br><br>"

val state = RichTextStateHtmlParser.encode(html)

assertEquals(5, state.richParagraphList.size)
assertEquals(html, state.toHtml())
}

@Test
fun testBrEncodeDecode2() {
val html = "<br><p>ABC</p><br><br><p>ABC</p><br><br>"

val state = RichTextStateHtmlParser.encode(html)

assertEquals(8, state.richParagraphList.size)
assertEquals(html, state.toHtml())
}

@Test
fun testBrInMiddleOrParagraph() {
val html = """
Expand Down
Loading