Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Mask: More updates from review - adresses comments from @jzaeffer

* make definitions test in the options.
* remove return true from _caretSelect
* check out backspace/delete see if i can remove double assign/ternary
  • Loading branch information...
commit 27e229a6b1f22c90f61517633ae8946bed192df5 1 parent f35821b
@gnarf gnarf authored
View
94 tests/unit/mask/mask_core.js
@@ -2,6 +2,16 @@
module( "mask: core" );
+test( "masked inputs get the '.ui-mask' class", function() {
+ expect( 3 );
+ var input = $( "#mask1" );
+ ok( !input.is( ".ui-mask" ), "Input is not masked" );
+ input.mask({ mask: "999" });
+ ok( input.is( ".ui-mask" ), "Input is now masked" );
+ input.mask( "destroy" );
+ ok( !input.is( ".ui-mask" ), "destroy clears masked class" );
+});
+
test( "_caret() can move and read the text cursor", function() {
expect( 3 );
@@ -30,90 +40,6 @@ test( "_caret() can move and read the text cursor", function() {
}, "Caret position set to 0, 2 results in 0, 2" );
});
-test( "Mask Parsed Properly", function() {
- var defs = {
- hh: function( value ) {
- value = parseInt( value, 10 );
- if ( value >= 1 || value <= 12 ) {
- return ( value < 10 ? "0" : "" ) + value;
- }
- },
- ss: function( value ) {
- value = parseInt( value, 10 );
- if ( value >= 0 || value <= 59 ) {
- return ( value < 10 ? "0" : "" ) + value;
- }
- }
- },
- input = $( "#mask1" ).mask({
- mask: "hh:ss:ss.999",
- definitions: defs
- }),
- instance = input.data( "mask" );
- deepEqual( instance.buffer, [
- {
- valid: defs.hh,
- start: 0,
- length: 2
- },
- {
- valid: defs.hh,
- start: 0,
- length: 2
- },
- {
- literal: ":",
- start: 2,
- length: 1
- },
- {
- valid: defs.ss,
- start: 3,
- length: 2
- },
- {
- valid: defs.ss,
- start: 3,
- length: 2
- },
- {
- literal: ":",
- start: 5,
- length: 1
- },
- {
- valid: defs.ss,
- start: 6,
- length: 2
- },
- {
- valid: defs.ss,
- start: 6,
- length: 2
- },
- {
- literal: ".",
- start: 8,
- length: 1
- },
- {
- valid: instance.options.definitions[ 9 ],
- start: 9,
- length: 1
- },
- {
- valid: instance.options.definitions[ 9 ],
- start: 10,
- length: 1
- },
- {
- valid: instance.options.definitions[ 9 ],
- start: 11,
- length: 1
- }
- ], "Buffer Calculated correctly" );
-});
-
test( "Parsing initial value skips literals", function() {
expect( 2 );
var input = $( "#mask1" );
View
4 tests/unit/mask/mask_events.js
@@ -126,7 +126,7 @@ test( "keypress: Typing with multiple values higlighted", function() {
deepEqual( mask._caret(), { begin: 6, end: 6 }, "Caret position correct");
});
-test( "keypress: Typing with multi-byte fields", function() {
+test( "keypress: Typing with multi-character fields", function() {
expect( 5 );
var input = $( "#mask1" ).val("").mask({
mask: "aa-aa-aa",
@@ -150,7 +150,7 @@ test( "keypress: Typing with multi-byte fields", function() {
deepEqual( mask._caret(), { begin: 3, end: 5 }, "Caret position correct");
});
-test( "keypress: Typing with multi-byte only accepts valid values", function() {
+test( "keypress: Typing with multi-character only accepts valid values", function() {
expect( 7 );
var input = $( "#mask1" ).val("").mask({
mask: "aa-aa-aa",
View
84 tests/unit/mask/mask_options.js
@@ -45,4 +45,88 @@ test( "mask", function() {
equal( input.val(), "(123)4__-____", "Mask changed" );
});
+test( "mask option parser", function() {
+ var defs = {
+ hh: function( value ) {
+ value = parseInt( value, 10 );
+ if ( value >= 1 || value <= 12 ) {
+ return ( value < 10 ? "0" : "" ) + value;
+ }
+ },
+ ss: function( value ) {
+ value = parseInt( value, 10 );
+ if ( value >= 0 || value <= 59 ) {
+ return ( value < 10 ? "0" : "" ) + value;
+ }
+ }
+ },
+ input = $( "#mask1" ).mask({
+ mask: "hh:ss:ss.999",
+ definitions: defs
+ }),
+ instance = input.data( "mask" );
+ deepEqual( instance.buffer, [
+ {
+ valid: defs.hh,
+ start: 0,
+ length: 2
+ },
+ {
+ valid: defs.hh,
+ start: 0,
+ length: 2
+ },
+ {
+ literal: ":",
+ start: 2,
+ length: 1
+ },
+ {
+ valid: defs.ss,
+ start: 3,
+ length: 2
+ },
+ {
+ valid: defs.ss,
+ start: 3,
+ length: 2
+ },
+ {
+ literal: ":",
+ start: 5,
+ length: 1
+ },
+ {
+ valid: defs.ss,
+ start: 6,
+ length: 2
+ },
+ {
+ valid: defs.ss,
+ start: 6,
+ length: 2
+ },
+ {
+ literal: ".",
+ start: 8,
+ length: 1
+ },
+ {
+ valid: instance.options.definitions[ 9 ],
+ start: 9,
+ length: 1
+ },
+ {
+ valid: instance.options.definitions[ 9 ],
+ start: 10,
+ length: 1
+ },
+ {
+ valid: instance.options.definitions[ 9 ],
+ start: 11,
+ length: 1
+ }
+ ], "Buffer correctly parsed" );
+});
+
}( jQuery ) );
View
15 ui/jquery.ui.mask.js
@@ -111,7 +111,6 @@ $.widget( "ui.mask", {
var bufferObject = this.buffer[ bufferPosition ];
if ( bufferObject && bufferObject.length > 1 ) {
this._caret( bufferObject.start, bufferObject.start + bufferObject.length );
- return true;
} else {
this._caret( bufferPosition );
}
@@ -177,11 +176,17 @@ $.widget( "ui.mask", {
case keyCode.BACKSPACE:
case keyCode.DELETE:
event.preventDefault();
+
+ // 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 ) {
- position.begin = position.end = ( key === keyCode.DELETE ?
- this._seekRight( position.begin - 1) :
- this._seekLeft( position.begin )
- );
+ if ( key === keyCode.DELETE ) {
+ position.begin = position.end = this._seekRight( position.begin - 1 );
+ } else {
+ position.begin = position.end = this._seekLeft( position.begin );
+ }
+
+ // nothing to backspace
if ( position.begin < 0 ) {
this._caret( this._seekRight( -1 ) );
return;
Please sign in to comment.
Something went wrong with that request. Please try again.