Skip to content

Commit 5ac3da2

Browse files
committed
fix: Use safer enumeration for accessibilityRange(forLine:)
The existing code used textElements(for:) which can return an empty array. This patch uses enumerateTextElements(from:) instead, which is safer since it can enumerate elements starting at a given location without the risk of an empty result.
1 parent bb9fe40 commit 5ac3da2

File tree

1 file changed

+11
-4
lines changed

1 file changed

+11
-4
lines changed

Sources/STTextViewAppKit/STTextView+NSAccessibilityProtocol.swift

+11-4
Original file line numberDiff line numberDiff line change
@@ -99,10 +99,17 @@ extension STTextView {
9999
}
100100

101101
open override func accessibilityRange(forLine line: Int) -> NSRange {
102-
guard let location = textContentManager.location(line: line),
103-
let textElement = textContentManager.textElements(for: NSTextRange(location: location)).first,
104-
let textElementRange = textElement.elementRange
105-
else {
102+
guard let location = textContentManager.location(line: line) else {
103+
return .notFound
104+
}
105+
106+
var textElement: NSTextElement?
107+
textContentManager.enumerateTextElements(from: location) { element in
108+
textElement = element
109+
return false
110+
}
111+
112+
guard let textElement, let textElementRange = textElement.elementRange else {
106113
return .notFound
107114
}
108115

0 commit comments

Comments
 (0)