2 var sample20simple
, sample1mb
, sample22mixed
;
4 module( 'jquery.byteLimit', QUnit
.newMwEnvironment() );
6 // Simple sample (20 chars, 20 bytes)
7 sample20simple
= '1234567890abcdefghij';
9 // Euro-symbol (1 char, 3 bytes)
12 // Multi-byte sample (22 chars, 26 bytes)
13 sample22mixed
= '1234567890' + sample1mb
+ 'abcdefghij' + sample1mb
;
16 * Basic emulation of character-by-charater insertion
17 * and triggering of keyup event after each character.
19 function simulateKeyUps( $input
, charstr
) {
20 var i
, code
, event
, liveVal
,
22 for ( i
= 0; i
< len
; i
+= 1 ) {
23 // Always use the live value as base
24 liveVal
= $input
.val();
26 // Get the key code for the to-be-inserted character
27 code
= charstr
.charCodeAt( i
);
29 $input
.val( liveVal
+ charstr
.charAt( i
) );
31 // Trigger keyup event
32 event
= new jQuery
.Event( 'keyup', {
36 $input
.trigger( event
);
41 * Test factory for $.fn.byteLimit
43 * @param Object options
45 function byteLimitTest( options
) {
54 test( opt
.description
, function () {
55 var rawVal
, fn
, useVal
;
57 opt
.$input
.appendTo( '#qunit-fixture' );
59 simulateKeyUps( opt
.$input
, opt
.sample
);
61 rawVal
= opt
.$input
.val();
62 fn
= opt
.$input
.data( 'byteLimitCallback' );
63 useVal
= $.isFunction( fn
) ? fn( rawVal
) : rawVal
;
69 $.byteLength( useVal
),
71 'Prevent keypresses after byteLimit was reached, length never exceeded the limit'
74 $.byteLength( rawVal
),
75 $.byteLength( opt
.expected
),
76 'Not preventing keypresses too early, length has reached the expected length'
78 equal( rawVal
, opt
.expected
, 'New value matches the expected string' );
82 equal( useVal
, opt
.expected
, 'New value matches the expected string' );
84 $.byteLength( useVal
),
85 $.byteLength( opt
.expected
),
86 'Unlimited scenarios are not affected, expected length reached'
92 test( '-- Initial check', function () {
94 ok( $.fn
.byteLimit
, 'jQuery.fn.byteLimit defined' );
98 description
: 'Plain text input',
99 $input
: $( '<input>' )
100 .attr( 'type', 'text' ),
101 sample
: sample20simple
,
102 expected
: sample20simple
106 description
: 'No .byteLimit() parameters and no maxLength property - should not throw exceptions (bug 36310)',
107 $input
: $( '<input>' )
108 .attr( 'type', 'text' )
110 sample
: sample20simple
,
111 expected
: sample20simple
115 description
: 'maxLength property',
116 $input
: $( '<input>' )
117 .attr( 'type', 'text' )
118 .prop( 'maxLength', '10' )
120 sample
: sample20simple
,
122 expected
: '1234567890'
126 description
: '.byteLimit( limit )',
127 $input
: $( '<input>' )
128 .attr( 'type', 'text' )
130 sample
: sample20simple
,
132 expected
: '1234567890'
136 description
: 'Limit passed to .byteLimit() takes precedence over maxLength property',
137 $input
: $( '<input>' )
138 .attr( 'type', 'text' )
139 .prop( 'maxLength', '10' )
141 sample
: sample20simple
,
143 expected
: '1234567890abcde'
147 description
: '.byteLimit( limit ) - mixed - cut in simplepart',
148 $input
: $( '<input>' )
149 .attr( 'type', 'text' )
151 sample
: sample22mixed
,
153 expected
: '1234567890' + sample1mb
+ 'a'
157 description
: '.byteLimit( limit ) - mixed - cut in multibyte',
158 $input
: $( '<input>' )
159 .attr( 'type', 'text' )
161 sample
: sample22mixed
,
163 // The 3-byte symbol after 0 would have exceeded the 12 byte limit.
164 // Later when the simulation resumed typing, the two simple characters
166 expected
: '1234567890' + 'ab'
170 description
: '.byteLimit( limit, fn ) callback can alter the value to be checked against',
171 $input
: $( '<input>' )
172 .attr( 'type', 'text' )
173 .byteLimit( 6, function ( val
) {
174 // Only construct mw.Title if the string is non-empty,
175 // since mw.Title throws an exception on invalid titles.
180 // Example: Use value without namespace prefix.
181 return new mw
.Title( String( val
) ).getMain();
184 // Limit is 6, text "User:Sample" would normally be too long,
185 // but in this case we test the callback's ability to only
186 // apply the limit to part of the input. The part "John" in this
187 // case is within the limit.
189 // The callback only affects the comparison, the actual input field
190 // should still contain the full value.
191 expected
: 'User:John'
195 description
: '.byteLimit( fn ) combined with maxLength property',
196 $input
: $( '<input>' )
197 .attr( 'type', 'text' )
198 .prop( 'maxLength', '6' )
199 .byteLimit( function ( val
) {
205 // Return without namespace prefix
206 return new mw
.Title( String( val
) ).getMain();
208 sample
: 'User:Sample',
209 limit
: 6, // 'Sample' length
210 expected
: 'User:Sample'