File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -744,9 +744,22 @@ + (NSArray *_Nonnull)getTextAndStylesFromHtml:(NSString *_Nonnull)fixedHtml {
744744 NSString *url =
745745 [params substringWithRange: NSMakeRange (hrefRange.location + 6 ,
746746 hrefRange.length - 7 )];
747+
748+ // tagRange location includes one extra offset per preceding image
749+ // placeholder, which don't exist in plainText. Subtract them to map
750+ // back to the correct plainText index.
747751 NSRange adjustedRange = tagRangeValue.rangeValue ;
748- NSRange plainTextRange = NSMakeRange (
749- adjustedRange.location - secondPassImageCount, adjustedRange.length );
752+ // Calculate the shifted location, ensuring it doesn't drop below 0
753+ NSUInteger rawLoc = (adjustedRange.location >= secondPassImageCount)
754+ ? adjustedRange.location - secondPassImageCount
755+ : 0 ;
756+ // Cap the location so it can never exceed the plain text length
757+ NSUInteger plainLoc = MIN (rawLoc, plainText.length );
758+ // Cap the length based on whatever space is left in the string
759+ NSUInteger plainLen =
760+ MIN (adjustedRange.length , plainText.length - plainLoc);
761+
762+ NSRange plainTextRange = NSMakeRange (plainLoc, plainLen);
750763 NSString *text = [plainText substringWithRange: plainTextRange];
751764
752765 LinkData *linkData = [[LinkData alloc ] init ];
You can’t perform that action at this time.
0 commit comments