From 43ec56d07f2ec7e37fa300ecc39be1233fa1e57c Mon Sep 17 00:00:00 2001 From: Mohamed Boudra Date: Sun, 22 Nov 2026 08:22:16 +0100 Subject: [PATCH] fix(app): keep Android assistant replies inside their turns From #10380, moved from the package patch into the vendored package. On Android, an assistant reply with blockquotes nested inside a list could overlap its "Worked for ..." footer and the next user message: the displayed text and the layout measurement applied spans in different orders, and the list-wide line height inflated the blockquote spacers when it ran after them. Use the same span order for Android Markdown measurement and display. Apply list line height before blockquote spacers so nested quotes keep their configured padding and the reply fits above the turn footer. test:android now also runs ListBlockquoteMeasurementTest. Applied on top of #9964; the two changes share only ListRenderer.kt, where #10380's import and line-height call sit beside #9964's marker width code. Every other file equals the #10380 patch applied to the pristine package. --- packages/app/package.json | 2 +- .../enriched/markdown/MeasurementStore.kt | 12 +- .../markdown/renderer/ListRenderer.kt | 11 +- .../segments/SegmentHeightMeasurer.kt | 4 +- .../markdown/utils/text/view/TextViewSetup.kt | 3 + .../markdown/ListBlockquoteMeasurementTest.kt | 244 ++++++++++++++++++ 6 files changed, 269 insertions(+), 7 deletions(-) create mode 100644 packages/react-native-enriched-markdown/android/src/test/java/com/swmansion/enriched/markdown/ListBlockquoteMeasurementTest.kt diff --git a/packages/app/package.json b/packages/app/package.json index 00c64936c1..125c49546e 100644 --- a/packages/app/package.json +++ b/packages/app/package.json @@ -40,7 +40,7 @@ "dev:ios": "npm --prefix ../.. run build:client && cross-env APP_VARIANT=development expo run:ios", "build:apk": "npm --prefix ../.. run build:apk", "build:apk:arm64": "npm --prefix ../.. run build:apk:arm64", - "test:android": "expo prebuild --platform android --no-install && cd android && ./gradlew :paseo-native-trace:testReleaseUnitTest :paseo-hardware-keyboard:testReleaseUnitTest :react-native-enriched-markdown:testReleaseUnitTest --tests '*InlineCodeFormattingTest' --tests '*ListRenderingTest' --tests '*TaskListInteractionTest' --no-daemon --max-workers=2 -Dorg.gradle.parallel=false" + "test:android": "expo prebuild --platform android --no-install && cd android && ./gradlew :paseo-native-trace:testReleaseUnitTest :paseo-hardware-keyboard:testReleaseUnitTest :react-native-enriched-markdown:testReleaseUnitTest --tests '*InlineCodeFormattingTest' --tests '*ListRenderingTest' --tests '*TaskListInteractionTest' --tests '*ListBlockquoteMeasurementTest' --no-daemon --max-workers=2 -Dorg.gradle.parallel=false" }, "dependencies": { "@codemirror/commands": "6.10.4", diff --git a/packages/react-native-enriched-markdown/android/src/main/java/com/swmansion/enriched/markdown/MeasurementStore.kt b/packages/react-native-enriched-markdown/android/src/main/java/com/swmansion/enriched/markdown/MeasurementStore.kt index e36b4448a0..6b7e61f508 100644 --- a/packages/react-native-enriched-markdown/android/src/main/java/com/swmansion/enriched/markdown/MeasurementStore.kt +++ b/packages/react-native-enriched-markdown/android/src/main/java/com/swmansion/enriched/markdown/MeasurementStore.kt @@ -45,6 +45,7 @@ import com.swmansion.enriched.markdown.utils.common.parseLinkPillContent import com.swmansion.enriched.markdown.utils.common.parseLinkRecognition import com.swmansion.enriched.markdown.utils.text.extensions.replaceMathSpansWithPlaceholders import com.swmansion.enriched.markdown.utils.text.span.prepareWidthAwareSpans +import com.swmansion.enriched.markdown.utils.text.view.wrapForInsertionOrderedLayout import java.util.concurrent.ConcurrentHashMap import kotlin.math.ceil @@ -681,7 +682,7 @@ object MeasurementStore { } } - private fun createStaticLayout( + internal fun createStaticLayout( text: CharSequence, fontSize: Float, widthPx: Int, @@ -689,8 +690,9 @@ object MeasurementStore { ): StaticLayout { measurePaint.textSize = fontSize prepareWidthAwareSpans(text, widthPx) + val measuredText = wrapForInsertionOrderedLayout(text) return StaticLayout.Builder - .obtain(text, 0, text.length, measurePaint, widthPx) + .obtain(measuredText, 0, measuredText.length, measurePaint, widthPx) .setIncludePad(false) .setLineSpacing(0f, 1f) .apply { @@ -776,10 +778,11 @@ object MeasurementStore { val content = text ?: "" val safeWidth = ceil(maxWidth).toInt().coerceAtLeast(1) prepareWidthAwareSpans(content, safeWidth) + val measuredText = wrapForInsertionOrderedLayout(content) val builder = StaticLayout.Builder - .obtain(content, 0, content.length, paint, safeWidth) + .obtain(measuredText, 0, measuredText.length, paint, safeWidth) .setIncludePad(false) .setLineSpacing(0f, 1f) @@ -882,10 +885,11 @@ object MeasurementStore { val content = text ?: "" val widthPx = ceil(maxWidth).toInt().coerceAtLeast(1) prepareWidthAwareSpans(content, widthPx) + val measuredText = wrapForInsertionOrderedLayout(content) val layout = StaticLayout.Builder - .obtain(content, 0, content.length, paint, widthPx) + .obtain(measuredText, 0, measuredText.length, paint, widthPx) .setIncludePad(false) .setLineSpacing(0f, 1f) .apply { diff --git a/packages/react-native-enriched-markdown/android/src/main/java/com/swmansion/enriched/markdown/renderer/ListRenderer.kt b/packages/react-native-enriched-markdown/android/src/main/java/com/swmansion/enriched/markdown/renderer/ListRenderer.kt index 4f87447b27..d3440014a2 100644 --- a/packages/react-native-enriched-markdown/android/src/main/java/com/swmansion/enriched/markdown/renderer/ListRenderer.kt +++ b/packages/react-native-enriched-markdown/android/src/main/java/com/swmansion/enriched/markdown/renderer/ListRenderer.kt @@ -7,6 +7,7 @@ import com.swmansion.enriched.markdown.spans.MarginBottomSpan import com.swmansion.enriched.markdown.spans.OrderedListSpan import com.swmansion.enriched.markdown.styles.ListStyle import com.swmansion.enriched.markdown.utils.text.span.SPAN_FLAGS_EXCLUSIVE_EXCLUSIVE +import com.swmansion.enriched.markdown.utils.text.span.SPAN_FLAGS_LINE_HEIGHT_PRIORITY import com.swmansion.enriched.markdown.utils.text.span.applyLineHeightSkippingImages import com.swmansion.enriched.markdown.utils.text.span.applyMarginTop @@ -87,7 +88,15 @@ class ListRenderer( // TODO: LineHeightSpan may also clip superscript/subscript glyphs that extend // outside the normal line bounds. A similar "skip" strategy as for images may // be needed here once super/subscript usage in practice is better understood. - applyLineHeightSkippingImages(builder, start, builder.length, style.lineHeight) + // Priority flag so the list-wide line height iterates before inner block spacer + // spans (blockquote padding/margin, heading margin) instead of inflating them. + applyLineHeightSkippingImages( + builder, + start, + builder.length, + style.lineHeight, + SPAN_FLAGS_LINE_HEIGHT_PRIORITY, + ) // Item spacing and external bottom margin are only handled by the root-level list if (depth == 0) { diff --git a/packages/react-native-enriched-markdown/android/src/main/java/com/swmansion/enriched/markdown/segments/SegmentHeightMeasurer.kt b/packages/react-native-enriched-markdown/android/src/main/java/com/swmansion/enriched/markdown/segments/SegmentHeightMeasurer.kt index 162ebfbffd..6e40bd8fb4 100644 --- a/packages/react-native-enriched-markdown/android/src/main/java/com/swmansion/enriched/markdown/segments/SegmentHeightMeasurer.kt +++ b/packages/react-native-enriched-markdown/android/src/main/java/com/swmansion/enriched/markdown/segments/SegmentHeightMeasurer.kt @@ -9,6 +9,7 @@ import android.text.TextPaint import com.swmansion.enriched.markdown.styles.StyleConfig import com.swmansion.enriched.markdown.utils.text.extensions.replaceMathSpansWithPlaceholders import com.swmansion.enriched.markdown.utils.text.span.prepareWidthAwareSpans +import com.swmansion.enriched.markdown.utils.text.view.wrapForInsertionOrderedLayout import kotlin.math.ceil /** @@ -105,8 +106,9 @@ object SegmentHeightMeasurer { textSize = fontSizePx } prepareWidthAwareSpans(text, widthPx) + val measuredText = wrapForInsertionOrderedLayout(text) return StaticLayout.Builder - .obtain(text, 0, text.length, paint, widthPx) + .obtain(measuredText, 0, measuredText.length, paint, widthPx) .setIncludePad(false) .setLineSpacing(0f, 1f) .apply { diff --git a/packages/react-native-enriched-markdown/android/src/main/java/com/swmansion/enriched/markdown/utils/text/view/TextViewSetup.kt b/packages/react-native-enriched-markdown/android/src/main/java/com/swmansion/enriched/markdown/utils/text/view/TextViewSetup.kt index 4b2ad01589..8742c0cee5 100644 --- a/packages/react-native-enriched-markdown/android/src/main/java/com/swmansion/enriched/markdown/utils/text/view/TextViewSetup.kt +++ b/packages/react-native-enriched-markdown/android/src/main/java/com/swmansion/enriched/markdown/utils/text/view/TextViewSetup.kt @@ -11,6 +11,9 @@ import androidx.appcompat.widget.AppCompatTextView import androidx.core.view.ViewCompat import com.swmansion.enriched.markdown.accessibility.AccessibleMarkdownTextView +internal fun wrapForInsertionOrderedLayout(source: CharSequence): CharSequence = + if (source is SpannableStringBuilder) InsertionOrderedSpannable(source) else source + /** * Adopts the renderer's buffer instead of copying it: the default factory's copy is * quadratic in span count. Safe because each segment's buffer is owned by one view. diff --git a/packages/react-native-enriched-markdown/android/src/test/java/com/swmansion/enriched/markdown/ListBlockquoteMeasurementTest.kt b/packages/react-native-enriched-markdown/android/src/test/java/com/swmansion/enriched/markdown/ListBlockquoteMeasurementTest.kt new file mode 100644 index 0000000000..6115a30630 --- /dev/null +++ b/packages/react-native-enriched-markdown/android/src/test/java/com/swmansion/enriched/markdown/ListBlockquoteMeasurementTest.kt @@ -0,0 +1,244 @@ +package com.swmansion.enriched.markdown + +import android.graphics.Paint +import android.text.Layout +import android.text.SpannableStringBuilder +import android.text.style.LineHeightSpan as AndroidLineHeightSpan +import android.util.TypedValue +import android.view.View.MeasureSpec +import com.facebook.react.bridge.JavaOnlyArray +import com.facebook.react.bridge.JavaOnlyMap +import com.facebook.react.uimanager.DisplayMetricsHolder +import com.swmansion.enriched.markdown.parser.MarkdownASTNode +import com.swmansion.enriched.markdown.parser.MarkdownASTNode.NodeType +import com.swmansion.enriched.markdown.renderer.Renderer +import com.swmansion.enriched.markdown.segments.SegmentHeightMeasurer +import com.swmansion.enriched.markdown.spans.LineHeightSpan +import com.swmansion.enriched.markdown.styles.StyleConfig +import com.swmansion.enriched.markdown.utils.text.span.SPAN_FLAGS_EXCLUSIVE_EXCLUSIVE +import org.junit.Assert.assertEquals +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 + +@RunWith(RobolectricTestRunner::class) +@GraphicsMode(GraphicsMode.Mode.NATIVE) +@Config(manifest = Config.NONE, sdk = [34]) +class ListBlockquoteMeasurementTest { + private fun styleConfig(): StyleConfig { + val context = RuntimeEnvironment.getApplication() + DisplayMetricsHolder.initDisplayMetricsIfNotInitialized(context) + val base = + JavaOnlyMap.of( + "fontFamily", "", + "fontWeight", "normal", + "fontStyle", "normal", + "fontSize", 16.0, + "lineHeight", 22.0, + "color", -16777216.0, + "backgroundColor", 0.0, + "borderColor", 0.0, + "borderWidth", 3.0, + "borderRadius", 0.0, + "padding", 12.0, + "gapWidth", 8.0, + "marginTop", 0.0, + "marginBottom", 12.0, + "marginLeft", 20.0, + "itemSpacing", 4.0, + "bulletColor", -16777216.0, + "bulletSize", 6.0, + "markerMinWidth", 0.0, + "markerColor", -16777216.0, + "markerFontWeight", "normal", + "textAlign", "left", + "underline", false, + "checkedColor", 0.0, + "checkmarkColor", 0.0, + "checkedTextColor", 0.0, + "checkboxSize", 18.0, + "checkboxBorderRadius", 2.0, + "checkedStrikethrough", false, + "particles", JavaOnlyMap.of("density", 1.0, "speed", 1.0), + "solid", JavaOnlyMap.of("borderRadius", 0.0), + "fontScale", 0.7, + "baselineOffsetScale", 0.3, + ) + val styles = JavaOnlyMap() + listOf( + "paragraph", + "h1", + "h2", + "h3", + "h4", + "h5", + "h6", + "strong", + "em", + "strikethrough", + "underline", + "link", + "code", + "blockquote", + "list", + "codeBlock", + "thematicBreak", + "image", + "inlineImage", + "video", + "table", + "taskList", + "math", + "inlineMath", + "spoiler", + "superscript", + "subscript", + "highlight", + ).forEach { styles.putMap(it, base) } + styles.putArray("linkVariants", JavaOnlyArray()) + return StyleConfig(styles, context, allowFontScaling = false, maxFontSizeMultiplier = 1f) + } + + private fun measureTextView( + text: CharSequence, + fontSizePx: Float, + widthPx: Int, + ): EnrichedMarkdownInternalText { + val context = RuntimeEnvironment.getApplication() + return EnrichedMarkdownInternalText(context).apply { + setTextSize(TypedValue.COMPLEX_UNIT_PX, fontSizePx) + breakStrategy = Layout.BREAK_STRATEGY_HIGH_QUALITY + applyStyledText(text) + measure( + MeasureSpec.makeMeasureSpec(widthPx, MeasureSpec.EXACTLY), + MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED), + ) + } + } + + @Test + fun listNestedBlockquotesMatchMeasurementAndKeepSpacerHeights() { + val context = RuntimeEnvironment.getApplication() + val style = styleConfig() + val widthPx = 640 + val fontSizePx = style.paragraphStyle.fontSize + + val ast = + MarkdownASTNode( + NodeType.Document, + children = + listOf( + MarkdownASTNode( + NodeType.OrderedList, + attributes = mapOf("start" to "1"), + children = + listOf( + MarkdownASTNode( + NodeType.ListItem, + children = + listOf( + MarkdownASTNode(NodeType.Paragraph, children = listOf(MarkdownASTNode(NodeType.Text, "First item"))), + MarkdownASTNode( + NodeType.Blockquote, + children = + listOf( + MarkdownASTNode(NodeType.Paragraph, children = listOf(MarkdownASTNode(NodeType.Text, "First quote"))), + ), + ), + ), + ), + MarkdownASTNode( + NodeType.ListItem, + children = + listOf( + MarkdownASTNode(NodeType.Paragraph, children = listOf(MarkdownASTNode(NodeType.Text, "Second item"))), + MarkdownASTNode( + NodeType.Blockquote, + children = + listOf( + MarkdownASTNode(NodeType.Paragraph, children = listOf(MarkdownASTNode(NodeType.Text, "Second quote"))), + ), + ), + ), + ), + ), + ), + MarkdownASTNode(NodeType.Paragraph, children = listOf(MarkdownASTNode(NodeType.Text, "Trailing paragraph"))), + ), + ) + + val renderer = Renderer().apply { configure(style, context) } + val rendered = renderer.renderDocument(ast) + + val storeLayout = MeasurementStore.createStaticLayout(rendered, fontSizePx, widthPx, null) + val segmentLayout = + SegmentHeightMeasurer.createStaticLayout(rendered, fontSizePx, widthPx, Layout.BREAK_STRATEGY_HIGH_QUALITY) + val textView = measureTextView(rendered, fontSizePx, widthPx) + + assertEquals("MeasurementStore and TextView must agree on height", textView.measuredHeight, storeLayout.height) + assertEquals("SegmentHeightMeasurer and TextView must agree on height", textView.measuredHeight, segmentLayout.height) + + // Verify blockquote top-padding and bottom-margin spacer lines keep their configured + // heights instead of being inflated to list.lineHeight (22dp). + val paddingPx = style.blockquoteStyle.padding.toInt() + val marginBottomPx = style.blockquoteStyle.marginBottom.toInt() + val itemSpacingPx = style.listStyle.itemSpacing.toInt() + val spacerLineHeights = + (0 until storeLayout.lineCount) + .filter { line -> + val start = storeLayout.getLineStart(line) + val end = storeLayout.getLineEnd(line) + end - start == 1 && rendered[start] == '\n' + }.map { line -> storeLayout.getLineBottom(line) - storeLayout.getLineTop(line) } + + assertEquals( + listOf(paddingPx, marginBottomPx + itemSpacingPx, paddingPx, marginBottomPx, marginBottomPx), + spacerLineHeights, + ) + } + + @Test + fun measurementStaticLayoutUsesInsertionOrderLikeTextView() { + val context = RuntimeEnvironment.getApplication() + DisplayMetricsHolder.initDisplayMetricsIfNotInitialized(context) + val widthPx = 400 + val fontSizePx = 16f + + // Inner priority-0 spacer inserted before outer priority-0 LineHeightSpan: + // SpannableStringBuilder's unsorted fast path orders by start offset (outer first), + // while InsertionOrderedSpannable orders by insertion order (inner first, so outer inflates it). + val builder = SpannableStringBuilder("first\n\nsecond") + builder.setSpan( + object : AndroidLineHeightSpan { + override fun chooseHeight( + text: CharSequence, + start: Int, + end: Int, + spanstartv: Int, + v: Int, + fm: Paint.FontMetricsInt, + ) { + fm.top = 0 + fm.ascent = 0 + fm.descent = 12 + fm.bottom = 12 + } + }, + 6, + 7, + SPAN_FLAGS_EXCLUSIVE_EXCLUSIVE, + ) + builder.setSpan(LineHeightSpan(40f), 0, builder.length, SPAN_FLAGS_EXCLUSIVE_EXCLUSIVE) + + val storeLayout = MeasurementStore.createStaticLayout(builder, fontSizePx, widthPx, null) + val segmentLayout = + SegmentHeightMeasurer.createStaticLayout(builder, fontSizePx, widthPx, Layout.BREAK_STRATEGY_HIGH_QUALITY) + val textView = measureTextView(builder, fontSizePx, widthPx) + + assertEquals(textView.measuredHeight, storeLayout.height) + assertEquals(textView.measuredHeight, segmentLayout.height) + } +}