Index: trunk/phase3/tests/qunit/index.html |
— | — | @@ -40,6 +40,7 @@ |
41 | 41 | |
42 | 42 | <!-- MW: Non-default modules --> |
43 | 43 | <script src="../../resources/jquery/jquery.autoEllipsis.js"></script> |
| 44 | + <script src="../../resources/jquery/jquery.byteLength.js"></script> |
44 | 45 | <script src="../../resources/jquery/jquery.colorUtil.js"></script> |
45 | 46 | <script src="../../resources/jquery/jquery.tabIndex.js"></script> |
46 | 47 | <script src="../../resources/jquery/jquery.tablesorter.js"></script> |
— | — | @@ -60,6 +61,7 @@ |
61 | 62 | <script src="suites/resources/mediawiki/mediawiki.util.js"></script> |
62 | 63 | |
63 | 64 | <script src="suites/resources/jquery/jquery.autoEllipsis.js"></script> |
| 65 | + <script src="suites/resources/jquery/jquery.byteLength.js"></script> |
64 | 66 | <script src="suites/resources/jquery/jquery.colorUtil.js"></script> |
65 | 67 | <script src="suites/resources/jquery/jquery.tabIndex.js"></script> |
66 | 68 | <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) ¢' ); |
| 37 | + strictEqual( $.byteLength( U_20AC ), 3, 'U+20AC: 3 bytes (euro sign) €' ); |
| 38 | + strictEqual( $.byteLength( U_024B62 ), 4, 'U+024B62: 4 bytes 𤭢 \U00024B62 ' ); |
| 39 | +} ); |
Property changes on: trunk/phase3/tests/qunit/suites/resources/jquery/jquery.byteLength.js |
___________________________________________________________________ |
Added: svn:eol-style |
1 | 40 | + native |
Index: trunk/phase3/resources/jquery/jquery.byteLimit.js |
— | — | @@ -1,5 +1,7 @@ |
2 | 2 | /** |
3 | 3 | * jQuery byteLimit |
| 4 | + * |
| 5 | + * @author Jan Paul Posma |
4 | 6 | */ |
5 | 7 | ( function( $ ) { |
6 | 8 | |
— | — | @@ -17,7 +19,7 @@ |
18 | 20 | this.attr( 'maxLength', limit ); |
19 | 21 | } |
20 | 22 | |
21 | | - // Nothing passed and/or empty attribute, return this for further chaining. |
| 23 | + // Nothing passed and/or empty attribute, return without binding an event. |
22 | 24 | if ( limit == null ) { |
23 | 25 | return this; |
24 | 26 | } |
— | — | @@ -40,16 +42,7 @@ |
41 | 43 | return true; //a special key (backspace, etc) so don't interfere. |
42 | 44 | } |
43 | 45 | |
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 ); |
54 | 47 | |
55 | 48 | // limit-3 as this doesn't count the character about to be inserted. |
56 | 49 | 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 |
1 | 21 | + native |
Index: trunk/phase3/resources/Resources.php |
— | — | @@ -79,8 +79,12 @@ |
80 | 80 | 'scripts' => 'resources/jquery/jquery.autoEllipsis.js', |
81 | 81 | 'dependencies' => 'jquery.highlightText', |
82 | 82 | ), |
| 83 | + 'jquery.byteLength' => array( |
| 84 | + 'scripts' => 'resources/jquery/jquery.byteLength.js', |
| 85 | + ), |
83 | 86 | 'jquery.byteLimit' => array( |
84 | 87 | 'scripts' => 'resources/jquery/jquery.byteLimit.js', |
| 88 | + 'dependencies' => 'jquery.byteLength', |
85 | 89 | ), |
86 | 90 | 'jquery.checkboxShiftClick' => array( |
87 | 91 | 'scripts' => 'resources/jquery/jquery.checkboxShiftClick.js', |