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 ) {
19 function x( $input, i ) {
20 // Add character to the value
21 return $input.val() + charstr.charAt( i );
24 for ( c = 0, len = charstr.length; c < len; c += 1 ) {
26 .val( x( $input, c ) )
32 * Test factory for $.fn.byteLimit
34 * @param $input {jQuery} jQuery object in an input element
35 * @param hasLimit {Boolean} Wether a limit should apply at all
36 * @param limit {Number} Limit (if used) otherwise undefined
37 * The limit should be less than 20 (the sample data's length)
39 function byteLimitTest( options ) {
49 QUnit.asyncTest( opt.description, opt.hasLimit ? 3 : 2, function ( assert ) {
50 setTimeout( function () {
51 var rawVal, fn, effectiveVal;
53 opt.$input.appendTo( '#qunit-fixture' );
55 // Simulate pressing keys for each of the sample characters
56 addChars( opt.$input, opt.sample );
58 rawVal = opt.$input.val();
59 fn = opt.$input.data( 'byteLimit.callback' );
60 effectiveVal = fn ? fn( rawVal ) : rawVal;
64 $.byteLength( effectiveVal ),
66 'Prevent keypresses after byteLimit was reached, length never exceeded the limit'
69 $.byteLength( rawVal ),
70 $.byteLength( opt.expected ),
71 'Not preventing keypresses too early, length has reached the expected length'
73 assert.equal( rawVal, opt.expected, 'New value matches the expected string' );
77 $.byteLength( effectiveVal ),
78 $.byteLength( opt.expected ),
79 'Unlimited scenarios are not affected, expected length reached'
81 assert.equal( rawVal, opt.expected, 'New value matches the expected string' );
89 description: 'Plain text input',
90 $input: $( '<input type="text"/>' ),
93 expected: simpleSample
97 description: 'Plain text input. Calling byteLimit with no parameters and no maxlength attribute (bug 36310)',
98 $input: $( '<input type="text"/>' )
100 sample: simpleSample,
102 expected: simpleSample
106 description: 'Limit using the maxlength attribute',
107 $input: $( '<input type="text"/>' )
108 .attr( 'maxlength', '10' )
110 sample: simpleSample,
113 expected: '1234567890'
117 description: 'Limit using a custom value',
118 $input: $( '<input type="text"/>' )
120 sample: simpleSample,
123 expected: '1234567890'
127 description: 'Limit using a custom value, overriding maxlength attribute',
128 $input: $( '<input type="text"/>' )
129 .attr( 'maxlength', '10' )
131 sample: simpleSample,
134 expected: '123456789012345'
138 description: 'Limit using a custom value (multibyte)',
139 $input: $( '<input type="text"/>' )
144 expected: '1234567890' + U_20AC + '1'
148 description: 'Limit using a custom value (multibyte) overlapping a byte',
149 $input: $( '<input type="text"/>' )
154 expected: '1234567890' + '12'
158 description: 'Pass the limit and a callback as input filter',
159 $input: $( '<input type="text"/>' )
160 .byteLimit( 6, function ( val ) {
166 // Return without namespace prefix
167 return new mw.Title( String( val ) ).getMain();
169 sample: 'User:Sample',
171 limit: 6, // 'Sample' length
172 expected: 'User:Sample'
176 description: 'Limit using the maxlength attribute and pass a callback as input filter',
177 $input: $( '<input type="text"/>' )
178 .attr( 'maxlength', '6' )
179 .byteLimit( function ( val ) {
185 // Return without namespace prefix
186 return new mw.Title( String( val ) ).getMain();
188 sample: 'User:Sample',
190 limit: 6, // 'Sample' length
191 expected: 'User:Sample'
194 QUnit.test( 'Confirm properties and attributes set', 4, function ( assert ) {
197 $el = $( '<input type="text"/>' )
198 .attr( 'maxlength', '7' )
199 .appendTo( '#qunit-fixture' )
202 assert.strictEqual( $el.attr( 'maxlength' ), '7', 'maxlength attribute unchanged for simple limit' );
204 $el = $( '<input type="text"/>' )
205 .attr( 'maxlength', '7' )
206 .appendTo( '#qunit-fixture' )
209 assert.strictEqual( $el.attr( 'maxlength' ), '12', 'maxlength attribute updated for custom limit' );
211 $el = $( '<input type="text"/>' )
212 .attr( 'maxlength', '7' )
213 .appendTo( '#qunit-fixture' )
214 .byteLimit( 12, function ( val ) {
218 assert.strictEqual( $el.attr( 'maxlength' ), undefined, 'maxlength attribute removed for limit with callback' );
220 $elA = $( '<input type="text"/>' )
221 .addClass( 'mw-test-byteLimit-foo' )
222 .attr( 'maxlength', '7' )
223 .appendTo( '#qunit-fixture' );
225 $elB = $( '<input type="text"/>' )
226 .addClass( 'mw-test-byteLimit-foo' )
227 .attr( 'maxlength', '12' )
228 .appendTo( '#qunit-fixture' );
230 $el = $( '.mw-test-byteLimit-foo' );
232 assert.strictEqual( $el.length, 2, 'Verify that there are no other elements clashing with this test suite' );
237 QUnit.test( 'Trim from insertion when limit exceeded', 2, function ( assert ) {
240 // Use a new <input /> because the bug only occurs on the first time
241 // the limit it reached (bug 40850)
242 $el = $( '<input type="text"/>' )
243 .appendTo( '#qunit-fixture' )
245 .val( 'abc' ).trigger( 'change' )
246 .val( 'zabc' ).trigger( 'change' );
248 assert.strictEqual( $el.val(), 'abc', 'Trim from the insertion point (at 0), not the end' );
250 $el = $( '<input type="text"/>' )
251 .appendTo( '#qunit-fixture' )
253 .val( 'abc' ).trigger( 'change' )
254 .val( 'azbc' ).trigger( 'change' );
256 assert.strictEqual( $el.val(), 'abc', 'Trim from the insertion point (at 1), not the end' );
258 }( jQuery, mediaWiki ) );