From 04ef849913575be9782777beec75859e082a57b4 Mon Sep 17 00:00:00 2001 From: aringenbach Date: Fri, 23 Sep 2022 15:06:40 +0200 Subject: [PATCH] Fix render of links with both characters requiring percent encoding and markdown-like syntax --- .../EventFormatter/MarkdownToHTMLRenderer.swift | 5 +++-- RiotTests/MarkdownToHTMLRendererTests.swift | 14 ++++++++++++++ changelog.d/6748.bugfix | 1 + 3 files changed, 18 insertions(+), 2 deletions(-) create mode 100644 changelog.d/6748.bugfix diff --git a/Riot/Modules/MatrixKit/Utils/EventFormatter/MarkdownToHTMLRenderer.swift b/Riot/Modules/MatrixKit/Utils/EventFormatter/MarkdownToHTMLRenderer.swift index 0800f4307..8c697dc6c 100644 --- a/Riot/Modules/MatrixKit/Utils/EventFormatter/MarkdownToHTMLRenderer.swift +++ b/Riot/Modules/MatrixKit/Utils/EventFormatter/MarkdownToHTMLRenderer.swift @@ -166,10 +166,11 @@ private extension CMarkNode { private extension String { /// Returns array of URLs detected inside the String. var containedUrls: [NSTextCheckingResult] { - guard let detector = try? NSDataDetector(types: NSTextCheckingResult.CheckingType.link.rawValue) else { + guard let detector = try? NSDataDetector(types: NSTextCheckingResult.CheckingType.link.rawValue), + let percentEncoded = self.addingPercentEncoding(withAllowedCharacters: .urlHostAllowed) else { return [] } - return detector.matches(in: self, options: [], range: NSRange(location: 0, length: self.utf16.count)) + return detector.matches(in: percentEncoded, options: [], range: NSRange(location: 0, length: percentEncoded.utf16.count)) } } diff --git a/RiotTests/MarkdownToHTMLRendererTests.swift b/RiotTests/MarkdownToHTMLRendererTests.swift index 58626c1e3..918b4d494 100644 --- a/RiotTests/MarkdownToHTMLRendererTests.swift +++ b/RiotTests/MarkdownToHTMLRendererTests.swift @@ -112,6 +112,20 @@ final class MarkdownToHTMLRendererTests: XCTestCase { testRenderHTML(input: input, expectedOutput: expectedOutput) } + func testRenderRepairedLinksWithCharactersRequiringPercentEncoding() { + let input = "Some link with special characters: " + + "https://matrix.to/#/#_oftc_#matrix-dev:matrix.org" + + " " + + "https://matrix.to/#/#?=+-_#_" + + "\n" + let expectedOutput = "

Some link with special characters: " + + "https://matrix.to/#/#_oftc_#matrix-dev:matrix.org" + + " " + + "https://matrix.to/#/#?=+-_#_

" + + "\n" + testRenderHTML(input: input, expectedOutput: expectedOutput) + } + /// Test links inside codeblocks. func testRenderLinksInCodeblock() { let input = "```" diff --git a/changelog.d/6748.bugfix b/changelog.d/6748.bugfix new file mode 100644 index 000000000..0c9d08465 --- /dev/null +++ b/changelog.d/6748.bugfix @@ -0,0 +1 @@ +Fix render of links with both characters requiring percent encoding and markdown-like syntax