Skip to content

Commit 0dbb3d1

Browse files
fix(iOS): add error handling in html parsing (#508)
# Summary This PR adds error handling in `html` parsing. ## Test Plan Throw test error from `getTextAndStylesFromHtml` and insert html in example app by "Set input's value" button. Expected result: - The app does not crash - Pasted html is displayed in input ## Screenshots / Videos n/a ## Compatibility | OS | Implemented | | ------- | :---------: | | iOS | ✅ | | Android | ❌ | ## Checklist - [ ] E2E tests are passing - [ ] Required E2E tests have been added (if applicable)
1 parent d8adb83 commit 0dbb3d1

1 file changed

Lines changed: 70 additions & 39 deletions

File tree

ios/inputParser/InputParser.mm

Lines changed: 70 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -592,55 +592,86 @@ - (NSString *)tagContentForStyle:(NSNumber *)style
592592
}
593593

594594
- (void)replaceWholeFromHtml:(NSString *_Nonnull)html {
595-
NSArray *processingResult = [self getTextAndStylesFromHtml:html];
596-
NSString *plainText = (NSString *)processingResult[0];
597-
NSArray *stylesInfo = (NSArray *)processingResult[1];
598-
599595
// reset the text first and reset typing attributes
600596
_input->textView.text = @"";
601597
_input->textView.typingAttributes = _input->defaultTypingAttributes;
602598

603-
// set new text
604-
_input->textView.text = plainText;
605-
606-
// re-apply the styles
607-
[self applyProcessedStyles:stylesInfo
608-
offsetFromBeginning:0
609-
plainTextLength:plainText.length];
599+
@try {
600+
NSArray *processingResult = [self getTextAndStylesFromHtml:html];
601+
NSString *plainText = (NSString *)processingResult[0];
602+
NSArray *stylesInfo = (NSArray *)processingResult[1];
603+
604+
// set new text
605+
_input->textView.text = plainText;
606+
607+
// re-apply the styles
608+
[self applyProcessedStyles:stylesInfo
609+
offsetFromBeginning:0
610+
plainTextLength:plainText.length];
611+
} @catch (NSException *exception) {
612+
RCTLogWarn(@"[EnrichedTextInput]: Failed to parse HTML: (%@), falling back "
613+
@"to raw input.",
614+
exception.reason);
615+
616+
// set new text
617+
_input->textView.text = html;
618+
}
610619
}
611620

612621
- (void)replaceFromHtml:(NSString *_Nonnull)html range:(NSRange)range {
613-
NSArray *processingResult = [self getTextAndStylesFromHtml:html];
614-
NSString *plainText = (NSString *)processingResult[0];
615-
NSArray *stylesInfo = (NSArray *)processingResult[1];
616-
617-
// we can use ready replace util
618-
[TextInsertionUtils replaceText:plainText
619-
at:range
620-
additionalAttributes:nil
621-
input:_input
622-
withSelection:YES];
623-
624-
[self applyProcessedStyles:stylesInfo
625-
offsetFromBeginning:range.location
626-
plainTextLength:plainText.length];
622+
@try {
623+
NSArray *processingResult = [self getTextAndStylesFromHtml:html];
624+
NSString *plainText = (NSString *)processingResult[0];
625+
NSArray *stylesInfo = (NSArray *)processingResult[1];
626+
627+
// we can use ready replace util
628+
[TextInsertionUtils replaceText:plainText
629+
at:range
630+
additionalAttributes:nil
631+
input:_input
632+
withSelection:YES];
633+
634+
[self applyProcessedStyles:stylesInfo
635+
offsetFromBeginning:range.location
636+
plainTextLength:plainText.length];
637+
} @catch (NSException *exception) {
638+
RCTLogWarn(@"[EnrichedTextInput]: Failed to parse HTML: (%@), falling back "
639+
@"to raw input.",
640+
exception.reason);
641+
[TextInsertionUtils replaceText:html
642+
at:range
643+
additionalAttributes:nil
644+
input:_input
645+
withSelection:YES];
646+
}
627647
}
628648

629649
- (void)insertFromHtml:(NSString *_Nonnull)html location:(NSInteger)location {
630-
NSArray *processingResult = [self getTextAndStylesFromHtml:html];
631-
NSString *plainText = (NSString *)processingResult[0];
632-
NSArray *stylesInfo = (NSArray *)processingResult[1];
633-
634-
// same here, insertion utils got our back
635-
[TextInsertionUtils insertText:plainText
636-
at:location
637-
additionalAttributes:nil
638-
input:_input
639-
withSelection:YES];
640-
641-
[self applyProcessedStyles:stylesInfo
642-
offsetFromBeginning:location
643-
plainTextLength:plainText.length];
650+
@try {
651+
NSArray *processingResult = [self getTextAndStylesFromHtml:html];
652+
NSString *plainText = (NSString *)processingResult[0];
653+
NSArray *stylesInfo = (NSArray *)processingResult[1];
654+
655+
// same here, insertion utils got our back
656+
[TextInsertionUtils insertText:plainText
657+
at:location
658+
additionalAttributes:nil
659+
input:_input
660+
withSelection:YES];
661+
662+
[self applyProcessedStyles:stylesInfo
663+
offsetFromBeginning:location
664+
plainTextLength:plainText.length];
665+
} @catch (NSException *exception) {
666+
RCTLogWarn(@"[EnrichedTextInput]: Failed to parse HTML: (%@), falling back "
667+
@"to raw input.",
668+
exception.reason);
669+
[TextInsertionUtils insertText:html
670+
at:location
671+
additionalAttributes:nil
672+
input:_input
673+
withSelection:YES];
674+
}
644675
}
645676

646677
- (void)applyProcessedStyles:(NSArray *)processedStyles

0 commit comments

Comments
 (0)