mw.Title: Use $.trimByteLength from jquery.byteLimit module
authorBartosz Dziewoński <matma.rex@gmail.com>
Mon, 19 Feb 2018 19:53:11 +0000 (20:53 +0100)
committerJforrester <jforrester@wikimedia.org>
Tue, 20 Feb 2018 22:03:21 +0000 (22:03 +0000)
Change-Id: I39ebd559a72ce72743cbe3bb86abe679d108d94a

resources/Resources.php
resources/src/mediawiki/mediawiki.Title.js

index e139c3a..4d89f87 100644 (file)
@@ -1202,6 +1202,7 @@ return [
                ],
                'dependencies' => [
                        'jquery.byteLength',
+                       'jquery.byteLimit',
                        'mediawiki.util',
                ],
                'targets' => [ 'desktop', 'mobile' ],
index 851f06c..6a4ebb1 100644 (file)
                 * @return {string}
                 */
                trimToByteLength = function ( s, length ) {
-                       var byteLength, chopOffChars, chopOffBytes;
-
-                       // bytelength is always greater or equal to the length in characters
-                       s = s.substr( 0, length );
-                       while ( ( byteLength = $.byteLength( s ) ) > length ) {
-                               // Calculate how many characters can be safely removed
-                               // First, we need to know how many bytes the string exceeds the threshold
-                               chopOffBytes = byteLength - length;
-                               // A character in UTF-8 is at most 4 bytes
-                               // One character must be removed in any case because the
-                               // string is too long
-                               chopOffChars = Math.max( 1, Math.floor( chopOffBytes / 4 ) );
-                               s = s.substr( 0, s.length - chopOffChars );
-                       }
-                       return s;
+                       return $.trimByteLength( '', s, length ).newVal;
                },
 
                /**