Merge "Rephrase tooltip-ca-nstab-special"
[lhc/web/wiklou.git] / tests / qunit / suites / resources / jquery / jquery.byteLimit.test.js
1 ( function ( $, mw ) {
2 var simpleSample, U_20AC, mbSample;
3
4 QUnit.module( 'jquery.byteLimit', QUnit.newMwEnvironment() );
5
6 // Simple sample (20 chars, 20 bytes)
7 simpleSample = '12345678901234567890';
8
9 // 3 bytes (euro-symbol)
10 U_20AC = '\u20AC';
11
12 // Multi-byte sample (22 chars, 26 bytes)
13 mbSample = '1234567890' + U_20AC + '1234567890' + U_20AC;
14
15 // Basic sendkey-implementation
16 function addChars( $input, charstr ) {
17 var c, len;
18
19 function x( $input, i ) {
20 // Add character to the value
21 return $input.val() + charstr.charAt( i );
22 }
23
24 for ( c = 0, len = charstr.length; c < len; c += 1 ) {
25 $input
26 .val( x( $input, c ) )
27 .trigger( 'change' );
28 }
29 }
30
31 /**
32 * Test factory for $.fn.byteLimit
33 *
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
38 * added one by one
39 * @param {string} options.expected Expected final value of `$input`
40 */
41 function byteLimitTest( options ) {
42 var opt = $.extend( {
43 description: '',
44 $input: null,
45 sample: '',
46 expected: ''
47 }, options );
48
49 QUnit.asyncTest( opt.description, 1, function ( assert ) {
50 setTimeout( function () {
51 opt.$input.appendTo( '#qunit-fixture' );
52
53 // Simulate pressing keys for each of the sample characters
54 addChars( opt.$input, opt.sample );
55
56 assert.equal(
57 opt.$input.val(),
58 opt.expected,
59 'New value matches the expected string'
60 );
61
62 QUnit.start();
63 } );
64 } );
65 }
66
67 byteLimitTest( {
68 description: 'Plain text input',
69 $input: $( '<input type="text"/>' ),
70 sample: simpleSample,
71 expected: simpleSample
72 } );
73
74 byteLimitTest( {
75 description: 'Plain text input. Calling byteLimit with no parameters and no maxlength attribute (bug 36310)',
76 $input: $( '<input type="text"/>' )
77 .byteLimit(),
78 sample: simpleSample,
79 expected: simpleSample
80 } );
81
82 byteLimitTest( {
83 description: 'Limit using the maxlength attribute',
84 $input: $( '<input type="text"/>' )
85 .attr( 'maxlength', '10' )
86 .byteLimit(),
87 sample: simpleSample,
88 expected: '1234567890'
89 } );
90
91 byteLimitTest( {
92 description: 'Limit using a custom value',
93 $input: $( '<input type="text"/>' )
94 .byteLimit( 10 ),
95 sample: simpleSample,
96 expected: '1234567890'
97 } );
98
99 byteLimitTest( {
100 description: 'Limit using a custom value, overriding maxlength attribute',
101 $input: $( '<input type="text"/>' )
102 .attr( 'maxlength', '10' )
103 .byteLimit( 15 ),
104 sample: simpleSample,
105 expected: '123456789012345'
106 } );
107
108 byteLimitTest( {
109 description: 'Limit using a custom value (multibyte)',
110 $input: $( '<input type="text"/>' )
111 .byteLimit( 14 ),
112 sample: mbSample,
113 expected: '1234567890' + U_20AC + '1'
114 } );
115
116 byteLimitTest( {
117 description: 'Limit using a custom value (multibyte) overlapping a byte',
118 $input: $( '<input type="text"/>' )
119 .byteLimit( 12 ),
120 sample: mbSample,
121 expected: '1234567890' + '12'
122 } );
123
124 byteLimitTest( {
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() : '';
131 } ),
132 sample: 'User:Sample',
133 expected: 'User:Sample'
134 } );
135
136 byteLimitTest( {
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() : '';
144 } ),
145 sample: 'User:Sample',
146 expected: 'User:Sample'
147 } );
148
149 byteLimitTest( {
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() : '';
156 } ),
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'
162 } );
163
164 byteLimitTest( {
165 description: 'Input filter that increases the length',
166 $input: $( '<input type="text"/>' )
167 .byteLimit( 10, function ( text ) {
168 return 'prefix' + text;
169 } ),
170 sample: simpleSample,
171 // Prefix adds 6 characters, limit is reached after 4
172 expected: '1234'
173 } );
174
175 // Regression tests for bug 41450
176 byteLimitTest( {
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;
181 } ),
182 sample: simpleSample,
183 hasLimit: true,
184 limit: 6, // 'prefix' length
185 expected: ''
186 } );
187
188 QUnit.test( 'Confirm properties and attributes set', 4, function ( assert ) {
189 var $el, $elA, $elB;
190
191 $el = $( '<input type="text"/>' )
192 .attr( 'maxlength', '7' )
193 .appendTo( '#qunit-fixture' )
194 .byteLimit();
195
196 assert.strictEqual( $el.attr( 'maxlength' ), '7', 'maxlength attribute unchanged for simple limit' );
197
198 $el = $( '<input type="text"/>' )
199 .attr( 'maxlength', '7' )
200 .appendTo( '#qunit-fixture' )
201 .byteLimit( 12 );
202
203 assert.strictEqual( $el.attr( 'maxlength' ), '12', 'maxlength attribute updated for custom limit' );
204
205 $el = $( '<input type="text"/>' )
206 .attr( 'maxlength', '7' )
207 .appendTo( '#qunit-fixture' )
208 .byteLimit( 12, function ( val ) {
209 return val;
210 } );
211
212 assert.strictEqual( $el.attr( 'maxlength' ), undefined, 'maxlength attribute removed for limit with callback' );
213
214 $elA = $( '<input type="text"/>' )
215 .addClass( 'mw-test-byteLimit-foo' )
216 .attr( 'maxlength', '7' )
217 .appendTo( '#qunit-fixture' );
218
219 $elB = $( '<input type="text"/>' )
220 .addClass( 'mw-test-byteLimit-foo' )
221 .attr( 'maxlength', '12' )
222 .appendTo( '#qunit-fixture' );
223
224 $el = $( '.mw-test-byteLimit-foo' );
225
226 assert.strictEqual( $el.length, 2, 'Verify that there are no other elements clashing with this test suite' );
227
228 $el.byteLimit();
229 } );
230
231 QUnit.test( 'Trim from insertion when limit exceeded', 2, function ( assert ) {
232 var $el;
233
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' )
238 .byteLimit( 3 )
239 .val( 'abc' ).trigger( 'change' )
240 .val( 'zabc' ).trigger( 'change' );
241
242 assert.strictEqual( $el.val(), 'abc', 'Trim from the insertion point (at 0), not the end' );
243
244 $el = $( '<input type="text"/>' )
245 .appendTo( '#qunit-fixture' )
246 .byteLimit( 3 )
247 .val( 'abc' ).trigger( 'change' )
248 .val( 'azbc' ).trigger( 'change' );
249
250 assert.strictEqual( $el.val(), 'abc', 'Trim from the insertion point (at 1), not the end' );
251 } );
252 }( jQuery, mediaWiki ) );