Skip to content
Browse files

Mask: First draft of parsing the mask into a 'buffer'

  • Loading branch information...
1 parent 9dacfd1 commit deb60238d1e3e34d5cc431aa694b71e14187f55f @gnarf gnarf committed Jul 29, 2011
Showing with 130 additions and 1 deletion.
  1. +84 −0 tests/unit/mask/mask_core.js
  2. +5 −0 tests/unit/mask/mask_defaults.js
  3. +41 −1 ui/jquery.ui.mask.js
View
84 tests/unit/mask/mask_core.js
@@ -28,4 +28,88 @@ 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" );
+});
+
}( jQuery ) );
View
5 tests/unit/mask/mask_defaults.js
@@ -1,5 +1,10 @@
commonWidgetTests( "mask", {
defaults: {
+ definitions: {
+ '9': /[0-9]/,
+ 'a': /[A-Za-z]/,
+ '*': /[A-Za-z0-9]/
+ },
disabled: false,
mask: null,
placeholder: "_",
View
42 ui/jquery.ui.mask.js
@@ -13,11 +13,16 @@ $.widget( "ui.mask", {
version: "@VERSION",
defaultElement: "<input>",
options: {
+ definitions: {
+ '9': /[0-9]/,
+ 'a': /[A-Za-z]/,
+ '*': /[A-Za-z0-9]/
+ },
mask: null,
placeholder: "_"
},
_create: function() {
-
+ this._parseMask();
},
// helper function to get or set position of text cursor (caret)
@@ -56,6 +61,41 @@ $.widget( "ui.mask", {
end: end
};
}
+ },
+ _parseMask: function() {
+ var key, x, bufferObject,
+ index = -1,
+ options = this.options,
+ mask = options.mask ;
+
+ this.buffer = [];
+ if ( !mask ) {
+ return;
+ }
+ // search for definied "masks"
+ for ( key in options.definitions ) {
+ while ( ( index = mask.indexOf( key, index + 1 ) ) > -1 ) {
+ bufferObject = {
+ start: index,
+ length: key.length,
+ valid: options.definitions[ key ]
+ };
+ for ( x = index ; x < index + key.length ; x++ ) {
+ this.buffer[ x ] = bufferObject;
+ }
+ }
+ }
+
+ // anything we didn't find is a literal
+ for ( index = 0 ; index < mask.length ; index++ ) {
+ if ( !this.buffer[ index ] ) {
+ this.buffer[ index ] = {
+ start: index,
+ literal: mask.charAt( index ),
+ length: 1
+ };
+ }
+ }
}
});

0 comments on commit deb6023

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