r91148 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r91147‎ | r91148 | r91149 >
Date:01:06, 30 June 2011
Author:krinkle
Status:ok
Tags:
Comment:
byteLimit/byteLength improvements:
* Split the byte length logic into a seperate method to allow it to be called directly on a string (easier to test and easier re-use)
* Added basic unit tests for it.
Modified paths:
  • /trunk/phase3/resources/Resources.php (modified) (history)
  • /trunk/phase3/resources/jquery/jquery.byteLength.js (added) (history)
  • /trunk/phase3/resources/jquery/jquery.byteLimit.js (modified) (history)
  • /trunk/phase3/tests/qunit/index.html (modified) (history)
  • /trunk/phase3/tests/qunit/suites/resources/jquery/jquery.byteLength.js (added) (history)

Diff [purge]

Index: trunk/phase3/tests/qunit/index.html
@@ -40,6 +40,7 @@
4141
4242 <!-- MW: Non-default modules -->
4343 <script src="../../resources/jquery/jquery.autoEllipsis.js"></script>
 44+ <script src="../../resources/jquery/jquery.byteLength.js"></script>
4445 <script src="../../resources/jquery/jquery.colorUtil.js"></script>
4546 <script src="../../resources/jquery/jquery.tabIndex.js"></script>
4647 <script src="../../resources/jquery/jquery.tablesorter.js"></script>
@@ -60,6 +61,7 @@
6162 <script src="suites/resources/mediawiki/mediawiki.util.js"></script>
6263
6364 <script src="suites/resources/jquery/jquery.autoEllipsis.js"></script>
 65+ <script src="suites/resources/jquery/jquery.byteLength.js"></script>
6466 <script src="suites/resources/jquery/jquery.colorUtil.js"></script>
6567 <script src="suites/resources/jquery/jquery.tabIndex.js"></script>
6668 <script src="suites/resources/jquery/jquery.tablesorter.test.js" charset="UTF-8"></script>
Index: trunk/phase3/tests/qunit/suites/resources/jquery/jquery.byteLength.js
@@ -0,0 +1,38 @@
 2+module( 'jquery.byteLength.js' );
 3+
 4+test( '-- Initial check', function() {
 5+ expect(1);
 6+ ok( $.byteLength, 'jQuery.byteLength defined' );
 7+} );
 8+
 9+test( 'Simple text', function() {
 10+ expect(5);
 11+
 12+ var azLc = 'abcdefghijklmnopqrstuvwxyz',
 13+ azUc = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ',
 14+ num = '0123456789',
 15+ x = '*',
 16+ space = ' ';
 17+
 18+ equal( $.byteLength( azLc ), 26, 'Lowercase a-z' );
 19+ equal( $.byteLength( azUc ), 26, 'Uppercase A-Z' );
 20+ equal( $.byteLength( num ), 10, 'Numbers 0-9' );
 21+ equal( $.byteLength( x ), 1, 'An asterisk' );
 22+ equal( $.byteLength( space ), 3, '3 spaces' );
 23+
 24+} );
 25+
 26+test( 'Special text', window.foo = function() {
 27+ expect(4);
 28+
 29+ // http://en.wikipedia.org/wiki/UTF-8
 30+ var U_0024 = '\u0024',
 31+ U_00A2 = '\u00A2',
 32+ U_20AC = '\u20AC',
 33+ U_024B62 = '\u024B62';
 34+
 35+ strictEqual( $.byteLength( U_0024 ), 1, 'U+0024: 1 byte (dollar sign) $' );
 36+ strictEqual( $.byteLength( U_00A2 ), 2, 'U+00A2: 2 bytes (cent sign) &#162;' );
 37+ strictEqual( $.byteLength( U_20AC ), 3, 'U+20AC: 3 bytes (euro sign) &#8364;' );
 38+ strictEqual( $.byteLength( U_024B62 ), 4, 'U+024B62: 4 bytes &#150370; \U00024B62 ' );
 39+} );
Property changes on: trunk/phase3/tests/qunit/suites/resources/jquery/jquery.byteLength.js
___________________________________________________________________
Added: svn:eol-style
140 + native
Index: trunk/phase3/resources/jquery/jquery.byteLimit.js
@@ -1,5 +1,7 @@
22 /**
33 * jQuery byteLimit
 4+ *
 5+ * @author Jan Paul Posma
46 */
57 ( function( $ ) {
68
@@ -17,7 +19,7 @@
1820 this.attr( 'maxLength', limit );
1921 }
2022
21 - // Nothing passed and/or empty attribute, return this for further chaining.
 23+ // Nothing passed and/or empty attribute, return without binding an event.
2224 if ( limit == null ) {
2325 return this;
2426 }
@@ -40,16 +42,7 @@
4143 return true; //a special key (backspace, etc) so don't interfere.
4244 }
4345
44 - // This basically figures out how many bytes a UTF-16 string (which is what js sees)
45 - // will take in UTF-8 by replacing a 2 byte character with 2 *'s, etc, and counting that.
46 - // Note, surrogate (\uD800-\uDFFF) characters are counted as 2 bytes, since there's two of them
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.
49 -
50 - var len = this.value
51 - .replace( /[\u0080-\u07FF\uD800-\uDFFF]/g, '**' )
52 - .replace( /[\u0800-\uD7FF\uE000-\uFFFF]/g, '***' )
53 - .length;
 46+ var len = $.byteLength( this.value );
5447
5548 // limit-3 as this doesn't count the character about to be inserted.
5649 if ( len > ( limit-3 ) ) {
Index: trunk/phase3/resources/jquery/jquery.byteLength.js
@@ -0,0 +1,19 @@
 2+/**
 3+ * jQuery.byteLength
 4+ *
 5+ * Calculate the byte length of a string (accounting for UTF-8).
 6+ *
 7+ * @author Jan Paul Posma
 8+ */
 9+jQuery.byteLength = function( str ) {
 10+
 11+ // This basically figures out how many bytes a UTF-16 string (which is what js sees)
 12+ // will take in UTF-8 by replacing a 2 byte character with 2 *'s, etc, and counting that.
 13+ // Note, surrogate (\uD800-\uDFFF) characters are counted as 2 bytes, since there's two of them
 14+ // and the actual character takes 4 bytes in UTF-8 (2*2=4). Might not work perfectly in
 15+ // edge cases such as illegal sequences, but that should never happen.
 16+ return str
 17+ .replace( /[\u0080-\u07FF\uD800-\uDFFF]/g, '**' )
 18+ .replace( /[\u0800-\uD7FF\uE000-\uFFFF]/g, '***' )
 19+ .length;
 20+}
Property changes on: trunk/phase3/resources/jquery/jquery.byteLength.js
___________________________________________________________________
Added: svn:eol-style
121 + native
Index: trunk/phase3/resources/Resources.php
@@ -79,8 +79,12 @@
8080 'scripts' => 'resources/jquery/jquery.autoEllipsis.js',
8181 'dependencies' => 'jquery.highlightText',
8282 ),
 83+ 'jquery.byteLength' => array(
 84+ 'scripts' => 'resources/jquery/jquery.byteLength.js',
 85+ ),
8386 'jquery.byteLimit' => array(
8487 'scripts' => 'resources/jquery/jquery.byteLimit.js',
 88+ 'dependencies' => 'jquery.byteLength',
8589 ),
8690 'jquery.checkboxShiftClick' => array(
8791 'scripts' => 'resources/jquery/jquery.checkboxShiftClick.js',

Follow-up revisions

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

Past revisions this follows-up on

RevisionCommit summaryAuthorDate
r86698(bug 28650) Refactor dynamic byte-based maxlength of edit summary into a jQue...catrope10:58, 22 April 2011

Status & tagging log