1 module( 'jquery.byteLimit' );
3 test( '-- Initial check', function() {
5 ok( $.fn
.byteLimit
, 'jQuery.fn.byteLimit defined' );
8 // Basic sendkey-implementation
9 $.addChars = function( $input
, charstr
) {
10 var len
= charstr
.length
;
11 for ( var i
= 0; i
< len
; i
++ ) {
12 // Keep track of the previous value
13 var prevVal
= $input
.val();
16 var code
= charstr
.charCodeAt(i
);
18 // Trigger event and undo if prevented
19 var event
= new jQuery
.Event( 'keypress', { keyCode
: code
, which
: code
, charCode
: code
} );
20 $input
.trigger( event
);
21 if ( !event
.isDefaultPrevented() ) {
22 $input
.val( prevVal
+ charstr
.charAt(i
) );
28 * Test factory for $.fn.byteLimit
30 * @param $input {jQuery} jQuery object in an input element
31 * @param hasLimit {Boolean} Wether a limit should apply at all
32 * @param limit {Number} Limit (if used) otherwise undefined
33 * The limit should be less than 20 (the sample data's length)
35 var byteLimitTest = function( options
) {
45 test( opt
.description
, function() {
47 opt
.$input
.appendTo( 'body' );
49 // Simulate pressing keys for each of the sample characters
50 $.addChars( opt
.$input
, opt
.sample
);
51 var rawVal
= opt
.$input
.val(),
52 fn
= opt
.$input
.data( 'byteLimit-callback' ),
53 newVal
= $.isFunction( fn
) ? fn( rawVal
) : rawVal
;
58 ltOrEq( $.byteLength( newVal
), opt
.limit
, 'Prevent keypresses after byteLimit was reached, length never exceeded the limit' );
59 equal( $.byteLength( rawVal
), $.byteLength( opt
.expected
), 'Not preventing keypresses too early, length has reached the expected length' );
60 equal( rawVal
, opt
.expected
, 'New value matches the expected string' );
64 equal( newVal
, opt
.expected
, 'New value matches the expected string' );
65 equal( $.byteLength( newVal
), $.byteLength( opt
.expected
), 'Unlimited scenarios are not affected, expected length reached' );
73 // Simple sample (20 chars, 20 bytes)
74 simpleSample
= '12345678901234567890',
76 // 3 bytes (euro-symbol)
79 // Multi-byte sample (22 chars, 26 bytes)
80 mbSample
= '1234567890' + U_20AC
+ '1234567890' + U_20AC
;
83 description
: 'Plain text input',
84 $input
: $( '<input>' )
90 expected
: simpleSample
94 description
: 'Limit using the maxlength attribute',
95 $input
: $( '<input>' )
96 .attr( 'type', 'text' )
97 .prop( 'maxLength', '10' )
102 expected
: '1234567890'
106 description
: 'Limit using a custom value',
107 $input
: $( '<input>' )
112 sample
: simpleSample
,
115 expected
: '1234567890'
119 description
: 'Limit using a custom value, overriding maxlength attribute',
120 $input
: $( '<input>' )
121 .attr( 'type', 'text' )
122 .prop( 'maxLength', '10' )
124 sample
: simpleSample
,
127 expected
: '123456789012345'
131 description
: 'Limit using a custom value (multibyte)',
132 $input
: $( '<input>' )
140 expected
: '1234567890' + U_20AC
+ '1'
144 description
: 'Limit using a custom value (multibyte) overlapping a byte',
145 $input
: $( '<input>' )
153 expected
: '1234567890' + '12'
157 description
: 'Pass the limit and a callback as input filter',
158 $input
: $( '<input>' )
162 .byteLimit( 6, function( val
) {
170 // Return without namespace prefix
171 return new mw
.Title( '' + val
).getMain();
173 sample
: 'User:Sample',
175 limit
: 6, // 'Sample' length
176 expected
: 'User:Sample'
180 description
: 'Limit using the maxlength attribute and pass a callback as input filter',
181 $input
: $( '<input>' )
182 .attr( 'type', 'text' )
183 .prop( 'maxLength', '6' )
184 .byteLimit( function( val
) {
192 // Return without namespace prefix
193 return new mw
.Title( '' + val
).getMain();
195 sample
: 'User:Sample',
197 limit
: 6, // 'Sample' length
198 expected
: 'User:Sample'