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
.test( opt
.description
, function ( assert
) {
50 opt
.$input
.appendTo( '#qunit-fixture' );
52 // Simulate pressing keys for each of the sample characters
53 addChars( opt
.$input
, opt
.sample
);
58 'New value matches the expected string'
64 description
: 'Plain text input',
65 $input
: $( '<input>' ).attr( 'type', 'text' ),
67 expected
: simpleSample
71 description
: 'Plain text input. Calling byteLimit with no parameters and no maxlength attribute (T38310)',
72 $input
: $( '<input>' ).attr( 'type', 'text' )
75 expected
: simpleSample
79 description
: 'Limit using the maxlength attribute',
80 $input
: $( '<input>' ).attr( 'type', 'text' )
81 .attr( 'maxlength', '10' )
84 expected
: '1234567890'
88 description
: 'Limit using a custom value',
89 $input
: $( '<input>' ).attr( 'type', 'text' )
92 expected
: '1234567890'
96 description
: 'Limit using a custom value, overriding maxlength attribute',
97 $input
: $( '<input>' ).attr( 'type', 'text' )
98 .attr( 'maxlength', '10' )
100 sample
: simpleSample
,
101 expected
: '123456789012345'
105 description
: 'Limit using a custom value (multibyte)',
106 $input
: $( '<input>' ).attr( 'type', 'text' )
109 expected
: '1234567890' + U_20AC
+ '1'
113 description
: 'Limit using a custom value (multibyte) overlapping a byte',
114 $input
: $( '<input>' ).attr( 'type', 'text' )
117 expected
: '123456789012'
121 description
: 'Pass the limit and a callback as input filter',
122 $input
: $( '<input>' ).attr( 'type', 'text' )
123 .byteLimit( 6, function ( val
) {
124 var title
= mw
.Title
.newFromText( String( val
) );
125 // Return without namespace prefix
126 return title
? title
.getMain() : '';
128 sample
: 'User:Sample',
129 expected
: 'User:Sample'
133 description
: 'Limit using the maxlength attribute and pass a callback as input filter',
134 $input
: $( '<input>' ).attr( 'type', 'text' )
135 .attr( 'maxlength', '6' )
136 .byteLimit( function ( val
) {
137 var title
= mw
.Title
.newFromText( String( val
) );
138 // Return without namespace prefix
139 return title
? title
.getMain() : '';
141 sample
: 'User:Sample',
142 expected
: 'User:Sample'
146 description
: 'Pass the limit and a callback as input filter',
147 $input
: $( '<input>' ).attr( 'type', 'text' )
148 .byteLimit( 6, function ( val
) {
149 var title
= mw
.Title
.newFromText( String( val
) );
150 // Return without namespace prefix
151 return title
? title
.getMain() : '';
153 sample
: 'User:Example',
154 // The callback alters the value to be used to calculeate
155 // the length. The altered value is "Exampl" which has
156 // a length of 6, the "e" would exceed the limit.
157 expected
: 'User:Exampl'
161 description
: 'Input filter that increases the length',
162 $input
: $( '<input>' ).attr( 'type', 'text' )
163 .byteLimit( 10, function ( text
) {
164 return 'prefix' + text
;
166 sample
: simpleSample
,
167 // Prefix adds 6 characters, limit is reached after 4
171 // Regression tests for T43450
173 description
: 'Input filter of which the base exceeds the limit',
174 $input
: $( '<input>' ).attr( 'type', 'text' )
175 .byteLimit( 3, function ( text
) {
176 return 'prefix' + text
;
178 sample
: simpleSample
,
180 limit
: 6, // 'prefix' length
184 QUnit
.test( 'Confirm properties and attributes set', function ( assert
) {
187 $el
= $( '<input>' ).attr( 'type', 'text' )
188 .attr( 'maxlength', '7' )
189 .appendTo( '#qunit-fixture' )
192 assert
.strictEqual( $el
.attr( 'maxlength' ), '7', 'maxlength attribute unchanged for simple limit' );
194 $el
= $( '<input>' ).attr( 'type', 'text' )
195 .attr( 'maxlength', '7' )
196 .appendTo( '#qunit-fixture' )
199 assert
.strictEqual( $el
.attr( 'maxlength' ), '12', 'maxlength attribute updated for custom limit' );
201 $el
= $( '<input>' ).attr( 'type', 'text' )
202 .attr( 'maxlength', '7' )
203 .appendTo( '#qunit-fixture' )
204 .byteLimit( 12, function ( val
) {
208 assert
.strictEqual( $el
.attr( 'maxlength' ), undefined, 'maxlength attribute removed for limit with callback' );
210 $( '<input>' ).attr( 'type', 'text' )
211 .addClass( 'mw-test-byteLimit-foo' )
212 .attr( 'maxlength', '7' )
213 .appendTo( '#qunit-fixture' );
215 $( '<input>' ).attr( 'type', 'text' )
216 .addClass( 'mw-test-byteLimit-foo' )
217 .attr( 'maxlength', '12' )
218 .appendTo( '#qunit-fixture' );
220 $el
= $( '.mw-test-byteLimit-foo' );
222 assert
.strictEqual( $el
.length
, 2, 'Verify that there are no other elements clashing with this test suite' );
227 QUnit
.test( 'Trim from insertion when limit exceeded', function ( assert
) {
230 // Use a new <input> because the bug only occurs on the first time
231 // the limit it reached (T42850)
232 $el
= $( '<input>' ).attr( 'type', 'text' )
233 .appendTo( '#qunit-fixture' )
235 .val( 'abc' ).trigger( 'change' )
236 .val( 'zabc' ).trigger( 'change' );
238 assert
.strictEqual( $el
.val(), 'abc', 'Trim from the insertion point (at 0), not the end' );
240 $el
= $( '<input>' ).attr( 'type', 'text' )
241 .appendTo( '#qunit-fixture' )
243 .val( 'abc' ).trigger( 'change' )
244 .val( 'azbc' ).trigger( 'change' );
246 assert
.strictEqual( $el
.val(), 'abc', 'Trim from the insertion point (at 1), not the end' );
248 }( jQuery
, mediaWiki
) );