Fix RCTText crashing when negative width is passed to measure

Summary: In some rare cases, RCTText.measure can receive negative width which will crash with an assertion in android.text.Layout because it expects a positive value. This is a temporary fix to treat negative values as unconstrained width until the original bug is fixed.

Reviewed By: sriramramani

Differential Revision: D3038767
This commit is contained in:
Denis Koroskin
2016-03-10 17:18:49 -08:00
committed by Ahmed El-Helw
parent aa609a46ad
commit 68580fcab6

View File

@@ -78,6 +78,10 @@ import com.facebook.react.uimanager.annotations.ReactProp;
mText = text;
// technically, width should never be negative, but there is currently a bug in
// LayoutEngine where a negative value can be passed.
boolean unconstrainedWidth = Float.isNaN(width) || width < 0;
BoringLayout.Metrics metrics = BoringLayout.isBoring(text, PAINT, sBoringLayoutMetrics);
if (metrics != null) {
sBoringLayoutMetrics = mBoringLayoutMetrics;
@@ -93,7 +97,7 @@ import com.facebook.react.uimanager.annotations.ReactProp;
mBoringLayoutMetrics = metrics;
float measuredWidth = (float) metrics.width;
if (Float.isNaN(width) || measuredWidth <= width) {
if (unconstrainedWidth || measuredWidth <= width) {
measureOutput.width = measuredWidth;
measureOutput.height = getMetricsHeight(metrics, INCLUDE_PADDING);
@@ -106,7 +110,7 @@ import com.facebook.react.uimanager.annotations.ReactProp;
// width < measuredWidth -> more that a single line -> not boring
}
int maximumWidth = Float.isNaN(width) ? Integer.MAX_VALUE : (int) width;
int maximumWidth = unconstrainedWidth ? Integer.MAX_VALUE : (int) width;
// Make sure we update the paint's text size. If we don't do this, ellipsis might be measured
// incorrecly (but drawn correctly, which almost feels like an Android bug, because width of the