Skip to content

Commit 34df02a

Browse files
committed
Fix issue when adding an image on a blank line after a paragraph
1 parent 2e7ceb6 commit 34df02a

File tree

2 files changed

+19
-2
lines changed

2 files changed

+19
-2
lines changed

aztec/src/main/kotlin/org/wordpress/aztec/formatting/LineBlockFormatter.kt

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -247,8 +247,12 @@ class LineBlockFormatter(editor: AztecText) : AztecFormatter(editor) {
247247
}
248248
}
249249
if (position <= 0 && selectionEnd != 0) {
250-
// If the text contains "\n" return that as the position, else set the position to the end of the text
251-
position = editableText.indexOf("\n", selectionEnd).takeIf { it >= 0 } ?: editableText.length
250+
position = if (editableText[selectionEnd - 1] == '\n') {
251+
selectionEnd
252+
} else {
253+
// If the text contains "\n" return that as the position, else set the position to the end of the text
254+
editableText.indexOf("\n", selectionEnd).takeIf { it >= 0 } ?: editableText.length
255+
}
252256
}
253257
return position
254258
}

aztec/src/test/kotlin/org/wordpress/aztec/ImageBlockTest.kt

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -198,4 +198,17 @@ class ImageBlockTest {
198198

199199
Assert.assertEquals("Test 1<br>test 2<img id=\"1234\" /><br>test 3", editText.toHtml())
200200
}
201+
202+
@Test
203+
@Throws(Exception::class)
204+
fun addImageInTheMiddleOfParagraphsWhenNoBlocksPresent() {
205+
editText.fromHtml("<p>Line 1</p><p>Line 2</p><p>Line 3</p>")
206+
207+
editText.setSelection(editText.editableText.indexOf("1") + 2)
208+
val attributes = AztecAttributes()
209+
attributes.setValue("id", "1234")
210+
editText.insertImage(null, attributes)
211+
212+
Assert.assertEquals("<p>Line 1</p><img id=\"1234\" /><p>Line 2</p><p>Line 3</p>", editText.toHtml())
213+
}
201214
}

0 commit comments

Comments
 (0)