@@ -33,12 +33,19 @@ - (NSString *)parseToHtmlFromRange:(NSRange)range {
3333 for (int i = 0 ; i < text.length ; i++) {
3434 NSRange currentRange = NSMakeRange (offset + i, 1 );
3535 NSMutableSet <NSNumber *>*currentActiveStyles = [[NSMutableSet <NSNumber *> alloc]init];
36+ NSMutableDictionary *currentActiveStylesBeginning = [[NSMutableDictionary alloc ] init ];
3637
3738 // check each existing style existence
3839 for (NSNumber * type in _editor->stylesDict ) {
3940 id <BaseStyleProtocol> style = _editor->stylesDict [type];
4041 if ([style detectStyle: currentRange]) {
41- [currentActiveStyles addObject: type];
42+ [currentActiveStyles addObject: type];
43+
44+ if (![previousActiveStyles member: type]) {
45+ currentActiveStylesBeginning[type] = [NSNumber numberWithInt: i];
46+ }
47+ } else if ([previousActiveStyles member: type]) {
48+ [currentActiveStylesBeginning removeObjectForKey: type];
4249 }
4350 }
4451
@@ -138,20 +145,43 @@ - (NSString *)parseToHtmlFromRange:(NSRange)range {
138145 }
139146 }
140147
141- // get styles that have ended: they are sorted in an ascending manner
148+ // get styles that have ended
142149 NSMutableSet <NSNumber *> *endedStyles = [previousActiveStyles mutableCopy ];
143150 [endedStyles minusSet: currentActiveStyles];
144- NSArray <NSNumber *> *sortedEndedStyles = [endedStyles sortedArrayUsingDescriptors: @[[NSSortDescriptor sortDescriptorWithKey:@"intValue" ascending:NO]] ];
151+
152+ // also finish styles that should be ended becasue they are nested in a style that ended
153+ NSMutableSet *fixedEndedStyles = [endedStyles copy ];
154+ NSMutableSet *stylesToBeReAdded = [[NSMutableSet alloc ] init ];
155+
156+ for (NSNumber *style in endedStyles) {
157+ NSInteger styleBeginning = [currentActiveStylesBeginning[style] integerValue ];
158+
159+ for (NSNumber *activeStyle in currentActiveStyles) {
160+ NSInteger activeStyleBeginning = [currentActiveStylesBeginning[activeStyle] integerValue ];
161+ // we end the styles that began after the currently ended style
162+ // also the ones that ended in the exact same place but are "inner" in relation to them due to StyleTypeEnum integer values
163+ // "activeStylesBeginning < i" is needed, so that we don't remove styles that have been freshly added now
164+ if ((activeStyleBeginning > styleBeginning) ||
165+ (activeStyleBeginning == styleBeginning && activeStyleBeginning < i && [activeStyle integerValue ] > [style integerValue ])) {
166+ [fixedEndedStyles addObject: activeStyle];
167+ [stylesToBeReAdded addObject: activeStyle];
168+ }
169+ }
170+ }
171+
172+ // they are sorted in a descending order
173+ NSArray <NSNumber *> *sortedEndedStyles = [fixedEndedStyles sortedArrayUsingDescriptors: @[[NSSortDescriptor sortDescriptorWithKey:@"intValue" ascending:NO]] ];
145174
146175 // append closing tags
147176 for (NSNumber *style in sortedEndedStyles) {
148177 NSString *tagContent = [self tagContentForStyle: style openingTag: NO location: currentRange.location];
149178 [result appendString: [NSString stringWithFormat: @" </%@ >" , tagContent]];
150179 }
151180
152- // get styles that have begun: they are sorted in a descending manner to properly keep tags' FILO order
181+ // get styles that have begun: they are sorted in a ascending manner to properly keep tags' FILO order
153182 NSMutableSet <NSNumber *> *newStyles = [currentActiveStyles mutableCopy ];
154183 [newStyles minusSet: previousActiveStyles];
184+ [newStyles unionSet: stylesToBeReAdded];
155185 NSArray <NSNumber *> *sortedNewStyles = [newStyles sortedArrayUsingDescriptors: @[[NSSortDescriptor sortDescriptorWithKey:@"intValue" ascending:YES]] ];
156186
157187 // append opening tags
0 commit comments