From: Timo Tijhof Date: Wed, 3 Sep 2014 15:19:41 +0000 (+0200) Subject: Use String#slice instead of String#substr or String#substring X-Git-Tag: 1.31.0-rc.0~14173 X-Git-Url: http://git.cyclocoop.org/%27.parametre_url%28%20%20%20generer_action_auteur%28%27charger_plugin%27%2C%20%27update_flux%27%29%2C%27update_flux%27%2C%20%27oui%27%29.%27?a=commitdiff_plain;h=c71818346673c91987c201543788d802f346cad5;p=lhc%2Fweb%2Fwiklou.git Use String#slice instead of String#substr or String#substring Quite a few reasons: * There is a bug in IE 8 and below where the startIndex argument does not support negative values, contrary to the ECMAScript spec and implementations in other browsers. IE8: 'faux'.substr( -1 ); // "faux" Standards: 'faux'.substr( -1 ); // "x" Code written for ES5 (and using the es5-shim) works as expected since the shim repairs this method. * String#substr and String#substring both exist but have different signatures which are easily mixed up. String.prototype.substr( start [, length] ) > Supports negative start, but not in IE8 and below. > Takes length, *not* second index. E.g. `substr( 2, 3 )` returns `slice( 2, 5 )`. String.prototype.substring( indexA [, indexB] ) > Doesn't support negative indices. > If indexA is larger than indexB, they are silently swapped! String.prototype.slice( start [, end] ) > Supports negative indices. 'faux'.substr( 0, 2 ); // "fa" 'faux'.substring( 0, 2 ); // "fa" 'faux'.slice( 0, 2 ); // "fa" 'faux'.substr( -2 ); // "ux" 'faux'.substring( -2 ); // "faux" 'faux'.slice( -2 ); // "ux" 'faux'.substr( 1, 2 ); // "au" 'faux'.substring( 1, 2 ); // "a" 'faux'.slice( 1, 2 ); // "a" 'faux'.substr( 1, -1 ); // "" 'faux'.substring( 1, -1 ); // "f" 'faux'.slice( 1, -1 ); // "au" 'faux'.substr( 2, 1 ); // "u" 'faux'.substring( 2, 1 ); // "a" 'faux'.slice( 2, 1 ); // "" * String#slice works the same way as Array#slice and slice methods elsewhere (jQuery, PHP, ..). * Also simplify calls: - Omit second argument where it explicitly calculated the length and passed it as is (default behaviour) - Pass negative values instead of length - x. - Use chatAt to extract a single character. * Change: - Replace all uses of substring() with slice(). - Replace all uses of substr() with slice() where only one parameter is passed or where the first parameter is 0. - Using substr()'s unique behaviour (length instead of endIndex) is fine, though there's only one instances of that, in mediawiki.jqueryMsg.js Change-Id: I9b6dd682a64fa28c7ea0da1846ccd3b42f9430cf --- diff --git a/mw-config/config.js b/mw-config/config.js index 2886e08e86..cf17aef77c 100644 --- a/mw-config/config.js +++ b/mw-config/config.js @@ -9,7 +9,7 @@ .replace( /__+/g, '_' ) .replace( /^_+/, '' ) .replace( /_+$/, '' ); - value = value.substr( 0, 1 ).toUpperCase() + value.substr( 1 ); + value = value.charAt( 0 ).toUpperCase() + value.slice( 1 ); $label.text( labelText.replace( '$1', value ) ); } diff --git a/resources/src/jquery/jquery.autoEllipsis.js b/resources/src/jquery/jquery.autoEllipsis.js index 061f5d6982..9a196b5dbc 100644 --- a/resources/src/jquery/jquery.autoEllipsis.js +++ b/resources/src/jquery/jquery.autoEllipsis.js @@ -108,7 +108,7 @@ $.fn.autoEllipsis = function ( options ) { r = trimmableText.length; do { m = Math.ceil( ( l + r ) / 2 ); - $trimmableText.text( trimmableText.substr( 0, m ) + '...' ); + $trimmableText.text( trimmableText.slice( 0, m ) + '...' ); if ( $trimmableText.width() + pw > w ) { // Text is too long r = m - 1; @@ -116,7 +116,7 @@ $.fn.autoEllipsis = function ( options ) { l = m; } } while ( l < r ); - $trimmableText.text( trimmableText.substr( 0, l ) + '...' ); + $trimmableText.text( trimmableText.slice( 0, l ) + '...' ); break; case 'center': // TODO: Use binary search like for 'right' @@ -124,7 +124,7 @@ $.fn.autoEllipsis = function ( options ) { // Begin with making the end shorter side = 1; while ( $trimmableText.outerWidth() + pw > w && i[0] > 0 ) { - $trimmableText.text( trimmableText.substr( 0, i[0] ) + '...' + trimmableText.substr( i[1] ) ); + $trimmableText.text( trimmableText.slice( 0, i[0] ) + '...' + trimmableText.slice( i[1] ) ); // Alternate between trimming the end and begining if ( side === 0 ) { // Make the begining shorter @@ -141,7 +141,7 @@ $.fn.autoEllipsis = function ( options ) { // TODO: Use binary search like for 'right' r = 0; while ( $trimmableText.outerWidth() + pw > w && r < trimmableText.length ) { - $trimmableText.text( '...' + trimmableText.substr( r ) ); + $trimmableText.text( '...' + trimmableText.slice( r ) ); r++; } break; diff --git a/resources/src/jquery/jquery.byteLimit.js b/resources/src/jquery/jquery.byteLimit.js index de05fdf26e..5551232a55 100644 --- a/resources/src/jquery/jquery.byteLimit.js +++ b/resources/src/jquery/jquery.byteLimit.js @@ -66,11 +66,11 @@ inpParts = [ // Same start - newVal.substring( 0, startMatches ), + newVal.slice( 0, startMatches ), // Inserted content - newVal.substring( startMatches, newVal.length - endMatches ), + newVal.slice( startMatches, newVal.length - endMatches ), // Same end - newVal.substring( newVal.length - endMatches ) + newVal.slice( newVal.length - endMatches ) ]; // Chop off characters from the end of the "inserted content" string diff --git a/resources/src/jquery/jquery.farbtastic.js b/resources/src/jquery/jquery.farbtastic.js index 18810857c1..d7024cc8f0 100644 --- a/resources/src/jquery/jquery.farbtastic.js +++ b/resources/src/jquery/jquery.farbtastic.js @@ -51,7 +51,7 @@ jQuery._farbtastic = function (container, callback) { $('*', e).each(function () { if (this.currentStyle.backgroundImage != 'none') { var image = this.currentStyle.backgroundImage; - image = this.currentStyle.backgroundImage.substring(5, image.length - 2); + image = this.currentStyle.backgroundImage.slice(5, image.length - 2); $(this).css({ 'backgroundImage': 'none', 'filter': "progid:DXImageTransform.Microsoft.AlphaImageLoader(enabled=true, sizingMethod=crop, src='" + image + "')" diff --git a/resources/src/jquery/jquery.hidpi.js b/resources/src/jquery/jquery.hidpi.js index 71b083b640..4ecfeb884e 100644 --- a/resources/src/jquery/jquery.hidpi.js +++ b/resources/src/jquery/jquery.hidpi.js @@ -110,7 +110,7 @@ $.matchSrcSet = function ( devicePixelRatio, srcset ) { bits = candidate.split( / +/ ); src = bits[0]; if ( bits.length > 1 && bits[1].charAt( bits[1].length - 1 ) === 'x' ) { - ratioStr = bits[1].substr( 0, bits[1].length - 1 ); + ratioStr = bits[1].slice( 0, -1 ); ratio = parseFloat( ratioStr ); if ( ratio <= devicePixelRatio && ratio > selectedRatio ) { selectedRatio = ratio; diff --git a/resources/src/jquery/jquery.mwExtension.js b/resources/src/jquery/jquery.mwExtension.js index cdcb8fb776..dc7aaa4587 100644 --- a/resources/src/jquery/jquery.mwExtension.js +++ b/resources/src/jquery/jquery.mwExtension.js @@ -12,7 +12,7 @@ '' : str.toString().replace( /\s+$/, '' ); }, ucFirst: function ( str ) { - return str.charAt( 0 ).toUpperCase() + str.substr( 1 ); + return str.charAt( 0 ).toUpperCase() + str.slice( 1 ); }, escapeRE: function ( str ) { return str.replace ( /([\\{}()|.?*+\-\^$\[\]])/g, '\\$1' ); diff --git a/resources/src/jquery/jquery.textSelection.js b/resources/src/jquery/jquery.textSelection.js index 289cd22086..8d440fdcee 100644 --- a/resources/src/jquery/jquery.textSelection.js +++ b/resources/src/jquery/jquery.textSelection.js @@ -116,12 +116,12 @@ } else { while ( selText.charAt( selText.length - 1 ) === ' ' ) { // Exclude ending space char - selText = selText.substring( 0, selText.length - 1 ); + selText = selText.slice( 0, -1 ); post += ' '; } while ( selText.charAt( 0 ) === ' ' ) { // Exclude prepending space char - selText = selText.substring( 1, selText.length ); + selText = selText.slice( 1 ); pre = ' ' + pre; } } @@ -233,8 +233,8 @@ post += '\n'; } } - this.value = this.value.substring( 0, startPos ) + insertText + - this.value.substring( endPos, this.value.length ); + this.value = this.value.slice( 0, startPos ) + insertText + + this.value.slice( endPos ); // Setting this.value scrolls the textarea to the top, restore the scroll position this.scrollTop = scrollTop; if ( window.opera ) { diff --git a/resources/src/mediawiki.language/languages/fi.js b/resources/src/mediawiki.language/languages/fi.js index 2382aae4c0..453a675d5d 100644 --- a/resources/src/mediawiki.language/languages/fi.js +++ b/resources/src/mediawiki.language/languages/fi.js @@ -34,7 +34,7 @@ mediaWiki.language.convertGrammar = function ( word, form ) { break; case 'illative': // Double the last letter and add 'n' - word += word.substr( word.length - 1 ) + 'n'; + word += word.slice( -1 ) + 'n'; break; case 'inessive': word += ( aou ? 'ssa' : 'ssä' ); diff --git a/resources/src/mediawiki.language/languages/he.js b/resources/src/mediawiki.language/languages/he.js index 48351bc5a4..d1eba43b4c 100644 --- a/resources/src/mediawiki.language/languages/he.js +++ b/resources/src/mediawiki.language/languages/he.js @@ -11,17 +11,17 @@ mediaWiki.language.convertGrammar = function ( word, form ) { case 'prefixed': case 'תחילית': // the same word in Hebrew // Duplicate prefixed "Waw", but only if it's not already double - if ( word.substr( 0, 1 ) === 'ו' && word.substr( 0, 2 ) !== 'וו' ) { + if ( word.slice( 0, 1 ) === 'ו' && word.slice( 0, 2 ) !== 'וו' ) { word = 'ו' + word; } // Remove the "He" if prefixed - if ( word.substr( 0, 1 ) === 'ה' ) { - word = word.substr( 1, word.length ); + if ( word.slice( 0, 1 ) === 'ה' ) { + word = word.slice( 1 ); } // Add a hyphen (maqaf) before numbers and non-Hebrew letters - if ( word.substr( 0, 1 ) < 'א' || word.substr( 0, 1 ) > 'ת' ) { + if ( word.slice( 0, 1 ) < 'א' || word.slice( 0, 1 ) > 'ת' ) { word = '־' + word; } } diff --git a/resources/src/mediawiki.language/languages/hy.js b/resources/src/mediawiki.language/languages/hy.js index ae16f247d9..9cae360b9d 100644 --- a/resources/src/mediawiki.language/languages/hy.js +++ b/resources/src/mediawiki.language/languages/hy.js @@ -14,12 +14,12 @@ mediaWiki.language.convertGrammar = function ( word, form ) { switch ( form ) { case 'genitive': // սեռական հոլով - if ( word.substr( -1 ) === 'ա' ) { - word = word.substr( 0, word.length - 1 ) + 'այի'; - } else if ( word.substr( -1 ) === 'ո' ) { - word = word.substr( 0, word.length - 1 ) + 'ոյի'; - } else if ( word.substr( -4 ) === 'գիրք' ) { - word = word.substr( 0, word.length - 4 ) + 'գրքի'; + if ( word.slice( -1 ) === 'ա' ) { + word = word.slice( 0, -1 ) + 'այի'; + } else if ( word.slice( -1 ) === 'ո' ) { + word = word.slice( 0, -1 ) + 'ոյի'; + } else if ( word.slice( -4 ) === 'գիրք' ) { + word = word.slice( 0, -4 ) + 'գրքի'; } else { word = word + 'ի'; } diff --git a/resources/src/mediawiki.language/languages/os.js b/resources/src/mediawiki.language/languages/os.js index bdf59beac4..787be36df3 100644 --- a/resources/src/mediawiki.language/languages/os.js +++ b/resources/src/mediawiki.language/languages/os.js @@ -19,7 +19,7 @@ mediaWiki.language.convertGrammar = function ( word, form ) { } // Checking if the $word is in plural form if ( word.match( /тæ$/i ) ) { - word = word.substring( 0, word.length - 1 ); + word = word.slice( 0, -1 ); endAllative = 'æм'; } // Works if word is in singular form. @@ -30,7 +30,7 @@ mediaWiki.language.convertGrammar = function ( word, form ) { // Checking if word ends on 'у'. 'У' can be either consonant 'W' or vowel 'U' in cyrillic Ossetic. // Examples: {{grammar:genitive|аунеу}} = аунеуы, {{grammar:genitive|лæппу}} = лæппуйы. else if ( word.match( /у$/i ) ) { - if ( !word.substring( word.length - 2, word.length - 1 ).match( /[аæеёиоыэюя]$/i ) ) { + if ( !word.slice( -2, -1 ).match( /[аæеёиоыэюя]$/i ) ) { jot = 'й'; } } else if ( !word.match( /[бвгджзйклмнопрстфхцчшщьъ]$/i ) ) { diff --git a/resources/src/mediawiki.language/languages/ru.js b/resources/src/mediawiki.language/languages/ru.js index b6ba59fb85..2077b6beb2 100644 --- a/resources/src/mediawiki.language/languages/ru.js +++ b/resources/src/mediawiki.language/languages/ru.js @@ -15,41 +15,41 @@ mediaWiki.language.convertGrammar = function ( word, form ) { } switch ( form ) { case 'genitive': // родительный падеж - if ( word.substr( word.length - 1 ) === 'ь' ) { - word = word.substr(0, word.length - 1 ) + 'я'; - } else if ( word.substr( word.length - 2 ) === 'ия' ) { - word = word.substr(0, word.length - 2 ) + 'ии'; - } else if ( word.substr( word.length - 2 ) === 'ка' ) { - word = word.substr(0, word.length - 2 ) + 'ки'; - } else if ( word.substr( word.length - 2 ) === 'ти' ) { - word = word.substr(0, word.length - 2 ) + 'тей'; - } else if ( word.substr( word.length - 2 ) === 'ды' ) { - word = word.substr(0, word.length - 2 ) + 'дов'; - } else if ( word.substr( word.length - 1 ) === 'д' ) { - word = word.substr(0, word.length - 1 ) + 'да'; - } else if ( word.substr( word.length - 3 ) === 'ные' ) { - word = word.substr(0, word.length - 3 ) + 'ных'; - } else if ( word.substr( word.length - 3 ) === 'ник' ) { - word = word.substr(0, word.length - 3 ) + 'ника'; + if ( word.slice( -1 ) === 'ь' ) { + word = word.slice( 0, -1 ) + 'я'; + } else if ( word.slice( -2 ) === 'ия' ) { + word = word.slice( 0, -2 ) + 'ии'; + } else if ( word.slice( -2 ) === 'ка' ) { + word = word.slice( 0, -2 ) + 'ки'; + } else if ( word.slice( -2 ) === 'ти' ) { + word = word.slice( 0, -2 ) + 'тей'; + } else if ( word.slice( -2 ) === 'ды' ) { + word = word.slice( 0, -2 ) + 'дов'; + } else if ( word.slice( -1 ) === 'д' ) { + word = word.slice( 0, -1 ) + 'да'; + } else if ( word.slice( -3 ) === 'ные' ) { + word = word.slice( 0, -3 ) + 'ных'; + } else if ( word.slice( -3 ) === 'ник' ) { + word = word.slice( 0, -3 ) + 'ника'; } break; case 'prepositional': // предложный падеж - if ( word.substr( word.length - 1 ) === 'ь' ) { - word = word.substr(0, word.length - 1 ) + 'е'; - } else if ( word.substr( word.length - 2 ) === 'ия' ) { - word = word.substr(0, word.length - 2 ) + 'ии'; - } else if ( word.substr( word.length - 2 ) === 'ка' ) { - word = word.substr(0, word.length - 2 ) + 'ке'; - } else if ( word.substr( word.length - 2 ) === 'ти' ) { - word = word.substr(0, word.length - 2 ) + 'тях'; - } else if ( word.substr( word.length - 2 ) === 'ды' ) { - word = word.substr(0, word.length - 2 ) + 'дах'; - } else if ( word.substr( word.length - 1 ) === 'д' ) { - word = word.substr(0, word.length - 1 ) + 'де'; - } else if ( word.substr( word.length - 3 ) === 'ные' ) { - word = word.substr(0, word.length - 3 ) + 'ных'; - } else if ( word.substr( word.length - 3 ) === 'ник' ) { - word = word.substr(0, word.length - 3 ) + 'нике'; + if ( word.slice( -1 ) === 'ь' ) { + word = word.slice( 0, -1 ) + 'е'; + } else if ( word.slice( -2 ) === 'ия' ) { + word = word.slice( 0, -2 ) + 'ии'; + } else if ( word.slice( -2 ) === 'ка' ) { + word = word.slice( 0, -2 ) + 'ке'; + } else if ( word.slice( -2 ) === 'ти' ) { + word = word.slice( 0, -2 ) + 'тях'; + } else if ( word.slice( -2 ) === 'ды' ) { + word = word.slice( 0, -2 ) + 'дах'; + } else if ( word.slice( -1 ) === 'д' ) { + word = word.slice( 0, -1 ) + 'де'; + } else if ( word.slice( -3 ) === 'ные' ) { + word = word.slice( 0, -3 ) + 'ных'; + } else if ( word.slice( -3 ) === 'ник' ) { + word = word.slice( 0, -3 ) + 'нике'; } break; } diff --git a/resources/src/mediawiki.language/languages/uk.js b/resources/src/mediawiki.language/languages/uk.js index 69f7ec540c..550a388c25 100644 --- a/resources/src/mediawiki.language/languages/uk.js +++ b/resources/src/mediawiki.language/languages/uk.js @@ -9,26 +9,26 @@ mediaWiki.language.convertGrammar = function ( word, form ) { } switch ( form ) { case 'genitive': // родовий відмінок - if ( word.substr( word.length - 4 ) !== 'вікі' && word.substr( word.length - 4 ) !== 'Вікі' ) { - if ( word.substr( word.length - 1 ) === 'ь' ) { - word = word.substr(0, word.length - 1 ) + 'я'; - } else if ( word.substr( word.length - 2 ) === 'ія' ) { - word = word.substr(0, word.length - 2 ) + 'ії'; - } else if ( word.substr( word.length - 2 ) === 'ка' ) { - word = word.substr(0, word.length - 2 ) + 'ки'; - } else if ( word.substr( word.length - 2 ) === 'ти' ) { - word = word.substr(0, word.length - 2 ) + 'тей'; - } else if ( word.substr( word.length - 2 ) === 'ды' ) { - word = word.substr(0, word.length - 2 ) + 'дов'; - } else if ( word.substr( word.length - 3 ) === 'ник' ) { - word = word.substr(0, word.length - 3 ) + 'ника'; + if ( word.slice( -4 ) !== 'вікі' && word.slice( -4 ) !== 'Вікі' ) { + if ( word.slice( -1 ) === 'ь' ) { + word = word.slice(0, -1 ) + 'я'; + } else if ( word.slice( -2 ) === 'ія' ) { + word = word.slice(0, -2 ) + 'ії'; + } else if ( word.slice( -2 ) === 'ка' ) { + word = word.slice(0, -2 ) + 'ки'; + } else if ( word.slice( -2 ) === 'ти' ) { + word = word.slice(0, -2 ) + 'тей'; + } else if ( word.slice( -2 ) === 'ды' ) { + word = word.slice(0, -2 ) + 'дов'; + } else if ( word.slice( -3 ) === 'ник' ) { + word = word.slice(0, -3 ) + 'ника'; } } break; case 'accusative': // знахідний відмінок - if ( word.substr( word.length - 4 ) !== 'вікі' && word.substr( word.length - 4 ) !== 'Вікі' ) { - if ( word.substr( word.length - 2 ) === 'ія' ) { - word = word.substr(0, word.length - 2 ) + 'ію'; + if ( word.slice( -4 ) !== 'вікі' && word.slice( -4 ) !== 'Вікі' ) { + if ( word.slice( -2 ) === 'ія' ) { + word = word.slice(0, -2 ) + 'ію'; } } break; diff --git a/resources/src/mediawiki.language/mediawiki.language.js b/resources/src/mediawiki.language/mediawiki.language.js index 2e848581c0..d4f3c69e0a 100644 --- a/resources/src/mediawiki.language/mediawiki.language.js +++ b/resources/src/mediawiki.language/mediawiki.language.js @@ -59,9 +59,9 @@ $.extend( mw.language, { form = forms[index]; if ( /^\d+=/.test( form ) ) { equalsPosition = form.indexOf( '=' ); - formCount = parseInt( form.substring( 0, equalsPosition ), 10 ); + formCount = parseInt( form.slice( 0, equalsPosition ), 10 ); if ( formCount === count ) { - return form.substr( equalsPosition + 1 ); + return form.slice( equalsPosition + 1 ); } forms[index] = undefined; } diff --git a/resources/src/mediawiki.language/mediawiki.language.numbers.js b/resources/src/mediawiki.language/mediawiki.language.numbers.js index 8bd2de9e39..a0b814108e 100644 --- a/resources/src/mediawiki.language/mediawiki.language.numbers.js +++ b/resources/src/mediawiki.language/mediawiki.language.numbers.js @@ -102,7 +102,7 @@ // Truncate fractional if ( maxPlaces < fractional.length ) { - valueParts[1] = fractional.substr( 0, maxPlaces ); + valueParts[1] = fractional.slice( 0, maxPlaces ); } } else { if ( valueParts[1] ) { @@ -124,7 +124,7 @@ // Truncate whole if ( patternDigits.indexOf( '#' ) === -1 ) { - valueParts[0] = valueParts[0].substr( valueParts[0].length - padLength ); + valueParts[0] = valueParts[0].slice( valueParts[0].length - padLength ); } } @@ -133,7 +133,7 @@ if ( index !== -1 ) { groupSize = patternParts[0].length - index - 1; - remainder = patternParts[0].substr( 0, index ); + remainder = patternParts[0].slice( 0, index ); index = remainder.lastIndexOf( ',' ); if ( index !== -1 ) { groupSize2 = remainder.length - index - 1; @@ -142,7 +142,7 @@ for ( whole = valueParts[0]; whole; ) { off = groupSize ? whole.length - groupSize : 0; - pieces.push( ( off > 0 ) ? whole.substr( off ) : whole ); + pieces.push( ( off > 0 ) ? whole.slice( off ) : whole ); whole = ( off > 0 ) ? whole.slice( 0, off ) : ''; if ( groupSize2 ) { diff --git a/resources/src/mediawiki.legacy/upload.js b/resources/src/mediawiki.legacy/upload.js index 144bdf992d..c81b388f73 100644 --- a/resources/src/mediawiki.legacy/upload.js +++ b/resources/src/mediawiki.legacy/upload.js @@ -187,9 +187,9 @@ if ( slash === -1 && backslash === -1 ) { fname = path; } else if ( slash > backslash ) { - fname = path.substring( slash + 1 ); + fname = path.slice( slash + 1 ); } else { - fname = path.substring( backslash + 1 ); + fname = path.slice( backslash + 1 ); } // Clear the filename if it does not have a valid extension. @@ -203,7 +203,7 @@ if ( fname.lastIndexOf( '.' ) === -1 || $.inArray( - fname.substr( fname.lastIndexOf( '.' ) + 1 ).toLowerCase(), + fname.slice( fname.lastIndexOf( '.' ) + 1 ).toLowerCase(), $.map( mw.config.get( 'wgFileExtensions' ), function ( element ) { return element.toLowerCase(); } ) @@ -225,7 +225,7 @@ fname = fname.replace( / /g, '_' ); // Capitalise first letter if needed if ( mw.config.get( 'wgCapitalizeUploads' ) ) { - fname = fname.charAt( 0 ).toUpperCase().concat( fname.substring( 1 ) ); + fname = fname.charAt( 0 ).toUpperCase().concat( fname.slice( 1 ) ); } // Output result diff --git a/resources/src/mediawiki.special/mediawiki.special.search.js b/resources/src/mediawiki.special/mediawiki.special.search.js index a4128f9e97..b27fe349b1 100644 --- a/resources/src/mediawiki.special/mediawiki.special.search.js +++ b/resources/src/mediawiki.special/mediawiki.special.search.js @@ -39,8 +39,8 @@ var parts = $( this ).attr( 'href' ).split( 'search=' ), lastpart = '', prefix = 'search='; - if ( parts.length > 1 && parts[1].indexOf( '&' ) >= 0 ) { - lastpart = parts[1].substring( parts[1].indexOf( '&' ) ); + if ( parts.length > 1 && parts[1].indexOf( '&' ) !== -1 ) { + lastpart = parts[1].slice( parts[1].indexOf( '&' ) ); } else { prefix = '&search='; } diff --git a/resources/src/mediawiki/mediawiki.Title.js b/resources/src/mediawiki/mediawiki.Title.js index 43876081b1..fc8e7e9cca 100644 --- a/resources/src/mediawiki/mediawiki.Title.js +++ b/resources/src/mediawiki/mediawiki.Title.js @@ -132,7 +132,7 @@ namespace = NS_MAIN; title = title // Strip colon - .substr( 1 ) + .slice( 1 ) // Trim underscores .replace( rUnderscoreTrim, '' ); } @@ -167,14 +167,14 @@ } else { fragment = title // Get segment starting after the hash - .substr( i + 1 ) + .slice( i + 1 ) // Convert to text // NB: Must not be trimmed ("Example#_foo" is not the same as "Example#foo") .replace( /_/g, ' ' ); title = title // Strip hash - .substr( 0, i ) + .slice( 0, i ) // Trim underscores, again (strips "_" from "bar" in "Foo_bar_#quux") .replace( rUnderscoreTrim, '' ); } @@ -192,8 +192,8 @@ title.indexOf( '../' ) === 0 || title.indexOf( '/./' ) !== -1 || title.indexOf( '/../' ) !== -1 || - title.substr( title.length - 2 ) === '/.' || - title.substr( title.length - 3 ) === '/..' + title.slice( -2 ) === '/.' || + title.slice( -3 ) === '/..' ) ) { return false; @@ -229,8 +229,8 @@ // Extensions are the non-empty segment after the last dot ext = null; } else { - ext = title.substr( i + 1 ); - title = title.substr( 0, i ); + ext = title.slice( i + 1 ); + title = title.slice( 0, i ); } return { diff --git a/resources/src/mediawiki/mediawiki.debug.js b/resources/src/mediawiki/mediawiki.debug.js index 505a9be399..4935984f9c 100644 --- a/resources/src/mediawiki/mediawiki.debug.js +++ b/resources/src/mediawiki/mediawiki.debug.js @@ -61,7 +61,7 @@ */ switchPane: function ( e ) { var currentPaneId = debug.$container.data( 'currentPane' ), - requestedPaneId = $( this ).prop( 'id' ).substr( 9 ), + requestedPaneId = $( this ).prop( 'id' ).slice( 9 ), $currentPane = $( '#mw-debug-pane-' + currentPaneId ), $requestedPane = $( '#mw-debug-pane-' + requestedPaneId ), hovDone = false; @@ -174,7 +174,7 @@ gitInfo = ''; if ( this.data.gitRevision !== false ) { - gitInfo = '(' + this.data.gitRevision.substring( 0, 7 ) + ')'; + gitInfo = '(' + this.data.gitRevision.slice( 0, 7 ) + ')'; if ( this.data.gitViewUrl !== false ) { gitInfo = $( '' ) .attr( 'href', this.data.gitViewUrl ) diff --git a/resources/src/mediawiki/mediawiki.debug.profile.js b/resources/src/mediawiki/mediawiki.debug.profile.js index b4d57732eb..7bbbbd39d0 100644 --- a/resources/src/mediawiki/mediawiki.debug.profile.js +++ b/resources/src/mediawiki/mediawiki.debug.profile.js @@ -342,7 +342,7 @@ ProfileData.groupOf = function ( label ) { var pos, prefix = 'Profile section ended by close(): '; if ( label.indexOf( prefix ) === 0 ) { - label = label.substring( prefix.length ); + label = label.slice( prefix.length ); } pos = [ '::', ':', '-' ].reduce( function ( result, separator ) { @@ -359,7 +359,7 @@ if ( pos === -1 ) { return label; } else { - return label.substring( 0, pos ); + return label.slice( 0, pos ); } }; diff --git a/resources/src/mediawiki/mediawiki.inspect.js b/resources/src/mediawiki/mediawiki.inspect.js index eda7689450..8e9fc89f4b 100644 --- a/resources/src/mediawiki/mediawiki.inspect.js +++ b/resources/src/mediawiki/mediawiki.inspect.js @@ -240,7 +240,7 @@ }, /** - * Perform a substring search across the JavaScript and CSS source code + * Perform a string search across the JavaScript and CSS source code * of all loaded modules and return an array of the names of the * modules that matched. * diff --git a/resources/src/mediawiki/mediawiki.jqueryMsg.js b/resources/src/mediawiki/mediawiki.jqueryMsg.js index a81730e514..ad71b08384 100644 --- a/resources/src/mediawiki/mediawiki.jqueryMsg.js +++ b/resources/src/mediawiki/mediawiki.jqueryMsg.js @@ -422,7 +422,7 @@ */ function makeRegexParser( regex ) { return function () { - var matches = input.substr( pos ).match( regex ); + var matches = input.slice( pos ).match( regex ); if ( matches === null ) { return null; } @@ -724,7 +724,7 @@ if ( parsedCloseTagResult === null ) { // Closing tag failed. Return the start tag and contents. - return [ 'CONCAT', input.substring( startOpenTagPos, endOpenTagPos ) ] + return [ 'CONCAT', input.slice( startOpenTagPos, endOpenTagPos ) ] .concat( parsedHtmlContents ); } @@ -748,8 +748,8 @@ // parsed HTML link. // // Concatenate everything from the tag, flattening the contents. - result = [ 'CONCAT', input.substring( startOpenTagPos, endOpenTagPos ) ] - .concat( parsedHtmlContents, input.substring( startCloseTagPos, endCloseTagPos ) ); + result = [ 'CONCAT', input.slice( startOpenTagPos, endOpenTagPos ) ] + .concat( parsedHtmlContents, input.slice( startCloseTagPos, endCloseTagPos ) ); } return result; diff --git a/resources/src/mediawiki/mediawiki.js b/resources/src/mediawiki/mediawiki.js index d50fe48f94..5e5a3acc27 100644 --- a/resources/src/mediawiki/mediawiki.js +++ b/resources/src/mediawiki/mediawiki.js @@ -1510,9 +1510,11 @@ for ( i = 0; i < modules.length; i += 1 ) { // Determine how many bytes this module would add to the query string lastDotIndex = modules[i].lastIndexOf( '.' ); - // Note that these substr() calls work even if lastDotIndex == -1 + + // If lastDotIndex is -1, substr() returns an empty string prefix = modules[i].substr( 0, lastDotIndex ); - suffix = modules[i].substr( lastDotIndex + 1 ); + suffix = modules[i].slice( lastDotIndex + 1 ); + bytesAdded = moduleMap[prefix] !== undefined ? suffix.length + 3 // '%2C'.length == 3 : modules[i].length + 3; // '%7C'.length == 3 @@ -2096,7 +2098,7 @@ } for ( key in mw.loader.store.items ) { - module = key.substring( 0, key.indexOf( '@' ) ); + module = key.slice( 0, key.indexOf( '@' ) ); if ( mw.loader.store.getModuleKey( module ) !== key ) { mw.loader.store.stats.expired++; delete mw.loader.store.items[key]; diff --git a/resources/src/mediawiki/mediawiki.user.js b/resources/src/mediawiki/mediawiki.user.js index 7933f1dec8..e93707ecdf 100644 --- a/resources/src/mediawiki/mediawiki.user.js +++ b/resources/src/mediawiki/mediawiki.user.js @@ -62,7 +62,7 @@ seed = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz'; for ( i = 0; i < 32; i++ ) { r = Math.floor( Math.random() * seed.length ); - id += seed.substring( r, r + 1 ); + id += seed.charAt( r ); } return id; }, diff --git a/tests/qunit/suites/resources/jquery/jquery.autoEllipsis.test.js b/tests/qunit/suites/resources/jquery/jquery.autoEllipsis.test.js index 1005316e74..e8c5121429 100644 --- a/tests/qunit/suites/resources/jquery/jquery.autoEllipsis.test.js +++ b/tests/qunit/suites/resources/jquery/jquery.autoEllipsis.test.js @@ -41,7 +41,7 @@ // Add two characters using scary black magic spanText = $span.text(); d = findDivergenceIndex( origText, spanText ); - spanTextNew = spanText.substr( 0, d ) + origText[d] + origText[d] + '...'; + spanTextNew = spanText.slice( 0, d ) + origText[d] + origText[d] + '...'; assert.gt( spanTextNew.length, spanText.length, 'Verify that the new span-length is indeed greater' ); diff --git a/tests/qunit/suites/resources/mediawiki/mediawiki.test.js b/tests/qunit/suites/resources/mediawiki/mediawiki.test.js index 96813305da..5bcd57bb93 100644 --- a/tests/qunit/suites/resources/mediawiki/mediawiki.test.js +++ b/tests/qunit/suites/resources/mediawiki/mediawiki.test.js @@ -809,7 +809,7 @@ // Confirm that mw.loader.load() works with protocol-relative URLs target = target.replace( /https?:/, '' ); - assert.equal( target.substr( 0, 2 ), '//', + assert.equal( target.slice( 0, 2 ), '//', 'URL must be relative to test relative URLs!' );