diff --git a/Mastodon.xcodeproj/project.pbxproj b/Mastodon.xcodeproj/project.pbxproj index 11403cdc..0c8d53cd 100644 --- a/Mastodon.xcodeproj/project.pbxproj +++ b/Mastodon.xcodeproj/project.pbxproj @@ -1634,7 +1634,7 @@ repositoryURL = "https://github.com/TwidereProject/ActiveLabel.swift"; requirement = { kind = upToNextMajorVersion; - minimumVersion = 3.0.0; + minimumVersion = 4.0.0; }; }; 2D61336725C18A4F00CAE157 /* XCRemoteSwiftPackageReference "AlamofireNetworkActivityIndicator" */ = { diff --git a/Mastodon.xcodeproj/xcuserdata/mainasuk.xcuserdatad/xcschemes/xcschememanagement.plist b/Mastodon.xcodeproj/xcuserdata/mainasuk.xcuserdatad/xcschemes/xcschememanagement.plist index 6f7ab589..24ba6665 100644 --- a/Mastodon.xcodeproj/xcuserdata/mainasuk.xcuserdatad/xcschemes/xcschememanagement.plist +++ b/Mastodon.xcodeproj/xcuserdata/mainasuk.xcuserdatad/xcschemes/xcschememanagement.plist @@ -7,7 +7,7 @@ CoreDataStack.xcscheme_^#shared#^_ orderHint - 8 + 7 Mastodon.xcscheme_^#shared#^_ diff --git a/Mastodon.xcworkspace/xcshareddata/swiftpm/Package.resolved b/Mastodon.xcworkspace/xcshareddata/swiftpm/Package.resolved index c1cd9295..f783b798 100644 --- a/Mastodon.xcworkspace/xcshareddata/swiftpm/Package.resolved +++ b/Mastodon.xcworkspace/xcshareddata/swiftpm/Package.resolved @@ -6,8 +6,8 @@ "repositoryURL": "https://github.com/TwidereProject/ActiveLabel.swift", "state": { "branch": null, - "revision": "3d8115c992c44358eabbb21ffc4616f4d56028b1", - "version": "3.0.0" + "revision": "d6cf96e0ca4f2269021bcf8f11381ab57897f84a", + "version": "4.0.0" } }, { diff --git a/Mastodon/Extension/MastodonContent.swift b/Mastodon/Extension/MastodonContent.swift index 3e6b072d..b1b1a635 100755 --- a/Mastodon/Extension/MastodonContent.swift +++ b/Mastodon/Extension/MastodonContent.swift @@ -24,7 +24,8 @@ enum TootContent { switch entity.type { case .url: guard let href = entity.href else { continue } - activeEntities.append(ActiveEntity(range: range, type: .url(original: href, trimmed: entity.hrefEllipsis ?? href))) + let text = String(entity.text) + activeEntities.append(ActiveEntity(range: range, type: .url(text, trimmed: entity.hrefEllipsis ?? text, url: href))) case .hashtag: var userInfo: [AnyHashable: Any] = [:] entity.href.flatMap { href in @@ -54,17 +55,17 @@ enum TootContent { document: toot, original: text, trimmed: trimmed, - activeEntities: activeEntities + activeEntities: validate(text: trimmed, activeEntities: activeEntities) ? activeEntities : [] ) } static func trimEntity(toot: inout String, activeEntity: ActiveEntity, activeEntities: [ActiveEntity]) { - guard case let .url(original, trimmed, _) = activeEntity.type else { return } + guard case let .url(text, trimmed, _, _) = activeEntity.type else { return } guard let index = activeEntities.firstIndex(where: { $0.range == activeEntity.range }) else { return } guard let range = Range(activeEntity.range, in: toot) else { return } toot.replaceSubrange(range, with: trimmed) - let offset = trimmed.count - original.count + let offset = trimmed.count - text.count activeEntity.range.length += offset let moveActiveEntities = Array(activeEntities[index...].dropFirst()) @@ -72,6 +73,19 @@ enum TootContent { moveActiveEntity.range.location += offset } } + + private static func validate(text: String, activeEntities: [ActiveEntity]) -> Bool { + for activeEntity in activeEntities { + let count = text.utf16.count + let endIndex = activeEntity.range.location + activeEntity.range.length + guard endIndex <= count else { + assertionFailure("Please file issue") + return false + } + } + + return true + } }