Skip to content

Commit 96468ce

Browse files
fix(iOS): rendering images from html with link (#662)
# Summary Fixes: #661 ## Test Plan Insert html by `Set input's value` button into example app: `<html><p><img src="xxx" width="100" height="100"/><img src="xxx" width="100" height="100"/><img src="xxx" width="100" height="100"/></p><p>See <a href="https://example.com">a link</a></p></html>` The html should be parsed properly. ## Screenshots / Videos after: https://github.com/user-attachments/assets/eb79f199-a5d5-4edc-ba4d-cc65074644aa ## Compatibility | OS | Implemented | | ------- | :---------: | | iOS | ✅ | | Android | ❌ | | Web | ❌ | ## Checklist - [x] E2E tests are passing - [ ] Required E2E tests have been added (if applicable)
1 parent 34ff98f commit 96468ce

2 files changed

Lines changed: 17 additions & 5 deletions

File tree

apps/example/ios/Podfile.lock

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2026,7 +2026,7 @@ EXTERNAL SOURCES:
20262026

20272027
SPEC CHECKSUMS:
20282028
FBLazyVector: c00c20551d40126351a6783c47ce75f5b374851b
2029-
hermes-engine: 484c595d9e6a0b7b7607e8ead508ba5c472493c7
2029+
hermes-engine: b4dad6ba67535bb03c8ff1006b337cba14db16cb
20302030
RCTDeprecation: 3bb167081b134461cfeb875ff7ae1945f8635257
20312031
RCTRequired: 74839f55d5058a133a0bc4569b0afec750957f64
20322032
RCTSwiftUI: 87a316382f3eab4dd13d2a0d0fd2adcce917361a
@@ -2035,7 +2035,7 @@ SPEC CHECKSUMS:
20352035
React: 1b1536b9099195944034e65b1830f463caaa8390
20362036
React-callinvoker: 6dff6d17d1d6cc8fdf85468a649bafed473c65f5
20372037
React-Core: 39ee05b5798296f433dd3c3624c57a187c1510e3
2038-
React-Core-prebuilt: 3ca7a49d919f940e7de8fb0c2a3f5cfcb665f09b
2038+
React-Core-prebuilt: 69556f895326f23c007f3a6869340045d7dca106
20392039
React-CoreModules: e78bfd2617075bc0e50c689df4a29232bd72ad82
20402040
React-cxxreact: 3fe21801d46097cf74c3dff6953677bebc4a3c2a
20412041
React-debug: e1f00fcd2cef58a2897471a6d76a4ef5f5f90c74
@@ -2097,8 +2097,8 @@ SPEC CHECKSUMS:
20972097
ReactAppDependencyProvider: 706b65371b90b5cc797b6639e8979f2e5cecd6da
20982098
ReactCodegen: ab01ebfffac5cda9140204eb872ed97c15df225f
20992099
ReactCommon: 47ef95b0920948a0b54d7439f7452501eeeac071
2100-
ReactNativeDependencies: 652705a9bc92800d0b1e15177a61ba70d89d24dd
2101-
ReactNativeEnrichedHtml: 93722241410f2daaa8c20ce6bcfcf4666bfd9166
2100+
ReactNativeDependencies: 8a208df374583424130645685d86306befc275cf
2101+
ReactNativeEnrichedHtml: 7d90df4aced7f533c7bd15ac296879b214413361
21022102
Yoga: e83c3121d079541e69f3c5c623faaaf933fb5812
21032103

21042104
PODFILE CHECKSUM: 88c10840d02e9884b2dc3f457d5120f83ac3803b

ios/htmlParser/HtmlParser.mm

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -645,6 +645,10 @@ + (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).
651+
NSInteger secondPassImageCount = 0;
648652

649653
for (NSArray *arr in initiallyProcessedTags) {
650654
NSString *tagName = (NSString *)arr[0];
@@ -715,6 +719,7 @@ + (NSArray *_Nonnull)getTextAndStylesFromHtml:(NSString *_Nonnull)fixedHtml {
715719
}
716720

717721
stylePair.styleValue = imageData;
722+
secondPassImageCount++;
718723
} else if ([tagName isEqualToString:@"u"]) {
719724
[styleArr addObject:@([UnderlineStyle getType])];
720725
} else if ([tagName isEqualToString:@"s"]) {
@@ -742,7 +747,14 @@ + (NSArray *_Nonnull)getTextAndStylesFromHtml:(NSString *_Nonnull)fixedHtml {
742747
NSString *url =
743748
[params substringWithRange:NSMakeRange(hrefRange.location + 6,
744749
hrefRange.length - 7)];
745-
NSString *text = [plainText substringWithRange:tagRangeValue.rangeValue];
750+
751+
// tagRange location includes one extra offset per preceding image
752+
// placeholder, which don't exist in plainText. Subtract them to map
753+
// back to the correct plainText index.
754+
NSRange adjustedRange = tagRangeValue.rangeValue;
755+
NSRange plainTextRange = NSMakeRange(
756+
adjustedRange.location - secondPassImageCount, adjustedRange.length);
757+
NSString *text = [plainText substringWithRange:plainTextRange];
746758

747759
LinkData *linkData = [[LinkData alloc] init];
748760
linkData.url = url;

0 commit comments

Comments
 (0)