Skip to content

Commit 52f3ab2

Browse files
fix: parsing html into native
1 parent f6bb4c2 commit 52f3ab2

3 files changed

Lines changed: 52 additions & 33 deletions

File tree

ios/inputParser/InputParser.mm

Lines changed: 35 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -456,6 +456,9 @@ - (void)applyProcessedStyles:(NSArray *)processedStyles offsetFromBeginning:(NSI
456456
} else if([styleType isEqualToNumber: @([MentionStyle getStyleType])]) {
457457
MentionParams *params = (MentionParams *)stylePair.styleValue;
458458
[((MentionStyle *)baseStyle) addMentionAtRange:styleRange params:params];
459+
} else if([styleType isEqualToNumber: @([ImageStyle getStyleType])]) {
460+
ImageData *imgData = (ImageData *)stylePair.styleValue;
461+
[((ImageStyle *)baseStyle) addImageAtRange:styleRange imageData:imgData];
459462
} else {
460463
[baseStyle addAttributes:styleRange];
461464
}
@@ -546,6 +549,24 @@ - (NSString *)stringByAddingNewlinesToTag:(NSString *)tag inString:(NSString *)h
546549
return str;
547550
}
548551

552+
- (void)finalizeTagEntry:(NSMutableString *)tagName ongoingTags:(NSMutableDictionary *)ongoingTags initiallyProcessedTags:(NSMutableArray *)processedTags plainText:(NSMutableString *)plainText
553+
{
554+
NSMutableArray *tagEntry = [[NSMutableArray alloc] init];
555+
556+
NSArray *tagData = ongoingTags[tagName];
557+
NSInteger tagLocation = [((NSNumber *)tagData[0]) intValue];
558+
NSRange tagRange = NSMakeRange(tagLocation, plainText.length - tagLocation);
559+
560+
[tagEntry addObject:[tagName copy]];
561+
[tagEntry addObject:[NSValue valueWithRange:tagRange]];
562+
if(tagData.count > 1) {
563+
[tagEntry addObject:[(NSString *)tagData[1] copy]];
564+
}
565+
566+
[processedTags addObject:tagEntry];
567+
[ongoingTags removeObjectForKey:tagName];
568+
}
569+
549570
- (NSArray *)getTextAndStylesFromHtml:(NSString *)fixedHtml {
550571
NSMutableString *plainText = [[NSMutableString alloc] initWithString: @""];
551572
NSMutableDictionary *ongoingTags = [[NSMutableDictionary alloc] init];
@@ -573,6 +594,14 @@ - (NSArray *)getTextAndStylesFromHtml:(NSString *)fixedHtml {
573594
gettingTagName = NO;
574595
gettingTagParams = NO;
575596

597+
BOOL isSelfClosing = NO;
598+
599+
// Check if params ended with '/' (e.g. <img src="" />)
600+
if ([currentTagParams hasSuffix:@"/"]) {
601+
[currentTagParams deleteCharactersInRange:NSMakeRange(currentTagParams.length - 1, 1)];
602+
isSelfClosing = YES;
603+
}
604+
576605
if([currentTagName isEqualToString:@"p"] || [currentTagName isEqualToString:@"br"] || [currentTagName isEqualToString:@"li"]) {
577606
// do nothing, we don't include these tags in styles
578607
} else if(!closingTag) {
@@ -588,6 +617,10 @@ - (NSArray *)getTextAndStylesFromHtml:(NSString *)fixedHtml {
588617
if([currentTagName isEqualToString:@"ul"] || [currentTagName isEqualToString:@"ol"] || [currentTagName isEqualToString:@"blockquote"] || [currentTagName isEqualToString:@"codeblock"]) {
589618
i += 1;
590619
}
620+
621+
if (isSelfClosing) {
622+
[self finalizeTagEntry:currentTagName ongoingTags:ongoingTags initiallyProcessedTags:initiallyProcessedTags plainText:plainText];
623+
}
591624
} else {
592625
// we finish closing tags - pack tag name, tag range and optionally tag params into an entry that goes inside initiallyProcessedTags
593626

@@ -596,20 +629,7 @@ - (NSArray *)getTextAndStylesFromHtml:(NSString *)fixedHtml {
596629
plainText = [[plainText substringWithRange: NSMakeRange(0, plainText.length - 1)] mutableCopy];
597630
}
598631

599-
NSMutableArray *tagEntry = [[NSMutableArray alloc] init];
600-
601-
NSArray *tagData = ongoingTags[currentTagName];
602-
NSInteger tagLocation = [((NSNumber *)tagData[0]) intValue];
603-
NSRange tagRange = NSMakeRange(tagLocation, plainText.length - tagLocation);
604-
605-
[tagEntry addObject:[currentTagName copy]];
606-
[tagEntry addObject:[NSValue valueWithRange:tagRange]];
607-
if(tagData.count > 1) {
608-
[tagEntry addObject:[(NSString *)tagData[1] copy]];
609-
}
610-
611-
[initiallyProcessedTags addObject:tagEntry];
612-
[ongoingTags removeObjectForKey:currentTagName];
632+
[self finalizeTagEntry:currentTagName ongoingTags:ongoingTags initiallyProcessedTags:initiallyProcessedTags plainText:plainText];
613633
}
614634
// post-tag cleanup
615635
closingTag = NO;
@@ -669,7 +689,7 @@ - (NSArray *)getTextAndStylesFromHtml:(NSString *)fixedHtml {
669689
} else if([tagName isEqualToString:@"i"]) {
670690
[styleArr addObject:@([ItalicStyle getStyleType])];
671691
} else if([tagName isEqualToString:@"img"]) {
672-
NSRegularExpression *srcRegex = [NSRegularExpression regularExpressionWithPattern:@"src=\".+\""
692+
NSRegularExpression *srcRegex = [NSRegularExpression regularExpressionWithPattern:@"src=\"([^\"]+)\""
673693
options:0
674694
error:nullptr
675695
];

ios/styles/ImageStyle.mm

Lines changed: 16 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -103,37 +103,35 @@ - (ImageData *)getImageDataAt:(NSUInteger)location
103103
return imageData;
104104
}
105105

106-
- (void)addImage:(NSString *)uri width:(CGFloat)width height:(CGFloat)height {
107-
ImageData *data = [[ImageData alloc] init];
108-
data.uri = uri;
109-
data.width = width;
110-
data.height = height;
111-
106+
- (void)addImageAtRange:(NSRange)range imageData:(ImageData *)imageData
107+
{
112108
NSMutableDictionary *attributes = [@{
113-
NSAttachmentAttributeName: [self prepareImageAttachement:uri width:width height:height],
114-
ImageAttributeName: data,
109+
NSAttachmentAttributeName: [self prepareImageAttachement:imageData.uri width:imageData.width height:imageData.height],
110+
ImageAttributeName: imageData,
115111
} mutableCopy];
116112

117113
// Use the Object Replacement Character for Image.
118114
// This tells TextKit "something non-text goes here".
119115
NSString *imagePlaceholder = @"\uFFFC";
120116

121-
if (_input->textView.selectedRange.length == 0) {
122-
[TextInsertionUtils insertText:imagePlaceholder at:_input->textView.selectedRange.location additionalAttributes:nullptr input:_input withSelection:YES];
117+
if (range.length == 0) {
118+
[TextInsertionUtils insertText:imagePlaceholder at:range.location additionalAttributes:attributes input:_input withSelection:YES];
123119
} else {
124120
[TextInsertionUtils replaceText:imagePlaceholder
125-
at:_input->textView.selectedRange
126-
additionalAttributes:nullptr
121+
at:range
122+
additionalAttributes:attributes
127123
input:_input
128124
withSelection:YES];
129125
}
126+
}
127+
128+
- (void)addImage:(NSString *)uri width:(CGFloat)width height:(CGFloat)height {
129+
ImageData *data = [[ImageData alloc] init];
130+
data.uri = uri;
131+
data.width = width;
132+
data.height = height;
130133

131-
NSRange newSelection = _input->textView.selectedRange;
132-
NSRange imageRange = NSMakeRange(newSelection.location - 1, 1);
133-
134-
[_input->textView.textStorage beginEditing];
135-
[_input->textView.textStorage addAttributes:attributes range:imageRange];
136-
[_input->textView.textStorage endEditing];
134+
[self addImageAtRange:_input->textView.selectedRange imageData:data];
137135
}
138136

139137
-(NSTextAttachment *)prepareImageAttachement:(NSString *)uri width:(CGFloat)width height:(CGFloat)height

ios/utils/StyleHeaders.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,5 +83,6 @@
8383

8484
@interface ImageStyle : NSObject<BaseStyleProtocol>
8585
- (void)addImage:(NSString *)uri width:(CGFloat)width height:(CGFloat)height;
86+
- (void)addImageAtRange:(NSRange)range imageData:(ImageData *)imageData;
8687
- (ImageData *)getImageDataAt:(NSUInteger)location;
8788
@end

0 commit comments

Comments
 (0)