Skip to content

Commit

Permalink
Minimize EditText Spans 2/N: Make stripAttributeEquivalentSpans generic
Browse files Browse the repository at this point in the history
Summary:
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.

This change generalizes `stripAttributeEquivalentSpans()` to allow plugging in different spans.

Changelog:
[Internal]

Differential Revision: D44240781

fbshipit-source-id: d21ca77a9fb0cee2ffc3723b1e1e2e91e923618d
  • Loading branch information
NickGerleman authored and facebook-github-bot committed Mar 21, 2023
1 parent 4b974c5 commit fe46928
Showing 1 changed file with 42 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -585,9 +585,7 @@ public void maybeSetText(ReactTextUpdate reactTextUpdate) {
new SpannableStringBuilder(reactTextUpdate.getText());

manageSpans(spannableStringBuilder, reactTextUpdate.mContainsMultipleFragments);

// Mitigation for https://github.com/facebook/react-native/issues/35936 (S318090)
stripAttributeEquivalentSpans(spannableStringBuilder);
stripStyleEquivalentSpans(spannableStringBuilder);

mContainsImages = reactTextUpdate.containsImages();

Expand Down Expand Up @@ -662,19 +660,44 @@ private void manageSpans(
}
}

private void stripAttributeEquivalentSpans(SpannableStringBuilder sb) {
// We have already set a font size on the EditText itself. We can safely remove sizing spans
// which are the same as the set font size, and not otherwise overlapped.
final int effectiveFontSize = mTextAttributes.getEffectiveFontSize();
ReactAbsoluteSizeSpan[] spans = sb.getSpans(0, sb.length(), ReactAbsoluteSizeSpan.class);
// TODO: Replace with Predicate<T> and lambdas once Java 8 builds in OSS
interface SpanPredicate<T> {
boolean test(T span);
}

/**
* Remove spans from the SpannableStringBuilder which can be represented by TextAppearance
* attributes on the underlying EditText. This works around instability on Samsung devices with
* the presence of spans https://github.com/facebook/react-native/issues/35936 (S318090)
*/
private void stripStyleEquivalentSpans(SpannableStringBuilder sb) {
stripSpansOfKind(
sb,
ReactAbsoluteSizeSpan.class,
new SpanPredicate<ReactAbsoluteSizeSpan>() {
@Override
public boolean test(ReactAbsoluteSizeSpan span) {
return span.getSize() == mTextAttributes.getEffectiveFontSize();
}
});
}

private <T> void stripSpansOfKind(
SpannableStringBuilder sb, Class<T> clazz, SpanPredicate<T> isEquivalentToAttributes) {
T[] spans = sb.getSpans(0, sb.length(), clazz);
outerLoop:
for (ReactAbsoluteSizeSpan span : spans) {
ReactAbsoluteSizeSpan[] overlappingSpans =
sb.getSpans(sb.getSpanStart(span), sb.getSpanEnd(span), ReactAbsoluteSizeSpan.class);
for (T span : spans) {
if (!isEquivalentToAttributes.test(span)) {
continue;
}

for (ReactAbsoluteSizeSpan overlappingSpan : overlappingSpans) {
if (span.getSize() != effectiveFontSize) {
int priority = sb.getSpanFlags(span) & Spannable.SPAN_PRIORITY;
T[] overlappingSpans = sb.getSpans(sb.getSpanStart(span), sb.getSpanEnd(span), clazz);

// Do not strip the span if removing it should show a non-equivalent span under it
for (T overlappingSpan : overlappingSpans) {
int overlappingPriority = sb.getSpanFlags(overlappingSpan) & Spanned.SPAN_PRIORITY;
if (!isEquivalentToAttributes.test(overlappingSpan) && priority < overlappingPriority) {
continue outerLoop;
}
}
Expand All @@ -683,7 +706,11 @@ private void stripAttributeEquivalentSpans(SpannableStringBuilder sb) {
}
}

private void unstripAttributeEquivalentSpans(SpannableStringBuilder workingText) {
/**
* Copy back styles represented as attributes to the underlying span, for later measurement
* outside the ReactEditText.
*/
private void restoreStyleEquivalentSpans(SpannableStringBuilder workingText) {
int spanFlags = Spannable.SPAN_INCLUSIVE_INCLUSIVE;

// Set all bits for SPAN_PRIORITY so that this span has the highest possible priority
Expand Down Expand Up @@ -1122,7 +1149,7 @@ private void updateCachedSpannable(boolean resetStyles) {
// - android.app.Activity.dispatchKeyEvent (Activity.java:3447)
try {
sb.append(currentText.subSequence(0, currentText.length()));
unstripAttributeEquivalentSpans(sb);
restoreStyleEquivalentSpans(sb);
} catch (IndexOutOfBoundsException e) {
ReactSoftExceptionLogger.logSoftException(TAG, e);
}
Expand Down

0 comments on commit fe46928

Please sign in to comment.