69f97883a5ca689cbea399f3d73b1ebdeda00d3e
2 var simpleSample
, U_20AC
, mbSample
;
4 module( 'jquery.byteLimit', QUnit
.newMwEnvironment() );
6 // Simple sample (20 chars, 20 bytes)
7 simpleSample
= '12345678901234567890';
9 // 3 bytes (euro-symbol)
12 // Multi-byte sample (22 chars, 26 bytes)
13 mbSample
= '1234567890' + U_20AC
+ '1234567890' + U_20AC
;
15 // Basic sendkey-implementation
16 function addChars( $input
, charstr
) {
17 var len
, i
, prevVal
, code
, event
;
19 for ( i
= 0; i
< len
; i
+= 1 ) {
20 // Keep track of the previous value
21 prevVal
= $input
.val();
24 code
= charstr
.charCodeAt( i
);
26 // Trigger event and undo if prevented
27 event
= new jQuery
.Event( 'keypress', {
32 $input
.trigger( event
);
33 if ( !event
.isDefaultPrevented() ) {
34 $input
.val( prevVal
+ charstr
.charAt( i
) );
40 * Test factory for $.fn.byteLimit
42 * @param $input {jQuery} jQuery object in an input element
43 * @param hasLimit {Boolean} Wether a limit should apply at all
44 * @param limit {Number} Limit (if used) otherwise undefined
45 * The limit should be less than 20 (the sample data's length)
47 function byteLimitTest( options
) {
57 test( opt
.description
, function () {
58 var rawVal
, fn
, newVal
;
60 opt
.$input
.appendTo( '#qunit-fixture' );
62 // Simulate pressing keys for each of the sample characters
63 addChars( opt
.$input
, opt
.sample
);
64 rawVal
= opt
.$input
.val();
65 fn
= opt
.$input
.data( 'byteLimit-callback' );
66 newVal
= $.isFunction( fn
) ? fn( rawVal
) : rawVal
;
72 $.byteLength( newVal
),
74 'Prevent keypresses after byteLimit was reached, length never exceeded the limit'
77 $.byteLength( rawVal
),
78 $.byteLength( opt
.expected
),
79 'Not preventing keypresses too early, length has reached the expected length'
81 equal( rawVal
, opt
.expected
, 'New value matches the expected string' );
85 equal( newVal
, opt
.expected
, 'New value matches the expected string' );
87 $.byteLength( newVal
),
88 $.byteLength( opt
.expected
),
89 'Unlimited scenarios are not affected, expected length reached'
95 test( '-- Initial check', function () {
97 ok( $.fn
.byteLimit
, 'jQuery.fn.byteLimit defined' );
101 description
: 'Plain text input',
102 $input
: $( '<input>' )
103 .attr( 'type', 'text' ),
104 sample
: simpleSample
,
106 expected
: simpleSample
110 description
: 'Limit using the maxlength attribute',
111 $input
: $( '<input>' )
112 .attr( 'type', 'text' )
113 .prop( 'maxLength', '10' )
115 sample
: simpleSample
,
118 expected
: '1234567890'
122 description
: 'Limit using a custom value',
123 $input
: $( '<input>' )
124 .attr( 'type', 'text' )
126 sample
: simpleSample
,
129 expected
: '1234567890'
133 description
: 'Limit using a custom value, overriding maxlength attribute',
134 $input
: $( '<input>' )
135 .attr( 'type', 'text' )
136 .prop( 'maxLength', '10' )
138 sample
: simpleSample
,
141 expected
: '123456789012345'
145 description
: 'Limit using a custom value (multibyte)',
146 $input
: $( '<input>' )
147 .attr( 'type', 'text' )
152 expected
: '1234567890' + U_20AC
+ '1'
156 description
: 'Limit using a custom value (multibyte) overlapping a byte',
157 $input
: $( '<input>' )
158 .attr( 'type', 'text' )
163 expected
: '1234567890' + '12'
167 description
: 'Pass the limit and a callback as input filter',
168 $input
: $( '<input>' )
169 .attr( 'type', 'text' )
170 .byteLimit( 6, function ( val
) {
176 // Return without namespace prefix
177 return new mw
.Title( String( val
) ).getMain();
179 sample
: 'User:Sample',
181 limit
: 6, // 'Sample' length
182 expected
: 'User:Sample'
186 description
: 'Limit using the maxlength attribute and pass a callback as input filter',
187 $input
: $( '<input>' )
188 .attr( 'type', 'text' )
189 .prop( 'maxLength', '6' )
190 .byteLimit( function ( val
) {
196 // Return without namespace prefix
197 return new mw
.Title( String( val
) ).getMain();
199 sample
: 'User:Sample',
201 limit
: 6, // 'Sample' length
202 expected
: 'User:Sample'