jquery.byteLimit: Fix infinite loop if text is longer than max
authorumherirrender <umherirrender_de.wp@web.de>
Mon, 13 May 2013 18:34:26 +0000 (20:34 +0200)
committerGerrit Code Review <gerrit@wikimedia.org>
Fri, 24 May 2013 09:26:47 +0000 (09:26 +0000)
When the length of the returned text of the callback is longer than the
max, the slicing of the input text, should not go into a endless loop.

Without this change, the added unit tests would fail as a result of
the infinite loop.

Bug: 41450
Change-Id: Iad84522659ab160e18828cbfc463db83bc7e4795

resources/jquery/jquery.byteLimit.js
tests/qunit/suites/resources/jquery/jquery.byteLimit.test.js

index f2b98f0..a8c0b06 100644 (file)
@@ -78,7 +78,8 @@
                // Chop off characters from the end of the "inserted content" string
                // until the limit is statisfied.
                if ( fn ) {
-                       while ( $.byteLength( fn( inpParts.join( '' ) ) ) > byteLimit ) {
+                       // stop, when there is nothing to slice - bug 41450
+                       while ( $.byteLength( fn( inpParts.join( '' ) ) ) > byteLimit && inpParts[1].length > 0 ) {
                                inpParts[1] = inpParts[1].slice( 0, -1 );
                        }
                } else {
index 7f4494e..596c57c 100644 (file)
                expected: '1234'
        } );
 
+       // Regression tests for bug 41450
+       byteLimitTest( {
+               description: 'Input filter of which the base exceeds the limit',
+               $input: $( '<input type="text"/>' )
+               .byteLimit( 3, function ( text ) {
+                       return 'prefix' + text;
+               } ),
+               sample: simpleSample,
+               hasLimit: true,
+               limit: 6, // 'prefix' length
+               expected: ''
+       } );
+
        QUnit.test( 'Confirm properties and attributes set', 4, function ( assert ) {
                var $el, $elA, $elB;