import Foundation /// Checks that text sent as keys or a paste landed: predict the field's value, then read it back. public enum TextInsertionReadback { public enum Outcome: Equatable { case landed case mismatch case unreadable } /// The value a field should hold once `text` replaces its selection. `selection` is in UTF-16 offsets, as /// AXSelectedTextRange reports it. Nil means the result can't be predicted: a non-empty field with no /// selection, or a range outside the value or inside a character. public static func expectedValue(current: String, selection: Range?, inserting text: String) -> String? { let units = Array(current.utf16) let range: Range if let selection { guard selection.lowerBound >= 0, selection.upperBound <= units.count else { return nil } range = selection } else if units.isEmpty { range = 0..<0 } else { return nil } let prefix = Array(units[.. Double = CFAbsoluteTimeGetCurrent, sleep: (Double) -> Void = { Thread.sleep(forTimeInterval: $0) }, read: () -> String? ) -> Outcome { let start = now() var quietDeadline = start + quietSeconds var readback = read() while let current = readback, !sameText(current, expected) { let time = now() guard time < quietDeadline, time < start + capSeconds else { return .mismatch } sleep(intervalSeconds) readback = read() if readback != current { quietDeadline = now() + quietSeconds } } return readback == nil ? .unreadable : .landed } // Why: Chromium contenteditables store typed trailing or repeated spaces as no-break spaces. private static func sameText(_ lhs: String, _ rhs: String) -> Bool { lhs.replacingOccurrences(of: "\u{00A0}", with: " ") == rhs.replacingOccurrences(of: "\u{00A0}", with: " ") } }