r91891 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r91890‎ | r91891 | r91892 >
Date:17:53, 11 July 2011
Author:brion
Status:ok
Tags:
Comment:
* (bug 29804) Fix byte-length-limited fields to check the length of to-be-added char instead of hardcoding a possible length of 3.

Since the Unicode code point of the character being inserted is passed with the keypress event, we can simply turn it into a string and toss it into jquery.byteLength to calculate the length in bytes.

Also fixed a broken test case that had the wrong number of expected chars.

Note that this will probably still not work right in many circumstances -- pastes, anything dealing with selection, and it's entirely possible that individual 'keypress' events might be totally unreliable when dealing with funky input methods.
Modified paths:
  • /trunk/phase3/resources/jquery/jquery.byteLimit.js (modified) (history)
  • /trunk/phase3/tests/qunit/suites/resources/jquery/jquery.byteLimit.js (modified) (history)

Diff [purge]

Index: trunk/phase3/tests/qunit/suites/resources/jquery/jquery.byteLimit.js
@@ -151,5 +151,5 @@
152152 sample: mbSample,
153153 useLimit: true,
154154 limit: 12,
155 - expected: 10 // 10 x 1-byte char. The next 3-byte char exceeds limit of 12
 155+ expected: 12 // 10 x 1-byte char. The next 3-byte char exceeds limit of 12, but 2 more 1-byte chars come in after.
156156 });
Index: trunk/phase3/resources/jquery/jquery.byteLimit.js
@@ -43,9 +43,11 @@
4444 }
4545
4646 var len = $.byteLength( this.value );
 47+ // Note that keypress returns a character code point, not a keycode.
 48+ // However, this may not be super reliable depending on how keys come in...
 49+ var charLen = $.byteLength( String.fromCharCode( e.which ) );
4750
48 - // limit-3 as this doesn't count the character about to be inserted.
49 - if ( len > ( limit-3 ) ) {
 51+ if ( ( len + charLen ) > limit ) {
5052 e.preventDefault();
5153 }
5254 });

Past revisions this follows-up on

RevisionCommit summaryAuthorDate
r91844Add test suite for jquery.byteLimit:...krinkle21:01, 10 July 2011

Status & tagging log