Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Mask: Adding refresh method, Adding unit tests for mask/placeholder o…

…ption, backspace & delete behavior.
  • Loading branch information...
commit d86229da692c4e98c16356ec5c23398b0276bb1b 1 parent e30e09d
@gnarf gnarf authored
View
37 tests/unit/mask/mask_events.js
@@ -2,4 +2,41 @@
module( "mask: events" );
+test( "keydown: delete and backspace behaviors", function() {
+ expect( 11 );
+ var input = $( "#mask1" ).val( "1234" ).mask({ mask: "99/99/99" }),
+ mask = input.data( "mask" );
+
+ equal( input.val(), "12/34/__", "Initial Value" );
+ mask._caret( 0 );
+ input.simulate( "keydown", { keyCode: $.ui.keyCode.DELETE });
+ equal( input.val(), "23/4_/__", "Deleted first value" );
+ mask._caret( 2 );
+ input.simulate( "keydown", { keyCode: $.ui.keyCode.DELETE });
+ equal( input.val(), "23/__/__", "Deleted value after literal" );
+
+ input.val( "123456" ).mask( "refresh" );
+ equal( input.val(), "12/34/56", "New Initial Value" );
+ mask._caret( 2, 4 );
+ input.simulate( "keydown", { keyCode: $.ui.keyCode.DELETE });
+ equal( input.val(), "12/56/__", "Deleted 34 out of the middle" );
+ deepEqual( mask._caret(), { begin: 2, end: 2 }, "Caret position" );
+
+ input.val( "123456" ).mask( "refresh" );
+ equal( input.val(), "12/34/56", "New Initial Value" );
+ mask._caret( 2, 4 );
+ input.simulate( "keydown", { keyCode: $.ui.keyCode.BACKSPACE });
+ equal( input.val(), "12/56/__", "Backspaced 34 out of the middle" );
+ deepEqual( mask._caret(), { begin: 2, end: 2 }, "Caret position" );
+
+ mask._caret( 6 );
+ input.simulate( "keydown", { keyCode: $.ui.keyCode.BACKSPACE });
+ equal( input.val(), "12/5_/__", "Backspaced last value" );
+
+ mask._caret( 2 );
+ input.simulate( "keydown", { keyCode: $.ui.keyCode.BACKSPACE });
+ equal( input.val(), "15/__/__", "Backspaced value from middle" );
+
+});
+
}( jQuery ) );
View
27 tests/unit/mask/mask_options.js
@@ -2,4 +2,31 @@
module( "mask: options" );
+test( "placeholder", function() {
+ expect( 2 );
+ var input = $( "#mask1" ).mask({
+ mask: "99/99/99",
+ placeholder: "_"
+ });
+
+ equal( input.val(), "__/__/__", "Initial value" );
+ input.mask( "option", "placeholder", " " );
+ equal( input.val(), " / / ", "Placeholder changed" );
+
+});
+
+test( "mask", function() {
+ expect( 2 );
+ var input = $( "#mask1" ).val("1234").mask({
+ mask: "99/99/99",
+ placeholder: "_"
+ });
+
+ equal( input.val(), "12/34/__", "Initial value" );
+ input.mask( "option", "mask", "(999)999-9999" );
+ equal( input.val(), "(123)4__-____", "Mask changed" );
+
+});
+
+
}( jQuery ) );
View
22 ui/jquery.ui.mask.js
@@ -31,6 +31,11 @@ $.widget( "ui.mask", {
this._keyBinding();
},
+ refresh: function() {
+ this._parseValue();
+ this._paint();
+ },
+
_setOption: function( key, value ) {
this._super( "_setOption", key, value );
if ( key === "mask" ) {
@@ -126,7 +131,7 @@ $.widget( "ui.mask", {
lastUnsavedValue = elem.val();
},
keydown: function( event ) {
- var key = event.which,
+ var key = event.keyCode,
position = that._caret();
if ( key === keyCode.ESCAPE ) {
@@ -136,15 +141,20 @@ $.widget( "ui.mask", {
}
if ( key === keyCode.BACKSPACE || key === keyCode.DELETE ) {
+ event.preventDefault();
if ( position.begin == position.end ) {
- position.begin = position.end = Math.max( that._seekRight( -1 ), that[ key === keyCode.BACKSPACE ?
- "_seekLeft" :
- "_seekRight" ]( position.begin ));
+ position.begin = position.end = ( key === keyCode.DELETE ?
+ that._seekRight( position.begin - 1) :
+ that._seekLeft( position.begin )
+ );
+ if ( position.begin < 0 ) {
+ that._caret( that.seekLeft( -1 ) );
+ return;
+ }
}
that._removeValues( position.begin, position.end );
that._paint();
that._caret( position.begin );
- event.preventDefault();
}
},
keypress: function( event ) {
@@ -253,7 +263,7 @@ $.widget( "ui.mask", {
delete bufferObject.value;
}
}
- this._shiftLeft( begin, end );
+ this._shiftLeft( begin, end + 1 );
return this;
},
Please sign in to comment.
Something went wrong with that request. Please try again.