Skip to content

Commit

Permalink
Merge pull request #1059 from tchapgouv/1056-des-messages-en-reponses…
Browse files Browse the repository at this point in the history
…-a-dautres-messages-saffichent-mal

Amélioration de la troncature des réponses à messages
  • Loading branch information
NicolasBuquet authored Jun 13, 2024
2 parents 8e37e78 + 28acf2b commit 4e53cab
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 1 deletion.
24 changes: 23 additions & 1 deletion Riot/Modules/MatrixKit/Utils/EventFormatter/MXKEventFormatter.m
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,11 @@

static NSString *const kHTMLATagRegexPattern = @"<a href=(?:'|\")(.*?)(?:'|\")>([^<]*)</a>";
static NSString *const kRepliedTextPattern = @"<mx-reply>.*<blockquote>.*<br>(.*)</blockquote></mx-reply>";
// Tchap: modify regex to define reply pattern// The Tchap pattern takes in account:
// - a text can span on multiple lines -> (?s) modifier make regex '.' to match any char or newline char.
// - the pattern doesn't truncate any quoted user defined by <a> tag at the begining of the replied to text
// else, truncating in first quoted users (if they exists) breaks the tyext rendering.
static NSString *const kTchapRepliedTextPattern = @"(?s)<mx-reply>.*<blockquote>.*<br>(?:<a .*?/a>[: ]*)*(.*)</blockquote></mx-reply>";

@interface MXKEventFormatter ()
{
Expand Down Expand Up @@ -2051,7 +2056,7 @@ - (NSString *)tchapTruncatedQuotedReplyFrom:(NSString *)fullQuotedReply {

static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
htmlQuotedTextRegex = [NSRegularExpression regularExpressionWithPattern:kRepliedTextPattern
htmlQuotedTextRegex = [NSRegularExpression regularExpressionWithPattern:kTchapRepliedTextPattern
options:NSRegularExpressionCaseInsensitive
error:nil];
});
Expand All @@ -2074,6 +2079,23 @@ - (NSString *)tchapTruncatedQuotedReplyFrom:(NSString *)fullQuotedReply {
if( quotedTextRange.location != NSNotFound && quotedTextRange.length > quotedTextMaxLength )
{
NSRange truncatedRange = NSMakeRange(quotedTextRange.location + quotedTextMaxLength, quotedTextRange.length - quotedTextMaxLength);

NSRange remainingRange = NSMakeRange(quotedTextRange.location, quotedTextMaxLength);

// Check if truncation is in the middle of an HTML <a> tag
NSRange lastOpeningTag = [fullQuotedReply rangeOfString:@"<a" options:NSBackwardsSearch range:remainingRange];
NSRange lastClosingTag = [fullQuotedReply rangeOfString:@"/a>" options:NSBackwardsSearch range:remainingRange];

if( lastOpeningTag.location != NSNotFound &&
(lastClosingTag.location == NSNotFound || lastOpeningTag.location > lastClosingTag.location) )
{
// An opening tag has no closing tag. This can break the display of the message.
// Cut at the beginning of the incomplete tag.
NSUInteger excedentLength = truncatedRange.location - lastOpeningTag.location;
truncatedRange.location -= excedentLength;
truncatedRange.length += excedentLength;
}

return [fullQuotedReply stringByReplacingCharactersInRange:truncatedRange withString:@""];
}

Expand Down
1 change: 1 addition & 0 deletions changelog.d/1056.bugfix
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Amélioration de la troncature des réponses à messages (certains messages tronqués apparaissaient vides).

0 comments on commit 4e53cab

Please sign in to comment.