Skip to content

Commit 22fca1f

Browse files
fix: ranges
1 parent 9aa7c72 commit 22fca1f

1 file changed

Lines changed: 5 additions & 11 deletions

File tree

ios/htmlParser/HtmlParser.mm

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -645,6 +645,9 @@ + (NSArray *_Nonnull)getTextAndStylesFromHtml:(NSString *_Nonnull)fixedHtml {
645645

646646
// process tags into proper StyleType + StylePair values
647647
NSMutableArray *processedStyles = [[NSMutableArray alloc] init];
648+
// Tracks the number of processed images to remove their pre-generated
649+
// placeholder offsets from tag ranges when reading from plainText
650+
// (which does not contain those placeholders).
648651
NSInteger secondPassImageCount = 0;
649652

650653
for (NSArray *arr in initiallyProcessedTags) {
@@ -749,17 +752,8 @@ + (NSArray *_Nonnull)getTextAndStylesFromHtml:(NSString *_Nonnull)fixedHtml {
749752
// placeholder, which don't exist in plainText. Subtract them to map
750753
// back to the correct plainText index.
751754
NSRange adjustedRange = tagRangeValue.rangeValue;
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);
755+
NSRange plainTextRange = NSMakeRange(
756+
adjustedRange.location - secondPassImageCount, adjustedRange.length);
763757
NSString *text = [plainText substringWithRange:plainTextRange];
764758

765759
LinkData *linkData = [[LinkData alloc] init];

0 commit comments

Comments
 (0)