Skip to content

Commit 410965c

Browse files
rigdernrozele
authored andcommitted
Make MeasureTextInput robust in small spaces (microsoft#572)
Currently, MeasureTextInput crashes when it calls textBlock.Measure with a negative value for normalizedWidth. This fixes the bug by ensuring normalizedWidth is at least 0. Additionally, normalizedWidth's calculation was refactored into a single assignment to minimize the chances that in the future somebody will use normalizedWidth before it has been clamped.
1 parent 5e572eb commit 410965c

File tree

1 file changed

+7
-7
lines changed

1 file changed

+7
-7
lines changed

ReactWindows/ReactNative/Views/TextInput/ReactTextInputShadowNode.cs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -252,16 +252,16 @@ private static MeasureOutput MeasureTextInput(CSSNode node, float width, CSSMeas
252252
var textInputNode = (ReactTextInputShadowNode)node;
253253
textInputNode._computedPadding = textInputNode.GetComputedPadding();
254254

255-
var normalizedWidth = CSSConstants.IsUndefined(width) ? double.PositiveInfinity : width;
256-
var normalizedHeight = CSSConstants.IsUndefined(height) ? double.PositiveInfinity : height;
257-
258255
var borderLeftWidth = textInputNode.GetBorder(CSSSpacingType.Left);
259256
var borderRightWidth = textInputNode.GetBorder(CSSSpacingType.Right);
260257

261-
normalizedWidth -= textInputNode._computedPadding[0];
262-
normalizedWidth -= textInputNode._computedPadding[2];
263-
normalizedWidth -= CSSConstants.IsUndefined(borderLeftWidth) ? 0 : borderLeftWidth;
264-
normalizedWidth -= CSSConstants.IsUndefined(borderRightWidth) ? 0 : borderRightWidth;
258+
var normalizedWidth = Math.Max(0,
259+
(CSSConstants.IsUndefined(width) ? double.PositiveInfinity : width)
260+
- textInputNode._computedPadding[0]
261+
- textInputNode._computedPadding[2]
262+
- (CSSConstants.IsUndefined(borderLeftWidth) ? 0 : borderLeftWidth)
263+
- (CSSConstants.IsUndefined(borderRightWidth) ? 0 : borderRightWidth));
264+
var normalizedHeight = Math.Max(0, CSSConstants.IsUndefined(height) ? double.PositiveInfinity : height);
265265

266266
// This is not a terribly efficient way of projecting the height of
267267
// the text elements. It requires that we have access to the

0 commit comments

Comments
 (0)