From 0406ae156a7441c345a6239fdef936c1945251c3 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Bartosz=20Dziewo=C5=84ski?= Date: Mon, 19 Feb 2018 20:53:11 +0100 Subject: [PATCH] mw.Title: Use $.trimByteLength from jquery.byteLimit module Change-Id: I39ebd559a72ce72743cbe3bb86abe679d108d94a --- resources/Resources.php | 1 + resources/src/mediawiki/mediawiki.Title.js | 16 +--------------- 2 files changed, 2 insertions(+), 15 deletions(-) diff --git a/resources/Resources.php b/resources/Resources.php index e139c3af81..4d89f87199 100644 --- a/resources/Resources.php +++ b/resources/Resources.php @@ -1202,6 +1202,7 @@ return [ ], 'dependencies' => [ 'jquery.byteLength', + 'jquery.byteLimit', 'mediawiki.util', ], 'targets' => [ 'desktop', 'mobile' ], diff --git a/resources/src/mediawiki/mediawiki.Title.js b/resources/src/mediawiki/mediawiki.Title.js index 851f06c2d1..6a4ebb1f14 100644 --- a/resources/src/mediawiki/mediawiki.Title.js +++ b/resources/src/mediawiki/mediawiki.Title.js @@ -407,21 +407,7 @@ * @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; }, /** -- 2.20.1