r86698 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r86697‎ | r86698 | r86699 >
Date:10:58, 22 April 2011
Author:catrope
Status:ok (Comments)
Tags:
Comment:
(bug 28650) Refactor dynamic byte-based maxlength of edit summary into a jQuery plugin. Patch by Jan Paul Posma
Modified paths:
  • /trunk/phase3/CREDITS (modified) (history)
  • /trunk/phase3/resources/Resources.php (modified) (history)
  • /trunk/phase3/resources/jquery/jquery.byteLimit.js (added) (history)
  • /trunk/phase3/resources/mediawiki.action/mediawiki.action.edit.js (modified) (history)

Diff [purge]

Index: trunk/phase3/CREDITS
@@ -92,6 +92,7 @@
9393 * FunPika
9494 * Harry Burt
9595 * Ireas
 96+* Jan Paul Posma
9697 * Jaska Zedlik
9798 * Jeremy Baron
9899 * Jidanni
Index: trunk/phase3/resources/jquery/jquery.byteLimit.js
@@ -0,0 +1,41 @@
 2+/**
 3+ * jQuery byteLimit
 4+ */
 5+( function( $ ) {
 6+
 7+ /**
 8+ * Enforces a byte limit to a textbox, so that UTF-8 entries are not arbitrarily truncated.
 9+ */
 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
 13+ // being pressed.
 14+ // Based on key-event info from http://unixpapa.com/js/key.html
 15+ // jQuery should also normalize e.which to be consistent cross-browser,
 16+ // however the same check is still needed regardless of jQuery.
 17+
 18+ // Note: At the moment, for some older opera versions (~< 10.5)
 19+ // some special keys won't be recognized (aka left arrow key).
 20+ // Backspace will be, so not big issue.
 21+
 22+ if ( e.which === 0 || e.charCode === 0 || e.which === 8 ||
 23+ e.ctrlKey || e.altKey || e.metaKey )
 24+ {
 25+ return true; //a special key (backspace, etc) so don't interfere.
 26+ }
 27+
 28+ // This basically figures out how many bytes a UTF-16 string (which is what js sees)
 29+ // will take in UTF-8 by replacing a 2 byte character with 2 *'s, etc, and counting that.
 30+ // 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.
 33+
 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.
 36+ if ( len > ( limit-3 ) ) {
 37+ e.preventDefault();
 38+ }
 39+ });
 40+ };
 41+
 42+} )( jQuery );
Property changes on: trunk/phase3/resources/jquery/jquery.byteLimit.js
___________________________________________________________________
Added: svn:eol-style
143 + native
Index: trunk/phase3/resources/mediawiki.action/mediawiki.action.edit.js
@@ -51,37 +51,8 @@
5252 window.insertTags = mw.toolbar.insertTags;
5353
5454 //make sure edit summary does not exceed byte limit
55 - $( '#wpSummary' ).attr( 'maxLength', 250 ).keypress( function( e ) {
56 - // first check to see if this is actually a character key
57 - // being pressed.
58 - // Based on key-event info from http://unixpapa.com/js/key.html
59 - // jQuery should also normalize e.which to be consistent cross-browser,
60 - // however the same check is still needed regardless of jQuery.
61 -
62 - // Note: At the moment, for some older opera versions (~< 10.5)
63 - // some special keys won't be recognized (aka left arrow key).
64 - // Backspace will be, so not big issue.
65 -
66 - if ( e.which === 0 || e.charCode === 0 || e.which === 8 ||
67 - e.ctrlKey || e.altKey || e.metaKey )
68 - {
69 - return true; //a special key (backspace, etc) so don't interfere.
70 - }
71 -
72 - // This basically figures out how many bytes a UTF-16 string (which is what js sees)
73 - // will take in UTF-8 by replacing a 2 byte character with 2 *'s, etc, and counting that.
74 - // Note, surrogate (\uD800-\uDFFF) characters are counted as 2 bytes, since there's two of them
75 - // and the actual character takes 4 bytes in UTF-8 (2*2=4). Might not work perfectly in edge cases
76 - // such as illegal sequences, but that should never happen.
77 -
78 - var len = this.value.replace( /[\u0080-\u07FF\uD800-\uDFFF]/g, '**' ).replace( /[\u0800-\uD7FF\uE000-\uFFFF]/g, '***' ).length;
79 - //247 as this doesn't count character about to be inserted.
80 - if ( len > 247 ) {
81 - e.preventDefault();
82 - }
83 - });
 55+ $( '#wpSummary' ).byteLimit( 250 );
8456
85 -
8657 $( document ).ready( function() {
8758 /**
8859 * Restore the edit box scroll state following a preview operation,
Index: trunk/phase3/resources/Resources.php
@@ -73,6 +73,9 @@
7474 'scripts' => 'resources/jquery/jquery.autoEllipsis.js',
7575 'dependencies' => 'jquery.highlightText',
7676 ),
 77+ 'jquery.byteLimit' => array(
 78+ 'scripts' => 'resources/jquery/jquery.byteLimit.js',
 79+ ),
7780 'jquery.checkboxShiftClick' => array(
7881 'scripts' => 'resources/jquery/jquery.checkboxShiftClick.js',
7982 ),
@@ -415,7 +418,10 @@
416419 ),
417420 'mediawiki.action.edit' => array(
418421 'scripts' => 'resources/mediawiki.action/mediawiki.action.edit.js',
419 - 'dependencies' => 'jquery.textSelection',
 422+ 'dependencies' => array(
 423+ 'jquery.textSelection',
 424+ 'jquery.byteLimit',
 425+ ),
420426 ),
421427 'mediawiki.action.view.rightClickEdit' => array(
422428 'scripts' => 'resources/mediawiki.action/mediawiki.action.view.rightClickEdit.js',

Follow-up revisions

RevisionCommit summaryAuthorDate
r91148byteLimit/byteLength improvements:...krinkle01:06, 30 June 2011
r91844Add test suite for jquery.byteLimit:...krinkle21:01, 10 July 2011

Comments

#Comment by Krinkle (talk | contribs)   00:08, 12 June 2011

We need a test suite to confirm the byteLength/byteLimit works properly.

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

Done in r91148.

Status & tagging log