Repository navigation
Find a Text's clickable spans only when a touch needs them #59583
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
Merged
meta-codesync
merged 2 commits into
react:main
from
hryhoriiK97:android-text-lazy-clickable-spans
Nov 4, 2026
+156
−21
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
141 changes: 141 additions & 0 deletions
141
...id/src/test/java/com/facebook/react/views/text/PreparedLayoutTextViewClickableSpanTest.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,141 @@ | ||
| /* | ||
| * Copyright (c) Meta Platforms, Inc. and affiliates. | ||
| * | ||
| * This source code is licensed under the MIT license found in the | ||
| * LICENSE file in the root directory of this source tree. | ||
| */ | ||
|
|
||
| package com.facebook.react.views.text | ||
|
|
||
| import android.annotation.SuppressLint | ||
| import android.graphics.text.LineBreakConfig | ||
| import android.os.SystemClock | ||
| import android.text.Layout | ||
| import android.text.Spannable | ||
| import android.text.SpannableString | ||
| import android.text.TextPaint | ||
| import android.text.style.ClickableSpan | ||
| import android.view.MotionEvent | ||
| import android.view.View | ||
| import com.facebook.react.common.annotations.UnstableReactNativeAPI | ||
| import com.facebook.react.common.mapbuffer.WritableMapBuffer | ||
| import com.facebook.yoga.YogaMeasureMode | ||
| import org.assertj.core.api.Assertions.assertThat | ||
| import org.junit.Test | ||
| import org.junit.runner.RunWith | ||
| import org.robolectric.RobolectricTestRunner | ||
| import org.robolectric.RuntimeEnvironment | ||
| import org.robolectric.annotation.Config | ||
| import org.robolectric.annotation.GraphicsMode | ||
|
|
||
| /** Tapping a [ClickableSpan] in a [PreparedLayoutTextView] clicks it, for the current text only. */ | ||
| @OptIn(UnstableReactNativeAPI::class) | ||
| @RunWith(RobolectricTestRunner::class) | ||
| @SuppressLint("NewApi") | ||
| @Config(sdk = [34]) | ||
| @GraphicsMode(GraphicsMode.Mode.NATIVE) | ||
| class PreparedLayoutTextViewClickableSpanTest { | ||
|
|
||
| private val view = PreparedLayoutTextView(RuntimeEnvironment.getApplication()) | ||
|
|
||
| @Test | ||
| fun `tapping a clickable span clicks it`() { | ||
| val span = CountingClickableSpan() | ||
| view.preparedLayout = preparedLayout(textWith(span)) | ||
|
|
||
| assertThat(tap()).isTrue() | ||
| assertThat(span.clicks).isEqualTo(1) | ||
| } | ||
|
|
||
| @Test | ||
| fun `tapping text without clickable spans does not click`() { | ||
| val span = CountingClickableSpan() | ||
| view.preparedLayout = preparedLayout(textWith(span)) | ||
| tap() | ||
|
|
||
| view.preparedLayout = preparedLayout(SpannableString(TEXT)) | ||
|
|
||
| assertThat(tap()).isFalse() | ||
| assertThat(span.clicks).isEqualTo(1) | ||
| } | ||
|
|
||
| @Test | ||
| fun `tapping a clickable span in new text clicks it`() { | ||
| view.preparedLayout = preparedLayout(SpannableString(TEXT)) | ||
| tap() | ||
|
|
||
| val span = CountingClickableSpan() | ||
| view.preparedLayout = preparedLayout(textWith(span)) | ||
|
|
||
| assertThat(tap()).isTrue() | ||
| assertThat(span.clicks).isEqualTo(1) | ||
| } | ||
|
|
||
| @Test | ||
| fun `a recycled view does not click spans of its previous text`() { | ||
| val span = CountingClickableSpan() | ||
| view.preparedLayout = preparedLayout(textWith(span)) | ||
| tap() | ||
|
|
||
| view.recycleView() | ||
|
|
||
| assertThat(tap()).isFalse() | ||
| assertThat(span.clicks).isEqualTo(1) | ||
| } | ||
|
|
||
| /** Sends a down and an up at the start of the text; returns whether the up was handled. */ | ||
| private fun tap(): Boolean { | ||
| val time = SystemClock.uptimeMillis() | ||
| val down = MotionEvent.obtain(time, time, MotionEvent.ACTION_DOWN, 5f, 5f, 0) | ||
| val up = MotionEvent.obtain(time, time, MotionEvent.ACTION_UP, 5f, 5f, 0) | ||
| view.onTouchEvent(down) | ||
| val handled = view.onTouchEvent(up) | ||
| down.recycle() | ||
| up.recycle() | ||
| return handled | ||
| } | ||
|
|
||
| private fun textWith(span: ClickableSpan): SpannableString = | ||
| SpannableString(TEXT).apply { | ||
| setSpan(span, 0, TEXT.length, Spannable.SPAN_INCLUSIVE_INCLUSIVE) | ||
| } | ||
|
|
||
| private fun preparedLayout(text: SpannableString): PreparedLayout = | ||
| PreparedLayout( | ||
| TextLayoutManager.createLayout( | ||
| text, | ||
| /* boring = */ null, | ||
| /* width = */ 400f, | ||
| YogaMeasureMode.EXACTLY, | ||
| /* includeFontPadding = */ false, | ||
| /* textBreakStrategy = */ Layout.BREAK_STRATEGY_HIGH_QUALITY, | ||
| /* hyphenationFrequency = */ Layout.HYPHENATION_FREQUENCY_NONE, | ||
| /* lineBreakStyle = */ LineBreakConfig.LINE_BREAK_STYLE_NONE, | ||
| /* lineBreakWordStyle = */ LineBreakConfig.LINE_BREAK_WORD_STYLE_NONE, | ||
| Layout.Alignment.ALIGN_NORMAL, | ||
| /* justificationMode = */ 0, | ||
| /* ellipsizeMode = */ null, | ||
| /* maxNumberOfLines = */ 0, | ||
| TextPaint(TextPaint.ANTI_ALIAS_FLAG).apply { textSize = 26f }, | ||
| ), | ||
| /* maximumNumberOfLines = */ 0, | ||
| /* verticalOffset = */ 0f, | ||
| /* reactTags = */ IntArray(0), | ||
| /* textBreakStrategy = */ Layout.BREAK_STRATEGY_HIGH_QUALITY, | ||
| /* justificationMode = */ 0, | ||
| baseTextAttributes = TextAttributeProps.fromMapBuffer(WritableMapBuffer()), | ||
| /* sourceText = */ text, | ||
| ) | ||
|
|
||
| private class CountingClickableSpan : ClickableSpan() { | ||
| var clicks = 0 | ||
|
|
||
| override fun onClick(widget: View) { | ||
| clicks++ | ||
| } | ||
| } | ||
|
|
||
| private companion object { | ||
| const val TEXT = "Tap this text" | ||
| } | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The old code explicitly cleared
clickableSpans = emptyList()here. Consider addinghasClickableSpansCache = nullfor defensive consistency — if a recycled view ever receives a touch before a newpreparedLayoutis set, the cache would still hold the stale value from the previous text.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@andrewdacenko done in e5873ce:
recycleView()now clearshasClickableSpansCachetoo, and I added a test for it. Thanks!