Skip to content

Commit 318937c

Browse files
committed
fix: fix inner tags closing logic
1 parent a9c7d6a commit 318937c

1 file changed

Lines changed: 40 additions & 39 deletions

File tree

ios/inputParser/InputParser.mm

Lines changed: 40 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -171,10 +171,11 @@ - (NSString *)parseToHtmlFromRange:(NSRange)range {
171171

172172
for(NSNumber *activeStyle in currentActiveStyles) {
173173
NSInteger activeStyleBeginning = [currentActiveStylesBeginning[activeStyle] integerValue];
174-
// we end the styles that began after the currently ended style
175-
// also the ones that ended in the exact same place but are "inner" in relation to them due to StyleTypeEnum integer values
176-
// "activeStylesBeginning < i" is needed, so that we don't remove styles that have been freshly added now
177-
if((activeStyleBeginning > styleBeginning) ||
174+
175+
// we end the styles that began after the currently ended style but not at the "i" (cause the old style ended at exactly "i-1"
176+
// also the ones that began in the exact same place but are "inner" in relation to them due to StyleTypeEnum integer values
177+
178+
if((activeStyleBeginning > styleBeginning && activeStyleBeginning < i) ||
178179
(activeStyleBeginning == styleBeginning && activeStyleBeginning < i && [activeStyle integerValue] > [style integerValue])) {
179180
[fixedEndedStyles addObject:activeStyle];
180181
[stylesToBeReAdded addObject:activeStyle];
@@ -409,12 +410,12 @@ - (NSString * _Nullable)initiallyProcessHtml:(NSString * _Nonnull)html {
409410
if([firstSix isEqualToString:@"<html>"] && [lastSeven isEqualToString:@"</html>"]) {
410411
// remove html tags, might be with newlines or without them
411412
fixedHtml = [html copy];
412-
// firstly remove newlined html tags if any:
413-
fixedHtml = [fixedHtml stringByReplacingOccurrencesOfString:@"<html>\n" withString:@""];
414-
fixedHtml = [fixedHtml stringByReplacingOccurrencesOfString:@"\n</html>" withString:@""];
415-
// fallback; remove html tags without their newlines
416-
fixedHtml = [fixedHtml stringByReplacingOccurrencesOfString:@"<html>" withString:@""];
417-
fixedHtml = [fixedHtml stringByReplacingOccurrencesOfString:@"</html>" withString:@""];
413+
// firstly remove newlined html tags if any:
414+
fixedHtml = [fixedHtml stringByReplacingOccurrencesOfString:@"<html>\n" withString:@""];
415+
fixedHtml = [fixedHtml stringByReplacingOccurrencesOfString:@"\n</html>" withString:@""];
416+
// fallback; remove html tags without their newlines
417+
fixedHtml = [fixedHtml stringByReplacingOccurrencesOfString:@"<html>" withString:@""];
418+
fixedHtml = [fixedHtml stringByReplacingOccurrencesOfString:@"</html>" withString:@""];
418419
} else {
419420
// in other case we are most likely working with some external html - try getting the styles from between body tags
420421
NSRange openingBodyRange = [html rangeOfString:@"<body>"];
@@ -430,35 +431,35 @@ - (NSString * _Nullable)initiallyProcessHtml:(NSString * _Nonnull)html {
430431

431432
// second processing - try fixing htmls with wrong newlines' setup
432433
if(fixedHtml != nullptr) {
433-
// add <br> tag wherever needed
434-
fixedHtml = [fixedHtml stringByReplacingOccurrencesOfString:@"<p></p>" withString:@"<br>"];
435-
436-
// remove <p> tags inside of <li>
437-
fixedHtml = [fixedHtml stringByReplacingOccurrencesOfString:@"<li><p>" withString:@"<li>"];
438-
fixedHtml = [fixedHtml stringByReplacingOccurrencesOfString:@"</p></li>" withString:@"</li>"];
439-
440-
// tags that have to be in separate lines
441-
fixedHtml = [self stringByAddingNewlinesToTag:@"<br>" inString:fixedHtml leading:YES trailing:YES];
442-
fixedHtml = [self stringByAddingNewlinesToTag:@"<ul>" inString:fixedHtml leading:YES trailing:YES];
443-
fixedHtml = [self stringByAddingNewlinesToTag:@"</ul>" inString:fixedHtml leading:YES trailing:YES];
444-
fixedHtml = [self stringByAddingNewlinesToTag:@"<ol>" inString:fixedHtml leading:YES trailing:YES];
445-
fixedHtml = [self stringByAddingNewlinesToTag:@"</ol>" inString:fixedHtml leading:YES trailing:YES];
446-
fixedHtml = [self stringByAddingNewlinesToTag:@"<blockquote>" inString:fixedHtml leading:YES trailing:YES];
447-
fixedHtml = [self stringByAddingNewlinesToTag:@"</blockquote>" inString:fixedHtml leading:YES trailing:YES];
448-
449-
// line opening tags
450-
fixedHtml = [self stringByAddingNewlinesToTag:@"<p>" inString:fixedHtml leading:YES trailing:NO];
451-
fixedHtml = [self stringByAddingNewlinesToTag:@"<li>" inString:fixedHtml leading:YES trailing:NO];
452-
fixedHtml = [self stringByAddingNewlinesToTag:@"<h1>" inString:fixedHtml leading:YES trailing:NO];
453-
fixedHtml = [self stringByAddingNewlinesToTag:@"<h2>" inString:fixedHtml leading:YES trailing:NO];
454-
fixedHtml = [self stringByAddingNewlinesToTag:@"<h3>" inString:fixedHtml leading:YES trailing:NO];
455-
456-
// line closing tags
457-
fixedHtml = [self stringByAddingNewlinesToTag:@"</p>" inString:fixedHtml leading:NO trailing:YES];
458-
fixedHtml = [self stringByAddingNewlinesToTag:@"</li>" inString:fixedHtml leading:NO trailing:YES];
459-
fixedHtml = [self stringByAddingNewlinesToTag:@"</h1>" inString:fixedHtml leading:NO trailing:YES];
460-
fixedHtml = [self stringByAddingNewlinesToTag:@"</h2>" inString:fixedHtml leading:NO trailing:YES];
461-
fixedHtml = [self stringByAddingNewlinesToTag:@"</h3>" inString:fixedHtml leading:NO trailing:YES];
434+
// add <br> tag wherever needed
435+
fixedHtml = [fixedHtml stringByReplacingOccurrencesOfString:@"<p></p>" withString:@"<br>"];
436+
437+
// remove <p> tags inside of <li>
438+
fixedHtml = [fixedHtml stringByReplacingOccurrencesOfString:@"<li><p>" withString:@"<li>"];
439+
fixedHtml = [fixedHtml stringByReplacingOccurrencesOfString:@"</p></li>" withString:@"</li>"];
440+
441+
// tags that have to be in separate lines
442+
fixedHtml = [self stringByAddingNewlinesToTag:@"<br>" inString:fixedHtml leading:YES trailing:YES];
443+
fixedHtml = [self stringByAddingNewlinesToTag:@"<ul>" inString:fixedHtml leading:YES trailing:YES];
444+
fixedHtml = [self stringByAddingNewlinesToTag:@"</ul>" inString:fixedHtml leading:YES trailing:YES];
445+
fixedHtml = [self stringByAddingNewlinesToTag:@"<ol>" inString:fixedHtml leading:YES trailing:YES];
446+
fixedHtml = [self stringByAddingNewlinesToTag:@"</ol>" inString:fixedHtml leading:YES trailing:YES];
447+
fixedHtml = [self stringByAddingNewlinesToTag:@"<blockquote>" inString:fixedHtml leading:YES trailing:YES];
448+
fixedHtml = [self stringByAddingNewlinesToTag:@"</blockquote>" inString:fixedHtml leading:YES trailing:YES];
449+
450+
// line opening tags
451+
fixedHtml = [self stringByAddingNewlinesToTag:@"<p>" inString:fixedHtml leading:YES trailing:NO];
452+
fixedHtml = [self stringByAddingNewlinesToTag:@"<li>" inString:fixedHtml leading:YES trailing:NO];
453+
fixedHtml = [self stringByAddingNewlinesToTag:@"<h1>" inString:fixedHtml leading:YES trailing:NO];
454+
fixedHtml = [self stringByAddingNewlinesToTag:@"<h2>" inString:fixedHtml leading:YES trailing:NO];
455+
fixedHtml = [self stringByAddingNewlinesToTag:@"<h3>" inString:fixedHtml leading:YES trailing:NO];
456+
457+
// line closing tags
458+
fixedHtml = [self stringByAddingNewlinesToTag:@"</p>" inString:fixedHtml leading:NO trailing:YES];
459+
fixedHtml = [self stringByAddingNewlinesToTag:@"</li>" inString:fixedHtml leading:NO trailing:YES];
460+
fixedHtml = [self stringByAddingNewlinesToTag:@"</h1>" inString:fixedHtml leading:NO trailing:YES];
461+
fixedHtml = [self stringByAddingNewlinesToTag:@"</h2>" inString:fixedHtml leading:NO trailing:YES];
462+
fixedHtml = [self stringByAddingNewlinesToTag:@"</h3>" inString:fixedHtml leading:NO trailing:YES];
462463
}
463464

464465
return fixedHtml;

0 commit comments

Comments
 (0)