Skip to content
This repository has been archived by the owner on Dec 12, 2022. It is now read-only.

BF: matrix.to links containing room ids are not hyperlinked #228

Merged
merged 1 commit into from
Jan 18, 2017
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions MatrixKit/Utils/MXKEventFormatter.m
Original file line number Diff line number Diff line change
Expand Up @@ -1195,6 +1195,8 @@ - (NSAttributedString*)postRenderAttributedString:(NSAttributedString*)attribute

- (void)createLinksInAttributedString:(NSAttributedString*)attributedString matchingRegex:(NSRegularExpression*)regex withWorkingAttributedString:(NSMutableAttributedString**)mutableAttributedString
{
__block NSArray *linkMatches;

// Enumerate each string matching the regex
[regex enumerateMatchesInString:attributedString.string options:0 range:NSMakeRange(0, attributedString.length) usingBlock:^(NSTextCheckingResult *match, NSMatchingFlags flags, BOOL *stop) {

Expand All @@ -1209,6 +1211,29 @@ - (void)createLinksInAttributedString:(NSAttributedString*)attributedString matc
}
}];

// Do not create a link if the match is part of an http link.
// The http link will be automatically generated by the UI afterwards.
// So, do not break it now by adding a link on a subset of this http link.
if (!hasAlreadyLink)
{
if (!linkMatches)
{
// Search for the links in the string only once
NSDataDetector *detector = [NSDataDetector dataDetectorWithTypes:NSTextCheckingTypeLink error:nil];
linkMatches = [detector matchesInString:attributedString.string options:0 range:NSMakeRange(0, attributedString.length)];
}

for (NSTextCheckingResult *linkMatch in linkMatches)
{
// If the match is fully in the link, skip it
if (NSIntersectionRange(match.range, linkMatch.range).length == match.range.length)
{
hasAlreadyLink = YES;
break;
}
}
}

if (!hasAlreadyLink)
{
// Create the output string only if it is necessary because attributed strings cost CPU
Expand Down