Merge branch 'bugfix/label-range-validation' into 'develop'

Validation of ranges

See merge request BPHV_MIPS/vds_ios!187
This commit is contained in:
Bruce, Matt R 2024-03-20 21:37:20 +00:00
commit 317b55a58e
5 changed files with 21 additions and 4 deletions

View File

@ -56,6 +56,8 @@ public struct ActionLabelAttribute: ActionLabelAttributeModel {
} }
public func setAttribute(on attributedString: NSMutableAttributedString) { public func setAttribute(on attributedString: NSMutableAttributedString) {
guard isValidRange(on: attributedString) else { return }
if(shouldUnderline){ if(shouldUnderline){
UnderlineLabelAttribute(location: location, length: length).setAttribute(on: attributedString) UnderlineLabelAttribute(location: location, length: length).setAttribute(on: attributedString)
} }

View File

@ -14,6 +14,8 @@ public protocol AttachmentLabelAttributeModel: LabelAttributeModel {
extension AttachmentLabelAttributeModel { extension AttachmentLabelAttributeModel {
public func setAttribute(on attributedString: NSMutableAttributedString) { public func setAttribute(on attributedString: NSMutableAttributedString) {
guard isValidRange(on: attributedString) else { return }
do { do {
let mutableString = NSMutableAttributedString() let mutableString = NSMutableAttributedString()
let attachment = try getAttachment() let attachment = try getAttachment()

View File

@ -31,11 +31,21 @@ extension LabelAttributeModel {
} }
public func isValidRange(on attributedString: NSMutableAttributedString) -> Bool { public func isValidRange(on attributedString: NSMutableAttributedString) -> Bool {
range.location + range.length <= attributedString.string.count attributedString.isValid(range: range)
}
}
public extension String {
func isValid(range: NSRange) -> Bool {
range.location >= 0 && range.length > 0 && range.location + range.length <= count
} }
} }
public extension NSAttributedString { public extension NSAttributedString {
func isValid(range: NSRange) -> Bool {
range.location >= 0 && range.length > 0 && range.location + range.length <= length
}
func createAttributeModels() -> [(any LabelAttributeModel)] { func createAttributeModels() -> [(any LabelAttributeModel)] {
var attributes: [any VDS.LabelAttributeModel] = [] var attributes: [any VDS.LabelAttributeModel] = []
enumerateAttributes(in: NSMakeRange(0, length)) { attributeMap, range, stop in enumerateAttributes(in: NSMakeRange(0, length)) { attributeMap, range, stop in
@ -61,6 +71,7 @@ public extension NSAttributedString {
extension NSMutableAttributedString { extension NSMutableAttributedString {
public func apply(attribute: any LabelAttributeModel) { public func apply(attribute: any LabelAttributeModel) {
guard isValid(range: attribute.range) else { return }
attribute.setAttribute(on: self) attribute.setAttribute(on: self)
} }

View File

@ -24,6 +24,8 @@ public class TooltipLabelAttribute: ActionLabelAttributeModel, TooltipLaunchable
public var presenter: UIView? public var presenter: UIView?
public func setAttribute(on attributedString: NSMutableAttributedString) { public func setAttribute(on attributedString: NSMutableAttributedString) {
guard isValidRange(on: attributedString) else { return }
//update the location //update the location
location = attributedString.string.count - 1 location = attributedString.string.count - 1

View File

@ -344,7 +344,7 @@ open class Label: UILabel, ViewProtocol, UserInfoable {
for attribute in attributes { for attribute in attributes {
//see if the attribute is Actionable //see if the attribute is Actionable
if let actionable = attribute as? any ActionLabelAttributeModel{ if let actionable = attribute as? any ActionLabelAttributeModel, mutableAttributedString.isValid(range: actionable.range) {
//create a accessibleAction //create a accessibleAction
let customAccessibilityAction = customAccessibilityAction(text: mutableAttributedString.string, range: actionable.range, accessibleText: actionable.accessibleText) let customAccessibilityAction = customAccessibilityAction(text: mutableAttributedString.string, range: actionable.range, accessibleText: actionable.accessibleText)
@ -379,7 +379,7 @@ open class Label: UILabel, ViewProtocol, UserInfoable {
guard let text = text, let attributedText else { return nil } guard let text = text, let attributedText else { return nil }
let actionText = accessibleText ?? NSString(string:text).substring(with: range) let actionText = accessibleText ?? (text.isValid(range: range) ? NSString(string:text).substring(with: range) : text)
// Calculate the frame of the substring // Calculate the frame of the substring
let layoutManager = NSLayoutManager() let layoutManager = NSLayoutManager()