From e7cd380dee656bfb08278116994490600bafe54f Mon Sep 17 00:00:00 2001 From: umherirrender Date: Mon, 13 May 2013 20:34:26 +0200 Subject: [PATCH] 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 --- resources/jquery/jquery.byteLimit.js | 3 ++- .../resources/jquery/jquery.byteLimit.test.js | 13 +++++++++++++ 2 files changed, 15 insertions(+), 1 deletion(-) 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; -- 2.20.1