Skip to content

Commit ec86b49

Browse files
committed
fix: properly handle new colours in parser
1 parent f4314b7 commit ec86b49

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
@@ -1284,7 +1284,7 @@ - (void)setColor:(NSString *)colorText {
12841284

12851285
- (void)removeColor {
12861286
ColorStyle *colorStyle = stylesDict[@(Colored)];
1287-
[colorStyle removeAttributes: textView.selectedRange];
1287+
[colorStyle removeColorInSelectedRange];
12881288
[self anyTextMayHaveBeenModified];
12891289
}
12901290

ios/inputParser/InputParser.mm

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -60,19 +60,30 @@ - (NSString *)parseToHtmlFromRange:(NSRange)range {
6060
}
6161
}
6262

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

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)