// 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;
} );
} );
};
$el.byteLimit();
});
+ QUnit.test( 'Trim from insertion when limit exceeded', 2, function ( assert ) {
+ var $el;
+
+ // Use a new <input /> because the bug only occurs on the first time
+ // the limit it reached (bug 40850)
+ $el = $( '<input type="text"/>' )
+ .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 = $( '<input type="text"/>' )
+ .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 ) );