MohamedRejeb / compose-rich-editor

A Rich text editor library for both Jetpack Compose and Compose Multiplatform, fully customizable, supports HTML and Markdown.
https://mohamedrejeb.github.io/compose-rich-editor/
Apache License 2.0
1.17k stars 76 forks source link

Crash when initial markdown contains "<br>" tag #385

Closed akishind-medlinx closed 1 month ago

akishind-medlinx commented 1 month ago

If we set to editor some text , containing <br>, and try typing some text, a crash occurs.

Of course, we can just remove/replace problematic parts before setting the text to the editor, but I think the crash is still worth to look at. Also, should we be aware of some other tags that can potentially cause similar issues?

https://github.com/user-attachments/assets/2c47607e-bfe1-421e-812c-b7e5c502f1e9

Library version: 1.0.0-rc09

java.lang.StringIndexOutOfBoundsException: begin 3, end 4, length 3
                    at java.lang.String.checkBoundsBeginEnd(String.java:4500)
                    at java.lang.String.substring(String.java:2527)
                    at com.mohamedrejeb.richeditor.utils.AnnotatedStringExtKt.append-UJKp_GQ(AnnotatedStringExt.kt:119)
                    at com.mohamedrejeb.richeditor.utils.AnnotatedStringExtKt.appendRichSpan-XtCa3Zc(AnnotatedStringExt.kt:58)
                    at com.mohamedrejeb.richeditor.utils.AnnotatedStringExtKt.appendRichSpan-XtCa3Zc$default(AnnotatedStringExt.kt:43)
                    at com.mohamedrejeb.richeditor.utils.AnnotatedStringExtKt.append-UJKp_GQ(AnnotatedStringExt.kt:30)
                    at com.mohamedrejeb.richeditor.model.RichTextState.updateAnnotatedString$richeditor_compose_release(RichTextState.kt:1216)
                    at com.mohamedrejeb.richeditor.model.RichTextState.updateTextFieldValue(RichTextState.kt:1163)
                    at com.mohamedrejeb.richeditor.model.RichTextState.updateTextFieldValue$default(RichTextState.kt:1147)
                    at com.mohamedrejeb.richeditor.model.RichTextState.onTextFieldValueChange$richeditor_compose_release(RichTextState.kt:1139)
                    at com.mohamedrejeb.richeditor.ui.BasicRichTextEditorKt$BasicRichTextEditor$8.invoke$lambda$1$lambda$0(BasicRichTextEditor.kt:239)
                    at com.mohamedrejeb.richeditor.ui.BasicRichTextEditorKt$BasicRichTextEditor$8.$r8$lambda$zwc0K8woDg8fz6deXxE_H1Ox0aI(Unknown Source:0)
                    at com.mohamedrejeb.richeditor.ui.BasicRichTextEditorKt$BasicRichTextEditor$8$$ExternalSyntheticLambda0.invoke(Unknown Source:8)
                    at androidx.compose.foundation.text.BasicTextFieldKt$BasicTextField$11$1.invoke(BasicTextField.kt:769)
                    at androidx.compose.foundation.text.BasicTextFieldKt$BasicTextField$11$1.invoke(BasicTextField.kt:767)
                    at androidx.compose.foundation.text.LegacyTextFieldState$onValueChange$1.invoke(CoreTextField.kt:1002)
                    at androidx.compose.foundation.text.LegacyTextFieldState$onValueChange$1.invoke(CoreTextField.kt:995)
                    at androidx.compose.foundation.text.TextFieldDelegate$Companion.onEditCommand$foundation_release(TextFieldDelegate.kt:298)
                    at androidx.compose.foundation.text.TextFieldDelegate$Companion$restartInput$1.invoke(TextFieldDelegate.kt:347)
                    at androidx.compose.foundation.text.TextFieldDelegate$Companion$restartInput$1.invoke(TextFieldDelegate.kt:344)
                    at androidx.compose.foundation.text.input.internal.LegacyTextInputMethodRequest$createInputConnection$1.onEditCommands(LegacyPlatformTextInputServiceAdapter.android.kt:272)
                    at androidx.compose.foundation.text.input.internal.RecordingInputConnection.endBatchEditInternal(RecordingInputConnection.android.kt:184)
                    at androidx.compose.foundation.text.input.internal.RecordingInputConnection.addEditCommandWithBatch(RecordingInputConnection.android.kt:160)
                    at androidx.compose.foundation.text.input.internal.RecordingInputConnection.commitText(RecordingInputConnection.android.kt:203)
                    at androidx.compose.ui.text.input.NullableInputConnectionWrapperApi21.commitText(NullableInputConnectionWrapper.android.kt:134)
                    at android.view.inputmethod.RemoteInputConnectionImpl.lambda$commitText$17(RemoteInputConnectionImpl.java:612)
                    at android.view.inputmethod.RemoteInputConnectionImpl.$r8$lambda$jNtA8MXobPnaECkNr8D9WTYrxk0(Unknown Source:0)
                    at android.view.inputmethod.RemoteInputConnectionImpl$$ExternalSyntheticLambda46.run(D8$$SyntheticClass:0)
                    at android.os.Handler.handleCallback(Handler.java:959)
                    at android.os.Handler.dispatchMessage(Handler.java:100)
                    at android.os.Looper.loopOnce(Looper.java:232)
                    at android.os.Looper.loop(Looper.java:317)
                    at android.app.ActivityThread.main(ActivityThread.java:8592)
                    at java.lang.reflect.Method.invoke(Native Method)
                    at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:580)
                    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:878)
MohamedRejeb commented 1 month ago

This shouldn't happen. I will check it. For now only br HTML tag is supported in Markdown, so using any other tag will just return the tag as a text and shouldn't cause a crash.

MohamedRejeb commented 1 month ago

br tag is mainly needed in Markdown to support multiple successive line breaks, hopefully in the future I will start supporting all other HTML tags in Markdown.

Screenshot 2024-10-02 at 4 26 31 PM
Goooler commented 1 month ago

I can repro it.

This is due to the incorrect initial state of markdown text that contains empty lines. See more details at #392.

MohamedRejeb commented 1 month ago

I managed to reproduce it, a fix is coming