Skip to content

Commit

Permalink
Fix losing link when having children inside it
Browse files Browse the repository at this point in the history
  • Loading branch information
MohamedRejeb committed Feb 4, 2025
1 parent d1b9cbe commit 95bc722
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -86,11 +86,19 @@ internal fun AnnotatedString.Builder.appendRichSpan(
if (
parent != null &&
parent.text.isEmpty() &&
richSpanList.size == 1
richSpanList.size == 1 &&
(parent.richSpanStyle is RichSpanStyle.Default || richSpanList.first().richSpanStyle is RichSpanStyle.Default)
) {
val firstChild = richSpanList.first()

val richSpanStyle =
if (firstChild.richSpanStyle !is RichSpanStyle.Default)
firstChild.richSpanStyle
else
parent.richSpanStyle

parent.spanStyle = parent.spanStyle.merge(firstChild.spanStyle)
parent.richSpanStyle = firstChild.richSpanStyle
parent.richSpanStyle = richSpanStyle
parent.text = firstChild.text
parent.textRange = firstChild.textRange
parent.children.clear()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import androidx.compose.ui.text.font.FontStyle
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.text.input.TextFieldValue
import androidx.compose.ui.text.style.TextAlign
import androidx.compose.ui.text.style.TextDecoration
import com.mohamedrejeb.richeditor.annotation.ExperimentalRichTextApi
import com.mohamedrejeb.richeditor.paragraph.RichParagraph
import com.mohamedrejeb.richeditor.paragraph.type.DefaultParagraph
Expand Down Expand Up @@ -1248,4 +1249,24 @@ class RichTextStateTest {
assertEquals("Hi Kotlin World! ", richTextState.annotatedString.text)
}

@OptIn(ExperimentalRichTextApi::class)
@Test
fun testLooseLinksAfterChangingConfig() {
val html = """
<a href="https://www.google.com"><b>Google</b></a>
""".trimIndent()

val richTextState = RichTextState()
richTextState.setHtml(html)
richTextState.config.linkTextDecoration = TextDecoration.None

val link = richTextState.richParagraphList[0].children.first()

assertEquals(1, richTextState.richParagraphList.size)
assertEquals(0, link.children.size)
assertIs<RichSpanStyle.Link>(link.richSpanStyle)
assertEquals("Google", link.text)
assertEquals(FontWeight.Bold, link.spanStyle.fontWeight)
}

}

0 comments on commit 95bc722

Please sign in to comment.