Skip to content

Commit 405edfe

Browse files
committed
Add warning if AXUIElement access is too deep
1 parent cb95060 commit 405edfe

1 file changed

Lines changed: 29 additions & 6 deletions

File tree

Tool/Sources/AXExtension/AXUIElement.swift

Lines changed: 29 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
import AppKit
22
import Foundation
3+
import Logger
4+
#if DEBUG
5+
import IssueReporting
6+
#endif
37

48
// MARK: - State
59

@@ -179,8 +183,15 @@ public extension AXUIElement {
179183
func child(
180184
identifier: String? = nil,
181185
title: String? = nil,
182-
role: String? = nil
186+
role: String? = nil,
187+
depth: Int = 0
183188
) -> AXUIElement? {
189+
#if DEBUG
190+
if depth >= 50 {
191+
reportIssue("AXUIElement.child: Exceeding recommended depth.")
192+
}
193+
#endif
194+
184195
for child in children {
185196
let match = {
186197
if let identifier, child.identifier != identifier { return false }
@@ -194,7 +205,8 @@ public extension AXUIElement {
194205
if let target = child.child(
195206
identifier: identifier,
196207
title: title,
197-
role: role
208+
role: role,
209+
depth: depth + 1
198210
) { return target }
199211
}
200212
return nil
@@ -210,13 +222,19 @@ public extension AXUIElement {
210222
renamed: "traverse(_:)",
211223
message: "Please make use ``AXUIElement\traverse(_:)`` instead."
212224
)
213-
func children(where match: (AXUIElement) -> Bool) -> [AXUIElement] {
225+
func children(depth: Int = 0, where match: (AXUIElement) -> Bool) -> [AXUIElement] {
226+
#if DEBUG
227+
if depth >= 50 {
228+
reportIssue("AXUIElement.children: Exceeding recommended depth.")
229+
}
230+
#endif
231+
214232
var all = [AXUIElement]()
215233
for child in children {
216234
if match(child) { all.append(child) }
217235
}
218236
for child in children {
219-
all.append(contentsOf: child.children(where: match))
237+
all.append(contentsOf: child.children(depth: depth + 1, where: match))
220238
}
221239
return all
222240
}
@@ -227,12 +245,17 @@ public extension AXUIElement {
227245
return parent.firstParent(where: match)
228246
}
229247

230-
func firstChild(where match: (AXUIElement) -> Bool) -> AXUIElement? {
248+
func firstChild(depth: Int = 0, where match: (AXUIElement) -> Bool) -> AXUIElement? {
249+
#if DEBUG
250+
if depth >= 50 {
251+
reportIssue("AXUIElement.firstChild: Exceeding recommended depth.")
252+
}
253+
#endif
231254
for child in children {
232255
if match(child) { return child }
233256
}
234257
for child in children {
235-
if let target = child.firstChild(where: match) {
258+
if let target = child.firstChild(depth: depth + 1, where: match) {
236259
return target
237260
}
238261
}

0 commit comments

Comments
 (0)