Skip to content

Commit 11755c1

Browse files
NickGerlemankelset
authored andcommitted
Minimize EditText Spans 1/9: Fix precedence (#36543)
Summary: Pull Request resolved: #36543 This is part of a series of changes to minimize the number of spans committed to EditText, as a mitigation for platform issues on Samsung devices. See this [GitHub thread]( #35936 (comment)) for greater context on the platform behavior. We cache the backing EditText span on text change to later measure. To measure outside of a TextInput we need to restore any spans we removed. Spans may overlap, so base attributes should be behind everything else. The logic here for dealing with precedence is incorrect, and we should instead accomplish this by twiddling with the `SPAN_PRIORITY` bits. Changelog: [Android][Fixed] - Minimize Spans 1/N: Fix precedence Reviewed By: javache Differential Revision: D44240779 fbshipit-source-id: f731b353587888faad946b8cf1e868095cdeced3
1 parent ccb9366 commit 11755c1

File tree

1 file changed

+8
-19
lines changed

1 file changed

+8
-19
lines changed

ReactAndroid/src/main/java/com/facebook/react/views/textinput/ReactEditText.java

Lines changed: 8 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -647,29 +647,18 @@ private void stripAtributeEquivalentSpans(SpannableStringBuilder sb) {
647647
}
648648
}
649649

650-
private void unstripAttributeEquivalentSpans(
651-
SpannableStringBuilder workingText, Spannable originalText) {
652-
// We must add spans back for Fabric to be able to measure, at lower precedence than any
653-
// existing spans. Remove all spans, add the attributes, then re-add the spans over
654-
workingText.append(originalText);
650+
private void unstripAttributeEquivalentSpans(SpannableStringBuilder workingText) {
651+
int spanFlags = Spannable.SPAN_INCLUSIVE_INCLUSIVE;
655652

656-
for (Object span : workingText.getSpans(0, workingText.length(), Object.class)) {
657-
workingText.removeSpan(span);
658-
}
653+
// Set all bits for SPAN_PRIORITY so that this span has the highest possible priority
654+
// (least precedence). This ensures the span is behind any overlapping spans.
655+
spanFlags |= Spannable.SPAN_PRIORITY;
659656

660657
workingText.setSpan(
661658
new ReactAbsoluteSizeSpan(mTextAttributes.getEffectiveFontSize()),
662659
0,
663660
workingText.length(),
664-
Spanned.SPAN_INCLUSIVE_INCLUSIVE);
665-
666-
for (Object span : originalText.getSpans(0, originalText.length(), Object.class)) {
667-
workingText.setSpan(
668-
span,
669-
originalText.getSpanStart(span),
670-
originalText.getSpanEnd(span),
671-
originalText.getSpanFlags(span));
672-
}
661+
spanFlags);
673662
}
674663

675664
private static boolean sameTextForSpan(
@@ -1086,8 +1075,8 @@ private void updateCachedSpannable(boolean resetStyles) {
10861075
// ...
10871076
// - android.app.Activity.dispatchKeyEvent (Activity.java:3447)
10881077
try {
1089-
Spannable text = (Spannable) currentText.subSequence(0, currentText.length());
1090-
unstripAttributeEquivalentSpans(sb, text);
1078+
sb.append(currentText.subSequence(0, currentText.length()));
1079+
unstripAttributeEquivalentSpans(sb);
10911080
} catch (IndexOutOfBoundsException e) {
10921081
ReactSoftExceptionLogger.logSoftException(TAG, e);
10931082
}

0 commit comments

Comments
 (0)