add logging and model handling

This commit is contained in:
Kyle Matthew Hedden 2022-07-19 12:56:35 -04:00
parent b3667dc868
commit fc512ca811
4 changed files with 50 additions and 3 deletions

View File

@ -831,11 +831,16 @@ extension Label {
guard let string = string,
range.location > 0 && range.location < string.length
else { return }
else {
MVMCoreLoggingHandler.shared()?.addError(toLog: MVMCoreErrorObject(title: nil, messageToLog: "Attribute starting location \(range.lowerBound) is out of bounds for '\(string?.string ?? "")'. Attribute is discarded.", code: ErrorCode.default.rawValue, domain: ErrorDomainNative, location: "\(#file): \(#function)")!)
return
}
var range = range
if range.upperBound > string.length {
range = NSRange(location: range.location, length: string.length - range.location)
let newRange = NSRange(location: range.location, length: string.length - range.location)
MVMCoreLoggingHandler.shared()?.addError(toLog: MVMCoreErrorObject(title: nil, messageToLog: "Attribute ending location \(range.upperBound) is out of bounds for '\(string)'. Adjusting to \(newRange.upperBound).", code: ErrorCode.default.rawValue, domain: ErrorDomainNative, location: "\(#file): \(#function)")!)
range = newRange
}
string.addAttributes([NSAttributedString.Key.underlineStyle: NSUnderlineStyle.single.rawValue], range: range)

View File

@ -36,6 +36,14 @@ class LabelAttributeImageModel: LabelAttributeModel {
case URL
}
public override func validateInRange(of text: String) -> Bool {
guard location > 0 && location <= text.count else {
MVMCoreLoggingHandler.shared()?.addError(toLog: MVMCoreErrorObject(title: nil, messageToLog: "Attribute starting location \(location) is out of bounds for '\(text)'. Attribute is discarded.", code: ErrorCode.default.rawValue, domain: ErrorDomainNative, location: "\(#file): \(#function)")!)
return false
}
return true
}
//--------------------------------------------------
// MARK: - Codec
//--------------------------------------------------

View File

@ -44,6 +44,23 @@
case length
}
public func validateInRange(of text: String) -> Bool {
// Prevent invalid starting locations.
guard location > 0 && location <= text.count else {
MVMCoreLoggingHandler.shared()?.addError(toLog: MVMCoreErrorObject(title: nil, messageToLog: "Attribute starting location \(location) is out of bounds for '\(text)'. Attribute is discarded.", code: ErrorCode.default.rawValue, domain: ErrorDomainNative, location: "\(#file): \(#function)")!)
return false
}
// Truncate long lengths
guard length + location <= text.count else {
MVMCoreLoggingHandler.shared()?.addError(toLog: MVMCoreErrorObject(title: nil, messageToLog: "Attribute length \(length) is out of bounds for '\(text)'. Adjusting to \(text.count - location).", code: ErrorCode.default.rawValue, domain: ErrorDomainNative, location: "\(#file): \(#function)")!)
length = text.count - location
return true
}
return true
}
//--------------------------------------------------
// MARK: - Codec
//--------------------------------------------------

View File

@ -21,7 +21,17 @@
public var fontName: String?
public var fontSize: CGFloat?
public var textAlignment: NSTextAlignment?
public var attributes: [LabelAttributeModel]?
private var _attributes: [LabelAttributeModel]?
public var attributes: [LabelAttributeModel]? {
get { _attributes }
set {
if let attributes = newValue {
_attributes = validate(attributes)
} else {
_attributes = nil
}
}
}
public var html: String?
public var hero: Int?
public var makeWholeViewClickable: Bool?
@ -60,6 +70,13 @@
self.text = text
}
//--------------------------------------------------
// MARK: - Validations
//--------------------------------------------------
public func validate(_ attributes: [LabelAttributeModel]) -> [LabelAttributeModel] {
return attributes.filter { $0.validateInRange(of: text) }
}
//--------------------------------------------------
// MARK: - Codec
//--------------------------------------------------