-
-
Notifications
You must be signed in to change notification settings - Fork 2.4k
Fixes #8676: Fix Chinese IME input in relation map #9140
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
Open
Dinis-Ou
wants to merge
1
commit into
TriliumNext:main
Choose a base branch
from
Dinis-Ou:bug-fix_8676
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+135
−1
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
121 changes: 121 additions & 0 deletions
121
apps/client/src/widgets/type_widgets/relation_map/RelationMap.spec.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,121 @@ | ||
| import { describe, it, expect, vi, beforeEach } from 'vitest' | ||
| import $ from 'jquery' | ||
| import utils from '../../../services/utils' | ||
| import dialog from '../../../services/dialog' | ||
|
|
||
| vi.mock('../../../services/utils', async (importOriginal) => { | ||
| const actual = await importOriginal() as any | ||
| return { | ||
| ...actual, | ||
| default: { | ||
| ...actual.default, | ||
| filterAttributeName: vi.fn((val: string) => val.replace(/[^a-z0-9]/gi, '')) | ||
| } | ||
| } | ||
| }) | ||
|
|
||
| vi.mock('../../../services/dialog', () => ({ | ||
| default: { | ||
| prompt: vi.fn() | ||
| } | ||
| })) | ||
|
|
||
| vi.mock('../../../services/attribute_autocomplete', () => ({ | ||
| default: { | ||
| initAttributeNameAutocomplete: vi.fn() | ||
| } | ||
| })) | ||
|
|
||
| vi.mock('../../../services/i18n', () => ({ | ||
| t: (key: string) => key | ||
| })) | ||
|
|
||
| // Call promptForRelationName and extract the $answer input element from the dialog's shown callback | ||
| async function getAnswerFromPrompt(): Promise<JQuery<HTMLInputElement>> { | ||
| const { promptForRelationName } = await import('./utils') | ||
|
|
||
| let $answer!: JQuery<HTMLInputElement> | ||
|
|
||
| vi.mocked(dialog.prompt).mockImplementation(({ shown }) => { | ||
| document.body.innerHTML = '<input type="text" />' | ||
| const input = document.querySelector('input') as HTMLInputElement | ||
| $answer = $(input) as JQuery<HTMLInputElement> | ||
| shown?.({ $answer }) | ||
| return Promise.resolve(null) | ||
| }) | ||
|
|
||
| promptForRelationName() | ||
| return $answer | ||
| } | ||
|
|
||
| describe('IME composition handling - Chinese input (promptForRelationName)', () => { | ||
| let input: HTMLInputElement | ||
| let $answer: JQuery<HTMLInputElement> | ||
|
|
||
| beforeEach(async () => { | ||
| vi.clearAllMocks() | ||
| $answer = await getAnswerFromPrompt() | ||
| input = $answer[0] as HTMLInputElement | ||
| }) | ||
|
|
||
| it('does not filter intermediate Chinese characters during composition', () => { | ||
| // user starts typing in Chinese IME | ||
| input.dispatchEvent(new Event('compositionstart')) | ||
|
|
||
| // intermediate IME states — these are pinyin keystrokes shown before final char | ||
| input.value = 'n' | ||
| input.dispatchEvent(new Event('input')) | ||
| input.value = 'ni' | ||
| input.dispatchEvent(new Event('input')) | ||
| input.value = 'nin' | ||
| input.dispatchEvent(new Event('input')) | ||
| input.value = 'ning' | ||
| input.dispatchEvent(new Event('input')) | ||
|
|
||
| expect(input.value).toBe('ning') | ||
| }) | ||
|
|
||
| it('filters invalid characters from input after IME composition ends', () => { | ||
| input.dispatchEvent(new Event('compositionstart')) | ||
|
|
||
| // intermediate pinyin | ||
| input.value = 'n' | ||
| input.dispatchEvent(new Event('input')) | ||
| input.value = 'ni' | ||
| input.dispatchEvent(new Event('input')) | ||
|
|
||
| // user selects the Chinese character 你 from the IME picker | ||
| input.value = '你' | ||
| input.dispatchEvent(new Event('compositionend')) | ||
|
|
||
| expect(input.value).toBe('') | ||
| }) | ||
|
|
||
| it('allows normal latin input after Chinese composition ends', () => { | ||
| // first do a Chinese composition | ||
| input.dispatchEvent(new Event('compositionstart')) | ||
| input.value = '你' | ||
| input.dispatchEvent(new Event('compositionend')) | ||
|
|
||
| // then type normally in latin | ||
| input.value = 'hello' | ||
| input.dispatchEvent(new Event('input')) | ||
|
|
||
| expect(input.value).toBe('hello') | ||
| }) | ||
|
|
||
| it('handles multiple Chinese characters in sequence', () => { | ||
| // first character | ||
| input.dispatchEvent(new Event('compositionstart')) | ||
| input.value = '你' | ||
| input.dispatchEvent(new Event('compositionend')) | ||
|
|
||
| // second character | ||
| input.dispatchEvent(new Event('compositionstart')) | ||
| input.value = '好' | ||
| input.dispatchEvent(new Event('compositionend')) | ||
|
|
||
| expect(input.value).toBe('') | ||
| }) | ||
| }); | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -432,3 +432,5 @@ function useRelationCreation({ mapApiRef, jsPlumbApiRef }: { mapApiRef: RefObjec | |
|
|
||
| return connectionCallback; | ||
| } | ||
|
|
||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
The mock implementation of
filterAttributeNamehere differs significantly from the actual implementation inapps/client/src/services/utils.ts.The mock (
val.replace(/[^a-z0-9]/gi, '')) strips all non-ASCII alphanumeric characters, including Chinese characters. However, the real implementation (name.replace(/[^\p{L}\p{N}_:]/gu, "")) allows Unicode letters (like '你') because of\p{L}.This discrepancy means the tests are not accurately reflecting the production behavior. For example, the test 'filters invalid characters from input after IME composition ends' expects '你' to be stripped, but it would be preserved in production. This could lead to unexpected behavior where invalid attribute names are created.
Please update the mock to match the real implementation, or if the intent is to disallow Unicode characters for relation names, the
filterAttributeNamefunction itself should be adjusted (though that might be a broader change). A consistent behavior between tests and production code is crucial.