Skip to content

Commit b6b9242

Browse files
committed
fix(mobile): check Android paste-as-text input limits
1 parent 91bfcc9 commit b6b9242

3 files changed

Lines changed: 147 additions & 5 deletions

File tree

‎apps/mobile/modules/t3-composer-editor/android/build.gradle‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,10 @@ android {
88
namespace 'expo.modules.t3composereditor'
99
compileSdk rootProject.ext.compileSdkVersion
1010

11+
testOptions {
12+
unitTests.includeAndroidResources = true
13+
}
14+
1115
defaultConfig {
1216
minSdkVersion rootProject.ext.minSdkVersion
1317
targetSdkVersion rootProject.ext.targetSdkVersion
@@ -17,4 +21,12 @@ android {
1721
dependencies {
1822
implementation project(':expo-modules-core')
1923
implementation project(':t3tools-mobile-markdown-text')
24+
testImplementation 'junit:junit:4.13.2'
25+
testImplementation 'org.robolectric:robolectric:4.16.1'
26+
}
27+
28+
tasks.withType(Test).configureEach {
29+
javaLauncher = javaToolchains.launcherFor {
30+
languageVersion = JavaLanguageVersion.of(21)
31+
}
2032
}

‎apps/mobile/modules/t3-composer-editor/android/src/main/java/expo/modules/t3composereditor/T3ComposerEditorView.kt‎

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -577,7 +577,7 @@ private fun parseTokens(value: String): List<ComposerToken> = try {
577577
emptyList()
578578
}
579579

580-
private class SelectionAwareEditText(context: Context) : EditText(context) {
580+
internal class SelectionAwareEditText(context: Context) : EditText(context) {
581581
var readOnly = false
582582
var selectionListener: ((Int, Int) -> Unit)? = null
583583
var pasteImagesListener: ((List<String>) -> Unit)? = null
@@ -650,6 +650,10 @@ private class SelectionAwareEditText(context: Context) : EditText(context) {
650650
}
651651
val handled = when {
652652
id == android.R.id.copy || id == android.R.id.cut -> copyContext(id == android.R.id.cut)
653+
id == android.R.id.pasteAsPlainText -> {
654+
val clipboard = context.getSystemService(Context.CLIPBOARD_SERVICE) as? ClipboardManager
655+
pasteInterceptedText(clipboard?.primaryClip, foldLargeText = false)
656+
}
653657
pasting -> pasteContextOrImages()
654658
else -> false
655659
}
@@ -696,14 +700,17 @@ private class SelectionAwareEditText(context: Context) : EditText(context) {
696700
}
697701
}
698702

699-
private fun pasteInterceptedText(clip: ClipData?): Boolean {
703+
private fun pasteInterceptedText(clip: ClipData?, foldLargeText: Boolean = true): Boolean {
700704
val text = if (textPasteThresholdBytes > 0) clip?.plainText() else null
701705
if (text.isNullOrEmpty()) return false
702706
val start = minOf(selectionStart, selectionEnd).coerceIn(0, length())
703707
val end = maxOf(selectionStart, selectionEnd).coerceIn(start, length())
704708
val exceedsInputLimit = length().toLong() - (end - start) + text.length > maxInputChars
705-
val shouldIntercept = exceedsInputLimit || text.length >= textPasteThresholdBytes ||
706-
text.toByteArray(Charsets.UTF_8).size >= textPasteThresholdBytes
709+
val shouldFold = foldLargeText && (
710+
text.length >= textPasteThresholdBytes ||
711+
text.toByteArray(Charsets.UTF_8).size >= textPasteThresholdBytes
712+
)
713+
val shouldIntercept = exceedsInputLimit || shouldFold
707714
if (shouldIntercept) {
708715
pasteTextListener?.invoke(text, start, end)
709716
}
@@ -724,7 +731,7 @@ private class SelectionAwareEditText(context: Context) : EditText(context) {
724731

725732
override fun onKeyShortcut(keyCode: Int, event: KeyEvent): Boolean {
726733
if (keyCode == KeyEvent.KEYCODE_V && event.isCtrlPressed && event.isShiftPressed) {
727-
return !readOnly && super.onTextContextMenuItem(android.R.id.pasteAsPlainText)
734+
return onTextContextMenuItem(android.R.id.pasteAsPlainText)
728735
}
729736
return super.onKeyShortcut(keyCode, event)
730737
}
Lines changed: 123 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,123 @@
1+
package expo.modules.t3composereditor
2+
3+
import android.content.ClipData
4+
import android.content.ClipboardManager
5+
import android.content.Context
6+
import android.view.KeyEvent
7+
import org.junit.Assert.assertEquals
8+
import org.junit.Assert.assertFalse
9+
import org.junit.Assert.assertNull
10+
import org.junit.Assert.assertTrue
11+
import org.junit.Test
12+
import org.junit.runner.RunWith
13+
import org.robolectric.RobolectricTestRunner
14+
import org.robolectric.RuntimeEnvironment
15+
import org.robolectric.annotation.Config
16+
17+
@RunWith(RobolectricTestRunner::class)
18+
@Config(sdk = [36], manifest = Config.NONE)
19+
class ComposerPasteTest {
20+
private val context = RuntimeEnvironment.getApplication()
21+
private val clipboard = context.getSystemService(Context.CLIPBOARD_SERVICE) as ClipboardManager
22+
private val editor = SelectionAwareEditText(context).apply {
23+
textPasteThresholdBytes = 32 * 1024
24+
maxInputChars = 120_000
25+
}
26+
27+
private fun pasteAsText(): Boolean = editor.onKeyShortcut(
28+
KeyEvent.KEYCODE_V,
29+
KeyEvent(
30+
0,
31+
0,
32+
KeyEvent.ACTION_DOWN,
33+
KeyEvent.KEYCODE_V,
34+
0,
35+
KeyEvent.META_CTRL_ON or KeyEvent.META_SHIFT_ON
36+
)
37+
)
38+
39+
@Test
40+
fun shortcutKeepsLargeTextInlineAndUndoable() {
41+
val pasted = "x".repeat(32 * 1024)
42+
clipboard.setPrimaryClip(ClipData.newPlainText("test", pasted))
43+
editor.setText("before old after")
44+
editor.setSelection(10, 7)
45+
editor.pasteTextListener = { _, _, _ -> error("Inline paste must stay native") }
46+
47+
assertTrue(pasteAsText())
48+
assertEquals("before $pasted after", editor.text.toString())
49+
assertTrue(editor.onTextContextMenuItem(android.R.id.undo))
50+
assertEquals("before old after", editor.text.toString())
51+
}
52+
53+
@Test
54+
fun shortcutInterceptsInputLimitOverflowWithoutChangingTheSelection() {
55+
clipboard.setPrimaryClip(ClipData.newPlainText("test", "hello"))
56+
editor.setText("x".repeat(119_999))
57+
editor.setSelection(10, 7)
58+
var intercepted: Triple<String, Int, Int>? = null
59+
editor.pasteTextListener = { text, start, end -> intercepted = Triple(text, start, end) }
60+
61+
assertTrue(pasteAsText())
62+
assertEquals(Triple("hello", 7, 10), intercepted)
63+
assertEquals(119_999, editor.length())
64+
assertEquals(10, editor.selectionStart)
65+
assertEquals(7, editor.selectionEnd)
66+
}
67+
68+
@Test
69+
fun shortcutAllowsReplacementAtTheInputLimit() {
70+
clipboard.setPrimaryClip(ClipData.newPlainText("test", "hello"))
71+
editor.setText("x".repeat(120_000))
72+
editor.setSelection(12, 7)
73+
editor.pasteTextListener = { _, _, _ -> error("Replacement fits the input limit") }
74+
75+
assertTrue(pasteAsText())
76+
assertEquals("hello", editor.text.substring(7, 12))
77+
assertEquals(120_000, editor.length())
78+
}
79+
80+
@Test
81+
fun regularPasteStillFoldsAtTheUtf8Threshold() {
82+
val pasted = "é".repeat(16 * 1024)
83+
clipboard.setPrimaryClip(ClipData.newPlainText("test", pasted))
84+
editor.setText("old")
85+
editor.setSelection(0, 3)
86+
var intercepted: String? = null
87+
editor.pasteTextListener = { text, _, _ -> intercepted = text }
88+
89+
assertTrue(editor.onTextContextMenuItem(android.R.id.paste))
90+
assertEquals(pasted, intercepted)
91+
assertEquals("old", editor.text.toString())
92+
}
93+
94+
@Test
95+
fun shortcutPastesStructuredClipboardAsText() {
96+
clipboard.setPrimaryClip(
97+
ClipData.newHtmlText(
98+
"test",
99+
"plain text",
100+
"<pre data-t3-context-fragment=\"x\">plain text</pre>"
101+
)
102+
)
103+
editor.setSelection(0)
104+
editor.pasteContextListener = { error("Paste as Text must not import structured context") }
105+
106+
assertTrue(pasteAsText())
107+
assertEquals("plain text", editor.text.toString())
108+
}
109+
110+
@Test
111+
fun readOnlyShortcutDoesNotPasteOrEmitAnEvent() {
112+
clipboard.setPrimaryClip(ClipData.newPlainText("test", "x".repeat(120_001)))
113+
editor.setText("unchanged")
114+
editor.setSelection(editor.length())
115+
editor.readOnly = true
116+
var intercepted: String? = null
117+
editor.pasteTextListener = { text, _, _ -> intercepted = text }
118+
119+
assertFalse(pasteAsText())
120+
assertEquals("unchanged", editor.text.toString())
121+
assertNull(intercepted)
122+
}
123+
}

0 commit comments

Comments
 (0)