| 1 |
QUnit.module( 'jquery.lengthLimit', () => { |
| 2 |
|
| 3 |
const simpleSample = '12345678901234567890'; |
| 4 |
|
| 5 |
|
| 6 |
const U_20AC = '\u20AC'; |
| 7 |
|
| 8 |
|
| 9 |
const poop = '\uD83D\uDCA9'; 💩" |
| 10 |
|
| 11 |
|
| 12 |
const mbSample = '1234567890' + U_20AC + '1234567890' + U_20AC; |
| 13 |
|
| 14 |
|
| 15 |
function addChars( $input, charstr ) { |
| 16 |
function x( $el, i ) { |
| 17 |
|
| 18 |
return $el.val() + charstr.charAt( i ); |
| 19 |
} |
| 20 |
|
| 21 |
for ( let c = 0; c < charstr.length; c++ ) { |
| 22 |
$input |
| 23 |
.val( x( $input, c ) ) |
| 24 |
.trigger( 'change' ); |
| 25 |
} |
| 26 |
} |
| 27 |
|
| 28 |
QUnit.test.each( 'byteLimit()', { |
| 29 |
|
| 30 |
'Plain text input': { |
| 31 |
$input: $( '<input>' ).attr( 'type', 'text' ), |
| 32 |
sample: simpleSample, |
| 33 |
expected: simpleSample |
| 34 |
}, |
| 35 |
|
| 36 |
'Plain text input. Calling byteLimit with no parameters and no maxlength attribute (T38310)': { |
| 37 |
$input: $( '<input>' ).attr( 'type', 'text' ) |
| 38 |
.byteLimit(), |
| 39 |
sample: simpleSample, |
| 40 |
expected: simpleSample |
| 41 |
}, |
| 42 |
|
| 43 |
'Limit using the maxlength attribute': { |
| 44 |
$input: $( '<input>' ).attr( 'type', 'text' ) |
| 45 |
.attr( 'maxlength', '10' ) |
| 46 |
.byteLimit(), |
| 47 |
sample: simpleSample, |
| 48 |
expected: '1234567890' |
| 49 |
}, |
| 50 |
|
| 51 |
'Limit using a custom value': { |
| 52 |
$input: $( '<input>' ).attr( 'type', 'text' ) |
| 53 |
.byteLimit( 10 ), |
| 54 |
sample: simpleSample, |
| 55 |
expected: '1234567890' |
| 56 |
}, |
| 57 |
|
| 58 |
'Limit using a custom value, overriding maxlength attribute': { |
| 59 |
$input: $( '<input>' ).attr( 'type', 'text' ) |
| 60 |
.attr( 'maxlength', '10' ) |
| 61 |
.byteLimit( 15 ), |
| 62 |
sample: simpleSample, |
| 63 |
expected: '123456789012345' |
| 64 |
}, |
| 65 |
|
| 66 |
'Limit using a custom value (multibyte)': { |
| 67 |
$input: $( '<input>' ).attr( 'type', 'text' ) |
| 68 |
.byteLimit( 14 ), |
| 69 |
sample: mbSample, |
| 70 |
expected: '1234567890' + U_20AC + '1' |
| 71 |
}, |
| 72 |
|
| 73 |
'Limit using a custom value (multibyte, outside BMP)': { |
| 74 |
$input: $( '<input>' ).attr( 'type', 'text' ) |
| 75 |
.byteLimit( 3 ), |
| 76 |
sample: poop, |
| 77 |
expected: '' |
| 78 |
}, |
| 79 |
|
| 80 |
'Limit using a custom value (multibyte) overlapping a byte': { |
| 81 |
$input: $( '<input>' ).attr( 'type', 'text' ) |
| 82 |
.byteLimit( 12 ), |
| 83 |
sample: mbSample, |
| 84 |
expected: '123456789012' |
| 85 |
}, |
| 86 |
|
| 87 |
'Pass the limit and a callback as input filter': { |
| 88 |
$input: $( '<input>' ).attr( 'type', 'text' ) |
| 89 |
.byteLimit( 6, ( val ) => { |
| 90 |
const title = mw.Title.newFromText( String( val ) ); |
| 91 |
|
| 92 |
return title ? title.getMain() : ''; |
| 93 |
} ), |
| 94 |
sample: 'User:Sample', |
| 95 |
expected: 'User:Sample' |
| 96 |
}, |
| 97 |
|
| 98 |
'Limit using the maxlength attribute and pass a callback as input filter': { |
| 99 |
$input: $( '<input>' ).attr( 'type', 'text' ) |
| 100 |
.attr( 'maxlength', '6' ) |
| 101 |
.byteLimit( ( val ) => { |
| 102 |
const title = mw.Title.newFromText( String( val ) ); |
| 103 |
|
| 104 |
return title ? title.getMain() : ''; |
| 105 |
} ), |
| 106 |
sample: 'User:Sample', |
| 107 |
expected: 'User:Sample' |
| 108 |
}, |
| 109 |
|
| 110 |
'Truncate with exceeded limit and filter callback': { |
| 111 |
$input: $( '<input>' ).attr( 'type', 'text' ) |
| 112 |
.byteLimit( 6, ( val ) => { |
| 113 |
const title = mw.Title.newFromText( String( val ) ); |
| 114 |
|
| 115 |
return title ? title.getMain() : ''; |
| 116 |
} ), |
| 117 |
sample: 'User:Example', |
| 118 |
|
| 119 |
|
| 120 |
|
| 121 |
expected: 'User:Exampl' |
| 122 |
}, |
| 123 |
|
| 124 |
'Input filter that increases the length': { |
| 125 |
$input: $( '<input>' ).attr( 'type', 'text' ) |
| 126 |
.byteLimit( 10, ( text ) => 'prefix' + text ), |
| 127 |
sample: simpleSample, |
| 128 |
|
| 129 |
expected: '1234' |
| 130 |
}, |
| 131 |
|
| 132 |
|
| 133 |
'Input filter of which the base exceeds the limit': { |
| 134 |
$input: $( '<input>' ).attr( 'type', 'text' ) |
| 135 |
.byteLimit( 3, ( text ) => 'prefix' + text ), |
| 136 |
sample: simpleSample, |
| 137 |
expected: '' |
| 138 |
}, |
| 139 |
'Unpaired surrogates do not crash': { |
| 140 |
$input: $( '<input>' ).attr( 'type', 'text' ).byteLimit( 4 ), |
| 141 |
sample: '\uD800\uD800\uDFFF', |
| 142 |
expected: '\uD800' |
| 143 |
} |
| 144 |
}, ( assert, opt ) => { |
| 145 |
opt.$input.appendTo( '#qunit-fixture' ); |
| 146 |
|
| 147 |
|
| 148 |
addChars( opt.$input, opt.sample ); |
| 149 |
|
| 150 |
assert.strictEqual( |
| 151 |
opt.$input.val(), |
| 152 |
opt.expected, |
| 153 |
'New value matches the expected string' |
| 154 |
); |
| 155 |
} ); |
| 156 |
|
| 157 |
QUnit.test( 'Confirm properties and attributes set', ( assert ) => { |
| 158 |
let $el; |
| 159 |
|
| 160 |
$el = $( '<input>' ).attr( 'type', 'text' ) |
| 161 |
.attr( 'maxlength', '7' ) |
| 162 |
.appendTo( '#qunit-fixture' ) |
| 163 |
.byteLimit(); |
| 164 |
|
| 165 |
assert.strictEqual( $el.attr( 'maxlength' ), '7', 'maxlength attribute unchanged for simple limit' ); |
| 166 |
|
| 167 |
$el = $( '<input>' ).attr( 'type', 'text' ) |
| 168 |
.attr( 'maxlength', '7' ) |
| 169 |
.appendTo( '#qunit-fixture' ) |
| 170 |
.byteLimit( 12 ); |
| 171 |
|
| 172 |
assert.strictEqual( $el.attr( 'maxlength' ), '12', 'maxlength attribute updated for custom limit' ); |
| 173 |
|
| 174 |
$el = $( '<input>' ).attr( 'type', 'text' ) |
| 175 |
.attr( 'maxlength', '7' ) |
| 176 |
.appendTo( '#qunit-fixture' ) |
| 177 |
.byteLimit( 12, ( val ) => val ); |
| 178 |
|
| 179 |
assert.strictEqual( $el.attr( 'maxlength' ), undefined, 'maxlength attribute removed for limit with callback' ); |
| 180 |
|
| 181 |
$( '<input>' ).attr( 'type', 'text' ) |
| 182 |
.addClass( 'mw-test-byteLimit-foo' ) |
| 183 |
.attr( 'maxlength', '7' ) |
| 184 |
.appendTo( '#qunit-fixture' ); |
| 185 |
|
| 186 |
$( '<input>' ).attr( 'type', 'text' ) |
| 187 |
.addClass( 'mw-test-byteLimit-foo' ) |
| 188 |
.attr( 'maxlength', '12' ) |
| 189 |
.appendTo( '#qunit-fixture' ); |
| 190 |
|
| 191 |
$el = $( '.mw-test-byteLimit-foo' ); |
| 192 |
|
| 193 |
assert.strictEqual( $el.length, 2, 'Verify that there are no other elements clashing with this test suite' ); |
| 194 |
|
| 195 |
$el.byteLimit(); |
| 196 |
} ); |
| 197 |
|
| 198 |
QUnit.test( 'Trim from insertion when limit exceeded', ( assert ) => { |
| 199 |
let $el; |
| 200 |
|
| 201 |
|
| 202 |
|
| 203 |
$el = $( '<input>' ).attr( 'type', 'text' ) |
| 204 |
.appendTo( '#qunit-fixture' ) |
| 205 |
.byteLimit( 3 ) |
| 206 |
.val( 'abc' ).trigger( 'change' ) |
| 207 |
.val( 'zabc' ).trigger( 'change' ); |
| 208 |
|
| 209 |
assert.strictEqual( $el.val(), 'abc', 'Trim from the insertion point (at 0), not the end' ); |
| 210 |
|
| 211 |
$el = $( '<input>' ).attr( 'type', 'text' ) |
| 212 |
.appendTo( '#qunit-fixture' ) |
| 213 |
.byteLimit( 3 ) |
| 214 |
.val( 'abc' ).trigger( 'change' ) |
| 215 |
.val( 'azbc' ).trigger( 'change' ); |
| 216 |
|
| 217 |
assert.strictEqual( $el.val(), 'abc', 'Trim from the insertion point (at 1), not the end' ); |
| 218 |
} ); |
| 219 |
|
| 220 |
QUnit.test( 'Do not cut up false matching substrings in emoji insertions', ( assert ) => { |
| 221 |
const oldVal = '\uD83D\uDCA9\uD83D\uDCA9', 💩" |
| 222 |
newVal = '\uD83D\uDCA9\uD83D\uDCB9\uD83E\uDCA9\uD83D\uDCA9', 🢩💩" |
| 223 |
expected = '\uD83D\uDCA9\uD83D\uDCB9\uD83D\uDCA9'; 💹💩" |
| 224 |
|
| 225 |
|
| 226 |
|
| 227 |
💹🢩" |
| 228 |
|
| 229 |
💹�" |
| 230 |
|
| 231 |
const $el = $( '<input>' ).attr( 'type', 'text' ) |
| 232 |
.appendTo( '#qunit-fixture' ) |
| 233 |
.byteLimit( 12 ) |
| 234 |
.val( oldVal ).trigger( 'change' ) |
| 235 |
.val( newVal ).trigger( 'change' ); |
| 236 |
|
| 237 |
assert.strictEqual( $el.val(), expected, 'Pasted emoji correctly trimmed at the end' ); |
| 238 |
} ); |
| 239 |
} ); |