Skip to content

Commit df8c17b

Browse files
fix(android): enriched text initial height measure (#703)
# Summary Fixes: #700 ## Test Plan Run reproduction steps from issue: #700 EnrichedText height should be measured properly. ## Screenshots / Videos https://github.com/user-attachments/assets/5cf47a83-320b-4542-9714-bc8ab1054c8b ## Compatibility | OS | Implemented | | ------- | :---------: | | iOS | ❌ | | Android | ✅ | | Web | ❌ | ## Checklist - [x] E2E tests are passing - [ ] Required E2E tests have been added (if applicable)
1 parent c4390c7 commit df8c17b

2 files changed

Lines changed: 19 additions & 5 deletions

File tree

android/src/main/java/com/swmansion/enriched/text/MeasurementStore.kt

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import com.facebook.react.views.text.ReactTypefaceUtils.parseFontWeight
1717
import com.facebook.yoga.YogaMeasureMode
1818
import com.facebook.yoga.YogaMeasureOutput
1919
import com.swmansion.enriched.common.EnrichedConstants
20+
import com.swmansion.enriched.common.GumboNormalizer
2021
import com.swmansion.enriched.common.allowFontScalingFromProps
2122
import com.swmansion.enriched.common.parser.EnrichedParser
2223
import com.swmansion.enriched.common.pixelFromSpOrDp
@@ -104,24 +105,35 @@ object MeasurementStore {
104105
props: ReadableMap?,
105106
): CharSequence {
106107
val text = props?.getString("text") ?: ""
108+
val isInternalHtml = text.startsWith("<html>") && text.endsWith("</html>")
109+
val useHtmlNormalizer = useHtmlNormalizerFromProps(props)
107110

108-
val isHtml = text.startsWith("<html>") && text.endsWith("</html>")
109-
if (!isHtml) return text
111+
if (!isInternalHtml && !useHtmlNormalizer) {
112+
return text
113+
}
110114

111115
try {
116+
val textToParse = if (isInternalHtml) text else GumboNormalizer.normalizeHtml(text)
112117
val style = props?.getMap("htmlStyle") ?: return text
113118
val allowFontScaling = allowFontScalingFromProps(props)
114-
val enrichedStyle =
115-
EnrichedTextStyle.fromReadableMap(context as ReactContext, fontSize, style, allowFontScaling)
119+
val enrichedStyle = EnrichedTextStyle.fromReadableMap(context as ReactContext, fontSize, style, allowFontScaling)
120+
116121
val factory = EnrichedTextSpanFactory()
117-
val parsed = EnrichedParser.fromHtml(text, enrichedStyle, factory)
122+
val parsed = EnrichedParser.fromHtml(textToParse, enrichedStyle, factory)
118123
return parsed.trimEnd('\n')
119124
} catch (e: Exception) {
120125
Log.w("MeasurementStore", "Error parsing initial HTML text: ${e.message}")
121126
return text
122127
}
123128
}
124129

130+
private fun useHtmlNormalizerFromProps(props: ReadableMap?): Boolean {
131+
if (props == null || !props.hasKey("useHtmlNormalizer") || props.isNull("useHtmlNormalizer")) {
132+
return false
133+
}
134+
return props.getBoolean("useHtmlNormalizer")
135+
}
136+
125137
private fun getInitialFontSize(props: ReadableMap?): Float {
126138
val propsFontSize = props?.getDouble("fontSize")?.toFloat() ?: EnrichedConstants.TEXT_DEFAULT_FONT_SIZE
127139
val fontSize =

android/src/main/new_arch/react/renderer/components/ReactNativeEnrichedSpec/conversions.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ inline folly::dynamic toDynamic(const EnrichedTextInputViewProps &props) {
1919
serializedProps["fontFamily"] = props.fontFamily;
2020
serializedProps["lineHeight"] = props.lineHeight;
2121
serializedProps["allowFontScaling"] = props.allowFontScaling;
22+
serializedProps["useHtmlNormalizer"] = props.useHtmlNormalizer;
2223
serializedProps["htmlStyle"] = toDynamic(props.htmlStyle);
2324

2425
return serializedProps;
@@ -36,6 +37,7 @@ inline folly::dynamic toDynamic(const EnrichedTextViewProps &props) {
3637
serializedProps["numberOfLines"] = props.numberOfLines;
3738
serializedProps["ellipsizeMode"] = props.ellipsizeMode;
3839
serializedProps["allowFontScaling"] = props.allowFontScaling;
40+
serializedProps["useHtmlNormalizer"] = props.useHtmlNormalizer;
3941
serializedProps["htmlStyle"] = toDynamic(props.htmlStyle);
4042

4143
return serializedProps;

0 commit comments

Comments
 (0)