Skip to content

Commit 8be1d79

Browse files
authored
Merge pull request #985 from wordpress-mobile/feature/insert-pasted-text-when-text-is-empty
Insert text when current text is empty to preserve formatting when pasting to empty edit text
2 parents ec0ff77 + 8db9071 commit 8be1d79

File tree

2 files changed

+19
-2
lines changed

2 files changed

+19
-2
lines changed

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1814,8 +1814,9 @@ open class AztecText : AppCompatEditText, TextWatcher, UnknownHtmlSpan.OnUnknown
18141814
disableTextChangedListener()
18151815

18161816
val length = text.length
1817-
if (min == 0 &&
1818-
(max == length || (length == 1 && text.toString() == Constants.END_OF_BUFFER_MARKER_STRING))) {
1817+
if (min == 0 && max == 0 && length == 1 && text.toString() == Constants.END_OF_BUFFER_MARKER_STRING) {
1818+
editable.insert(min, Constants.REPLACEMENT_MARKER_STRING)
1819+
} else if (min == 0 && max == length) {
18191820
setText(Constants.REPLACEMENT_MARKER_STRING)
18201821
} else {
18211822
// prevent changes here from triggering the crash preventer

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

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ package org.wordpress.aztec
22

33
import android.annotation.SuppressLint
44
import android.app.Activity
5+
import android.content.ClipData
6+
import android.content.Context
57
import org.junit.Assert
68
import org.junit.Before
79
import org.junit.Test
@@ -338,4 +340,18 @@ class ClipboardTest {
338340

339341
Assert.assertEquals(LONG_TEXT_EXPECTED, editText.toHtml())
340342
}
343+
344+
@Test
345+
@Throws(Exception::class)
346+
fun pasteIntoEmptyTextPreservesFormatting() {
347+
editText.fromHtml("<h1></h1>")
348+
349+
editText.setSelection(0)
350+
val clipboard = editText.context.getSystemService(Context.CLIPBOARD_SERVICE) as android.content.ClipboardManager
351+
clipboard.primaryClip = ClipData.newPlainText("aztec", "Heading")
352+
353+
TestUtils.pasteFromClipboard(editText)
354+
355+
Assert.assertEquals("<h1>Heading</h1>", editText.toHtml())
356+
}
341357
}

0 commit comments

Comments
 (0)