From c4b9e74bcd5ae180f7478d1d8cef1505e5e3687a Mon Sep 17 00:00:00 2001 From: Thomas Ricouard Date: Wed, 15 Feb 2023 06:41:28 +0100 Subject: [PATCH] Escape characters when editing close #811 --- .../NotificationServiceSupport.swift | 22 ------------------ Packages/Network/Sources/Network/String.swift | 23 +++++++++++++++++++ .../Status/Editor/StatusEditorViewModel.swift | 2 +- 3 files changed, 24 insertions(+), 23 deletions(-) create mode 100644 Packages/Network/Sources/Network/String.swift diff --git a/IceCubesNotifications/NotificationServiceSupport.swift b/IceCubesNotifications/NotificationServiceSupport.swift index 66bc4445..698ce296 100644 --- a/IceCubesNotifications/NotificationServiceSupport.swift +++ b/IceCubesNotifications/NotificationServiceSupport.swift @@ -59,25 +59,3 @@ extension NotificationService { return info } } - -extension String { - func escape() -> String { - return replacingOccurrences(of: "&", with: "&") - .replacingOccurrences(of: "<", with: "<") - .replacingOccurrences(of: ">", with: ">") - .replacingOccurrences(of: """, with: "\"") - .replacingOccurrences(of: "'", with: "'") - .replacingOccurrences(of: "'", with: "’") - } - - func URLSafeBase64ToBase64() -> String { - var base64 = replacingOccurrences(of: "-", with: "+").replacingOccurrences(of: "_", with: "/") - let countMod4 = count % 4 - - if countMod4 != 0 { - base64.append(String(repeating: "=", count: 4 - countMod4)) - } - - return base64 - } -} diff --git a/Packages/Network/Sources/Network/String.swift b/Packages/Network/Sources/Network/String.swift new file mode 100644 index 00000000..898c089c --- /dev/null +++ b/Packages/Network/Sources/Network/String.swift @@ -0,0 +1,23 @@ +import Foundation + +extension String { + public func escape() -> String { + return replacingOccurrences(of: "&", with: "&") + .replacingOccurrences(of: "<", with: "<") + .replacingOccurrences(of: ">", with: ">") + .replacingOccurrences(of: """, with: "\"") + .replacingOccurrences(of: "'", with: "'") + .replacingOccurrences(of: "'", with: "’") + } + + public func URLSafeBase64ToBase64() -> String { + var base64 = replacingOccurrences(of: "-", with: "+").replacingOccurrences(of: "_", with: "/") + let countMod4 = count % 4 + + if countMod4 != 0 { + base64.append(String(repeating: "=", count: 4 - countMod4)) + } + + return base64 + } +} diff --git a/Packages/Status/Sources/Status/Editor/StatusEditorViewModel.swift b/Packages/Status/Sources/Status/Editor/StatusEditorViewModel.swift index 74f5b607..4505d7bc 100644 --- a/Packages/Status/Sources/Status/Editor/StatusEditorViewModel.swift +++ b/Packages/Status/Sources/Status/Editor/StatusEditorViewModel.swift @@ -256,7 +256,7 @@ public class StatusEditorViewModel: NSObject, ObservableObject { self.visibility = visibility selectedRange = .init(location: statusText.string.utf16.count, length: 0) case let .edit(status): - var rawText = status.content.asRawText + var rawText = status.content.asRawText.escape() for mention in status.mentions { rawText = rawText.replacingOccurrences(of: "@\(mention.username)", with: "@\(mention.acct)") }