Permalink
Browse files

Mask: Handling overtyping multiple values better - rewrote shiftRight…

… function to make more sense
  • Loading branch information...
1 parent 548ccae commit 3ac0bb4243ad4cd6f086f4a8e7344acc5cc0e030 @gnarf gnarf committed Aug 2, 2011
Showing with 43 additions and 12 deletions.
  1. +28 −0 tests/unit/mask/mask_events.js
  2. +15 −12 ui/jquery.ui.mask.js
View
28 tests/unit/mask/mask_events.js
@@ -88,6 +88,34 @@ test( "keydown: Backspace with the cursor to the right of a mask literal", funct
deepEqual( mask._caret(), { begin: 0, end: 0 }, "Caret position correct");
});
+test( "keydown: Backspace with multiple values higlighted", function() {
+ expect( 3 );
+ var input = $( "#mask1" ).val("1234567890").mask({ mask: "(999)999-9999" }),
+ mask = input.data( "mask" );
+
+ input.focus();
+ equal( input.val(), "(123)456-7890", "Initial Value Expected" );
+
+ mask._caret( 5, 8 );
+ input.simulate( "keydown", { keyCode: $.ui.keyCode.BACKSPACE });
+ equal( input.val(), "(123)789-0___", "Deleted three highlighted values, pulled values from right" );
+ deepEqual( mask._caret(), { begin: 5, end: 5 }, "Caret position correct");
+});
+
+test( "keypress: Typing with multiple values higlighted", function() {
+ expect( 3 );
+ var input = $( "#mask1" ).val("1234567890").mask({ mask: "(999)999-9999" }),
+ mask = input.data( "mask" );
+
+ input.focus();
+ equal( input.val(), "(123)456-7890", "Initial Value Expected" );
+
+ mask._caret( 5, 8 );
+ input.simulate( "keypress", { keyCode: "0".charCodeAt( 0 ) });
+ equal( input.val(), "(123)078-90__", "Deleted three highlighted values, pulled values from right" );
+ deepEqual( mask._caret(), { begin: 6, end: 6 }, "Caret position correct");
+});
+
test( "keydown: Delete pulling values", function() {
expect( 18 );
var input = $( "#mask1" ).val("123").mask({ mask: "9-99" }),
View
27 ui/jquery.ui.mask.js
@@ -212,7 +212,7 @@ $.widget( "ui.mask", {
if ( event.metaKey || event.altKey || event.ctrlKey || key < 32 ) {
return;
}
- if ( position.begin != position.end ) {
+ if ( position.begin !== position.end ) {
that._removeValues( position.begin, position.end );
}
if ( bufferObject ) {
@@ -282,20 +282,23 @@ $.widget( "ui.mask", {
},
_shiftRight: function ( bufferPosition ) {
var bufferObject,
- destPosition = this._seekRight( bufferPosition ),
- destObject,
+ temp,
+ shiftingValue = false,
bufferLength = this.buffer.length;
- for ( ; bufferPosition < bufferLength ; bufferPosition += bufferObject.length ) {
+ bufferPosition--;
+ while ( ( bufferPosition = this._seekRight( bufferPosition ) ) < bufferLength )
+ {
bufferObject = this.buffer[ bufferPosition ];
- if ( bufferObject.valid ) {
- if ( destPosition < bufferLength ) {
- destObject = this.buffer[ destPosition ];
- if ( this._validValue( destObject, bufferObject.value ) ) {
- destObject.value = bufferObject.value;
- destPosition = this._seekRight( destPosition );
- }
- delete bufferObject.value;
+ if ( shiftingValue === false ) {
+ shiftingValue = bufferObject.value;
+ } else {
+ if ( this._validValue( bufferObject, shiftingValue ) ) {
+ temp = bufferObject.value;
+ bufferObject.value = shiftingValue;
+ shiftingValue = temp;
+ } else {
+ return;
}
}
}

0 comments on commit 3ac0bb4

Please sign in to comment.