From: MatmaRex Date: Thu, 15 Nov 2012 12:23:44 +0000 (+0100) Subject: (bug 40850) jquery.byteLimit: Always update prevSafeVal. X-Git-Tag: 1.31.0-rc.0~21615^2 X-Git-Url: http://git.cyclocoop.org/%7B%24www_url%7Dadmin/password.php?a=commitdiff_plain;h=88c0badb351937f4eb9ca6a1daa4e0981af01273;p=lhc%2Fweb%2Fwiklou.git (bug 40850) jquery.byteLimit: Always update prevSafeVal. Not doing this sometimes caused trimValForByteLength to compare the new value to an empty string instead of the old value, resulting in trimming always at the end instead of at the position of insertion. Change-Id: I2e46961efa4f82732d577f7e5f98fc80719c88bb --- diff --git a/resources/jquery/jquery.byteLimit.js b/resources/jquery/jquery.byteLimit.js index 75dc2b908a..f2b98f09bb 100644 --- a/resources/jquery/jquery.byteLimit.js +++ b/resources/jquery/jquery.byteLimit.js @@ -221,8 +221,11 @@ // This is a side-effect of limiting after the fact. if ( res.trimmed === true ) { this.value = res.newVal; - prevSafeVal = res.newVal; } + // Always adjust prevSafeVal to reflect the input value. Not doing this could cause + // trimValForByteLength to compare the new value to an empty string instead of the + // old value, resulting in trimming always from the end (bug 40850). + prevSafeVal = res.newVal; } ); } ); }; diff --git a/tests/qunit/suites/resources/jquery/jquery.byteLimit.test.js b/tests/qunit/suites/resources/jquery/jquery.byteLimit.test.js index 4fe6770296..2990de2148 100644 --- a/tests/qunit/suites/resources/jquery/jquery.byteLimit.test.js +++ b/tests/qunit/suites/resources/jquery/jquery.byteLimit.test.js @@ -232,4 +232,26 @@ $el.byteLimit(); }); + QUnit.test( 'Trim from insertion when limit exceeded', 2, function ( assert ) { + var $el; + + // Use a new because the bug only occurs on the first time + // the limit it reached (bug 40850) + $el = $( '' ) + .appendTo( '#qunit-fixture' ) + .byteLimit( 3 ) + .val( 'abc' ).trigger( 'change' ) + .val( 'zabc' ).trigger( 'change' ); + + assert.strictEqual( $el.val(), 'abc', 'Trim from the insertion point (at 0), not the end' ); + + $el = $( '' ) + .appendTo( '#qunit-fixture' ) + .byteLimit( 3 ) + .val( 'abc' ).trigger( 'change' ) + .val( 'azbc' ).trigger( 'change' ); + + assert.strictEqual( $el.val(), 'abc', 'Trim from the insertion point (at 1), not the end' ); + }); + }( jQuery, mediaWiki ) );