From 88c0badb351937f4eb9ca6a1daa4e0981af01273 Mon Sep 17 00:00:00 2001 From: MatmaRex Date: Thu, 15 Nov 2012 13:23:44 +0100 Subject: [PATCH] (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 --- resources/jquery/jquery.byteLimit.js | 5 ++++- .../resources/jquery/jquery.byteLimit.test.js | 22 +++++++++++++++++++ 2 files changed, 26 insertions(+), 1 deletion(-) 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 ) ); -- 2.20.1