~ubuntu-branches/ubuntu/trusty/mediawiki/trusty

« back to all changes in this revision

Viewing changes to tests/qunit/suites/resources/jquery/jquery.byteLimit.test.js

  • Committer: Package Import Robot
  • Author(s): Thorsten Glaser
  • Date: 2014-03-28 09:56:29 UTC
  • mfrom: (1.3.14)
  • Revision ID: package-import@ubuntu.com-20140328095629-1526y9tchdd507id
Tags: 1:1.19.14+dfsg-1
* New upstream security fix release (Closes: #742857):
  - (bug 62497) SECURITY: Add CSRF token on Special:ChangePassword
  - (bug 62467) Set a title for the context during import on the cli
* Use upstream-provided signing key bundle

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
( function () {
 
2
 
 
3
module( 'jquery.byteLimit', QUnit.newMwEnvironment() );
 
4
 
 
5
test( '-- Initial check', function() {
 
6
        expect(1);
 
7
        ok( $.fn.byteLimit, 'jQuery.fn.byteLimit defined' );
 
8
} );
 
9
 
 
10
// Basic sendkey-implementation
 
11
$.addChars = function( $input, charstr ) {
 
12
        var len = charstr.length;
 
13
        for ( var i = 0; i < len; i++ ) {
 
14
                // Keep track of the previous value
 
15
                var prevVal = $input.val();
 
16
 
 
17
                // Get the key code
 
18
                var code = charstr.charCodeAt(i);
 
19
 
 
20
                // Trigger event and undo if prevented
 
21
                var event = new jQuery.Event( 'keypress', { keyCode: code, which: code, charCode: code } );
 
22
                $input.trigger( event );
 
23
                if ( !event.isDefaultPrevented() ) {
 
24
                        $input.val( prevVal + charstr.charAt(i) );
 
25
                }
 
26
        }
 
27
};
 
28
 
 
29
/**
 
30
 * Test factory for $.fn.byteLimit
 
31
 *
 
32
 * @param $input {jQuery} jQuery object in an input element
 
33
 * @param hasLimit {Boolean} Wether a limit should apply at all
 
34
 * @param limit {Number} Limit (if used) otherwise undefined
 
35
 * The limit should be less than 20 (the sample data's length)
 
36
 */
 
37
var byteLimitTest = function( options ) {
 
38
        var opt = $.extend({
 
39
                description: '',
 
40
                $input: null,
 
41
                sample: '',
 
42
                hasLimit: false,
 
43
                expected: '',
 
44
                limit: null
 
45
        }, options);
 
46
 
 
47
        test( opt.description, function() {
 
48
 
 
49
                opt.$input.appendTo( '#qunit-fixture' );
 
50
 
 
51
                // Simulate pressing keys for each of the sample characters
 
52
                $.addChars( opt.$input, opt.sample );
 
53
                var     rawVal = opt.$input.val(),
 
54
                        fn = opt.$input.data( 'byteLimit-callback' ),
 
55
                        newVal = $.isFunction( fn ) ? fn( rawVal ) : rawVal;
 
56
 
 
57
                if ( opt.hasLimit ) {
 
58
                        expect(3);
 
59
 
 
60
                        ltOrEq( $.byteLength( newVal ), opt.limit, 'Prevent keypresses after byteLimit was reached, length never exceeded the limit' );
 
61
                        equal( $.byteLength( rawVal ), $.byteLength( opt.expected ), 'Not preventing keypresses too early, length has reached the expected length' );
 
62
                        equal( rawVal, opt.expected, 'New value matches the expected string' );
 
63
 
 
64
                } else {
 
65
                        expect(2);
 
66
                        equal( newVal, opt.expected, 'New value matches the expected string' );
 
67
                        equal( $.byteLength( newVal ), $.byteLength( opt.expected ), 'Unlimited scenarios are not affected, expected length reached' );
 
68
                }
 
69
        } );
 
70
};
 
71
 
 
72
var
 
73
        // Simple sample (20 chars, 20 bytes)
 
74
        simpleSample = '12345678901234567890',
 
75
 
 
76
        // 3 bytes (euro-symbol)
 
77
        U_20AC = '\u20AC',
 
78
 
 
79
        // Multi-byte sample (22 chars, 26 bytes)
 
80
        mbSample = '1234567890' + U_20AC + '1234567890' + U_20AC;
 
81
 
 
82
byteLimitTest({
 
83
        description: 'Plain text input',
 
84
        $input: $( '<input>' )
 
85
                .attr( 'type', 'text' ),
 
86
        sample: simpleSample,
 
87
        hasLimit: false,
 
88
        expected: simpleSample
 
89
});
 
90
 
 
91
byteLimitTest({
 
92
        description: 'Limit using the maxlength attribute',
 
93
        $input: $( '<input>' )
 
94
                .attr( 'type', 'text' )
 
95
                .prop( 'maxLength', '10' )
 
96
                .byteLimit(),
 
97
        sample: simpleSample,
 
98
        hasLimit: true,
 
99
        limit: 10,
 
100
        expected: '1234567890'
 
101
});
 
102
 
 
103
byteLimitTest({
 
104
        description: 'Limit using a custom value',
 
105
        $input: $( '<input>' )
 
106
                .attr( 'type', 'text' )
 
107
                .byteLimit( 10 ),
 
108
        sample: simpleSample,
 
109
        hasLimit: true,
 
110
        limit: 10,
 
111
        expected: '1234567890'
 
112
});
 
113
 
 
114
byteLimitTest({
 
115
        description: 'Limit using a custom value, overriding maxlength attribute',
 
116
        $input: $( '<input>' )
 
117
                .attr( 'type', 'text' )
 
118
                .prop( 'maxLength', '10' )
 
119
                .byteLimit( 15 ),
 
120
        sample: simpleSample,
 
121
        hasLimit: true,
 
122
        limit: 15,
 
123
        expected: '123456789012345'
 
124
});
 
125
 
 
126
byteLimitTest({
 
127
        description: 'Limit using a custom value (multibyte)',
 
128
        $input: $( '<input>' )
 
129
                .attr( 'type', 'text' )
 
130
                .byteLimit( 14 ),
 
131
        sample: mbSample,
 
132
        hasLimit: true,
 
133
        limit: 14,
 
134
        expected: '1234567890' + U_20AC + '1'
 
135
});
 
136
 
 
137
byteLimitTest({
 
138
        description: 'Limit using a custom value (multibyte) overlapping a byte',
 
139
        $input: $( '<input>' )
 
140
                .attr( 'type', 'text' )
 
141
                .byteLimit( 12 ),
 
142
        sample: mbSample,
 
143
        hasLimit: true,
 
144
        limit: 12,
 
145
        expected: '1234567890' + '12'
 
146
});
 
147
 
 
148
byteLimitTest({
 
149
        description: 'Pass the limit and a callback as input filter',
 
150
        $input: $( '<input>' )
 
151
                .attr( 'type', 'text' )
 
152
                .byteLimit( 6, function( val ) {
 
153
                        // Invalid title
 
154
                        if ( val == '' ) {
 
155
                                return '';
 
156
                        }
 
157
 
 
158
                        // Return without namespace prefix
 
159
                        return new mw.Title( '' + val ).getMain();
 
160
                } ),
 
161
        sample: 'User:Sample',
 
162
        hasLimit: true,
 
163
        limit: 6, // 'Sample' length
 
164
        expected: 'User:Sample'
 
165
});
 
166
 
 
167
byteLimitTest({
 
168
        description: 'Limit using the maxlength attribute and pass a callback as input filter',
 
169
        $input: $( '<input>' )
 
170
                .attr( 'type', 'text' )
 
171
                .prop( 'maxLength', '6' )
 
172
                .byteLimit( function( val ) {
 
173
                        // Invalid title
 
174
                        if ( val === '' ) {
 
175
                                return '';
 
176
                        }
 
177
 
 
178
                        // Return without namespace prefix
 
179
                        return new mw.Title( '' + val ).getMain();
 
180
                } ),
 
181
        sample: 'User:Sample',
 
182
        hasLimit: true,
 
183
        limit: 6, // 'Sample' length
 
184
        expected: 'User:Sample'
 
185
});
 
186
 
 
187
}() );
 
 
b'\\ No newline at end of file'