Repository navigation
Fix stray 'C' insertion from Speakly dictation #2413
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 1 commit
4ac8a2e
be6db5a
9f34443
5ba224d
484c95c
73ad5ed
c070205
b71d6fb
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -9855,8 +9855,11 @@ final class GhosttySurfaceScrollView: NSView { | |
| // MARK: - NSTextInputClient | ||
|
|
||
| extension GhosttyNSView: NSTextInputClient { | ||
| /// Match upstream Ghostty committed-text behavior: committed IME/AX text | ||
| /// should go through the paste-like text path, not keyboard event encoding. | ||
| fileprivate func sendTextToSurface(_ chars: String) { | ||
| guard let surface = surface else { return } | ||
| guard let data = chars.data(using: .utf8), !data.isEmpty else { return } | ||
| #if DEBUG | ||
| let typingTimingStart = CmuxTypingTiming.start() | ||
| #endif | ||
|
|
@@ -9869,15 +9872,9 @@ extension GhosttyNSView: NSTextInputClient { | |
| increments: ["probeInsertTextCount": 1] | ||
| ) | ||
| #endif | ||
| chars.withCString { ptr in | ||
| var keyEvent = ghostty_input_key_s() | ||
| keyEvent.action = GHOSTTY_ACTION_PRESS | ||
| keyEvent.keycode = 0 | ||
| keyEvent.mods = GHOSTTY_MODS_NONE | ||
| keyEvent.consumed_mods = GHOSTTY_MODS_NONE | ||
| keyEvent.text = ptr | ||
| keyEvent.composing = false | ||
| _ = ghostty_surface_key(surface, keyEvent) | ||
| data.withUnsafeBytes { rawBuffer in | ||
| guard let baseAddress = rawBuffer.baseAddress?.assumingMemoryBound(to: CChar.self) else { return } | ||
| ghostty_surface_text(surface, baseAddress, UInt(rawBuffer.count)) | ||
| } | ||
| #if DEBUG | ||
| CmuxTypingTiming.logDuration( | ||
|
|
@@ -9888,6 +9885,116 @@ extension GhosttyNSView: NSTextInputClient { | |
| #endif | ||
| } | ||
|
|
||
| /// External accessibility/dictation tools should commit plain text, but | ||
| /// some inject a leading escape/control sequence first. Strip those bytes | ||
| /// on the committed-text path so they can't leak into the PTY as literals. | ||
| static func sanitizeExternalCommittedText(_ text: String) -> String { | ||
| let bytes = Array(text.utf8) | ||
| guard !bytes.isEmpty else { return text } | ||
|
|
||
| var index = 0 | ||
| while index < bytes.count { | ||
| let byte = bytes[index] | ||
| if byte == 0x1B { | ||
| index = consumeLeadingEscapeSequence(in: bytes, from: index) | ||
| continue | ||
| } | ||
|
|
||
| if byte == 0x9B { | ||
| index = consumeLeadingCSISequence(in: bytes, from: index + 1) | ||
| continue | ||
|
coderabbitai[bot] marked this conversation as resolved.
Resolved
|
||
| } | ||
|
|
||
| if byte < 0x20 || byte == 0x7F { | ||
| index += 1 | ||
| continue | ||
| } | ||
|
|
||
| break | ||
| } | ||
|
|
||
| if index == 0 { | ||
| return text | ||
| } | ||
|
|
||
| guard index < bytes.count else { return "" } | ||
| return String(decoding: bytes[index...], as: UTF8.self) | ||
| } | ||
|
|
||
| private static func consumeLeadingEscapeSequence( | ||
| in bytes: [UInt8], | ||
| from start: Int | ||
| ) -> Int { | ||
| let next = start + 1 | ||
| guard next < bytes.count else { return bytes.count } | ||
|
|
||
| switch bytes[next] { | ||
| case 0x5B: | ||
| // CSI: ESC [ ... final | ||
| return consumeLeadingCSISequence(in: bytes, from: next + 1) | ||
| case 0x4F: | ||
| // SS3: ESC O final | ||
| return min(bytes.count, next + 2) | ||
| case 0x50, 0x5D, 0x5E, 0x5F: | ||
| // DCS/OSC/PM/APC: consume until BEL/ST or EOF. | ||
| return consumeLeadingEscapedStringSequence(in: bytes, from: next + 1) | ||
| default: | ||
| // Single-character escape. | ||
| return min(bytes.count, next + 1) | ||
| } | ||
|
Comment on lines
+10007
to
+10013
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
The Because In practice dictation tools do not inject nF sequences, so this shouldn't affect the Speakly fix. Worth a comment or a narrow guard to prevent silent partial strips if the sanitizer is extended later: case 0x20...0x2F:
// nF sequences: intermediate + final. Consume through the final byte (0x20–0x7E).
var i = next + 1
while i < bytes.count, (0x20...0x2F).contains(bytes[i]) { i += 1 }
return i < bytes.count ? i + 1 : i |
||
| } | ||
|
|
||
| private static func consumeLeadingCSISequence( | ||
| in bytes: [UInt8], | ||
| from start: Int | ||
| ) -> Int { | ||
| var index = start | ||
| while index < bytes.count { | ||
| let byte = bytes[index] | ||
| if (0x20...0x3F).contains(byte) { | ||
| index += 1 | ||
| continue | ||
| } | ||
|
|
||
| if (0x40...0x7E).contains(byte) { | ||
| return index + 1 | ||
| } | ||
|
|
||
| break | ||
| } | ||
|
|
||
| return index | ||
| } | ||
|
|
||
| private static func consumeLeadingEscapedStringSequence( | ||
| in bytes: [UInt8], | ||
| from start: Int | ||
| ) -> Int { | ||
| var index = start | ||
| while index < bytes.count { | ||
| let byte = bytes[index] | ||
| if byte == 0x07 { | ||
| return index + 1 | ||
| } | ||
|
|
||
| if byte == 0x1B { | ||
| let next = index + 1 | ||
| if next < bytes.count, bytes[next] == 0x5C { | ||
| return next + 1 | ||
| } | ||
| return index | ||
| } | ||
|
|
||
| if byte < 0x20 || byte == 0x7F { | ||
| return index + 1 | ||
| } | ||
|
|
||
| index += 1 | ||
| } | ||
|
|
||
| return bytes.count | ||
| } | ||
|
coderabbitai[bot] marked this conversation as resolved.
Resolved
|
||
|
|
||
| func hasMarkedText() -> Bool { | ||
| return markedText.length > 0 | ||
| } | ||
|
|
@@ -10113,8 +10220,25 @@ extension GhosttyNSView: NSTextInputClient { | |
| return | ||
| } | ||
|
|
||
| let sanitizedChars = if NSApp.currentEvent == nil { | ||
| Self.sanitizeExternalCommittedText(chars) | ||
| } else { | ||
| chars | ||
| } | ||
|
|
||
| #if DEBUG | ||
| if sanitizedChars != chars { | ||
| dlog( | ||
| "ime.insertText.sanitized originalBytes=\(chars.utf8.count) " + | ||
| "sanitizedBytes=\(sanitizedChars.utf8.count)" | ||
| ) | ||
| } | ||
| #endif | ||
|
|
||
| guard !sanitizedChars.isEmpty else { return } | ||
|
|
||
| // Otherwise send directly to the terminal | ||
| sendTextToSurface(chars) | ||
| sendTextToSurface(sanitizedChars) | ||
|
coderabbitai[bot] marked this conversation as resolved.
Resolved
|
||
| } | ||
| } | ||
|
|
||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -870,6 +870,31 @@ final class CJKIMEKeyTextAccumulatorTests: XCTestCase { | |
| } | ||
| } | ||
|
|
||
| // MARK: - External committed-text sanitization | ||
|
|
||
| final class ExternalCommittedTextSanitizationTests: XCTestCase { | ||
| func testStripsLeadingCSISequenceFromExternalCommittedText() { | ||
| XCTAssertEqual( | ||
| GhosttyNSView.sanitizeExternalCommittedText("\u{1B}[Chello"), | ||
| "hello" | ||
| ) | ||
| } | ||
|
|
||
| func testStripsMultipleLeadingControlAndEscapeSequences() { | ||
| XCTAssertEqual( | ||
| GhosttyNSView.sanitizeExternalCommittedText("\u{1B}[1;5C\u{1B}OChello"), | ||
| "hello" | ||
| ) | ||
| } | ||
|
|
||
| func testLeavesLiteralBracketPrefixedTextUntouched() { | ||
| XCTAssertEqual( | ||
| GhosttyNSView.sanitizeExternalCommittedText("[Code] review"), | ||
| "[Code] review" | ||
| ) | ||
| } | ||
| } | ||
|
Comment on lines
+875
to
+914
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Per
Both the new |
||
|
|
||
| // MARK: - Shift+Space fallback suppression (IME source-switch shortcut) | ||
|
|
||
| final class CJKIMEShiftSpaceFallbackTests: XCTestCase { | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Swift's
Stringstores characters in UTF-8. The Unicode character U+009B (C1 CSI) encodes as the two-byte sequence0xC2 0x9B. When iterating overtext.utf8, byte index 0 will be0xC2, which is ≥0x20and not0x1B, so the outer loop'sbreakfires immediately before this branch is ever reached.The
0x9Bcheck would only trigger for ISO-8859-1 / Latin-1 raw bytes — not for valid SwiftStringinput.This is dead code on the committed-text path (no real-world dictation tool produces a bare
0x9Bvia the Cocoa text-input pipeline), but it could create a false sense of coverage. A quick comment documenting this limitation would help future readers, or the branch could be removed.