fix: update ActiveLabel to v4.0.0 to fix url active entity may raise crash issue

This commit is contained in:
CMK 2021-02-04 16:29:03 +08:00
parent 945331944f
commit 45301fb72c
4 changed files with 22 additions and 8 deletions

View File

@ -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" */ = {

View File

@ -7,7 +7,7 @@
<key>CoreDataStack.xcscheme_^#shared#^_</key>
<dict>
<key>orderHint</key>
<integer>8</integer>
<integer>7</integer>
</dict>
<key>Mastodon.xcscheme_^#shared#^_</key>
<dict>

View File

@ -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"
}
},
{

View File

@ -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
}
}