Skip to content

Commit 67c9cc0

Browse files
authored
fix: iOS nested tags (#274)
<!-- 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 Turns out nested tags logic isn't perfect on iOS yet. Adding a styling like this: <img width="382" height="284" alt="image" src="https://github.com/user-attachments/assets/04b96797-43d4-42c4-8ef0-f4ba98fa5f86" /> (bold and strikethrough, then only strikethrough, then again both of them) Results in an invalid html: ``` <html> <p><b><s>Both</s></b><s>onlystrike<b>both</s></b></p> </html> ``` The `s` tag should have been closed before another `b` tag and opened again after it. This PR fixes that situation, making sure all tags that were active index ago and are still active on the index under consideration, if inner to a newly opened tag, will be closed and re-opened. ## Test Plan Try re-creating the text described in the Summary. See that it produces a valid html now: ``` <html> <p><b><s>Both</s></b><s>onlystrike</s><b><s>both</s></b></p> </html> ``` ## Screenshots / Videos -- ## Compatibility | OS | Implemented | | ------- | :---------: | | iOS | ✅ | | Android | doesn't apply |
1 parent 0db5cfe commit 67c9cc0

File tree

1 file changed

+21
-3
lines changed

1 file changed

+21
-3
lines changed

ios/inputParser/InputParser.mm

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -184,6 +184,25 @@ - (NSString *)parseToHtmlFromRange:(NSRange)range {
184184
}
185185
}
186186

187+
// if a style begins but there is a style inner to it that is (and was previously) active, it also should be closed and readded
188+
189+
// newly added styles
190+
NSMutableSet *newStyles = [currentActiveStyles mutableCopy];
191+
[newStyles minusSet: previousActiveStyles];
192+
// styles that were and still are active
193+
NSMutableSet *stillActiveStyles = [previousActiveStyles mutableCopy];
194+
[stillActiveStyles intersectSet:currentActiveStyles];
195+
196+
for(NSNumber *style in newStyles) {
197+
for(NSNumber *ongoingStyle in stillActiveStyles) {
198+
if([ongoingStyle integerValue] > [style integerValue]) {
199+
// the prev style is inner; needs to be closed and re-added later
200+
[fixedEndedStyles addObject:ongoingStyle];
201+
[stylesToBeReAdded addObject:ongoingStyle];
202+
}
203+
}
204+
}
205+
187206
// they are sorted in a descending order
188207
NSArray<NSNumber*> *sortedEndedStyles = [fixedEndedStyles sortedArrayUsingDescriptors:@[[NSSortDescriptor sortDescriptorWithKey:@"intValue" ascending:NO]]];
189208

@@ -193,9 +212,8 @@ - (NSString *)parseToHtmlFromRange:(NSRange)range {
193212
[result appendString: [NSString stringWithFormat:@"</%@>", tagContent]];
194213
}
195214

196-
// get styles that have begun: they are sorted in a ascending manner to properly keep tags' FILO order
197-
NSMutableSet<NSNumber *> *newStyles = [currentActiveStyles mutableCopy];
198-
[newStyles minusSet: previousActiveStyles];
215+
// all styles that have begun: new styles + the ones that need to be re-added
216+
// they are sorted in a ascending manner to properly keep tags' FILO order
199217
[newStyles unionSet: stylesToBeReAdded];
200218
NSArray<NSNumber*> *sortedNewStyles = [newStyles sortedArrayUsingDescriptors:@[[NSSortDescriptor sortDescriptorWithKey:@"intValue" ascending:YES]]];
201219

0 commit comments

Comments
 (0)