Skip to content
Browse files

Mask: Handle backspacing when in an already deleted field

  • Loading branch information...
1 parent 76db764 commit 371146999b9465f3538f7a97bf3e1b8e05f4be79 @gnarf gnarf committed Aug 7, 2012
Showing with 54 additions and 1 deletion.
  1. +38 −0 tests/unit/mask/mask_events.js
  2. +16 −1 ui/jquery.ui.mask.js
View
38 tests/unit/mask/mask_events.js
@@ -184,6 +184,44 @@ test( "keypress: Typing with multi-character only accepts valid values", functio
deepEqual( mask._caret(), { begin: 6, end: 8 }, "Caret position correct");
});
+test( "keypress: Backspace with multi-character ", 6, function() {
+ var input = $( "#mask1" ).val( "aa-bb-cc" ).mask({
+ mask: "aa-aa-aa",
+ definitions: {
+ aa: function( value ) {
+ if ( !value.length ) {
+ return;
+ }
+ if ( value.charAt( 0 ) === "_" ) {
+ return;
+ }
+ if ( value.length === 1 ) {
+ return value+value;
+ }
+ if ( value.charAt( 0 ) === value.charAt( 1 ) ) {
+ return value;
+ }
+ }
+ }
+ }),
+ mask = input.data( "mask" );
+
+ TestHelpers.focus( input );
+ equal( input.val(), "aa-bb-cc", "Initial Value Expected" );
+ mask._caret( 6, 8 );
+
+ deepEqual( mask._caret(), { begin: 6, end: 8 }, "Caret position correct");
+
+ input.simulate( "keydown", { keyCode: $.ui.keyCode.BACKSPACE });
+ equal( input.val(), "aa-bb-__", "Deleted Value Expected" );
+ deepEqual( mask._caret(), { begin: 6, end: 8 }, "Caret position correct");
+
+ input.simulate( "keydown", { keyCode: $.ui.keyCode.BACKSPACE });
+ equal( input.val(), "aa-__-__", "Deleted Value Expected" );
+ deepEqual( mask._caret(), { begin: 3, end: 5 }, "Caret position correct");
+
+});
+
test( "keydown: Delete pulling values", function() {
expect( 18 );
var input = $( "#mask1" ).val("123").mask({ mask: "9-99" }),
View
17 ui/jquery.ui.mask.js
@@ -197,7 +197,7 @@ $.widget( "ui.mask", {
// if the caret is not "selecting" values, we need to find the proper
// character in the buffer to delete/backspace over.
- if ( position.begin === position.end ) {
+ if ( position.begin === position.end || this._isEmpty( position.begin, position.end ) ) {
if ( key === keyCode.DELETE ) {
position.begin = position.end = this._seekRight( position.begin - 1 );
} else {
@@ -277,6 +277,21 @@ $.widget( "ui.mask", {
paste: "_paste",
input: "_paste"
},
+ _isEmpty: function( begin, end ) {
+ var index;
+ if ( begin === undefined ) {
+ begin = 0;
+ end = this.buffer.length - 1;
+ } else if ( end === undefined ) {
+ end = begin;
+ }
+ for ( index = begin; index <= end; index++ ) {
+ if ( this.buffer[ index ] && this.buffer[ index ].value ) {
+ return false;
+ }
+ }
+ return true;
+ },
_paste: function(event) {
this._delay( function() {
var position = this._parseValue();

0 comments on commit 3711469

Please sign in to comment.
Something went wrong with that request. Please try again.