@@ -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