From: Roan Kattouw Date: Fri, 22 Apr 2011 10:58:58 +0000 (+0000) Subject: (bug 28650) Refactor dynamic byte-based maxlength of edit summary into a jQuery plugi... X-Git-Tag: 1.31.0-rc.0~30640 X-Git-Url: https://git.cyclocoop.org/%7B%24www_url%7Dadmin/compta/banques/%7B%7B%20url_for%28%27admin_users%27%29%20%7D%7D?a=commitdiff_plain;h=233a49e612528de6b3f1cc4c585a5b9d92380dbe;p=lhc%2Fweb%2Fwiklou.git (bug 28650) Refactor dynamic byte-based maxlength of edit summary into a jQuery plugin. Patch by Jan Paul Posma --- diff --git a/CREDITS b/CREDITS index 9c8c010c7a..c656f9ce15 100644 --- a/CREDITS +++ b/CREDITS @@ -92,6 +92,7 @@ following names for their contribution to the product. * FunPika * Harry Burt * Ireas +* Jan Paul Posma * Jaska Zedlik * Jeremy Baron * Jidanni diff --git a/resources/Resources.php b/resources/Resources.php index 16832b23a0..876874a464 100644 --- a/resources/Resources.php +++ b/resources/Resources.php @@ -73,6 +73,9 @@ return array( 'scripts' => 'resources/jquery/jquery.autoEllipsis.js', 'dependencies' => 'jquery.highlightText', ), + 'jquery.byteLimit' => array( + 'scripts' => 'resources/jquery/jquery.byteLimit.js', + ), 'jquery.checkboxShiftClick' => array( 'scripts' => 'resources/jquery/jquery.checkboxShiftClick.js', ), @@ -415,7 +418,10 @@ return array( ), 'mediawiki.action.edit' => array( 'scripts' => 'resources/mediawiki.action/mediawiki.action.edit.js', - 'dependencies' => 'jquery.textSelection', + 'dependencies' => array( + 'jquery.textSelection', + 'jquery.byteLimit', + ), ), 'mediawiki.action.view.rightClickEdit' => array( 'scripts' => 'resources/mediawiki.action/mediawiki.action.view.rightClickEdit.js', diff --git a/resources/jquery/jquery.byteLimit.js b/resources/jquery/jquery.byteLimit.js new file mode 100644 index 0000000000..800dec606c --- /dev/null +++ b/resources/jquery/jquery.byteLimit.js @@ -0,0 +1,41 @@ +/** + * jQuery byteLimit + */ +( function( $ ) { + + /** + * 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 + // 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, + // however the same check is still needed regardless of jQuery. + + // Note: At the moment, for some older opera versions (~< 10.5) + // some special keys won't be recognized (aka left arrow key). + // Backspace will be, so not big issue. + + if ( e.which === 0 || e.charCode === 0 || e.which === 8 || + e.ctrlKey || e.altKey || e.metaKey ) + { + return true; //a special key (backspace, etc) so don't interfere. + } + + // 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. + + 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. + if ( len > ( limit-3 ) ) { + e.preventDefault(); + } + }); + }; + +} )( jQuery ); diff --git a/resources/mediawiki.action/mediawiki.action.edit.js b/resources/mediawiki.action/mediawiki.action.edit.js index 61c1bfe64f..3005c17f4a 100644 --- a/resources/mediawiki.action/mediawiki.action.edit.js +++ b/resources/mediawiki.action/mediawiki.action.edit.js @@ -51,36 +51,7 @@ window.insertTags = mw.toolbar.insertTags; //make sure edit summary does not exceed byte limit - $( '#wpSummary' ).attr( 'maxLength', 250 ).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, - // however the same check is still needed regardless of jQuery. - - // Note: At the moment, for some older opera versions (~< 10.5) - // some special keys won't be recognized (aka left arrow key). - // Backspace will be, so not big issue. - - if ( e.which === 0 || e.charCode === 0 || e.which === 8 || - e.ctrlKey || e.altKey || e.metaKey ) - { - return true; //a special key (backspace, etc) so don't interfere. - } - - // 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. - - var len = this.value.replace( /[\u0080-\u07FF\uD800-\uDFFF]/g, '**' ).replace( /[\u0800-\uD7FF\uE000-\uFFFF]/g, '***' ).length; - //247 as this doesn't count character about to be inserted. - if ( len > 247 ) { - e.preventDefault(); - } - }); - + $( '#wpSummary' ).byteLimit( 250 ); $( document ).ready( function() { /**