Skip to content

Commit 521e7d6

Browse files
fix(iOS): inline styles parsing (#617)
# Summary We introduced a parser regression after merging #403, causing the `inline_styles_display.yaml` test to fail. When multiple inline styles are applied to the same word, the styles incorrectly extend onto the surrounding text <img width="1266" height="424" alt="image" src="https://github.com/user-attachments/assets/a50ccadf-1421-4215-b42c-1f7dd76980a0" /> Also fixes applying alignment inside input from parsed html ## Test Plan `inline_styles_display.yaml` test passes. Parsing works properly when alignment set ## Screenshots / Videos n/a ## Compatibility | OS | Implemented | | ------- | :---------: | | iOS | ✅ | | Android | ❌ | ## Checklist - [X] E2E tests are passing - [ ] Required E2E tests have been added (if applicable)
1 parent ae47f3c commit 521e7d6

3 files changed

Lines changed: 41 additions & 38 deletions

File tree

69 Bytes
Loading

ios/htmlParser/HtmlParser.mm

Lines changed: 38 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,9 @@ + (void)finalizeTagEntry:(NSMutableString *)tagName
155155
NSMutableArray *tagEntry = [[NSMutableArray alloc] init];
156156

157157
NSArray *tagData = ongoingTags[tagName];
158+
if (tagData == nil) {
159+
return;
160+
}
158161
NSInteger tagLocation = [((NSNumber *)tagData[0]) intValue];
159162
NSInteger openImageCount = [((NSNumber *)tagData[1]) intValue];
160163
NSInteger currentImageCount = *precedingImageCount;
@@ -495,46 +498,44 @@ + (NSArray *_Nonnull)getTextAndStylesFromHtml:(NSString *_Nonnull)fixedHtml {
495498
checkboxStates[@(plainText.length)] = @(isChecked);
496499
}
497500
} else if (!closingTag) {
498-
BOOL isPlainParagraph =
499-
[currentTagName isEqualToString:@"p"] &&
500-
(!currentTagParams || [currentTagParams length] == 0);
501-
502-
if (isPlainParagraph) {
503-
continue;
504-
}
505-
// we finish opening tag - get its location, the current
506-
// precedingImageCount and optionally params and put them under tag name
507-
// key in ongoingTags. Storing the open-time image count lets
508-
// finalizeTagEntry: correctly shift the start and extend the length
509-
// so the range covers any images finalized between open and close.
510-
NSMutableArray *tagArr = [[NSMutableArray alloc] init];
511-
[tagArr addObject:[NSNumber numberWithInteger:plainText.length]];
512-
[tagArr addObject:[NSNumber numberWithInteger:precedingImageCount]];
513-
if (currentTagParams.length > 0) {
514-
[tagArr addObject:[currentTagParams copy]];
515-
}
516-
ongoingTags[currentTagName] = tagArr;
501+
BOOL isPlainParagraph = [currentTagName isEqualToString:@"p"] &&
502+
currentTagParams.length == 0;
503+
504+
if (!isPlainParagraph) {
505+
// we finish opening tag - get its location, the current
506+
// precedingImageCount and optionally params and put them under tag
507+
// name key in ongoingTags. Storing the open-time image count lets
508+
// finalizeTagEntry: correctly shift the start and extend the length
509+
// so the range covers any images finalized between open and close.
510+
NSMutableArray *tagArr = [[NSMutableArray alloc] init];
511+
[tagArr addObject:[NSNumber numberWithInteger:plainText.length]];
512+
[tagArr addObject:[NSNumber numberWithInteger:precedingImageCount]];
513+
if (currentTagParams.length > 0) {
514+
[tagArr addObject:[currentTagParams copy]];
515+
}
516+
ongoingTags[currentTagName] = tagArr;
517517

518-
// Check if this is a checkbox list
519-
if ([currentTagName isEqualToString:@"ul"] &&
520-
[self isUlCheckboxList:currentTagParams]) {
521-
insideCheckboxList = YES;
522-
}
518+
// Check if this is a checkbox list
519+
if ([currentTagName isEqualToString:@"ul"] &&
520+
[self isUlCheckboxList:currentTagParams]) {
521+
insideCheckboxList = YES;
522+
}
523523

524-
// skip one newline if it was added after opening tags that are in
525-
// separate lines
526-
if ([self isBlockTag:currentTagName] && i + 1 < fixedHtml.length &&
527-
[[NSCharacterSet newlineCharacterSet]
528-
characterIsMember:[fixedHtml characterAtIndex:i + 1]]) {
529-
i += 1;
530-
}
524+
// skip one newline if it was added after opening tags that are in
525+
// separate lines
526+
if ([self isBlockTag:currentTagName] && i + 1 < fixedHtml.length &&
527+
[[NSCharacterSet newlineCharacterSet]
528+
characterIsMember:[fixedHtml characterAtIndex:i + 1]]) {
529+
i += 1;
530+
}
531531

532-
if (isSelfClosing) {
533-
[self finalizeTagEntry:currentTagName
534-
ongoingTags:ongoingTags
535-
initiallyProcessedTags:initiallyProcessedTags
536-
plainText:plainText
537-
precedingImageCount:&precedingImageCount];
532+
if (isSelfClosing) {
533+
[self finalizeTagEntry:currentTagName
534+
ongoingTags:ongoingTags
535+
initiallyProcessedTags:initiallyProcessedTags
536+
plainText:plainText
537+
precedingImageCount:&precedingImageCount];
538+
}
538539
}
539540
} else {
540541
// we finish closing tags - pack tag name, tag range and optionally tag

ios/inputHtmlParser/InputHtmlParser.mm

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ - (void)replaceWholeFromHtml:(NSString *_Nonnull)html {
3737
offsetFromBeginning:0
3838
plainTextLength:plainText.length];
3939
[self applyProcessedAlignments:alignments offset:0];
40+
[_input anyTextMayHaveBeenModified];
4041
} @catch (NSException *exception) {
4142
RCTLogWarn(@"[EnrichedTextInput]: Failed to parse HTML: (%@), falling back "
4243
@"to raw input.",
@@ -65,6 +66,7 @@ - (void)replaceFromHtml:(NSString *_Nonnull)html range:(NSRange)range {
6566
offsetFromBeginning:range.location
6667
plainTextLength:plainText.length];
6768
[self applyProcessedAlignments:alignments offset:range.location];
69+
[_input anyTextMayHaveBeenModified];
6870
} @catch (NSException *exception) {
6971
RCTLogWarn(@"[EnrichedTextInput]: Failed to parse HTML: (%@), falling back "
7072
@"to raw input.",
@@ -95,6 +97,7 @@ - (void)insertFromHtml:(NSString *_Nonnull)html location:(NSInteger)location {
9597
offsetFromBeginning:location
9698
plainTextLength:plainText.length];
9799
[self applyProcessedAlignments:alignments offset:location];
100+
[_input anyTextMayHaveBeenModified];
98101
} @catch (NSException *exception) {
99102
RCTLogWarn(@"[EnrichedTextInput]: Failed to parse HTML: (%@), falling back "
100103
@"to raw input.",
@@ -191,7 +194,6 @@ - (void)applyProcessedStyles:(NSArray *)processedStyles
191194
zeroWidthSpaceOffset += delta;
192195
}
193196
}
194-
[_input anyTextMayHaveBeenModified];
195197
}
196198

197199
- (void)applyProcessedAlignments:(NSArray<AlignmentEntry *> *)alignments

0 commit comments

Comments
 (0)