r86982 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r86981‎ | r86982 | r86983 >
Date:20:00, 26 April 2011
Author:krinkle
Status:ok (Comments)
Tags:
Comment:
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.
Modified paths:
  • /trunk/phase3/resources/jquery/jquery.byteLimit.js (modified) (history)
  • /trunk/phase3/resources/mediawiki.special/mediawiki.special.movePage.js (modified) (history)

Diff [purge]

Index: trunk/phase3/resources/jquery/jquery.byteLimit.js
@@ -7,8 +7,24 @@
88 * Enforces a byte limit to a textbox, so that UTF-8 entries are not arbitrarily truncated.
99 */
1010 $.fn.byteLimit = function( limit ) {
11 - return $(this).attr( 'maxLength', limit ).keypress( function( e ) {
12 - // first check to see if this is actually a character key
 11+
 12+ // Default to current attribute value
 13+ if ( limit == null ) {
 14+ limit = this.attr( 'maxLength' );
 15+
 16+ // If passed, update/set attribute value instead
 17+ } else {
 18+ this.attr( 'maxLength', limit );
 19+ }
 20+
 21+ // Nothing passed and/or empty attribute, return this for further chaining.
 22+ if ( limit == null ) {
 23+ return this;
 24+ }
 25+
 26+ // We've got something, go for it:
 27+ return this.keypress( function( e ) {
 28+ // First check to see if this is actually a character key
1329 // being pressed.
1430 // Based on key-event info from http://unixpapa.com/js/key.html
1531 // jQuery should also normalize e.which to be consistent cross-browser,
@@ -27,11 +43,15 @@
2844 // This basically figures out how many bytes a UTF-16 string (which is what js sees)
2945 // will take in UTF-8 by replacing a 2 byte character with 2 *'s, etc, and counting that.
3046 // Note, surrogate (\uD800-\uDFFF) characters are counted as 2 bytes, since there's two of them
31 - // and the actual character takes 4 bytes in UTF-8 (2*2=4). Might not work perfectly in edge cases
32 - // such as illegal sequences, but that should never happen.
 47+ // and the actual character takes 4 bytes in UTF-8 (2*2=4). Might not work perfectly in
 48+ // edge cases such as illegal sequences, but that should never happen.
3349
34 - var len = this.value.replace( /[\u0080-\u07FF\uD800-\uDFFF]/g, '**' ).replace( /[\u0800-\uD7FF\uE000-\uFFFF]/g, '***' ).length;
35 - // limit-3 as this doesn't count character about to be inserted.
 50+ var len = this.value
 51+ .replace( /[\u0080-\u07FF\uD800-\uDFFF]/g, '**' )
 52+ .replace( /[\u0800-\uD7FF\uE000-\uFFFF]/g, '***' )
 53+ .length;
 54+
 55+ // limit-3 as this doesn't count the character about to be inserted.
3656 if ( len > ( limit-3 ) ) {
3757 e.preventDefault();
3858 }
Index: trunk/phase3/resources/mediawiki.special/mediawiki.special.movePage.js
@@ -1,5 +1,5 @@
22 /* JavaScript for Special:MovePage */
33
44 jQuery( function( $ ) {
5 - $( '#wpReason' ).byteLimit( 200 );
 5+ $( '#wpReason' ).byteLimit();
66 });

Comments

#Comment by Brion VIBBER (talk | contribs)   20:34, 14 June 2011

Tagging for qunit test cases.

#Comment by Krinkle (talk | contribs)   01:08, 30 June 2011

Done in r91148 (byteLength logic)

Status & tagging log