Update string attribute range clipping

This commit is contained in:
Ali 2022-12-08 17:20:18 +04:00
parent 94ccf7ed3b
commit 71bed257bb
2 changed files with 21 additions and 2 deletions

View File

@ -1822,11 +1822,23 @@ class ChatListItemNode: ItemListRevealOptionsItemNode {
let mutableString = NSMutableAttributedString(string: messageText, font: textFont, textColor: theme.messageTextColor) let mutableString = NSMutableAttributedString(string: messageText, font: textFont, textColor: theme.messageTextColor)
if let spoilers = spoilers { if let spoilers = spoilers {
for range in spoilers { for range in spoilers {
var range = range
if range.location > mutableString.length {
continue
} else if range.location + range.length > mutableString.length {
range.length = mutableString.length - range.location
}
mutableString.addAttribute(NSAttributedString.Key(rawValue: TelegramTextAttributes.Spoiler), value: true, range: range) mutableString.addAttribute(NSAttributedString.Key(rawValue: TelegramTextAttributes.Spoiler), value: true, range: range)
} }
} }
if let customEmojiRanges = customEmojiRanges { if let customEmojiRanges = customEmojiRanges {
for (range, attribute) in customEmojiRanges { for (range, attribute) in customEmojiRanges {
var range = range
if range.location > mutableString.length {
continue
} else if range.location + range.length > mutableString.length {
range.length = mutableString.length - range.location
}
mutableString.addAttribute(ChatTextInputAttributes.customEmoji, value: attribute, range: range) mutableString.addAttribute(ChatTextInputAttributes.customEmoji, value: attribute, range: range)
} }
} }
@ -1857,6 +1869,12 @@ class ChatListItemNode: ItemListRevealOptionsItemNode {
for range in chatListSearchResult.resultRanges { for range in chatListSearchResult.resultRanges {
let stringRange = NSRange(range, in: chatListSearchResult.text) let stringRange = NSRange(range, in: chatListSearchResult.text)
if stringRange.location >= 0 && stringRange.location + stringRange.length <= composedString.length { if stringRange.location >= 0 && stringRange.location + stringRange.length <= composedString.length {
var stringRange = stringRange
if stringRange.location > composedString.length {
continue
} else if stringRange.location + stringRange.length > composedString.length {
stringRange.length = composedString.length - stringRange.location
}
composedString.addAttribute(.foregroundColor, value: theme.messageHighlightedTextColor, range: stringRange) composedString.addAttribute(.foregroundColor, value: theme.messageHighlightedTextColor, range: stringRange)
} }
} }

View File

@ -18,8 +18,10 @@ public func chatInputStateStringWithAppliedEntities(_ text: String, entities: [M
if nsString == nil { if nsString == nil {
nsString = text as NSString nsString = text as NSString
} }
if range.location >= stringLength {
continue
}
if range.location + range.length > stringLength { if range.location + range.length > stringLength {
range.location = max(0, stringLength - range.length)
range.length = stringLength - range.location range.length = stringLength - range.location
} }
switch entity.type { switch entity.type {
@ -75,7 +77,6 @@ public func stringWithAppliedEntities(_ text: String, entities: [MessageTextEnti
if range.location > stringLength { if range.location > stringLength {
continue continue
} else if range.location + range.length > stringLength { } else if range.location + range.length > stringLength {
range.location = max(0, stringLength - range.length)
range.length = stringLength - range.location range.length = stringLength - range.location
} }
switch entity.type { switch entity.type {