Merge "user: Allow "CAS update failed" exceptions to be normalised"
[lhc/web/wiklou.git] / tests / qunit / suites / resources / jquery / jquery.lengthLimit.test.js
1 ( function () {
2 var simpleSample, U_20AC, poop, mbSample;
3
4 QUnit.module( 'jquery.lengthLimit', 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 // Outside of the BMP (pile of poo emoji)
13 poop = '\uD83D\uDCA9'; // "💩"
14
15 // Multi-byte sample (22 chars, 26 bytes)
16 mbSample = '1234567890' + U_20AC + '1234567890' + U_20AC;
17
18 // Basic sendkey-implementation
19 function addChars( $input, charstr ) {
20 var c, len;
21
22 function x( $input, i ) {
23 // Add character to the value
24 return $input.val() + charstr.charAt( i );
25 }
26
27 for ( c = 0, len = charstr.length; c < len; c += 1 ) {
28 $input
29 .val( x( $input, c ) )
30 .trigger( 'change' );
31 }
32 }
33
34 /**
35 * Test factory for $.fn.byteLimit
36 *
37 * @param {Object} options
38 * @param {string} options.description Test name
39 * @param {jQuery} options.$input jQuery object in an input element
40 * @param {string} options.sample Sequence of characters to simulate being
41 * added one by one
42 * @param {string} options.expected Expected final value of `$input`
43 */
44 function byteLimitTest( options ) {
45 var opt = $.extend( {
46 description: '',
47 $input: null,
48 sample: '',
49 expected: ''
50 }, options );
51
52 QUnit.test( opt.description, function ( assert ) {
53 opt.$input.appendTo( '#qunit-fixture' );
54
55 // Simulate pressing keys for each of the sample characters
56 addChars( opt.$input, opt.sample );
57
58 assert.strictEqual(
59 opt.$input.val(),
60 opt.expected,
61 'New value matches the expected string'
62 );
63 } );
64 }
65
66 byteLimitTest( {
67 description: 'Plain text input',
68 $input: $( '<input>' ).attr( 'type', 'text' ),
69 sample: simpleSample,
70 expected: simpleSample
71 } );
72
73 byteLimitTest( {
74 description: 'Plain text input. Calling byteLimit with no parameters and no maxlength attribute (T38310)',
75 $input: $( '<input>' ).attr( 'type', 'text' )
76 .byteLimit(),
77 sample: simpleSample,
78 expected: simpleSample
79 } );
80
81 byteLimitTest( {
82 description: 'Limit using the maxlength attribute',
83 $input: $( '<input>' ).attr( 'type', 'text' )
84 .attr( 'maxlength', '10' )
85 .byteLimit(),
86 sample: simpleSample,
87 expected: '1234567890'
88 } );
89
90 byteLimitTest( {
91 description: 'Limit using a custom value',
92 $input: $( '<input>' ).attr( 'type', 'text' )
93 .byteLimit( 10 ),
94 sample: simpleSample,
95 expected: '1234567890'
96 } );
97
98 byteLimitTest( {
99 description: 'Limit using a custom value, overriding maxlength attribute',
100 $input: $( '<input>' ).attr( 'type', 'text' )
101 .attr( 'maxlength', '10' )
102 .byteLimit( 15 ),
103 sample: simpleSample,
104 expected: '123456789012345'
105 } );
106
107 byteLimitTest( {
108 description: 'Limit using a custom value (multibyte)',
109 $input: $( '<input>' ).attr( 'type', 'text' )
110 .byteLimit( 14 ),
111 sample: mbSample,
112 expected: '1234567890' + U_20AC + '1'
113 } );
114
115 byteLimitTest( {
116 description: 'Limit using a custom value (multibyte, outside BMP)',
117 $input: $( '<input>' ).attr( 'type', 'text' )
118 .byteLimit( 3 ),
119 sample: poop,
120 expected: ''
121 } );
122
123 byteLimitTest( {
124 description: 'Limit using a custom value (multibyte) overlapping a byte',
125 $input: $( '<input>' ).attr( 'type', 'text' )
126 .byteLimit( 12 ),
127 sample: mbSample,
128 expected: '123456789012'
129 } );
130
131 byteLimitTest( {
132 description: 'Pass the limit and a callback as input filter',
133 $input: $( '<input>' ).attr( 'type', 'text' )
134 .byteLimit( 6, function ( val ) {
135 var title = mw.Title.newFromText( String( val ) );
136 // Return without namespace prefix
137 return title ? title.getMain() : '';
138 } ),
139 sample: 'User:Sample',
140 expected: 'User:Sample'
141 } );
142
143 byteLimitTest( {
144 description: 'Limit using the maxlength attribute and pass a callback as input filter',
145 $input: $( '<input>' ).attr( 'type', 'text' )
146 .attr( 'maxlength', '6' )
147 .byteLimit( function ( val ) {
148 var title = mw.Title.newFromText( String( val ) );
149 // Return without namespace prefix
150 return title ? title.getMain() : '';
151 } ),
152 sample: 'User:Sample',
153 expected: 'User:Sample'
154 } );
155
156 byteLimitTest( {
157 description: 'Pass the limit and a callback as input filter',
158 $input: $( '<input>' ).attr( 'type', 'text' )
159 .byteLimit( 6, function ( val ) {
160 var title = mw.Title.newFromText( String( val ) );
161 // Return without namespace prefix
162 return title ? title.getMain() : '';
163 } ),
164 sample: 'User:Example',
165 // The callback alters the value to be used to calculeate
166 // the length. The altered value is "Exampl" which has
167 // a length of 6, the "e" would exceed the limit.
168 expected: 'User:Exampl'
169 } );
170
171 byteLimitTest( {
172 description: 'Input filter that increases the length',
173 $input: $( '<input>' ).attr( 'type', 'text' )
174 .byteLimit( 10, function ( text ) {
175 return 'prefix' + text;
176 } ),
177 sample: simpleSample,
178 // Prefix adds 6 characters, limit is reached after 4
179 expected: '1234'
180 } );
181
182 // Regression tests for T43450
183 byteLimitTest( {
184 description: 'Input filter of which the base exceeds the limit',
185 $input: $( '<input>' ).attr( 'type', 'text' )
186 .byteLimit( 3, function ( text ) {
187 return 'prefix' + text;
188 } ),
189 sample: simpleSample,
190 expected: ''
191 } );
192
193 QUnit.test( 'Confirm properties and attributes set', function ( assert ) {
194 var $el;
195
196 $el = $( '<input>' ).attr( 'type', 'text' )
197 .attr( 'maxlength', '7' )
198 .appendTo( '#qunit-fixture' )
199 .byteLimit();
200
201 assert.strictEqual( $el.attr( 'maxlength' ), '7', 'maxlength attribute unchanged for simple limit' );
202
203 $el = $( '<input>' ).attr( 'type', 'text' )
204 .attr( 'maxlength', '7' )
205 .appendTo( '#qunit-fixture' )
206 .byteLimit( 12 );
207
208 assert.strictEqual( $el.attr( 'maxlength' ), '12', 'maxlength attribute updated for custom limit' );
209
210 $el = $( '<input>' ).attr( 'type', 'text' )
211 .attr( 'maxlength', '7' )
212 .appendTo( '#qunit-fixture' )
213 .byteLimit( 12, function ( val ) {
214 return val;
215 } );
216
217 assert.strictEqual( $el.attr( 'maxlength' ), undefined, 'maxlength attribute removed for limit with callback' );
218
219 $( '<input>' ).attr( 'type', 'text' )
220 .addClass( 'mw-test-byteLimit-foo' )
221 .attr( 'maxlength', '7' )
222 .appendTo( '#qunit-fixture' );
223
224 $( '<input>' ).attr( 'type', 'text' )
225 .addClass( 'mw-test-byteLimit-foo' )
226 .attr( 'maxlength', '12' )
227 .appendTo( '#qunit-fixture' );
228
229 $el = $( '.mw-test-byteLimit-foo' );
230
231 assert.strictEqual( $el.length, 2, 'Verify that there are no other elements clashing with this test suite' );
232
233 $el.byteLimit();
234 } );
235
236 QUnit.test( 'Trim from insertion when limit exceeded', function ( assert ) {
237 var $el;
238
239 // Use a new <input> because the bug only occurs on the first time
240 // the limit it reached (T42850)
241 $el = $( '<input>' ).attr( 'type', 'text' )
242 .appendTo( '#qunit-fixture' )
243 .byteLimit( 3 )
244 .val( 'abc' ).trigger( 'change' )
245 .val( 'zabc' ).trigger( 'change' );
246
247 assert.strictEqual( $el.val(), 'abc', 'Trim from the insertion point (at 0), not the end' );
248
249 $el = $( '<input>' ).attr( 'type', 'text' )
250 .appendTo( '#qunit-fixture' )
251 .byteLimit( 3 )
252 .val( 'abc' ).trigger( 'change' )
253 .val( 'azbc' ).trigger( 'change' );
254
255 assert.strictEqual( $el.val(), 'abc', 'Trim from the insertion point (at 1), not the end' );
256 } );
257
258 QUnit.test( 'Do not cut up false matching substrings in emoji insertions', function ( assert ) {
259 var $el,
260 oldVal = '\uD83D\uDCA9\uD83D\uDCA9', // "💩💩"
261 newVal = '\uD83D\uDCA9\uD83D\uDCB9\uD83E\uDCA9\uD83D\uDCA9', // "💩💹🢩💩"
262 expected = '\uD83D\uDCA9\uD83D\uDCB9\uD83D\uDCA9'; // "💩💹💩"
263
264 // Possible bad results:
265 // * With no surrogate support:
266 // '\uD83D\uDCA9\uD83D\uDCB9\uD83E\uDCA9' "💩💹🢩"
267 // * With correct trimming but bad detection of inserted text:
268 // '\uD83D\uDCA9\uD83D\uDCB9\uDCA9' "💩💹�"
269
270 $el = $( '<input>' ).attr( 'type', 'text' )
271 .appendTo( '#qunit-fixture' )
272 .byteLimit( 12 )
273 .val( oldVal ).trigger( 'change' )
274 .val( newVal ).trigger( 'change' );
275
276 assert.strictEqual( $el.val(), expected, 'Pasted emoji correctly trimmed at the end' );
277 } );
278
279 byteLimitTest( {
280 description: 'Unpaired surrogates do not crash',
281 $input: $( '<input>' ).attr( 'type', 'text' ).byteLimit( 4 ),
282 sample: '\uD800\uD800\uDFFF',
283 expected: '\uD800'
284 } );
285
286 }() );