-
Notifications
You must be signed in to change notification settings - Fork 13
fix(ip-input): retain cursor position when inserting characters #1349
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
base: main
Are you sure you want to change the base?
Changes from all commits
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 |
|---|---|---|
|
|
@@ -173,4 +173,46 @@ describe('SiIp6InputDirective', () => { | |
| }); | ||
| }); | ||
| }); | ||
|
|
||
| describe('cursor positioning', () => { | ||
| it('should retain cursor position when typing in middle of section', () => { | ||
| input.value = '2001:0d0'; | ||
| input.dispatchEvent(new InputEvent('input', { data: '0', inputType: 'insertText' })); | ||
| expect(input.value).toBe('2001:0D0'); | ||
| expect(input.selectionStart).toBe(8); | ||
| }); | ||
|
|
||
| it('should handle multiple section splits correctly', () => { | ||
| for (const c of '12345') { | ||
| input.value += c; | ||
| input.dispatchEvent(new InputEvent('input', { data: c, inputType: 'insertText' })); | ||
| } | ||
| expect(input.value).toBe('1234:5'); | ||
| expect(input.selectionStart).toBe(6); | ||
| }); | ||
|
|
||
| it('should retain cursor at end when typing at end', () => { | ||
| input.value = '2001:d'; | ||
| input.dispatchEvent(new InputEvent('input', { data: 'd', inputType: 'insertText' })); | ||
| expect(input.value).toBe('2001:D'); | ||
| expect(input.selectionStart).toBe(6); | ||
| }); | ||
|
|
||
| it('should retain cursor position with zero compression', () => { | ||
| input.value = '::1'; | ||
| input.dispatchEvent(new InputEvent('input', { data: '1', inputType: 'insertText' })); | ||
| expect(input.value).toBe('::1'); | ||
| expect(input.selectionStart).toBe(3); | ||
| }); | ||
|
|
||
| it('should handle cursor in CIDR section', () => { | ||
| component.cidr.set(true); | ||
| fixture.detectChanges(); | ||
|
|
||
| input.value = '2001:db8::1/64'; | ||
| input.dispatchEvent(new InputEvent('input', { data: '4', inputType: 'insertText' })); | ||
| expect(input.value).toBe('2001:DB8::1/64'); | ||
| expect(input.selectionStart).toBe(14); | ||
| }); | ||
| }); | ||
|
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 current tests for cursor positioning do not cover cases where a section split is triggered by inserting characters in the middle of a string. This scenario is where the it('should retain cursor position when section splits from middle insertion', () => {
// Simulate typing 'a' in '12345678' at position 4 -> '1234a|5678'
input.value = '1234a5678';
input.setSelectionRange(5, 5);
input.dispatchEvent(new InputEvent('input', { data: 'a', inputType: 'insertText' }));
expect(input.value).toBe('1234:A567:8');
expect(input.selectionStart).toBe(6);
}); |
||
| }); | ||
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 logic to calculate
cursorDeltais incorrect because it comparespos(an index in the rawinputstring) withcharsProcessed(an index within a cleaned sectionvalue). This can lead to incorrect cursor positioning when inserting characters in the middle of an IP address segment, asposandcharsProcessedare in different coordinate systems.To fix this, you need to determine the cursor's position relative to the hexadecimal characters within its section.
Here is a suggested approach:
In the first
forloop (lines 111-128), determine the cursor's position relative to the hex characters in its section and store it.Then, use this
cursorRelativePoshere to correctly calculatecursorDelta.Additionally, the new tests for cursor positioning in
si-ip6-input.directive.spec.tsonly cover appending characters. It would be beneficial to add a test case for inserting a character in the middle of a section to verify the fix and prevent future regressions.