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
) {
133 // Return without namespace prefix
134 return new mw
.Title( String( val
) ).getMain();
136 sample
: 'User:Sample',
137 expected
: 'User:Sample'
141 description
: 'Limit using the maxlength attribute and pass a callback as input filter',
142 $input
: $( '<input type="text"/>' )
143 .attr( 'maxlength', '6' )
144 .byteLimit( function ( val
) {
150 // Return without namespace prefix
151 return new mw
.Title( String( val
) ).getMain();
153 sample
: 'User:Sample',
154 expected
: 'User:Sample'
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:Example',
170 // The callback alters the value to be used to calculeate
171 // the length. The altered value is "Exampl" which has
172 // a length of 6, the "e" would exceed the limit.
173 expected
: 'User:Exampl'
177 description
: 'Input filter that increases the length',
178 $input
: $( '<input type="text"/>' )
179 .byteLimit( 10, function ( text
) {
180 return 'prefix' + text
;
182 sample
: simpleSample
,
183 // Prefix adds 6 characters, limit is reached after 4
187 QUnit
.test( 'Confirm properties and attributes set', 4, function ( assert
) {
190 $el
= $( '<input type="text"/>' )
191 .attr( 'maxlength', '7' )
192 .appendTo( '#qunit-fixture' )
195 assert
.strictEqual( $el
.attr( 'maxlength' ), '7', 'maxlength attribute unchanged for simple limit' );
197 $el
= $( '<input type="text"/>' )
198 .attr( 'maxlength', '7' )
199 .appendTo( '#qunit-fixture' )
202 assert
.strictEqual( $el
.attr( 'maxlength' ), '12', 'maxlength attribute updated for custom limit' );
204 $el
= $( '<input type="text"/>' )
205 .attr( 'maxlength', '7' )
206 .appendTo( '#qunit-fixture' )
207 .byteLimit( 12, function ( val
) {
211 assert
.strictEqual( $el
.attr( 'maxlength' ), undefined, 'maxlength attribute removed for limit with callback' );
213 $elA
= $( '<input type="text"/>' )
214 .addClass( 'mw-test-byteLimit-foo' )
215 .attr( 'maxlength', '7' )
216 .appendTo( '#qunit-fixture' );
218 $elB
= $( '<input type="text"/>' )
219 .addClass( 'mw-test-byteLimit-foo' )
220 .attr( 'maxlength', '12' )
221 .appendTo( '#qunit-fixture' );
223 $el
= $( '.mw-test-byteLimit-foo' );
225 assert
.strictEqual( $el
.length
, 2, 'Verify that there are no other elements clashing with this test suite' );
230 QUnit
.test( 'Trim from insertion when limit exceeded', 2, function ( assert
) {
233 // Use a new <input /> because the bug only occurs on the first time
234 // the limit it reached (bug 40850)
235 $el
= $( '<input type="text"/>' )
236 .appendTo( '#qunit-fixture' )
238 .val( 'abc' ).trigger( 'change' )
239 .val( 'zabc' ).trigger( 'change' );
241 assert
.strictEqual( $el
.val(), 'abc', 'Trim from the insertion point (at 0), not the end' );
243 $el
= $( '<input type="text"/>' )
244 .appendTo( '#qunit-fixture' )
246 .val( 'abc' ).trigger( 'change' )
247 .val( 'azbc' ).trigger( 'change' );
249 assert
.strictEqual( $el
.val(), 'abc', 'Trim from the insertion point (at 1), not the end' );
251 }( jQuery
, mediaWiki
) );