Skip to content

Commit 6bf0b4b

Browse files
authored
iOS parser format fixes (#147)
1 parent 3df6590 commit 6bf0b4b

File tree

1 file changed

+42
-52
lines changed

1 file changed

+42
-52
lines changed

ios/inputParser/InputParser.mm

Lines changed: 42 additions & 52 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];
@@ -405,24 +406,16 @@ - (NSString * _Nullable)initiallyProcessHtml:(NSString * _Nonnull)html {
405406
if(html.length >= 13) {
406407
NSString *firstSix = [html substringWithRange:NSMakeRange(0, 6)];
407408
NSString *lastSeven = [html substringWithRange:NSMakeRange(html.length-7, 7)];
408-
NSInteger newlinesCount = [[html componentsSeparatedByString:@"\n"] count] - 1;
409409

410410
if([firstSix isEqualToString:@"<html>"] && [lastSeven isEqualToString:@"</html>"]) {
411-
if(newlinesCount >= 2) {
412-
// looks like our format
413-
// we want to get the string without <html> and </html> and their newlines
414-
// so we skip first 7 characters and get the string 7+8 = 15 characters shorter
415-
fixedHtml = [html substringWithRange: NSMakeRange(7, html.length - 15)];
416-
} else {
417-
// most likely a valid html but with some newline differences
418-
fixedHtml = [html copy];
419-
// firstly remove newlined html tags if any:
420-
fixedHtml = [fixedHtml stringByReplacingOccurrencesOfString:@"<html>\n" withString:@""];
421-
fixedHtml = [fixedHtml stringByReplacingOccurrencesOfString:@"\n</html>" withString:@""];
422-
// fallback; remove html tags without their newlines
423-
fixedHtml = [fixedHtml stringByReplacingOccurrencesOfString:@"<html>" withString:@""];
424-
fixedHtml = [fixedHtml stringByReplacingOccurrencesOfString:@"</html>" withString:@""];
425-
}
411+
// remove html tags, might be with newlines or without them
412+
fixedHtml = [html copy];
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:@""];
426419
} else {
427420
// in other case we are most likely working with some external html - try getting the styles from between body tags
428421
NSRange openingBodyRange = [html rangeOfString:@"<body>"];
@@ -438,38 +431,35 @@ - (NSString * _Nullable)initiallyProcessHtml:(NSString * _Nonnull)html {
438431

439432
// second processing - try fixing htmls with wrong newlines' setup
440433
if(fixedHtml != nullptr) {
441-
NSInteger newlinesCount = [[fixedHtml componentsSeparatedByString:@"/n"] count] - 1;
442-
if(newlinesCount == 0) {
443-
// add <br> tag wherever needed
444-
fixedHtml = [fixedHtml stringByReplacingOccurrencesOfString:@"<p></p>" withString:@"<br>"];
445-
446-
// remove <p> tags inside of <li>
447-
fixedHtml = [fixedHtml stringByReplacingOccurrencesOfString:@"<li><p>" withString:@"<li>"];
448-
fixedHtml = [fixedHtml stringByReplacingOccurrencesOfString:@"</p></li>" withString:@"</li>"];
449-
450-
// tags that have to be in separate lines
451-
fixedHtml = [self stringByAddingNewlinesToTag:@"<br>" inString:fixedHtml leading:YES trailing:YES];
452-
fixedHtml = [self stringByAddingNewlinesToTag:@"<ul>" inString:fixedHtml leading:YES trailing:YES];
453-
fixedHtml = [self stringByAddingNewlinesToTag:@"</ul>" inString:fixedHtml leading:YES trailing:YES];
454-
fixedHtml = [self stringByAddingNewlinesToTag:@"<ol>" inString:fixedHtml leading:YES trailing:YES];
455-
fixedHtml = [self stringByAddingNewlinesToTag:@"</ol>" inString:fixedHtml leading:YES trailing:YES];
456-
fixedHtml = [self stringByAddingNewlinesToTag:@"<blockquote>" inString:fixedHtml leading:YES trailing:YES];
457-
fixedHtml = [self stringByAddingNewlinesToTag:@"</blockquote>" inString:fixedHtml leading:YES trailing:YES];
458-
459-
// line opening tags
460-
fixedHtml = [self stringByAddingNewlinesToTag:@"<p>" inString:fixedHtml leading:YES trailing:NO];
461-
fixedHtml = [self stringByAddingNewlinesToTag:@"<li>" inString:fixedHtml leading:YES trailing:NO];
462-
fixedHtml = [self stringByAddingNewlinesToTag:@"<h1>" inString:fixedHtml leading:YES trailing:NO];
463-
fixedHtml = [self stringByAddingNewlinesToTag:@"<h2>" inString:fixedHtml leading:YES trailing:NO];
464-
fixedHtml = [self stringByAddingNewlinesToTag:@"<h3>" inString:fixedHtml leading:YES trailing:NO];
465-
466-
// line closing tags
467-
fixedHtml = [self stringByAddingNewlinesToTag:@"</p>" inString:fixedHtml leading:NO trailing:YES];
468-
fixedHtml = [self stringByAddingNewlinesToTag:@"</li>" inString:fixedHtml leading:NO trailing:YES];
469-
fixedHtml = [self stringByAddingNewlinesToTag:@"</h1>" inString:fixedHtml leading:NO trailing:YES];
470-
fixedHtml = [self stringByAddingNewlinesToTag:@"</h2>" inString:fixedHtml leading:NO trailing:YES];
471-
fixedHtml = [self stringByAddingNewlinesToTag:@"</h3>" inString:fixedHtml leading:NO trailing:YES];
472-
}
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];
473463
}
474464

475465
return fixedHtml;

0 commit comments

Comments
 (0)