Skip to content

Fix cursor position for single-digit integer in currency mask (x.xx case)#2875

Draft
Copilot wants to merge 2 commits into5.xfrom
copilot/fix-cursor-position-currency-mask
Draft

Fix cursor position for single-digit integer in currency mask (x.xx case)#2875
Copilot wants to merge 2 commits into5.xfrom
copilot/fix-cursor-position-currency-mask

Conversation

Copy link
Copy Markdown
Contributor

Copilot AI commented Mar 31, 2026

When a currency input has a single integer digit (e.g. "1.50"), clicking anywhere in the field positions the cursor incorrectly (jumps to end), while "0.xx" (radixFocus snap) and "xx.xx" (two+ digits, lvp) both behave correctly.

Root Cause

In lib/positioning.js, doRadixFocus iterates validPositions using vps.length as the loop bound:

for (let vp = 0, vpl = vps.length; vp < vpl; vp++) {
  if (vps[vp] && radixPos < vp && vps[vp].input !== getPlaceholder(...)) {
    return false;
  }
}
return true; // incorrectly returns true → radixFocus snaps caret to wrong position

After stripLeadingZeroes splices entries from the sparse validPositions array, vps.length can be smaller than the actual highest-indexed integer digit position. The loop never reaches the integer digit, doRadixFocus incorrectly returns true, and seekNext(radixPos) with numericInput resolves to position 0 — which translatePosition maps to the display end.

Fix

Switch the loop from index-based (vps.length bound) to for...in over the sparse array, so all defined positions are checked regardless of array length anomalies from splicing:

for (const vp in vps) {
  const vpInt = parseInt(vp);
  if (vps[vp] && radixPos < vpInt && vps[vp].input !== getPlaceholder.call(inputmask, vpInt)) {
    return false;
  }
}

This ensures doRadixFocus correctly returns false when a non-placeholder integer digit exists, falling through to the lvp path that places the caret at the clicked position.

Copilot AI linked an issue Mar 31, 2026 that may be closed by this pull request
Copilot AI changed the title [WIP] Fix cursor position issue on currency mask input Fix cursor position for single-digit integer in currency mask (x.xx case) Mar 31, 2026
Copilot AI requested a review from RobinHerbots March 31, 2026 14:06
Copy link
Copy Markdown
Owner

@RobinHerbots RobinHerbots left a comment

Choose a reason for hiding this comment

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

@copilot ,

No code changes?

Copy link
Copy Markdown
Contributor Author

Copilot AI commented Apr 7, 2026

@copilot ,

No code changes?

Fixed in 0b9ce8f. The change is in lib/positioning.js — the doRadixFocus loop now uses for...in to correctly iterate over the sparse validPositions array instead of the index-based loop bounded by vps.length. After stripLeadingZeroes splices entries, vps.length can be smaller than the actual position index of the single integer digit, so the old loop would never reach it — causing doRadixFocus to incorrectly return true and snap the caret to the radix point position instead of the clicked position.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Cursor position on currency mask

2 participants