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 for ( c
= 0, len
= charstr
.length
; c
< len
; c
+= 1 ) {
20 .val( function ( i
, val
) {
21 // Add character to the value
22 return val
+ charstr
.charAt( c
);
29 * Test factory for $.fn.byteLimit
31 * @param $input {jQuery} jQuery object in an input element
32 * @param hasLimit {Boolean} Wether a limit should apply at all
33 * @param limit {Number} Limit (if used) otherwise undefined
34 * The limit should be less than 20 (the sample data's length)
36 function byteLimitTest( options
) {
46 QUnit
.asyncTest( opt
.description
, opt
.hasLimit
? 3 : 2, function ( assert
) {
47 setTimeout( function () {
48 var rawVal
, fn
, effectiveVal
;
50 opt
.$input
.appendTo( '#qunit-fixture' );
52 // Simulate pressing keys for each of the sample characters
53 addChars( opt
.$input
, opt
.sample
);
55 rawVal
= opt
.$input
.val();
56 fn
= opt
.$input
.data( 'byteLimit.callback' );
57 effectiveVal
= fn
? fn( rawVal
) : rawVal
;
61 $.byteLength( effectiveVal
),
63 'Prevent keypresses after byteLimit was reached, length never exceeded the limit'
66 $.byteLength( rawVal
),
67 $.byteLength( opt
.expected
),
68 'Not preventing keypresses too early, length has reached the expected length'
70 assert
.equal( rawVal
, opt
.expected
, 'New value matches the expected string' );
74 $.byteLength( effectiveVal
),
75 $.byteLength( opt
.expected
),
76 'Unlimited scenarios are not affected, expected length reached'
78 assert
.equal( rawVal
, opt
.expected
, 'New value matches the expected string' );
86 description
: 'Plain text input',
87 $input
: $( '<input type="text"/>' ),
90 expected
: simpleSample
94 description
: 'Plain text input. Calling byteLimit with no parameters and no maxlength attribute (bug 36310)',
95 $input
: $( '<input type="text"/>' )
99 expected
: simpleSample
103 description
: 'Limit using the maxlength attribute',
104 $input
: $( '<input type="text"/>' )
105 .attr( 'maxlength', '10' )
107 sample
: simpleSample
,
110 expected
: '1234567890'
114 description
: 'Limit using a custom value',
115 $input
: $( '<input type="text"/>' )
117 sample
: simpleSample
,
120 expected
: '1234567890'
124 description
: 'Limit using a custom value, overriding maxlength attribute',
125 $input
: $( '<input type="text"/>' )
126 .attr( 'maxlength', '10' )
128 sample
: simpleSample
,
131 expected
: '123456789012345'
135 description
: 'Limit using a custom value (multibyte)',
136 $input
: $( '<input type="text"/>' )
141 expected
: '1234567890' + U_20AC
+ '1'
145 description
: 'Limit using a custom value (multibyte) overlapping a byte',
146 $input
: $( '<input type="text"/>' )
151 expected
: '1234567890' + '12'
155 description
: 'Pass the limit and a callback as input filter',
156 $input
: $( '<input type="text"/>' )
157 .byteLimit( 6, function ( val
) {
163 // Return without namespace prefix
164 return new mw
.Title( String( val
) ).getMain();
166 sample
: 'User:Sample',
168 limit
: 6, // 'Sample' length
169 expected
: 'User:Sample'
173 description
: 'Limit using the maxlength attribute and pass a callback as input filter',
174 $input
: $( '<input type="text"/>' )
175 .attr( 'maxlength', '6' )
176 .byteLimit( function ( val
) {
182 // Return without namespace prefix
183 return new mw
.Title( String( val
) ).getMain();
185 sample
: 'User:Sample',
187 limit
: 6, // 'Sample' length
188 expected
: 'User:Sample'
191 QUnit
.test( 'Confirm properties and attributes set', 4, function ( assert
) {
194 $el
= $( '<input type="text"/>' )
195 .attr( 'maxlength', '7' )
196 .appendTo( '#qunit-fixture' )
199 assert
.strictEqual( $el
.attr( 'maxlength' ), '7', 'maxlength attribute unchanged for simple limit' );
201 $el
= $( '<input type="text"/>' )
202 .attr( 'maxlength', '7' )
203 .appendTo( '#qunit-fixture' )
206 assert
.strictEqual( $el
.attr( 'maxlength' ), '12', 'maxlength attribute updated for custom limit' );
208 $el
= $( '<input type="text"/>' )
209 .attr( 'maxlength', '7' )
210 .appendTo( '#qunit-fixture' )
211 .byteLimit( 12, function ( val
) {
215 assert
.strictEqual( $el
.attr( 'maxlength' ), undefined, 'maxlength attribute removed for limit with callback' );
217 $elA
= $( '<input type="text"/>' )
218 .addClass( 'mw-test-byteLimit-foo' )
219 .attr( 'maxlength', '7' )
220 .appendTo( '#qunit-fixture' );
222 $elB
= $( '<input type="text"/>' )
223 .addClass( 'mw-test-byteLimit-foo' )
224 .attr( 'maxlength', '12' )
225 .appendTo( '#qunit-fixture' );
227 $el
= $( '.mw-test-byteLimit-foo' );
229 assert
.strictEqual( $el
.length
, 2, 'Verify that there are no other elements clashing with this test suite' );
234 }( jQuery
, mediaWiki
) );