2 var simpleSample
, U_20AC
, mbSample
;
4 QUnit
.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
) {
18 function x( $input
, i
) {
19 // Add character to the value
20 return $input
.val() + charstr
.charAt( i
);
22 for ( c
= 0, len
= charstr
.length
; c
< len
; c
+= 1 ) {
24 .val( x( $input
, c
) )
30 * Test factory for $.fn.byteLimit
32 * @param $input {jQuery} jQuery object in an input element
33 * @param hasLimit {Boolean} Wether a limit should apply at all
34 * @param limit {Number} Limit (if used) otherwise undefined
35 * The limit should be less than 20 (the sample data's length)
37 function byteLimitTest( options
) {
47 QUnit
.asyncTest( opt
.description
, opt
.hasLimit
? 3 : 2, function ( assert
) {
48 setTimeout( function () {
49 var rawVal
, fn
, effectiveVal
;
51 opt
.$input
.appendTo( '#qunit-fixture' );
53 // Simulate pressing keys for each of the sample characters
54 addChars( opt
.$input
, opt
.sample
);
56 rawVal
= opt
.$input
.val();
57 fn
= opt
.$input
.data( 'byteLimit.callback' );
58 effectiveVal
= fn
? fn( rawVal
) : rawVal
;
62 $.byteLength( effectiveVal
),
64 'Prevent keypresses after byteLimit was reached, length never exceeded the limit'
67 $.byteLength( rawVal
),
68 $.byteLength( opt
.expected
),
69 'Not preventing keypresses too early, length has reached the expected length'
71 assert
.equal( rawVal
, opt
.expected
, 'New value matches the expected string' );
75 $.byteLength( effectiveVal
),
76 $.byteLength( opt
.expected
),
77 'Unlimited scenarios are not affected, expected length reached'
79 assert
.equal( rawVal
, opt
.expected
, 'New value matches the expected string' );
87 description
: 'Plain text input',
88 $input
: $( '<input type="text"/>' ),
91 expected
: simpleSample
95 description
: 'Plain text input. Calling byteLimit with no parameters and no maxlength attribute (bug 36310)',
96 $input
: $( '<input type="text"/>' )
100 expected
: simpleSample
104 description
: 'Limit using the maxlength attribute',
105 $input
: $( '<input type="text"/>' )
106 .attr( 'maxlength', '10' )
108 sample
: simpleSample
,
111 expected
: '1234567890'
115 description
: 'Limit using a custom value',
116 $input
: $( '<input type="text"/>' )
118 sample
: simpleSample
,
121 expected
: '1234567890'
125 description
: 'Limit using a custom value, overriding maxlength attribute',
126 $input
: $( '<input type="text"/>' )
127 .attr( 'maxlength', '10' )
129 sample
: simpleSample
,
132 expected
: '123456789012345'
136 description
: 'Limit using a custom value (multibyte)',
137 $input
: $( '<input type="text"/>' )
142 expected
: '1234567890' + U_20AC
+ '1'
146 description
: 'Limit using a custom value (multibyte) overlapping a byte',
147 $input
: $( '<input type="text"/>' )
152 expected
: '1234567890' + '12'
156 description
: 'Pass the limit and a callback as input filter',
157 $input
: $( '<input type="text"/>' )
158 .byteLimit( 6, function ( val
) {
164 // Return without namespace prefix
165 return new mw
.Title( String( val
) ).getMain();
167 sample
: 'User:Sample',
169 limit
: 6, // 'Sample' length
170 expected
: 'User:Sample'
174 description
: 'Limit using the maxlength attribute and pass a callback as input filter',
175 $input
: $( '<input type="text"/>' )
176 .attr( 'maxlength', '6' )
177 .byteLimit( function ( val
) {
183 // Return without namespace prefix
184 return new mw
.Title( String( val
) ).getMain();
186 sample
: 'User:Sample',
188 limit
: 6, // 'Sample' length
189 expected
: 'User:Sample'
192 QUnit
.test( 'Confirm properties and attributes set', 4, function ( assert
) {
195 $el
= $( '<input type="text"/>' )
196 .attr( 'maxlength', '7' )
197 .appendTo( '#qunit-fixture' )
200 assert
.strictEqual( $el
.attr( 'maxlength' ), '7', 'maxlength attribute unchanged for simple limit' );
202 $el
= $( '<input type="text"/>' )
203 .attr( 'maxlength', '7' )
204 .appendTo( '#qunit-fixture' )
207 assert
.strictEqual( $el
.attr( 'maxlength' ), '12', 'maxlength attribute updated for custom limit' );
209 $el
= $( '<input type="text"/>' )
210 .attr( 'maxlength', '7' )
211 .appendTo( '#qunit-fixture' )
212 .byteLimit( 12, function ( val
) {
216 assert
.strictEqual( $el
.attr( 'maxlength' ), undefined, 'maxlength attribute removed for limit with callback' );
218 $elA
= $( '<input type="text"/>' )
219 .addClass( 'mw-test-byteLimit-foo' )
220 .attr( 'maxlength', '7' )
221 .appendTo( '#qunit-fixture' );
223 $elB
= $( '<input type="text"/>' )
224 .addClass( 'mw-test-byteLimit-foo' )
225 .attr( 'maxlength', '12' )
226 .appendTo( '#qunit-fixture' );
228 $el
= $( '.mw-test-byteLimit-foo' );
230 assert
.strictEqual( $el
.length
, 2, 'Verify that there are no other elements clashing with this test suite' );
235 QUnit
.test( 'Trim from insertion when limit exceeded', 2, function ( assert
) {
238 // Use a new <input /> because the bug only occurs on the first time
239 // the limit it reached (bug 40850)
240 $el
= $( '<input type="text"/>' )
241 .appendTo( '#qunit-fixture' )
243 .val( 'abc' ).trigger( 'change' )
244 .val( 'zabc' ).trigger( 'change' );
246 assert
.strictEqual( $el
.val(), 'abc', 'Trim from the insertion point (at 0), not the end' );
248 $el
= $( '<input type="text"/>' )
249 .appendTo( '#qunit-fixture' )
251 .val( 'abc' ).trigger( 'change' )
252 .val( 'azbc' ).trigger( 'change' );
254 assert
.strictEqual( $el
.val(), 'abc', 'Trim from the insertion point (at 1), not the end' );
257 }( jQuery
, mediaWiki
) );