Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
95 changes: 95 additions & 0 deletions mobile/src/terminal/terminal-ios-hangul-nfd-input.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
import { describe, expect, it } from 'vitest'
import {
buildTerminalLiveMirrorPayload,
computeTerminalLiveMirrorStep
} from './terminal-live-hangul-mirror'
import { normalizeTerminalTextInput } from './terminal-text-input-normalization'

// 두벌식 (2-set) composition states for 안녕하세요, as the field shows them
// keystroke by keystroke: ㅇ ㅏ ㄴ ㄴ ㅕ ㅇ ㅎ ㅏ ㅅ ㅔ ㅇ ㅛ.
const DUBEOLSIK_FIELD_STATES = [
'ㅇ',
'아',
'안',
'안ㄴ',
'안녀',
'안녕',
'안녕ㅎ',
'안녕하',
'안녕핫',
'안녕하세',
'안녕하셍',
'안녕하세요'
] as const

const DEL = '\x7f'

// Minimal PTY line model: DEL erases one code point, everything else appends.
function applyToPty(pty: string, payload: string): string {
let next = Array.from(pty)
for (const codePoint of Array.from(payload)) {
if (codePoint === DEL) {
next = next.slice(0, -1)
continue
}
next.push(codePoint)
}
return next.join('')
}

// Replays handleLiveInputChange → applyLiveInputMirror, then the settle-timer
// commit that flushes the last held syllable.
function replayLiveInput(fieldStates: readonly string[]): string {
let sentText = ''
let heldText = ''
let pty = ''

const step = (fieldText: string, commitHeld: boolean): void => {
const mirror = computeTerminalLiveMirrorStep(sentText, normalizeTerminalTextInput(fieldText), {
commitHeld
})
pty = applyToPty(pty, buildTerminalLiveMirrorPayload(mirror))
sentText = mirror.nextSentText
heldText = mirror.heldText
}

for (const fieldText of fieldStates) {
step(fieldText, false)
}
step(sentText + heldText, true)
return pty
}

describe('iOS decomposed (NFD) Hangul live input', () => {
it('Given an NFD field (iOS) When the user types 안녕하세요 Then the PTY receives composed syllables, not jamo', () => {
// Given: Apple text input hands React Native decomposed conjoining jamo, so
// one syllable arrives as 2-3 code points instead of one.
const nfdStates = DUBEOLSIK_FIELD_STATES.map((state) => state.normalize('NFD'))
expect(Array.from(nfdStates.at(-1) ?? '')).toHaveLength(12)

// When
const pty = replayLiveInput(nfdStates)

// Then: the mirror holds only the trailing code point, so without
// recomposition every leading jamo is committed on its own (#6995 iOS).
expect(Array.from(pty)).toHaveLength(5)
expect(pty).toBe('안녕하세요')
})

it('Given a precomposed NFC field (Android/Gboard) When the same keys are typed Then the PTY result is unchanged', () => {
// Given / When
const pty = replayLiveInput(DUBEOLSIK_FIELD_STATES)

// Then
expect(pty).toBe('안녕하세요')
})

it('Given NFD Latin diacritics When mirrored Then combining marks reach the PTY already composed', () => {
// Given / When: iOS also hands back NFD for Vietnamese/European accents.
const pty = replayLiveInput(['tie', 'tiế', 'tiếng'].map((state) => state.normalize('NFD')))

// Then
expect(pty).toBe('tiếng')
expect(Array.from(pty)).toHaveLength(5)
})
})
25 changes: 25 additions & 0 deletions mobile/src/terminal/terminal-text-input-normalization.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,29 @@ describe('normalizeTerminalTextInput', () => {
it('keeps ASCII hyphens unchanged', () => {
expect(normalizeTerminalTextInput('git checkout -- file')).toBe('git checkout -- file')
})

it('recomposes decomposed Hangul that iOS hands back as conjoining jamo', () => {
const decomposed = '안녕하세요'.normalize('NFD')
expect(Array.from(decomposed)).toHaveLength(12)

const normalized = normalizeTerminalTextInput(decomposed)

expect(normalized).toBe('안녕하세요')
expect(Array.from(normalized)).toHaveLength(5)
})

it('recomposes decomposed Latin diacritics and stays idempotent on composed text', () => {
expect(normalizeTerminalTextInput('tiếng'.normalize('NFD'))).toBe('tiếng')
expect(normalizeTerminalTextInput('안녕하세요')).toBe('안녕하세요')
expect(normalizeTerminalTextInput(normalizeTerminalTextInput('café'.normalize('NFD')))).toBe(
'café'
)
})

it('leaves compatibility forms alone so NFC never becomes a lossy NFKC fold', () => {
// NFKC would fold these into different characters and silently rewrite a command.
expect(normalizeTerminalTextInput('ㅇㅏㄴ')).toBe('ㅇㅏㄴ')
expect(normalizeTerminalTextInput('git')).toBe('git')
expect(normalizeTerminalTextInput('①')).toBe('①')
})
})
7 changes: 6 additions & 1 deletion mobile/src/terminal/terminal-text-input-normalization.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,11 @@
// value written back into the field, and that write-back kills iOS dictation (#7925).
const IOS_SMART_DASH_REPLACEMENT_PATTERN = /[\u2013\u2014]/g

// Why: Apple text input hands back decomposed (NFD) text, so one Hangul syllable
// arrives as 2-3 conjoining jamo. The live mirror holds only the trailing code
// point, so every leading jamo would commit on its own (#6995 iOS). NFC — never
// NFKC, which rewrites compatibility jamo and full-width forms — recomposes them
// before the PTY sees anything; it is a no-op for already-composed input.
Comment on lines +7 to +11

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

📐 Maintainability & Code Quality | 🟡 Minor | ⚡ Quick win

Correct and shorten this rationale comment.

Line 9 references #6995, but the PR objective identifies the affected issue as #11235. Reduce this to one concise rationale line while correcting or removing the ticket reference.

Proposed fix
-// Why: Apple text input hands back decomposed (NFD) text, so one Hangul syllable
-// arrives as 2-3 conjoining jamo. The live mirror holds only the trailing code
-// point, so every leading jamo would commit on its own (`#6995` iOS). NFC — never
-// NFKC, which rewrites compatibility jamo and full-width forms — recomposes them
-// before the PTY sees anything; it is a no-op for already-composed input.
+// NFC recomposes iOS NFD input; avoid NFKC to preserve compatibility forms (`#11235`).

As per coding guidelines, “Comments must be concise, limited to non-obvious information, and preferably one line.”

📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
// Why: Apple text input hands back decomposed (NFD) text, so one Hangul syllable
// arrives as 2-3 conjoining jamo. The live mirror holds only the trailing code
// point, so every leading jamo would commit on its own (#6995 iOS). NFC — never
// NFKC, which rewrites compatibility jamo and full-width forms — recomposes them
// before the PTY sees anything; it is a no-op for already-composed input.
// NFC recomposes iOS NFD input; avoid NFKC to preserve compatibility forms (`#11235`).

Source: Coding guidelines

export function normalizeTerminalTextInput(text: string): string {
return text.replace(IOS_SMART_DASH_REPLACEMENT_PATTERN, '--')
return text.normalize('NFC').replace(IOS_SMART_DASH_REPLACEMENT_PATTERN, '--')
}
Loading