Merge "Title: Refactor JS/CSS page handling to be more sane"
[lhc/web/wiklou.git] / tests / qunit / suites / resources / jquery / jquery.byteLimit.test.js
index c7b7cc0..d3233da 100644 (file)
@@ -1,5 +1,5 @@
 ( function ( $, mw ) {
-       var simpleSample, U_20AC, mbSample;
+       var simpleSample, U_20AC, poop, mbSample;
 
        QUnit.module( 'jquery.byteLimit', QUnit.newMwEnvironment() );
 
@@ -9,6 +9,9 @@
        // 3 bytes (euro-symbol)
        U_20AC = '\u20AC';
 
+       // Outside of the BMP (pile of poo emoji)
+       poop = '\uD83D\uDCA9'; // "💩"
+
        // Multi-byte sample (22 chars, 26 bytes)
        mbSample = '1234567890' + U_20AC + '1234567890' + U_20AC;
 
                expected: '1234567890' + U_20AC + '1'
        } );
 
+       byteLimitTest( {
+               description: 'Limit using a custom value (multibyte, outside BMP)',
+               $input: $( '<input>' ).attr( 'type', 'text' )
+                       .byteLimit( 3 ),
+               sample: poop,
+               expected: ''
+       } );
+
        byteLimitTest( {
                description: 'Limit using a custom value (multibyte) overlapping a byte',
                $input: $( '<input>' ).attr( 'type', 'text' )
                        .byteLimit( 12 ),
                sample: mbSample,
-               expected: '1234567890' + '12'
+               expected: '123456789012'
        } );
 
        byteLimitTest( {
        byteLimitTest( {
                description: 'Input filter that increases the length',
                $input: $( '<input>' ).attr( 'type', 'text' )
-               .byteLimit( 10, function ( text ) {
-                       return 'prefix' + text;
-               } ),
+                       .byteLimit( 10, function ( text ) {
+                               return 'prefix' + text;
+                       } ),
                sample: simpleSample,
                // Prefix adds 6 characters, limit is reached after 4
                expected: '1234'
        byteLimitTest( {
                description: 'Input filter of which the base exceeds the limit',
                $input: $( '<input>' ).attr( 'type', 'text' )
-               .byteLimit( 3, function ( text ) {
-                       return 'prefix' + text;
-               } ),
+                       .byteLimit( 3, function ( text ) {
+                               return 'prefix' + text;
+                       } ),
                sample: simpleSample,
-               hasLimit: true,
-               limit: 6, // 'prefix' length
                expected: ''
        } );
 
 
                assert.strictEqual( $el.val(), 'abc', 'Trim from the insertion point (at 1), not the end' );
        } );
+
+       QUnit.test( 'Do not cut up false matching substrings in emoji insertions', function ( assert ) {
+               var $el,
+                       oldVal = '\uD83D\uDCA9\uD83D\uDCA9', // "💩💩"
+                       newVal = '\uD83D\uDCA9\uD83D\uDCB9\uD83E\uDCA9\uD83D\uDCA9', // "💩💹🢩💩"
+                       expected = '\uD83D\uDCA9\uD83D\uDCB9\uD83D\uDCA9'; // "💩💹💩"
+
+               // Possible bad results:
+               // * With no surrogate support:
+               //   '\uD83D\uDCA9\uD83D\uDCB9\uD83E\uDCA9' "💩💹🢩"
+               // * With correct trimming but bad detection of inserted text:
+               //   '\uD83D\uDCA9\uD83D\uDCB9\uDCA9' "💩💹�"
+
+               $el = $( '<input>' ).attr( 'type', 'text' )
+                       .appendTo( '#qunit-fixture' )
+                       .byteLimit( 12 )
+                       .val( oldVal ).trigger( 'change' )
+                       .val( newVal ).trigger( 'change' );
+
+               assert.strictEqual( $el.val(), expected, 'Pasted emoji correctly trimmed at the end' );
+       } );
+
+       byteLimitTest( {
+               description: 'Unpaired surrogates do not crash',
+               $input: $( '<input>' ).attr( 'type', 'text' ).byteLimit( 4 ),
+               sample: '\uD800\uD800\uDFFF',
+               expected: '\uD800'
+       } );
+
 }( jQuery, mediaWiki ) );