Merge pull request #7 from tootsuite/fix/poll-option
Fix entity decode and URL parse issue
This commit is contained in:
commit
dee84f5ec6
|
@ -1634,7 +1634,7 @@
|
||||||
repositoryURL = "https://github.com/TwidereProject/ActiveLabel.swift";
|
repositoryURL = "https://github.com/TwidereProject/ActiveLabel.swift";
|
||||||
requirement = {
|
requirement = {
|
||||||
kind = upToNextMajorVersion;
|
kind = upToNextMajorVersion;
|
||||||
minimumVersion = 3.0.0;
|
minimumVersion = 4.0.0;
|
||||||
};
|
};
|
||||||
};
|
};
|
||||||
2D61336725C18A4F00CAE157 /* XCRemoteSwiftPackageReference "AlamofireNetworkActivityIndicator" */ = {
|
2D61336725C18A4F00CAE157 /* XCRemoteSwiftPackageReference "AlamofireNetworkActivityIndicator" */ = {
|
||||||
|
|
|
@ -7,7 +7,7 @@
|
||||||
<key>CoreDataStack.xcscheme_^#shared#^_</key>
|
<key>CoreDataStack.xcscheme_^#shared#^_</key>
|
||||||
<dict>
|
<dict>
|
||||||
<key>orderHint</key>
|
<key>orderHint</key>
|
||||||
<integer>8</integer>
|
<integer>7</integer>
|
||||||
</dict>
|
</dict>
|
||||||
<key>Mastodon.xcscheme_^#shared#^_</key>
|
<key>Mastodon.xcscheme_^#shared#^_</key>
|
||||||
<dict>
|
<dict>
|
||||||
|
|
|
@ -6,8 +6,8 @@
|
||||||
"repositoryURL": "https://github.com/TwidereProject/ActiveLabel.swift",
|
"repositoryURL": "https://github.com/TwidereProject/ActiveLabel.swift",
|
||||||
"state": {
|
"state": {
|
||||||
"branch": null,
|
"branch": null,
|
||||||
"revision": "3d8115c992c44358eabbb21ffc4616f4d56028b1",
|
"revision": "d6cf96e0ca4f2269021bcf8f11381ab57897f84a",
|
||||||
"version": "3.0.0"
|
"version": "4.0.0"
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
|
|
|
@ -24,7 +24,8 @@ enum TootContent {
|
||||||
switch entity.type {
|
switch entity.type {
|
||||||
case .url:
|
case .url:
|
||||||
guard let href = entity.href else { continue }
|
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:
|
case .hashtag:
|
||||||
var userInfo: [AnyHashable: Any] = [:]
|
var userInfo: [AnyHashable: Any] = [:]
|
||||||
entity.href.flatMap { href in
|
entity.href.flatMap { href in
|
||||||
|
@ -54,17 +55,17 @@ enum TootContent {
|
||||||
document: toot,
|
document: toot,
|
||||||
original: text,
|
original: text,
|
||||||
trimmed: trimmed,
|
trimmed: trimmed,
|
||||||
activeEntities: activeEntities
|
activeEntities: validate(text: trimmed, activeEntities: activeEntities) ? activeEntities : []
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
static func trimEntity(toot: inout String, activeEntity: ActiveEntity, activeEntities: [ActiveEntity]) {
|
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 index = activeEntities.firstIndex(where: { $0.range == activeEntity.range }) else { return }
|
||||||
guard let range = Range(activeEntity.range, in: toot) else { return }
|
guard let range = Range(activeEntity.range, in: toot) else { return }
|
||||||
toot.replaceSubrange(range, with: trimmed)
|
toot.replaceSubrange(range, with: trimmed)
|
||||||
|
|
||||||
let offset = trimmed.count - original.count
|
let offset = trimmed.count - text.count
|
||||||
activeEntity.range.length += offset
|
activeEntity.range.length += offset
|
||||||
|
|
||||||
let moveActiveEntities = Array(activeEntities[index...].dropFirst())
|
let moveActiveEntities = Array(activeEntities[index...].dropFirst())
|
||||||
|
@ -72,6 +73,19 @@ enum TootContent {
|
||||||
moveActiveEntity.range.location += offset
|
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
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -13,7 +13,7 @@ extension Mastodon.Entity {
|
||||||
/// - Since: 2.8.0
|
/// - Since: 2.8.0
|
||||||
/// - Version: 3.3.0
|
/// - Version: 3.3.0
|
||||||
/// # Last Update
|
/// # Last Update
|
||||||
/// 2021/1/28
|
/// 2021/2/4
|
||||||
/// # Reference
|
/// # Reference
|
||||||
/// [Document](https://docs.joinmastodon.org/entities/poll/)
|
/// [Document](https://docs.joinmastodon.org/entities/poll/)
|
||||||
public struct Poll: Codable {
|
public struct Poll: Codable {
|
||||||
|
@ -51,7 +51,7 @@ extension Mastodon.Entity.Poll {
|
||||||
public let title: String
|
public let title: String
|
||||||
/// nil if results are not published yet
|
/// nil if results are not published yet
|
||||||
public let votesCount: Int?
|
public let votesCount: Int?
|
||||||
public let emojis: [Mastodon.Entity.Emoji]
|
public let emojis: [Mastodon.Entity.Emoji]?
|
||||||
|
|
||||||
enum CodingKeys: String, CodingKey {
|
enum CodingKeys: String, CodingKey {
|
||||||
case title
|
case title
|
||||||
|
|
Loading…
Reference in New Issue