Skip to content

Commit 4a51e61

Browse files
authored
feat: make <a> tags with no href or empty href not treated as links (#260)
<!-- Thanks for submitting a pull request! We appreciate you spending the time to work on these changes. Please follow the template so that the reviewers can easily understand what the code changes affect --> # Summary This PR is an alternative to other PR in here handling iOS crashes due to `<a>` tag not having `href` attribute. While the other on e decides to allow empty `hrefs` as proper links, Android has no such logic. We discussed it a bit with @exploIF and are more eager to go the following way; - No `href` attribute or an empty one => `<a>` tag not treated as a a link ## Test Plan - Open iOS example app - Input following html either into `Set input's value` field or into the component's `defaultValue` prop in `App.tsx`; ``` <html> <p><a href="xd.com">Test2xdd</a></p> </html> ``` Should be nicely treated as a link. Now make the href empty or completely remove the attribute from the tag. It should not be treated as a link and a new html won't have an `<a>` tag in there anymore. ## Screenshots / Videos -- ## Compatibility | OS | Implemented | | ------- | :---------: | | iOS | ✅ | | Android | ✅ (already handled that way) |
1 parent fe30050 commit 4a51e61

File tree

1 file changed

+14
-2
lines changed

1 file changed

+14
-2
lines changed

ios/inputParser/InputParser.mm

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -610,9 +610,21 @@ - (NSArray *)getTextAndStylesFromHtml:(NSString *)fixedHtml {
610610
} else if([tagName isEqualToString:@"code"]) {
611611
[styleArr addObject:@([InlineCodeStyle getStyleType])];
612612
} else if([tagName isEqualToString:@"a"]) {
613+
NSRegularExpression *hrefRegex = [NSRegularExpression regularExpressionWithPattern:@"href=\".+\""
614+
options:0
615+
error:nullptr
616+
];
617+
NSTextCheckingResult* match = [hrefRegex firstMatchInString:params options:0 range: NSMakeRange(0, params.length)];
618+
619+
if(match == nullptr) {
620+
// same as on Android, no href (or empty href) equals no link style
621+
continue;
622+
}
623+
624+
NSRange hrefRange = match.range;
613625
[styleArr addObject:@([LinkStyle getStyleType])];
614626
// cut only the url from the href="..." string
615-
NSString *url = [params substringWithRange:NSMakeRange(6, params.length - 7)];
627+
NSString *url = [params substringWithRange:NSMakeRange(hrefRange.location + 6, hrefRange.length - 7)];
616628
stylePair.styleValue = url;
617629
} else if([tagName isEqualToString:@"mention"]) {
618630
[styleArr addObject:@([MentionStyle getStyleType])];
@@ -657,7 +669,7 @@ - (NSArray *)getTextAndStylesFromHtml:(NSString *)fixedHtml {
657669
} else if([tagName isEqualToString:@"blockquote"]) {
658670
[styleArr addObject:@([BlockQuoteStyle getStyleType])];
659671
} else {
660-
// some other external tags like span just don't get put into the processed styles
672+
// some other external tags like span just don't get put into the processed styles
661673
continue;
662674
}
663675

0 commit comments

Comments
 (0)