From: Krinkle Date: Tue, 26 Apr 2011 20:00:59 +0000 (+0000) Subject: jquery.byteLimit fixes: X-Git-Tag: 1.31.0-rc.0~30542 X-Git-Url: https://git.cyclocoop.org/%27.WWW_URL.%27admin/?a=commitdiff_plain;h=dc32287e25b8768c6c039b84ceb4eda05e313c14;p=lhc%2Fweb%2Fwiklou.git jquery.byteLimit fixes: * 'this' in a fn/prototype context already points to the jQuery object. Calling $(this) is redundant and goes through $.fn.init again untill it reaches the last else case (luckily $.fn.init() has a failsafe case for that, but also makes it harder to spot) * Adding support for using the current attribute value (to avoid having to duplicate the values between PHP and JavaScript). Fully backwards compatible, I think it's a very handy feature to be able to pass a custom length, but using the maxLength attribute as a default makes sense. --- diff --git a/resources/jquery/jquery.byteLimit.js b/resources/jquery/jquery.byteLimit.js index 800dec606c..e45aa632d2 100644 --- a/resources/jquery/jquery.byteLimit.js +++ b/resources/jquery/jquery.byteLimit.js @@ -7,8 +7,24 @@ * Enforces a byte limit to a textbox, so that UTF-8 entries are not arbitrarily truncated. */ $.fn.byteLimit = function( limit ) { - return $(this).attr( 'maxLength', limit ).keypress( function( e ) { - // first check to see if this is actually a character key + + // Default to current attribute value + if ( limit == null ) { + limit = this.attr( 'maxLength' ); + + // If passed, update/set attribute value instead + } else { + this.attr( 'maxLength', limit ); + } + + // Nothing passed and/or empty attribute, return this for further chaining. + if ( limit == null ) { + return this; + } + + // We've got something, go for it: + return this.keypress( function( e ) { + // First check to see if this is actually a character key // being pressed. // Based on key-event info from http://unixpapa.com/js/key.html // jQuery should also normalize e.which to be consistent cross-browser, @@ -27,11 +43,15 @@ // This basically figures out how many bytes a UTF-16 string (which is what js sees) // will take in UTF-8 by replacing a 2 byte character with 2 *'s, etc, and counting that. // Note, surrogate (\uD800-\uDFFF) characters are counted as 2 bytes, since there's two of them - // and the actual character takes 4 bytes in UTF-8 (2*2=4). Might not work perfectly in edge cases - // such as illegal sequences, but that should never happen. + // and the actual character takes 4 bytes in UTF-8 (2*2=4). Might not work perfectly in + // edge cases such as illegal sequences, but that should never happen. - var len = this.value.replace( /[\u0080-\u07FF\uD800-\uDFFF]/g, '**' ).replace( /[\u0800-\uD7FF\uE000-\uFFFF]/g, '***' ).length; - // limit-3 as this doesn't count character about to be inserted. + var len = this.value + .replace( /[\u0080-\u07FF\uD800-\uDFFF]/g, '**' ) + .replace( /[\u0800-\uD7FF\uE000-\uFFFF]/g, '***' ) + .length; + + // limit-3 as this doesn't count the character about to be inserted. if ( len > ( limit-3 ) ) { e.preventDefault(); } diff --git a/resources/mediawiki.special/mediawiki.special.movePage.js b/resources/mediawiki.special/mediawiki.special.movePage.js index 4ae0dac43e..2f94cc06c3 100644 --- a/resources/mediawiki.special/mediawiki.special.movePage.js +++ b/resources/mediawiki.special/mediawiki.special.movePage.js @@ -1,5 +1,5 @@ /* JavaScript for Special:MovePage */ jQuery( function( $ ) { - $( '#wpReason' ).byteLimit( 200 ); + $( '#wpReason' ).byteLimit(); });