Skip to content

Commit e264b73

Browse files
committed
fix: add missing new lines for <br> in compact mode by ensuring the extra new line is only consumed once at each paragraph style boundary
1 parent 370feea commit e264b73

1 file changed

Lines changed: 13 additions & 9 deletions

File tree

htmlconverter/src/commonMain/kotlin/be/digitalia/compose/htmlconverter/internal/AnnotatedStringHtmlHandler.kt

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -37,13 +37,19 @@ internal class AnnotatedStringHtmlHandler(
3737
private val linkInteractionListener: LinkInteractionListener?
3838
) : HtmlHandler {
3939
private val textWriter = HtmlTextWriter(builder, object : HtmlTextWriter.Callbacks {
40+
private var consumedNewLineIndex = -1
41+
4042
override fun onWriteNewLines(newLineCount: Int): Int {
4143
val currentIndex = builder.length
42-
val paragraphIndex = paragraphStartIndex
43-
// Paragraph style will automatically add one new line at its start and its end
44-
return if (currentIndex == paragraphIndex || currentIndex == paragraphIndex.inv())
45-
newLineCount - 1
46-
else newLineCount
44+
if (currentIndex != consumedNewLineIndex) {
45+
val paragraphIndex = paragraphStartIndex
46+
if (currentIndex == paragraphIndex || currentIndex == paragraphIndex.inv()) {
47+
// Paragraph style will automatically add a single new line at each boundary
48+
consumedNewLineIndex = currentIndex
49+
return newLineCount - 1
50+
}
51+
}
52+
return newLineCount
4753
}
4854

4955
override fun onWriteContentStart() {
@@ -279,11 +285,9 @@ internal class AnnotatedStringHtmlHandler(
279285
if (indent) {
280286
blockIndentLevel--
281287
}
282-
paragraphStartIndex = if (level == 0) {
288+
if (hasTrailingParagraph) {
283289
// Encode the end position of the trailing paragraph as a negative value using bit inversion
284-
if (hasTrailingParagraph) currentIndex.inv() else -1
285-
} else {
286-
currentIndex
290+
paragraphStartIndex = if (level == 0) currentIndex.inv() else currentIndex
287291
}
288292
}
289293
textWriter.markBlockBoundary(if (compactMode) 1 else suffixNewLineCount, 0)

0 commit comments

Comments
 (0)