iOS: Handle positionFromPosition out of bounds

Based on the documentation for positionFromPosition, if the resulting
position is less than 0 or longer than the text, then we should return
nil. This fixes problems with placement of the cursor and selection
rectangle when auto-completion is used.

Fixes: QTBUG-79445
Change-Id: I44a18881527a8a22012fe5fcbbc3216e60c48bc9
Reviewed-by: Tor Arne Vestbø <tor.arne.vestbo@qt.io>
This commit is contained in:
Andy Shaw 2020-01-08 14:48:12 +01:00
parent 1e42c97cf0
commit 1c75f59588

View File

@ -745,7 +745,11 @@
- (UITextPosition *)positionFromPosition:(UITextPosition *)position offset:(NSInteger)offset
{
int p = static_cast<QUITextPosition *>(position).index;
return [QUITextPosition positionWithIndex:p + offset];
const int posWithIndex = p + offset;
const int textLength = [self currentImeState:Qt::ImSurroundingText].toString().length();
if (posWithIndex < 0 || posWithIndex > textLength)
return nil;
return [QUITextPosition positionWithIndex:posWithIndex];
}
- (UITextPosition *)positionFromPosition:(UITextPosition *)position inDirection:(UITextLayoutDirection)direction offset:(NSInteger)offset