From 3b29e879ba0e9119a0f91abfd7ee0ca2055660c0 Mon Sep 17 00:00:00 2001 From: Nicolas Buquet Date: Thu, 13 Jun 2024 18:58:29 +0200 Subject: [PATCH 1/2] =?UTF-8?q?Am=C3=A9lioration=20de=20la=20troncature=20?= =?UTF-8?q?des=20r=C3=A9ponses=20=C3=A0=20messages?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Utils/EventFormatter/MXKEventFormatter.m | 25 ++++++++++++++++++- changelog.d/1056.bugfix | 1 + 2 files changed, 25 insertions(+), 1 deletion(-) create mode 100644 changelog.d/1056.bugfix diff --git a/Riot/Modules/MatrixKit/Utils/EventFormatter/MXKEventFormatter.m b/Riot/Modules/MatrixKit/Utils/EventFormatter/MXKEventFormatter.m index 1fd543a036..b38a60cb35 100644 --- a/Riot/Modules/MatrixKit/Utils/EventFormatter/MXKEventFormatter.m +++ b/Riot/Modules/MatrixKit/Utils/EventFormatter/MXKEventFormatter.m @@ -31,7 +31,13 @@ #import "GeneratedInterface-Swift.h" static NSString *const kHTMLATagRegexPattern = @"([^<]*)"; -static NSString *const kRepliedTextPattern = @".*
.*
(.*)
"; +// Tchap: modify regex to define reply paqttern +// static NSString *const kRepliedTextPattern = @".*
.*
(.*)
"; +// 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 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 kRepliedTextPattern = @"(?s).*
.*
(?:
[: ]*)*(.*)
"; @interface MXKEventFormatter () { @@ -2074,6 +2080,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 tag + NSRange lastOpeningTag = [fullQuotedReply rangeOfString:@"" 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:@"…"]; } diff --git a/changelog.d/1056.bugfix b/changelog.d/1056.bugfix new file mode 100644 index 0000000000..7bf20272a6 --- /dev/null +++ b/changelog.d/1056.bugfix @@ -0,0 +1 @@ +Amélioration de la troncature des réponses à messages (certains messages tronqués apparaissaient vides). \ No newline at end of file From 28acf2bcaa8228593b72505c658a0844401280d6 Mon Sep 17 00:00:00 2001 From: Nicolas Buquet Date: Thu, 13 Jun 2024 20:34:23 +0200 Subject: [PATCH 2/2] Let original Element `kRepliedTextPattern` in place for use in `renderPollEndedReplyTo` method. --- .../MatrixKit/Utils/EventFormatter/MXKEventFormatter.m | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/Riot/Modules/MatrixKit/Utils/EventFormatter/MXKEventFormatter.m b/Riot/Modules/MatrixKit/Utils/EventFormatter/MXKEventFormatter.m index b38a60cb35..a7c24eafb5 100644 --- a/Riot/Modules/MatrixKit/Utils/EventFormatter/MXKEventFormatter.m +++ b/Riot/Modules/MatrixKit/Utils/EventFormatter/MXKEventFormatter.m @@ -31,13 +31,12 @@ #import "GeneratedInterface-Swift.h" static NSString *const kHTMLATagRegexPattern = @"([^<]*)"; -// Tchap: modify regex to define reply paqttern -// static NSString *const kRepliedTextPattern = @".*
.*
(.*)
"; -// The Tchap pattern takes in account: +static NSString *const kRepliedTextPattern = @".*
.*
(.*)
"; +// 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 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 kRepliedTextPattern = @"(?s).*
.*
(?:
[: ]*)*(.*)
"; +static NSString *const kTchapRepliedTextPattern = @"(?s).*
.*
(?:
[: ]*)*(.*)
"; @interface MXKEventFormatter () { @@ -2057,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]; });