diff --git a/aztec/src/main/kotlin/org/wordpress/aztec/formatting/LineBlockFormatter.kt b/aztec/src/main/kotlin/org/wordpress/aztec/formatting/LineBlockFormatter.kt index 83f364efd..aae670b53 100644 --- a/aztec/src/main/kotlin/org/wordpress/aztec/formatting/LineBlockFormatter.kt +++ b/aztec/src/main/kotlin/org/wordpress/aztec/formatting/LineBlockFormatter.kt @@ -247,8 +247,12 @@ class LineBlockFormatter(editor: AztecText) : AztecFormatter(editor) { } } if (position <= 0 && selectionEnd != 0) { - // If the text contains "\n" return that as the position, else set the position to the end of the text - position = editableText.indexOf("\n", selectionEnd).takeIf { it >= 0 } ?: editableText.length + position = if (editableText[selectionEnd - 1] == '\n') { + selectionEnd + } else { + // If the text contains "\n" return that as the position, else set the position to the end of the text + editableText.indexOf("\n", selectionEnd).takeIf { it >= 0 } ?: editableText.length + } } return position } diff --git a/aztec/src/test/kotlin/org/wordpress/aztec/ImageBlockTest.kt b/aztec/src/test/kotlin/org/wordpress/aztec/ImageBlockTest.kt index f174cf041..2ebd39dd6 100644 --- a/aztec/src/test/kotlin/org/wordpress/aztec/ImageBlockTest.kt +++ b/aztec/src/test/kotlin/org/wordpress/aztec/ImageBlockTest.kt @@ -198,4 +198,17 @@ class ImageBlockTest { Assert.assertEquals("Test 1
test 2
test 3", editText.toHtml()) } + + @Test + @Throws(Exception::class) + fun addImageInTheMiddleOfParagraphsWhenNoBlocksPresent() { + editText.fromHtml("

Line 1

Line 2

Line 3

") + + editText.setSelection(editText.editableText.indexOf("1") + 2) + val attributes = AztecAttributes() + attributes.setValue("id", "1234") + editText.insertImage(null, attributes) + + Assert.assertEquals("

Line 1

Line 2

Line 3

", editText.toHtml()) + } }