Skip to content

Commit 606218a

Browse files
committed
fix: properly handle new colours in parser
1 parent ee75a80 commit 606218a

4 files changed

Lines changed: 30 additions & 8 deletions

File tree

ios/EnrichedTextInputView.mm

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -970,7 +970,7 @@ - (void)setColor:(NSString *)colorText {
970970

971971
- (void)removeColor {
972972
ColorStyle *colorStyle = stylesDict[@(Colored)];
973-
[colorStyle removeAttributes: textView.selectedRange];
973+
[colorStyle removeColorInSelectedRange];
974974
[self anyTextMayHaveBeenModified];
975975
}
976976

ios/inputParser/InputParser.mm

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -55,19 +55,30 @@ - (NSString *)parseToHtmlFromRange:(NSRange)range {
5555
}
5656
}
5757

58-
// Handle valued styles changes
5958
UIColor *currentColor = nil;
60-
59+
6160
if([currentActiveStyles containsObject:@(Colored)]) {
6261
ColorStyle *colorStyle = _input->stylesDict[@(Colored)];
6362
currentColor = [colorStyle getColorAt:currentRange.location];
64-
if(previousColor && ![currentColor isEqual:previousColor]) {
65-
// Treat as end of previous color and start of new
66-
[currentActiveStyles removeObject:@(Colored)];
67-
currentActiveStylesBeginning[@(Colored)] = [NSNumber numberWithInt:i];
63+
// If the end of one color overlaps exactly with the start of the next
64+
// the previous font tag should be closed and a new one opened using the new color.
65+
if (previousColor && ![currentColor isEqual:previousColor]) {
66+
NSString *closeTag = [self tagContentForStyle:@(Colored)
67+
openingTag:NO
68+
location:currentRange.location];
69+
[result appendFormat:@"</%@>", closeTag];
70+
NSString *openTag = [self tagContentForStyle:@(Colored)
71+
openingTag:YES
72+
location:currentRange.location];
73+
[result appendFormat:@"<%@>", openTag];
74+
NSString *currentCharacterStr = [_input->textView.textStorage.string substringWithRange:currentRange];
75+
[result appendString: currentCharacterStr];
76+
previousColor = currentColor;
77+
[currentActiveStyles addObject:@(Colored)];
78+
continue;
6879
}
6980
}
70-
81+
7182
NSString *currentCharacterStr = [_input->textView.textStorage.string substringWithRange:currentRange];
7283
unichar currentCharacterChar = [_input->textView.textStorage.string characterAtIndex:currentRange.location];
7384

ios/styles/ColorStyle.mm

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -303,4 +303,14 @@ - (UIColor *)baseColorForLocation:(NSUInteger)location {
303303
return baseColor;
304304
}
305305

306+
- (void)removeColorInSelectedRange {
307+
NSRange selectedRange = _input->textView.selectedRange;
308+
309+
if(selectedRange.length > 0) {
310+
[self removeAttributes: selectedRange];
311+
} else {
312+
[self removeTypingAttributes];
313+
}
314+
}
315+
306316
@end

ios/utils/StyleHeaders.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
- (UIColor *)getColorAt:(NSUInteger)location;
2626
- (void)applyStyle:(NSRange)range color:(UIColor *)color;
2727
- (BOOL)detectExcludingColor:(UIColor *)excludedColor inRange:(NSRange)range;
28+
- (void)removeColorInSelectedRange;
2829
@end
2930

3031
@interface LinkStyle : NSObject <BaseStyleProtocol>

0 commit comments

Comments
 (0)