cappuccino/cappuccino — Issue #1398: CPTokenField corrupts selection when deleting a token
Author: walisser
Created: 2011-11-15 23:18:36+00:00
State: open

This was a tricky bugger to pin down. The internal selection state became invalid, which caused a later crash when it was referenced elsewhere.

Summary: If a token is deleted that not in the current selection, and occurs before the current selection, the selection becomes invalid, rather than take into account the deletion.

The fix is below from 0.9.5 branch between

```objective-j
- (void)_deleteToken:(_CPTokenFieldToken)token
{
var indexOfToken = [[self _tokens] indexOfObject:token],
objectValue = [self objectValue];

// If the token was selected, deselect it for selection preservation.
[self _deselectToken:token];

// : if the removed token is in front of the selected range,
// when removed, the range becomes invalid
//
if (indexOfToken < _selectedRange.location)
_selectedRange.location--;
//

var selection = CPCopyRange(_selectedRange);
[objectValue removeObjectAtIndex:indexOfToken];
[self setObjectValue:objectValue];
_selectedRange = selection;

[self setNeedsLayout];
[self _controlTextDidChange];
}
```