Skip to content

Commit 40c80af

Browse files
authored
fix(iOS): body tag handling crash (#480)
# Summary Fixes #479. We were handling htmls with `body` tags only if we had no `html` tags or didn't use `htmlNormalizer`. Now it is always being handled after either of these steps, which fixes the crash. Also improved the way it handles newlines around `body` tags, because it previously just always assumed they are there. ## Test Plan Try pasting the following HTML via example app's `insert html` and see that it properly works with and without `useHtmlNormalizer`: ```html <html> <head> <meta charset="UTF-8"> </head> <body> <p class="p1">Some text</p> </body> </html> ``` ## Screenshots / Videos - ## Compatibility | OS | Implemented | | ------- | :---------: | | iOS | ✅ | | Android | ❌ |
1 parent acc0fe2 commit 40c80af

1 file changed

Lines changed: 18 additions & 12 deletions

File tree

ios/inputParser/InputParser.mm

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -772,18 +772,24 @@ - (NSString *_Nullable)initiallyProcessHtml:(NSString *_Nonnull)html {
772772
if (normalized != nil) {
773773
fixedHtml = normalized;
774774
}
775-
} else {
776-
// in other case we are most likely working with some external html - try
777-
// getting the styles from between body tags
778-
NSRange openingBodyRange = [htmlWithoutSpaces rangeOfString:@"<body>"];
779-
NSRange closingBodyRange = [htmlWithoutSpaces rangeOfString:@"</body>"];
780-
781-
if (openingBodyRange.length != 0 && closingBodyRange.length != 0) {
782-
NSInteger newStart = openingBodyRange.location + 7;
783-
NSInteger newEnd = closingBodyRange.location - 1;
784-
fixedHtml = [htmlWithoutSpaces
785-
substringWithRange:NSMakeRange(newStart, newEnd - newStart + 1)];
786-
}
775+
}
776+
777+
// Additionally, try getting the content from between body tags if there are
778+
// some:
779+
780+
// Firstly make sure there are no newlines between them.
781+
fixedHtml = [fixedHtml stringByReplacingOccurrencesOfString:@"<body>\n"
782+
withString:@"<body>"];
783+
fixedHtml = [fixedHtml stringByReplacingOccurrencesOfString:@"\n</body>"
784+
withString:@"</body>"];
785+
// Then, if there actually are body tags, use the content between them.
786+
NSRange openingBodyRange = [htmlWithoutSpaces rangeOfString:@"<body>"];
787+
NSRange closingBodyRange = [htmlWithoutSpaces rangeOfString:@"</body>"];
788+
if (openingBodyRange.length != 0 && closingBodyRange.length != 0) {
789+
NSInteger newStart = openingBodyRange.location + 6;
790+
NSInteger newEnd = closingBodyRange.location - 1;
791+
fixedHtml = [htmlWithoutSpaces
792+
substringWithRange:NSMakeRange(newStart, newEnd - newStart + 1)];
787793
}
788794
}
789795

0 commit comments

Comments
 (0)