33#import " StyleHeaders.h"
44#import " UIView+React.h"
55#import " TextInsertionUtils.h"
6+ #import " StringExtension.h"
67
78@implementation InputParser {
89 EnrichedTextInputView *_input;
@@ -204,8 +205,8 @@ - (NSString *)parseToHtmlFromRange:(NSRange)range {
204205 [result appendString: [NSString stringWithFormat: @" <%@ >" , tagContent]];
205206 }
206207
207- // append the letter
208- [result appendString: currentCharacterStr];
208+ // append the letter and escape it if needed
209+ [result appendString: [ NSString stringByEscapingHtml: currentCharacterStr] ];
209210
210211 // save current styles for next character's checks
211212 previousActiveStyles = currentActiveStyles;
@@ -490,6 +491,7 @@ - (NSArray *)getTextAndStylesFromHtml:(NSString *)fixedHtml {
490491 BOOL closingTag = NO ;
491492 NSMutableString *currentTagName = [[NSMutableString alloc ] initWithString: @" " ];
492493 NSMutableString *currentTagParams = [[NSMutableString alloc ] initWithString: @" " ];
494+ NSDictionary *htmlEntitiesDict = [NSString getEscapedCharactersInfoFrom: fixedHtml];
493495
494496 // firstly, extract text and initially processed tags
495497 for (int i = 0 ; i < fixedHtml.length ; i++) {
@@ -511,7 +513,7 @@ - (NSArray *)getTextAndStylesFromHtml:(NSString *)fixedHtml {
511513 } else if (!closingTag) {
512514 // we finish opening tag - get its location and optionally params and put them under tag name key in ongoingTags
513515 NSMutableArray *tagArr = [[NSMutableArray alloc ] init ];
514- [tagArr addObject: [NSNumber numberWithInt : plainText.length]];
516+ [tagArr addObject: [NSNumber numberWithInteger : plainText.length]];
515517 if (currentTagParams.length > 0 ) {
516518 [tagArr addObject: [currentTagParams copy ]];
517519 }
@@ -550,8 +552,19 @@ - (NSArray *)getTextAndStylesFromHtml:(NSString *)fixedHtml {
550552 currentTagParams = [[NSMutableString alloc ] initWithString: @" " ];
551553 } else {
552554 if (!insideTag) {
553- // no tags logic - just append text
554- [plainText appendString: currentCharacterStr];
555+ // no tags logic - just append the right text
556+
557+ // html entity on the index; use unescaped character and forward iterator accordingly
558+ NSArray *entityInfo = htmlEntitiesDict[@(i)];
559+ if (entityInfo != nullptr ) {
560+ NSString *escaped = entityInfo[0 ];
561+ NSString *unescaped = entityInfo[1 ];
562+ [plainText appendString: unescaped];
563+ // the iterator will forward by 1 itself
564+ i += escaped.length - 1 ;
565+ } else {
566+ [plainText appendString: currentCharacterStr];
567+ }
555568 } else {
556569 if (gettingTagName) {
557570 if (currentCharacterChar == ' ' ) {
0 commit comments