Skip to content

Commit 90363fd

Browse files
committed
fix: close nested ongoing tags if an outer tag is opened
1 parent 0db5cfe commit 90363fd

1 file changed

Lines changed: 21 additions & 3 deletions

File tree

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)