* (bug 29804) Fix byte-length-limited fields to check the length of to-be-added char...
[lhc/web/wiklou.git] / tests / qunit / suites / resources / jquery / jquery.byteLimit.js
1 module( 'jquery.byteLimit.js' );
2
3 test( '-- Initial check', function() {
4 expect(1);
5 ok( $.fn.byteLimit, 'jQuery.fn.byteLimit defined' );
6 } );
7
8 // Basic sendkey-implementation
9 $.addChars = function( $input, charstr ) {
10 var len = charstr.length;
11 for ( var i = 0; i < len; i++ ) {
12 // Keep track of the previous value
13 var prevVal = $input.val();
14
15 // Get the key code
16 var code = charstr.charCodeAt(i);
17
18 // Trigger event and undo if prevented
19 var event = new jQuery.Event( 'keypress', { keyCode: code, which: code, charCode: code } );
20 $input.trigger( event );
21 if ( !event.isDefaultPrevented() ) {
22 $input.val( prevVal + charstr[i] );
23 }
24 }
25 };
26 var blti = 0;
27 /**
28 * Test factory for $.fn.byteLimit
29 *
30 * @param $input {jQuery} jQuery object in an input element
31 * @param useLimit {Boolean} Wether a limit should apply at all
32 * @param limit {Number} Limit (if used) otherwise undefined
33 * The limit should be less than 20 (the sample data's length)
34 */
35 var byteLimitTest = function( options ) {
36 var opt = $.extend({
37 description: '',
38 $input: null,
39 sample: '',
40 useLimit: false,
41 expected: 0,
42 limit: null
43 }, options);
44 var i = blti++;
45
46 test( opt.description, function() {
47
48 opt.$input.appendTo( 'body' );
49
50 // Simulate pressing keys for each of the sample characters
51 $.addChars( opt.$input, opt.sample );
52 var newVal = opt.$input.val();
53
54 if ( opt.useLimit ) {
55 expect(2);
56
57 ltOrEq( $.byteLength( newVal ), opt.limit, 'Prevent keypresses after byteLimit was reached, length never exceeded the limit' );
58 equal( $.byteLength( newVal ), opt.expected, 'Not preventing keypresses too early, length has reached the expected length' );
59
60 } else {
61 expect(1);
62 equal( $.byteLength( newVal ), opt.expected, 'Unlimited scenarios are not affected, expected length reached' );
63 }
64
65 opt.$input.remove();
66 } );
67 };
68
69 var
70 // Simple sample (20 chars, 20 bytes)
71 simpleSample = '12345678901234567890',
72
73 // 3 bytes (euro-symbol)
74 U_20AC = '\u20AC',
75
76 // Multi-byte sample (22 chars, 26 bytes)
77 mbSample = '1234567890' + U_20AC + '1234567890' + U_20AC;
78
79 byteLimitTest({
80 description: 'Plain text input',
81 $input: $( '<input>' )
82 .attr( {
83 'type': 'text'
84 }),
85 sample: simpleSample,
86 useLimit: false,
87 expected: $.byteLength( simpleSample )
88 });
89
90 byteLimitTest({
91 description: 'Limit using the maxlength attribute',
92 $input: $( '<input>' )
93 .attr( {
94 'type': 'text',
95 'maxlength': '10'
96 })
97 .byteLimit(),
98 sample: simpleSample,
99 useLimit: true,
100 limit: 10,
101 expected: 10
102 });
103
104 byteLimitTest({
105 description: 'Limit using a custom value',
106 $input: $( '<input>' )
107 .attr( {
108 'type': 'text'
109 })
110 .byteLimit( 10 ),
111 sample: simpleSample,
112 useLimit: true,
113 limit: 10,
114 expected: 10
115 });
116
117 byteLimitTest({
118 description: 'Limit using a custom value, overriding maxlength attribute',
119 $input: $( '<input>' )
120 .attr( {
121 'type': 'text',
122 'maxLength': '10'
123 })
124 .byteLimit( 15 ),
125 sample: simpleSample,
126 useLimit: true,
127 limit: 15,
128 expected: 15
129 });
130
131 byteLimitTest({
132 description: 'Limit using a custom value (multibyte)',
133 $input: $( '<input>' )
134 .attr( {
135 'type': 'text'
136 })
137 .byteLimit( 14 ),
138 sample: mbSample,
139 useLimit: true,
140 limit: 14,
141 expected: 14 // (10 x 1-byte char) + (1 x 3-byte char) + (1 x 1-byte char)
142 });
143
144 byteLimitTest({
145 description: 'Limit using a custom value (multibyte) overlapping a byte',
146 $input: $( '<input>' )
147 .attr( {
148 'type': 'text'
149 })
150 .byteLimit( 12 ),
151 sample: mbSample,
152 useLimit: true,
153 limit: 12,
154 expected: 12 // 10 x 1-byte char. The next 3-byte char exceeds limit of 12, but 2 more 1-byte chars come in after.
155 });