From 6a85ff5e539f10d92edbde5ce374c1f21029ec37 Mon Sep 17 00:00:00 2001 From: Tomek Zawadzki Date: Thu, 5 Dec 2024 17:02:12 +0100 Subject: [PATCH] Move skipping invalid ranges to parser on iOS (#561) --- apple/MarkdownParser.mm | 4 ++++ apple/RCTMarkdownUtils.mm | 4 ---- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/apple/MarkdownParser.mm b/apple/MarkdownParser.mm index c9f3d018..739f106d 100644 --- a/apple/MarkdownParser.mm +++ b/apple/MarkdownParser.mm @@ -46,6 +46,10 @@ @implementation MarkdownParser { const auto &length = static_cast(item.getProperty(rt, "length").asNumber()); const auto &depth = item.hasProperty(rt, "depth") ? static_cast(item.getProperty(rt, "depth").asNumber()) : 1; + if (length == 0 || start + length > text.length) { + continue; + } + NSRange range = NSMakeRange(start, length); MarkdownRange *markdownRange = [[MarkdownRange alloc] initWithType:@(type.c_str()) range:range depth:depth]; [markdownRanges addObject:markdownRange]; diff --git a/apple/RCTMarkdownUtils.mm b/apple/RCTMarkdownUtils.mm index 11a20bca..57d90774 100644 --- a/apple/RCTMarkdownUtils.mm +++ b/apple/RCTMarkdownUtils.mm @@ -70,10 +70,6 @@ - (NSAttributedString *)parseMarkdown:(nullable NSAttributedString *)input withA } - (void)applyRangeToAttributedString:(NSMutableAttributedString *)attributedString type:(const std::string)type range:(NSRange)range depth:(const int)depth { - if (range.length == 0 || range.location + range.length > attributedString.length) { - return; - } - if (type == "bold" || type == "italic" || type == "code" || type == "pre" || type == "h1" || type == "emoji") { UIFont *font = [attributedString attribute:NSFontAttributeName atIndex:range.location effectiveRange:NULL]; if (type == "bold") {