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 {Object} options
35 * @param {string} options.description Test name
36 * @param {jQuery} options.$input jQuery object in an input element
37 * @param {string} options.sample Sequence of characters to simulate being
39 * @param {string} options.expected Expected final value of `$input`
41 function byteLimitTest( options ) {
49 QUnit.asyncTest( opt.description, 1, function ( assert ) {
50 setTimeout( function () {
51 opt.$input.appendTo( '#qunit-fixture' );
53 // Simulate pressing keys for each of the sample characters
54 addChars( opt.$input, opt.sample );
59 'New value matches the expected string'
68 description: 'Plain text input',
69 $input: $( '<input type="text"/>' ),
71 expected: simpleSample
75 description: 'Plain text input. Calling byteLimit with no parameters and no maxlength attribute (bug 36310)',
76 $input: $( '<input type="text"/>' )
79 expected: simpleSample
83 description: 'Limit using the maxlength attribute',
84 $input: $( '<input type="text"/>' )
85 .attr( 'maxlength', '10' )
88 expected: '1234567890'
92 description: 'Limit using a custom value',
93 $input: $( '<input type="text"/>' )
96 expected: '1234567890'
100 description: 'Limit using a custom value, overriding maxlength attribute',
101 $input: $( '<input type="text"/>' )
102 .attr( 'maxlength', '10' )
104 sample: simpleSample,
105 expected: '123456789012345'
109 description: 'Limit using a custom value (multibyte)',
110 $input: $( '<input type="text"/>' )
113 expected: '1234567890' + U_20AC + '1'
117 description: 'Limit using a custom value (multibyte) overlapping a byte',
118 $input: $( '<input type="text"/>' )
121 expected: '1234567890' + '12'
125 description: 'Pass the limit and a callback as input filter',
126 $input: $( '<input type="text"/>' )
127 .byteLimit( 6, function ( val ) {
128 var title = mw.Title.newFromText( String( val ) );
129 // Return without namespace prefix
130 return title ? title.getMain() : '';
132 sample: 'User:Sample',
133 expected: 'User:Sample'
137 description: 'Limit using the maxlength attribute and pass a callback as input filter',
138 $input: $( '<input type="text"/>' )
139 .attr( 'maxlength', '6' )
140 .byteLimit( function ( val ) {
141 var title = mw.Title.newFromText( String( val ) );
142 // Return without namespace prefix
143 return title ? title.getMain() : '';
145 sample: 'User:Sample',
146 expected: 'User:Sample'
150 description: 'Pass the limit and a callback as input filter',
151 $input: $( '<input type="text"/>' )
152 .byteLimit( 6, function ( val ) {
153 var title = mw.Title.newFromText( String( val ) );
154 // Return without namespace prefix
155 return title ? title.getMain() : '';
157 sample: 'User:Example',
158 // The callback alters the value to be used to calculeate
159 // the length. The altered value is "Exampl" which has
160 // a length of 6, the "e" would exceed the limit.
161 expected: 'User:Exampl'
165 description: 'Input filter that increases the length',
166 $input: $( '<input type="text"/>' )
167 .byteLimit( 10, function ( text ) {
168 return 'prefix' + text;
170 sample: simpleSample,
171 // Prefix adds 6 characters, limit is reached after 4
175 // Regression tests for bug 41450
177 description: 'Input filter of which the base exceeds the limit',
178 $input: $( '<input type="text"/>' )
179 .byteLimit( 3, function ( text ) {
180 return 'prefix' + text;
182 sample: simpleSample,
184 limit: 6, // 'prefix' length
188 QUnit.test( 'Confirm properties and attributes set', 4, function ( assert ) {
191 $el = $( '<input type="text"/>' )
192 .attr( 'maxlength', '7' )
193 .appendTo( '#qunit-fixture' )
196 assert.strictEqual( $el.attr( 'maxlength' ), '7', 'maxlength attribute unchanged for simple limit' );
198 $el = $( '<input type="text"/>' )
199 .attr( 'maxlength', '7' )
200 .appendTo( '#qunit-fixture' )
203 assert.strictEqual( $el.attr( 'maxlength' ), '12', 'maxlength attribute updated for custom limit' );
205 $el = $( '<input type="text"/>' )
206 .attr( 'maxlength', '7' )
207 .appendTo( '#qunit-fixture' )
208 .byteLimit( 12, function ( val ) {
212 assert.strictEqual( $el.attr( 'maxlength' ), undefined, 'maxlength attribute removed for limit with callback' );
214 $elA = $( '<input type="text"/>' )
215 .addClass( 'mw-test-byteLimit-foo' )
216 .attr( 'maxlength', '7' )
217 .appendTo( '#qunit-fixture' );
219 $elB = $( '<input type="text"/>' )
220 .addClass( 'mw-test-byteLimit-foo' )
221 .attr( 'maxlength', '12' )
222 .appendTo( '#qunit-fixture' );
224 $el = $( '.mw-test-byteLimit-foo' );
226 assert.strictEqual( $el.length, 2, 'Verify that there are no other elements clashing with this test suite' );
231 QUnit.test( 'Trim from insertion when limit exceeded', 2, function ( assert ) {
234 // Use a new <input /> because the bug only occurs on the first time
235 // the limit it reached (bug 40850)
236 $el = $( '<input type="text"/>' )
237 .appendTo( '#qunit-fixture' )
239 .val( 'abc' ).trigger( 'change' )
240 .val( 'zabc' ).trigger( 'change' );
242 assert.strictEqual( $el.val(), 'abc', 'Trim from the insertion point (at 0), not the end' );
244 $el = $( '<input type="text"/>' )
245 .appendTo( '#qunit-fixture' )
247 .val( 'abc' ).trigger( 'change' )
248 .val( 'azbc' ).trigger( 'change' );
250 assert.strictEqual( $el.val(), 'abc', 'Trim from the insertion point (at 1), not the end' );
252 }( jQuery, mediaWiki ) );