From f7fc8b807a94d76a6cc5023eb3ecfb4fd53752d5 Mon Sep 17 00:00:00 2001 From: Hugh Nimmo-Smith Date: Wed, 7 May 2025 12:36:23 +0100 Subject: [PATCH] Support for experimental MSC4286 to not render external payment details Ref: https://github.com/matrix-org/matrix-spec-proposals/pull/4286 --- .../Categories/DTHTMLElement+MatrixKit.swift | 10 ++++++++++ .../MatrixKitTests/MXKEventFormatterTests.m | 18 ++++++++++++++++++ 2 files changed, 28 insertions(+) diff --git a/Riot/Modules/MatrixKit/Categories/DTHTMLElement+MatrixKit.swift b/Riot/Modules/MatrixKit/Categories/DTHTMLElement+MatrixKit.swift index 012c4eace..6dc0f9cfb 100644 --- a/Riot/Modules/MatrixKit/Categories/DTHTMLElement+MatrixKit.swift +++ b/Riot/Modules/MatrixKit/Categories/DTHTMLElement+MatrixKit.swift @@ -23,6 +23,16 @@ public extension DTHTMLElement { // Remove any attachments to fix rendering. textAttachment = nil + // Handle special case for span with data-mx-external-payment-details + // This could be based on Storefront.current.countryCode to show the link + // content in unrestricted countries. e.g. currently USA + if name == "span", + let attributes = attributes as? [String: String], + attributes["data-msc4286-external-payment-details"] != nil { + parent.removeChildNode(self) + return + } + // If the element has plain text content show that, // otherwise prevent the tag from displaying. if let stringContent = attributedString()?.string, diff --git a/RiotTests/MatrixKitTests/MXKEventFormatterTests.m b/RiotTests/MatrixKitTests/MXKEventFormatterTests.m index f16f786eb..24457b990 100644 --- a/RiotTests/MatrixKitTests/MXKEventFormatterTests.m +++ b/RiotTests/MatrixKitTests/MXKEventFormatterTests.m @@ -239,6 +239,24 @@ Please see LICENSE in the repository root for full details. XCTAssertFalse(hasAttachment, @"iFrame attachments should be removed as they're not included in the allowedHTMLTags array."); } +- (void)testMxExternalPaymentDetailsRemoved +{ + // Given an HTML string containing a with data-mx-external-payment-details. + NSString *html = @"This is visible. But text is hidden and this link too"; + + // When rendering this string as an attributed string. + NSAttributedString *attributedString = [eventFormatter renderHTMLString:html + forEvent:anEvent + withRoomState:nil + andLatestRoomState:nil]; + + // Then the attributed string should have the stripped and not include any attachments. + XCTAssertEqualObjects(attributedString.string, @"This is visible", @"The tag content should be removed."); + + BOOL hasAttachment = [attributedString containsAttachmentsInRange:NSMakeRange(0, attributedString.length)]; + XCTAssertFalse(hasAttachment, @"span attachments should be removed as they're not included in the allowedHTMLTags array."); +} + - (void)testRenderHTMLStringWithMXReply { // Given an HTML string representing a matrix reply.