Skip to content

Commit

Permalink
Merge pull request #357 from MohamedRejeb/1.x
Browse files Browse the repository at this point in the history
Fix RichTextState.toHtml() append an additional <br> when the last line is empty
  • Loading branch information
MohamedRejeb authored Sep 24, 2024
2 parents 3990c22 + 4c5b32a commit d07edeb
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 5 deletions.
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

0 comments on commit d07edeb

Please sign in to comment.