From: umherirrender Date: Mon, 13 May 2013 18:34:26 +0000 (+0200) Subject: jquery.byteLimit: Fix infinite loop if text is longer than max X-Git-Tag: 1.31.0-rc.0~19593^2 X-Git-Url: https://git.cyclocoop.org/%28%28?a=commitdiff_plain;h=e7cd380dee656bfb08278116994490600bafe54f;p=lhc%2Fweb%2Fwiklou.git jquery.byteLimit: Fix infinite loop if text is longer than max 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 --- diff --git a/resources/jquery/jquery.byteLimit.js b/resources/jquery/jquery.byteLimit.js index f2b98f09bb..a8c0b06590 100644 --- a/resources/jquery/jquery.byteLimit.js +++ b/resources/jquery/jquery.byteLimit.js @@ -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 { diff --git a/tests/qunit/suites/resources/jquery/jquery.byteLimit.test.js b/tests/qunit/suites/resources/jquery/jquery.byteLimit.test.js index 7f4494eefd..596c57c5ba 100644 --- a/tests/qunit/suites/resources/jquery/jquery.byteLimit.test.js +++ b/tests/qunit/suites/resources/jquery/jquery.byteLimit.test.js @@ -184,6 +184,19 @@ expected: '1234' } ); + // Regression tests for bug 41450 + byteLimitTest( { + description: 'Input filter of which the base exceeds the limit', + $input: $( '' ) + .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;