Merge pull request #7 from tootsuite/fix/poll-option

Fix entity decode and URL parse issue
This commit is contained in:
CMK 2021-02-04 16:48:13 +08:00 committed by GitHub
commit dee84f5ec6
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 24 additions and 10 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())
@ -73,6 +74,19 @@ enum TootContent {
}
}
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
}
}
extension String {

View File

@ -13,7 +13,7 @@ extension Mastodon.Entity {
/// - Since: 2.8.0
/// - Version: 3.3.0
/// # Last Update
/// 2021/1/28
/// 2021/2/4
/// # Reference
/// [Document](https://docs.joinmastodon.org/entities/poll/)
public struct Poll: Codable {
@ -51,7 +51,7 @@ extension Mastodon.Entity.Poll {
public let title: String
/// nil if results are not published yet
public let votesCount: Int?
public let emojis: [Mastodon.Entity.Emoji]
public let emojis: [Mastodon.Entity.Emoji]?
enum CodingKeys: String, CodingKey {
case title