diff --git a/Components/Sources/ComponentsObjC/WKSourceEditorFormatterLink.h b/Components/Sources/ComponentsObjC/WKSourceEditorFormatterLink.h index 0c51bf39747..321d69279e5 100644 --- a/Components/Sources/ComponentsObjC/WKSourceEditorFormatterLink.h +++ b/Components/Sources/ComponentsObjC/WKSourceEditorFormatterLink.h @@ -3,7 +3,8 @@ NS_ASSUME_NONNULL_BEGIN @interface WKSourceEditorFormatterLink : WKSourceEditorFormatter - +- (BOOL)attributedString:(NSMutableAttributedString *)attributedString isSimpleLinkInRange:(NSRange)range; +- (BOOL)attributedString:(NSMutableAttributedString *)attributedString isLinkWithNestedLinkInRange:(NSRange)range; @end NS_ASSUME_NONNULL_END diff --git a/Components/Sources/ComponentsObjC/WKSourceEditorFormatterLink.m b/Components/Sources/ComponentsObjC/WKSourceEditorFormatterLink.m index 1d3bd1382bc..700a64a8460 100644 --- a/Components/Sources/ComponentsObjC/WKSourceEditorFormatterLink.m +++ b/Components/Sources/ComponentsObjC/WKSourceEditorFormatterLink.m @@ -137,6 +137,84 @@ - (void)updateFonts:(WKSourceEditorFonts *)fonts inAttributedString:(NSMutableAt // No special font handling needed } +#pragma mark - Public + +- (BOOL)attributedString:(NSMutableAttributedString *)attributedString isSimpleLinkInRange:(NSRange)range { + __block BOOL isContentKey = NO; + if (range.length == 0) { + + if (attributedString.length > range.location) { + NSDictionary *attrs = [attributedString attributesAtIndex:range.location effectiveRange:nil]; + + if (attrs[WKSourceEditorCustomKeyContentLink] != nil) { + isContentKey = YES; + } else { + // Edge case, check previous character if we are up against closing markup + if (attrs[WKSourceEditorCustomKeyMarkupLink]) { + attrs = [attributedString attributesAtIndex:range.location - 1 effectiveRange:nil]; + if (attrs[WKSourceEditorCustomKeyContentLink] != nil) { + isContentKey = YES; + } + } + } + } + + } else { + __block NSRange unionRange = NSMakeRange(NSNotFound, 0); + [attributedString enumerateAttributesInRange:range options:nil usingBlock:^(NSDictionary * _Nonnull attrs, NSRange loopRange, BOOL * _Nonnull stop) { + if (attrs[WKSourceEditorCustomKeyContentLink] != nil) { + if (unionRange.location == NSNotFound) { + unionRange = loopRange; + } else { + unionRange = NSUnionRange(unionRange, loopRange); + } + stop = YES; + } + }]; + + if (NSEqualRanges(unionRange, range)) { + isContentKey = YES; + } + } + + return isContentKey; +} + +- (BOOL)attributedString:(NSMutableAttributedString *)attributedString isLinkWithNestedLinkInRange:(NSRange)range { + + __block BOOL isKey = NO; + if (range.length == 0) { + + if (attributedString.length > range.location) { + NSDictionary *attrs = [attributedString attributesAtIndex:range.location effectiveRange:nil]; + + if (attrs[WKSourceEditorCustomKeyMarkupAndContentLinkWithNestedLink] != nil) { + isKey = YES; + } + } + + } else { + __block NSRange unionRange = NSMakeRange(NSNotFound, 0); + [attributedString enumerateAttributesInRange:range options:nil usingBlock:^(NSDictionary * _Nonnull attrs, NSRange loopRange, BOOL * _Nonnull stop) { + if (attrs[WKSourceEditorCustomKeyMarkupAndContentLinkWithNestedLink] != nil) { + if (unionRange.location == NSNotFound) { + unionRange = loopRange; + } else { + unionRange = NSUnionRange(unionRange, loopRange); + } + stop = YES; + } + }]; + + if (NSEqualRanges(unionRange, range)) { + isKey = YES; + } + } + + return isKey; + +} + #pragma mark - Private - (NSArray *)linkWithNestedLinkRangesInString: (NSString *)string startingIndex: (NSUInteger)index {