Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix issue when adding an image on a blank line after a paragraph #1022

Merged
merged 1 commit into from
Dec 14, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
13 changes: 13 additions & 0 deletions aztec/src/test/kotlin/org/wordpress/aztec/ImageBlockTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -198,4 +198,17 @@ class ImageBlockTest {

Assert.assertEquals("Test 1<br>test 2<img id=\"1234\" /><br>test 3", editText.toHtml())
}

@Test
@Throws(Exception::class)
fun addImageInTheMiddleOfParagraphsWhenNoBlocksPresent() {
editText.fromHtml("<p>Line 1</p><p>Line 2</p><p>Line 3</p>")

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

Assert.assertEquals("<p>Line 1</p><img id=\"1234\" /><p>Line 2</p><p>Line 3</p>", editText.toHtml())
}
}