diff --git a/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/views/text/ReactBaseTextShadowNode.java b/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/views/text/ReactBaseTextShadowNode.java index 47d8e550370fcb..406e9ded3329ca 100644 --- a/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/views/text/ReactBaseTextShadowNode.java +++ b/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/views/text/ReactBaseTextShadowNode.java @@ -237,41 +237,12 @@ private static void buildSpannedFromShadowNode( supportsInlineViews, inlineViews, sb.length()); - } else if (child instanceof ReactTextInlineImageShadowNode) { - // We make the image take up 1 character in the span and put a corresponding character into - // the text so that the image doesn't run over any following text. - sb.append(INLINE_VIEW_PLACEHOLDER); - ops.add( - new SetSpanOperation( - sb.length() - INLINE_VIEW_PLACEHOLDER.length(), - sb.length(), - ((ReactTextInlineImageShadowNode) child).buildInlineImageSpan())); + } else if (child instanceof ReactTextInlineImageShadowNode inlineImageChild) { + addInlineImageSpan(ops, sb, inlineImageChild); } else if (supportsInlineViews) { - int reactTag = child.getReactTag(); - YogaValue widthValue = child.getStyleWidth(); - YogaValue heightValue = child.getStyleHeight(); - - float width; - float height; - if (widthValue.unit != YogaUnit.POINT || heightValue.unit != YogaUnit.POINT) { - // If the measurement of the child isn't calculated, we calculate the layout for the - // view using Yoga - child.calculateLayout(); - width = child.getLayoutWidth(); - height = child.getLayoutHeight(); - } else { - width = widthValue.value; - height = heightValue.value; - } - - // We make the inline view take up 1 character in the span and put a corresponding character - // into - // the text so that the inline view doesn't run over any following text. - sb.append(INLINE_VIEW_PLACEHOLDER); + addInlineViewPlaceholderSpan(ops, sb, child); - TextLayoutUtils.addInlineViewPlaceholderSpan(ops, sb, reactTag, width, height); - - inlineViews.put(reactTag, child); + inlineViews.put(child.getReactTag(), child); } else { throw new IllegalViewOperationException( "Unexpected view type nested under a or node: " + child.getClass()); @@ -287,6 +258,40 @@ private static void buildSpannedFromShadowNode( } } + private static void addInlineImageSpan(List ops, SpannableStringBuilder sb, + ReactTextInlineImageShadowNode child) { + // We make the image take up 1 character in the span and put a corresponding character into + // the text so that the image doesn't run over any following text. + sb.append(INLINE_VIEW_PLACEHOLDER); + ops.add(new SetSpanOperation(sb.length() - INLINE_VIEW_PLACEHOLDER.length(), sb.length(), + child.buildInlineImageSpan())); + } + + private static void addInlineViewPlaceholderSpan(List ops, SpannableStringBuilder sb, + ReactShadowNode child) { + YogaValue widthValue = child.getStyleWidth(); + YogaValue heightValue = child.getStyleHeight(); + + float width; + float height; + if (widthValue.unit != YogaUnit.POINT || heightValue.unit != YogaUnit.POINT) { + // If the measurement of the child isn't calculated, we calculate the layout for the + // view using Yoga + child.calculateLayout(); + width = child.getLayoutWidth(); + height = child.getLayoutHeight(); + } else { + width = widthValue.value; + height = heightValue.value; + } + + // We make the inline view take up 1 character in the span and put a corresponding character into the text so that + // the inline view doesn't run over any following text. + sb.append(INLINE_VIEW_PLACEHOLDER); + + TextLayoutUtils.addInlineViewPlaceholderSpan(ops, sb, child.getReactTag(), width, height); + } + // `nativeViewHierarchyOptimizer` can be `null` as long as `supportsInlineViews` is `false`. protected Spannable spannedFromShadowNode( ReactBaseTextShadowNode textShadowNode,