Skip to content

Commit c4572f2

Browse files
committed
Fix issue when removing media in the middle of a paragraph
1 parent 5317329 commit c4572f2

File tree

2 files changed

+36
-1
lines changed

2 files changed

+36
-1
lines changed

aztec/src/main/kotlin/org/wordpress/aztec/AztecText.kt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2260,9 +2260,10 @@ open class AztecText : AppCompatEditText, TextWatcher, UnknownHtmlSpan.OnUnknown
22602260
}
22612261
text.delete(start, endPlus1)
22622262
spans.forEach { temporarySpan ->
2263+
val newStart = if (temporarySpan.start >= start) temporarySpan.start - 2 else temporarySpan.start
22632264
text.setSpan(
22642265
temporarySpan.span,
2265-
(temporarySpan.start - 2).coerceAtLeast(0),
2266+
newStart.coerceAtLeast(0),
22662267
(temporarySpan.end - 2).coerceAtMost(text.length),
22672268
temporarySpan.flags
22682269
)

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

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -278,4 +278,38 @@ class ImageBlockTest {
278278

279279
Assert.assertEquals(initialHtml, editText.toHtml())
280280
}
281+
282+
@Test
283+
@Throws(Exception::class)
284+
fun removeImageInSpanBeforeBlankLine() {
285+
val initialHtml = "<p>Line 1</p><p>Line 2<img id=\"1234\" /><br>Line 3</p><p>Line 4</p>"
286+
editText.fromHtml(initialHtml)
287+
288+
editText.setSelection(1)
289+
290+
editText.removeMedia(object : AztecText.AttributePredicate {
291+
override fun matches(attrs: Attributes): Boolean {
292+
return attrs.getValue("id") == "1234"
293+
}
294+
})
295+
296+
Assert.assertEquals("<p>Line 1</p><p>Line 2<br><br>Line 3</p><p>Line 4</p>", editText.toHtml())
297+
}
298+
299+
@Test
300+
@Throws(Exception::class)
301+
fun removeImageInSpanAfterBlankLine() {
302+
val initialHtml = "<p>Line 1</p><p>Line 2<br><img id=\"1234\" />Line 3</p><p>Line 4</p>"
303+
editText.fromHtml(initialHtml)
304+
305+
editText.setSelection(1)
306+
307+
editText.removeMedia(object : AztecText.AttributePredicate {
308+
override fun matches(attrs: Attributes): Boolean {
309+
return attrs.getValue("id") == "1234"
310+
}
311+
})
312+
313+
Assert.assertEquals("<p>Line 1</p><p>Line 2<br>Line 3</p><p>Line 4</p>", editText.toHtml())
314+
}
281315
}

0 commit comments

Comments
 (0)