Fix TypeError when selecting autocomplete on empty masked input#2872
Draft
Fix TypeError when selecting autocomplete on empty masked input#2872
Conversation
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
Owner
There was a problem hiding this comment.
Don't update this file as It will be updated by the build process automatically.
Owner
|
@copilot , |
Agent-Logs-Url: https://github.com/RobinHerbots/Inputmask/sessions/e240923d-85a5-4a06-88c2-5dacc20a1ae1 Co-authored-by: RobinHerbots <318447+RobinHerbots@users.noreply.github.com>
Contributor
Author
Owner
|
@copilot , Can you workout a testcase, so it is easier to test the changes. Ex a codepen link or simular. |
…ed input Agent-Logs-Url: https://github.com/RobinHerbots/Inputmask/sessions/17a08e56-7030-4315-86bd-458db7c45fcd Co-authored-by: RobinHerbots <318447+RobinHerbots@users.noreply.github.com>
Contributor
Author
Added a regression test in commit c781c07:
To run it manually you can also open |
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Selecting a browser autocomplete suggestion on an empty masked input throws
TypeError: Cannot read properties of undefined (reading 'length')insidekeyEvent.Root Cause
analyseChangesininputFallBackEventmisclassifies theinputevent as"deleteContentBackward"when:This causes
backPartto be front-padded with marker characters (~) to match buffer length. The leading marker innewBuffer[0]hits condition 4 in the comparison loop →action = "deleteContentBackward". The code then dispatches a synthetic Backspacekeydown, which cascades into the TypeError insidekeyEvent.Fix
In the
"deleteContentBackward"switch case ofinputFallBackEvent, guard against the contradiction wheree.inputTypesignals an insertion (e.g."insertReplacementText"from browser autocomplete) butanalyseChangesreturned a deletion. In that case, callapplyInputValuedirectly instead of dispatching the erroneous Backspace event.Legitimate
deleteContentBackwardevents (wheree.inputTypeis"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 aninputevent withinputType: "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-9999masked 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.