Index: trunk/phase3/resources/jquery/jquery.byteLimit.js |
— | — | @@ -7,8 +7,24 @@ |
8 | 8 | * Enforces a byte limit to a textbox, so that UTF-8 entries are not arbitrarily truncated. |
9 | 9 | */ |
10 | 10 | $.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 |
13 | 29 | // being pressed. |
14 | 30 | // Based on key-event info from http://unixpapa.com/js/key.html |
15 | 31 | // jQuery should also normalize e.which to be consistent cross-browser, |
— | — | @@ -27,11 +43,15 @@ |
28 | 44 | // This basically figures out how many bytes a UTF-16 string (which is what js sees) |
29 | 45 | // will take in UTF-8 by replacing a 2 byte character with 2 *'s, etc, and counting that. |
30 | 46 | // 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. |
33 | 49 | |
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. |
36 | 56 | if ( len > ( limit-3 ) ) { |
37 | 57 | e.preventDefault(); |
38 | 58 | } |
Index: trunk/phase3/resources/mediawiki.special/mediawiki.special.movePage.js |
— | — | @@ -1,5 +1,5 @@ |
2 | 2 | /* JavaScript for Special:MovePage */ |
3 | 3 | |
4 | 4 | jQuery( function( $ ) { |
5 | | - $( '#wpReason' ).byteLimit( 200 ); |
| 5 | + $( '#wpReason' ).byteLimit(); |
6 | 6 | }); |