@@ -52,19 +52,23 @@ + (void)removeSpacesIfNeededinInput:(EnrichedTextInputView *)input {
5252
5353 // do the removing
5454 NSInteger offset = 0 ;
55- NSInteger postRemoveOffset = 0 ;
55+ NSInteger postRemoveLocationOffset = 0 ;
56+ NSInteger postRemoveLengthOffset = 0 ;
5657 for (NSNumber *index in indexesToBeRemoved) {
5758 NSRange replaceRange = NSMakeRange ([index integerValue ] + offset, 1 );
5859 [TextInsertionUtils replaceText: @" " at: replaceRange additionalAttributes: nullptr input: input withSelection: NO ];
5960 offset -= 1 ;
6061 if ([index integerValue ] < preRemoveSelection.location ) {
61- postRemoveOffset -= 1 ;
62+ postRemoveLocationOffset -= 1 ;
63+ }
64+ if ([index integerValue ] >= preRemoveSelection.location && [index integerValue ] < NSMaxRange (preRemoveSelection)) {
65+ postRemoveLengthOffset -= 1 ;
6266 }
6367 }
6468
6569 // fix the selection if needed
6670 if ([input->textView isFirstResponder ]) {
67- input->textView .selectedRange = NSMakeRange (preRemoveSelection.location + postRemoveOffset , preRemoveSelection.length );
71+ input->textView .selectedRange = NSMakeRange (preRemoveSelection.location + postRemoveLocationOffset , preRemoveSelection.length + postRemoveLengthOffset );
6872 }
6973}
7074
@@ -93,13 +97,17 @@ + (void)addSpacesIfNeededinInput:(EnrichedTextInputView *)input {
9397
9498 // do the replacing
9599 NSInteger offset = 0 ;
96- NSInteger postAddOffset = 0 ;
100+ NSInteger postAddLocationOffset = 0 ;
101+ NSInteger postAddLengthOffset = 0 ;
97102 for (NSNumber *index in indexesToBeInserted) {
98103 NSRange replaceRange = NSMakeRange ([index integerValue ] + offset, 1 );
99104 [TextInsertionUtils replaceText: @" \u200B\n " at: replaceRange additionalAttributes: nullptr input: input withSelection: NO ];
100105 offset += 1 ;
101106 if ([index integerValue ] < preAddSelection.location ) {
102- postAddOffset += 1 ;
107+ postAddLocationOffset += 1 ;
108+ }
109+ if ([index integerValue ] >= preAddSelection.location && [index integerValue ] < NSMaxRange (preAddSelection)) {
110+ postAddLengthOffset += 1 ;
103111 }
104112 }
105113
@@ -112,7 +120,7 @@ + (void)addSpacesIfNeededinInput:(EnrichedTextInputView *)input {
112120
113121 // fix the selection if needed
114122 if ([input->textView isFirstResponder ]) {
115- input->textView .selectedRange = NSMakeRange (preAddSelection.location + postAddOffset , preAddSelection.length );
123+ input->textView .selectedRange = NSMakeRange (preAddSelection.location + postAddLocationOffset , preAddSelection.length + postAddLengthOffset );
116124 }
117125}
118126
0 commit comments