Skip to content

Fix TypeError when selecting autocomplete on empty masked input#2872

Draft
Copilot wants to merge 4 commits into5.xfrom
copilot/fix-auto-complete-select-error
Draft

Fix TypeError when selecting autocomplete on empty masked input#2872
Copilot wants to merge 4 commits into5.xfrom
copilot/fix-auto-complete-select-error

Conversation

Copy link
Copy Markdown
Contributor

Copilot AI commented Mar 28, 2026

Selecting a browser autocomplete suggestion on an empty masked input throws TypeError: Cannot read properties of undefined (reading 'length') inside keyEvent.

Root Cause

analyseChanges in inputFallBackEvent misclassifies the input event as "deleteContentBackward" when:

  • caret is at position 0 (browser behavior after autofill in some cases)
  • mask template buffer is longer than the autocomplete value

This causes backPart to be front-padded with marker characters (~) to match buffer length. The leading marker in newBuffer[0] hits condition 4 in the comparison loop → action = "deleteContentBackward". The code then dispatches a synthetic Backspace keydown, which cascades into the TypeError inside keyEvent.

Fix

In the "deleteContentBackward" switch case of inputFallBackEvent, guard against the contradiction where e.inputType signals an insertion (e.g. "insertReplacementText" from browser autocomplete) but analyseChanges returned a deletion. In that case, call applyInputValue directly instead of dispatching the erroneous Backspace event.

case "deleteContentBackward":
  if (e.inputType && e.inputType.startsWith("insert")) {
    // analyseChanges misdetected deletion due to caret/buffer length mismatch
    applyInputValue(input, inputValue);
    caret.call(inputmask, input, caretPos.begin, caretPos.end, true);
  } else {
    var keydown = new $.Event("keydown");
    keydown.key = keys.Backspace;
    EventHandlers.keyEvent.call(input, keydown);
  }
  break;

Legitimate deleteContentBackward events (where e.inputType is "deleteContentBackward" or absent) are unaffected.

Test Case

A regression test has been added to the QUnit suite to verify the fix:

  • qunit/simulator.js – new $.fn.autocomplete(value, caretBegin, caretEnd) helper that sets the native input value and fires an input event with inputType: "insertReplacementText", matching real browser autocomplete behaviour.
  • qunit/tests_inputeventonly.js – new test "(999) 999-9999 - autocomplete on empty input (insertReplacementText)" that simulates selecting an autocomplete suggestion on a fresh empty (999) 999-9999 masked input (caret at position 0) and asserts both that no error is thrown and that the value is correctly formatted as (123) 123-1234.

🔒 GitHub Advanced Security automatically protects Copilot coding agent pull requests. You can protect all pull requests by enabling Advanced Security for your repositories. Learn more about Advanced Security.

Copilot AI linked an issue Mar 28, 2026 that may be closed by this pull request
Agent-Logs-Url: https://github.com/RobinHerbots/Inputmask/sessions/067db837-52f8-443c-9ef1-1c0a979dfe97

Co-authored-by: RobinHerbots <318447+RobinHerbots@users.noreply.github.com>
Copilot AI changed the title [WIP] Fix auto-complete select error with empty input Fix TypeError when selecting autocomplete on empty masked input Mar 28, 2026
Copilot AI requested a review from RobinHerbots March 28, 2026 22:43
Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

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

Don't update this file as It will be updated by the build process automatically.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Reverted in 30f5dbf.

@RobinHerbots
Copy link
Copy Markdown
Owner

@copilot ,
Exclude updates in the dist folder. Only keep updates in the lib folder and the changelog

Copy link
Copy Markdown
Contributor Author

Copilot AI commented Mar 31, 2026

@copilot ,
Exclude updates in the dist folder. Only keep updates in the lib folder and the changelog

Done in 30f5dbf — dist files and inputmask-pages/src/assets/Changelog.md have been removed from the PR. Only lib/eventhandlers.js and the root Changelog.md are changed.

@RobinHerbots
Copy link
Copy Markdown
Owner

@copilot ,

Can you workout a testcase, so it is easier to test the changes. Ex a codepen link or simular.

Copy link
Copy Markdown
Contributor Author

Copilot AI commented Mar 31, 2026

@copilot ,

Can you workout a testcase, so it is easier to test the changes. Ex a codepen link or simular.

Added a regression test in commit c781c07:

  • qunit/simulator.js – new $.fn.autocomplete(value, caretBegin, caretEnd) helper that sets the native value and fires an input event with inputType: "insertReplacementText", matching real browser autocomplete behaviour.
  • qunit/tests_inputeventonly.js – new test "(999) 999-9999 - autocomplete on empty input (insertReplacementText)" that applies the helper to a fresh empty masked input (caret at 0) and asserts both that no error is thrown and that the value is formatted correctly as (123) 123-1234.

To run it manually you can also open qunit/qunit.html in a browser and use a local server (the test will appear under the inputEventOnly: true module).

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.

auto-complete select error

2 participants