From: Fomafix Date: Thu, 11 Jan 2018 14:27:08 +0000 (+0100) Subject: Use native ES5 Array prototype methods instead of jQuery X-Git-Tag: 1.31.0-rc.0~929^2 X-Git-Url: http://git.heureux-cyclage.org/?a=commitdiff_plain;h=1edba8029a561919febf8b90f5df689d090665dd;p=lhc%2Fweb%2Fwiklou.git Use native ES5 Array prototype methods instead of jQuery Replace * $.each( array, function ( index, value ) { ... } ) by array.forEach( function ( value, index ) { ... } ) * $.grep( array, function ( value ) { ... } ) by array.filter( function ( value ) { ... } ) * $.map( array, function ( value ) { ... } ) by array.map( function ( value ) { ... } ) This change is a follow-up to 9d67e9973e6766e057c70ea8c811be8e269fb80a. Change-Id: I8ef9af8c4d2f440faca65ec7c78a977ea7c31ad2 --- diff --git a/resources/src/jquery/jquery.tablesorter.js b/resources/src/jquery/jquery.tablesorter.js index cac103ee66..21209f67d2 100644 --- a/resources/src/jquery/jquery.tablesorter.js +++ b/resources/src/jquery/jquery.tablesorter.js @@ -345,7 +345,7 @@ } ); } ); // We want to find the row that has the most columns (ignoring colspan) - $.each( exploded, function ( index, cellArray ) { + exploded.forEach( function ( cellArray, index ) { headerCount = $( uniqueElements( cellArray ) ).filter( 'th' ).length; if ( headerCount >= maxSeen ) { maxSeen = headerCount; @@ -423,9 +423,9 @@ */ function setHeadersOrder( $headers, sortList, headerToColumns ) { // Loop through all headers to retrieve the indices of the columns the header spans across: - $.each( headerToColumns, function ( headerIndex, columns ) { + headerToColumns.forEach( function ( columns, headerIndex ) { - $.each( columns, function ( i, columnIndex ) { + columns.forEach( function ( columnIndex, i ) { var header = $headers[ headerIndex ], $header = $( header ); @@ -437,7 +437,7 @@ } ); } else { // Column shall be sorted: Apply designated count and order. - $.each( sortList, function ( j, sortColumn ) { + sortList.forEach( function ( sortColumn ) { if ( sortColumn[ 0 ] === i ) { $header.data( { order: sortColumn[ 1 ], @@ -622,8 +622,8 @@ } return ret; } ); - $.each( rowspanCells, function () { - $.data( this, 'tablesorter' ).needResort = false; + rowspanCells.forEach( function ( cell ) { + $.data( cell, 'tablesorter' ).needResort = false; } ); } resortCells(); diff --git a/resources/src/mediawiki.rcfilters/dm/mw.rcfilters.dm.FiltersViewModel.js b/resources/src/mediawiki.rcfilters/dm/mw.rcfilters.dm.FiltersViewModel.js index c9f59248d4..5564d1ec46 100644 --- a/resources/src/mediawiki.rcfilters/dm/mw.rcfilters.dm.FiltersViewModel.js +++ b/resources/src/mediawiki.rcfilters/dm/mw.rcfilters.dm.FiltersViewModel.js @@ -185,7 +185,7 @@ mw.rcfilters.dm.FiltersViewModel.prototype.getFirstConflictedItem = function () { var conflictedItem; - $.each( this.getItems(), function ( index, filterItem ) { + this.getItems().forEach( function ( filterItem ) { if ( filterItem.isSelected() && filterItem.isConflicted() ) { conflictedItem = filterItem; return false; diff --git a/resources/src/mediawiki.special/mediawiki.special.apisandbox.js b/resources/src/mediawiki.special/mediawiki.special.apisandbox.js index 534af05bb7..a6450e9136 100644 --- a/resources/src/mediawiki.special/mediawiki.special.apisandbox.js +++ b/resources/src/mediawiki.special/mediawiki.special.apisandbox.js @@ -1074,7 +1074,7 @@ label: Util.parseMsg( 'apisandbox-request-selectformat-label' ) } ).$element, - $.map( formatItems, function ( item ) { + formatItems.map( function ( item ) { return item.getData().$element; } ), $result diff --git a/resources/src/mediawiki.widgets.datetime/CalendarWidget.js b/resources/src/mediawiki.widgets.datetime/CalendarWidget.js index 54a5a850bc..fa05991431 100644 --- a/resources/src/mediawiki.widgets.datetime/CalendarWidget.js +++ b/resources/src/mediawiki.widgets.datetime/CalendarWidget.js @@ -216,7 +216,7 @@ if ( dates instanceof Date ) { dates = [ dates ]; } else if ( Array.isArray( dates ) ) { - dates = $.grep( dates, function ( dt ) { return dt instanceof Date; } ); + dates = dates.filter( function ( dt ) { return dt instanceof Date; } ); dates.sort(); } else { dates = []; diff --git a/resources/src/mediawiki.widgets/mw.widgets.CategoryCapsuleItemWidget.js b/resources/src/mediawiki.widgets/mw.widgets.CategoryCapsuleItemWidget.js index 488d9e099f..42ef1ac620 100644 --- a/resources/src/mediawiki.widgets/mw.widgets.CategoryCapsuleItemWidget.js +++ b/resources/src/mediawiki.widgets/mw.widgets.CategoryCapsuleItemWidget.js @@ -61,7 +61,7 @@ $.each( response.query.pages, function ( index, page ) { pages[ page.title ] = !page.missing; } ); - $.each( titles, function ( index, title ) { + titles.forEach( function ( title ) { var normalizedTitle = title; while ( normalized[ normalizedTitle ] ) { normalizedTitle = normalized[ normalizedTitle ]; diff --git a/resources/src/mediawiki/mediawiki.Uri.js b/resources/src/mediawiki/mediawiki.Uri.js index 59261cdccf..450b415d96 100644 --- a/resources/src/mediawiki/mediawiki.Uri.js +++ b/resources/src/mediawiki/mediawiki.Uri.js @@ -288,7 +288,7 @@ // Apply parser regex and set all properties based on the result matches = parser[ options.strictMode ? 'strict' : 'loose' ].exec( str ); - $.each( properties, function ( i, property ) { + properties.forEach( function ( property, i ) { uri[ property ] = matches[ i + 1 ]; } ); @@ -367,7 +367,7 @@ $.each( this.query, function ( key, val ) { var k = Uri.encode( key ), vals = Array.isArray( val ) ? val : [ val ]; - $.each( vals, function ( i, v ) { + vals.forEach( function ( v ) { if ( v === null ) { args.push( k ); } else if ( k === 'title' ) { diff --git a/resources/src/mediawiki/mediawiki.inspect.js b/resources/src/mediawiki/mediawiki.inspect.js index 006ca1f26e..f91ffbb41b 100644 --- a/resources/src/mediawiki/mediawiki.inspect.js +++ b/resources/src/mediawiki/mediawiki.inspect.js @@ -55,7 +55,7 @@ } graph[ moduleName ].requires = dependencies; - $.each( dependencies, function ( depIndex, depName ) { + dependencies.forEach( function ( depName ) { if ( !hasOwn.call( graph, depName ) ) { graph[ depName ] = { requiredBy: [] }; } diff --git a/tests/qunit/data/testrunner.js b/tests/qunit/data/testrunner.js index e944ef0120..59432945c2 100644 --- a/tests/qunit/data/testrunner.js +++ b/tests/qunit/data/testrunner.js @@ -309,14 +309,14 @@ // Test should use fake XHR, wait for requests, or call abort() $activeLen = $.active; if ( $activeLen !== undefined && $activeLen !== 0 ) { - pending = $.grep( ajaxRequests, function ( ajax ) { + pending = ajaxRequests.filter( function ( ajax ) { return ajax.xhr.state() === 'pending'; } ); if ( pending.length !== $activeLen ) { mw.log.warn( 'Pending requests does not match jQuery.active count' ); } // Force requests to stop to give the next test a clean start - $.each( ajaxRequests, function ( i, ajax ) { + ajaxRequests.forEach( function ( ajax, i ) { mw.log.warn( 'AJAX request #' + i + ' (state: ' + ajax.xhr.state() + ')', ajax.options diff --git a/tests/qunit/suites/resources/jquery/jquery.highlightText.test.js b/tests/qunit/suites/resources/jquery/jquery.highlightText.test.js index 0dac22e9df..277ba3f233 100644 --- a/tests/qunit/suites/resources/jquery/jquery.highlightText.test.js +++ b/tests/qunit/suites/resources/jquery/jquery.highlightText.test.js @@ -222,7 +222,7 @@ } ]; - $.each( cases, function ( i, item ) { + cases.forEach( function ( item ) { $fixture = $( '

' ).text( item.text ).highlightText( item.highlight ); assert.equal( $fixture.html(), diff --git a/tests/qunit/suites/resources/jquery/jquery.tablesorter.parsers.test.js b/tests/qunit/suites/resources/jquery/jquery.tablesorter.parsers.test.js index 01589c3360..2865cbbab7 100644 --- a/tests/qunit/suites/resources/jquery/jquery.tablesorter.parsers.test.js +++ b/tests/qunit/suites/resources/jquery/jquery.tablesorter.parsers.test.js @@ -62,7 +62,7 @@ } parser = $.tablesorter.getParser( parserId ); - $.each( data, function ( index, testcase ) { + data.forEach( function ( testcase ) { extractedR = parser.is( testcase[ 0 ] ); extractedF = parser.format( testcase[ 0 ] ); diff --git a/tests/qunit/suites/resources/jquery/jquery.tablesorter.test.js b/tests/qunit/suites/resources/jquery/jquery.tablesorter.test.js index 27d7e8daf5..1db8c61d49 100644 --- a/tests/qunit/suites/resources/jquery/jquery.tablesorter.test.js +++ b/tests/qunit/suites/resources/jquery/jquery.tablesorter.test.js @@ -238,7 +238,7 @@ $tbody = $table.find( 'tbody' ), $tr = $( '' ); - $.each( header, function ( i, str ) { + header.forEach( function ( str ) { var $th = $( '' ); $th.text( str ).appendTo( $tr ); } ); @@ -247,7 +247,7 @@ for ( i = 0; i < data.length; i++ ) { $tr = $( '' ); // eslint-disable-next-line no-loop-func - $.each( data[ i ], function ( j, str ) { + data[ i ].forEach( function ( str ) { var $td = $( '' ); $td.text( str ).appendTo( $tr ); } ); diff --git a/tests/qunit/suites/resources/mediawiki.api/mediawiki.api.test.js b/tests/qunit/suites/resources/mediawiki.api/mediawiki.api.test.js index 6c2d51ee4a..5a92da4f0c 100644 --- a/tests/qunit/suites/resources/mediawiki.api/mediawiki.api.test.js +++ b/tests/qunit/suites/resources/mediawiki.api/mediawiki.api.test.js @@ -449,7 +449,7 @@ } ); this.api.abort(); assert.ok( this.requests.length === 2, 'Check both requests triggered' ); - $.each( this.requests, function ( i, request ) { + this.requests.forEach( function ( request, i ) { assert.ok( request.abort.calledOnce, 'abort request number ' + i ); } ); } ); diff --git a/tests/qunit/suites/resources/mediawiki/mediawiki.RegExp.test.js b/tests/qunit/suites/resources/mediawiki/mediawiki.RegExp.test.js index 97c82fb385..4e15cf0115 100644 --- a/tests/qunit/suites/resources/mediawiki/mediawiki.RegExp.test.js +++ b/tests/qunit/suites/resources/mediawiki/mediawiki.RegExp.test.js @@ -1,4 +1,4 @@ -( function ( mw, $ ) { +( function ( mw ) { QUnit.module( 'mediawiki.RegExp' ); QUnit.test( 'escape', function ( assert ) { @@ -28,11 +28,11 @@ '0123456789' ].join( '' ); - $.each( specials, function ( i, str ) { + specials.forEach( function ( str ) { assert.propEqual( str.match( new RegExp( mw.RegExp.escape( str ) ) ), [ str ], 'Match ' + str ); } ); assert.equal( mw.RegExp.escape( normal ), normal, 'Alphanumerals are left alone' ); } ); -}( mediaWiki, jQuery ) ); +}( mediaWiki ) ); diff --git a/tests/qunit/suites/resources/mediawiki/mediawiki.Uri.test.js b/tests/qunit/suites/resources/mediawiki/mediawiki.Uri.test.js index e56c93379a..84b8f06354 100644 --- a/tests/qunit/suites/resources/mediawiki/mediawiki.Uri.test.js +++ b/tests/qunit/suites/resources/mediawiki/mediawiki.Uri.test.js @@ -1,4 +1,4 @@ -( function ( mw, $ ) { +( function ( mw ) { QUnit.module( 'mediawiki.Uri', QUnit.newMwEnvironment( { setup: function () { this.mwUriOrg = mw.Uri; @@ -10,7 +10,7 @@ } } ) ); - $.each( [ true, false ], function ( i, strictMode ) { + [ true, false ].forEach( function ( strictMode ) { QUnit.test( 'Basic construction and properties (' + ( strictMode ? '' : 'non-' ) + 'strict mode)', function ( assert ) { var uriString, uri; uriString = 'http://www.ietf.org/rfc/rfc2396.txt'; @@ -504,4 +504,4 @@ href = uri.toString(); assert.equal( href, testProtocol + testServer + ':' + testPort + testPath, 'Root-relative URL gets host, protocol, and port supplied' ); } ); -}( mediaWiki, jQuery ) ); +}( mediaWiki ) ); diff --git a/tests/qunit/suites/resources/mediawiki/mediawiki.jqueryMsg.test.js b/tests/qunit/suites/resources/mediawiki/mediawiki.jqueryMsg.test.js index db51fb32f5..67ee3b8564 100644 --- a/tests/qunit/suites/resources/mediawiki/mediawiki.jqueryMsg.test.js +++ b/tests/qunit/suites/resources/mediawiki/mediawiki.jqueryMsg.test.js @@ -898,7 +898,7 @@ var queue; mw.messages.set( 'formatnum-msg', '{{formatnum:$1}}' ); mw.messages.set( 'formatnum-msg-int', '{{formatnum:$1|R}}' ); - queue = $.map( formatnumTests, function ( test ) { + queue = formatnumTests.map( function ( test ) { var done = assert.async(); return function ( next, abort ) { getMwLanguage( test.lang ) diff --git a/tests/qunit/suites/resources/mediawiki/mediawiki.util.test.js b/tests/qunit/suites/resources/mediawiki/mediawiki.util.test.js index 1730575914..ef894a9482 100644 --- a/tests/qunit/suites/resources/mediawiki/mediawiki.util.test.js +++ b/tests/qunit/suites/resources/mediawiki/mediawiki.util.test.js @@ -53,7 +53,7 @@ ]; Array.prototype.push.apply( IPV6_CASES, - $.map( [ + [ 'fc:100::', 'fc:100:a::', 'fc:100:a:d::', @@ -69,8 +69,8 @@ '::fc:100:a:d:1:e', '::fc:100:a:d:1:e:ac', 'fc:100:a:d:1:e:ac:0' - ], function ( el ) { - return [ [ true, el, el + ' is a valid IP' ] ]; + ].map( function ( el ) { + return [ true, el, el + ' is a valid IP' ]; } ) ); @@ -132,7 +132,7 @@ newExperimental = [ 'html5', 'html5-legacy' ]; // Test cases are kept in sync with SanitizerTest.php - $.each( [ + [ // Pure legacy: how MW worked before 2017 [ legacy, text, legacyEncoded ], // Transition to a new world: legacy links with HTML5 fallback @@ -145,7 +145,7 @@ [ experimentalLegacy, text, html5Experimental ], // Migration from $wgExperimentalHtmlIds to modern HTML5 [ newExperimental, text, html5Encoded ] - ], function ( index, testCase ) { + ].forEach( function ( testCase ) { mw.config.set( 'wgFragmentMode', testCase[ 0 ] ); assert.equal( util.escapeIdForAttribute( testCase[ 1 ] ), testCase[ 2 ] ); @@ -166,7 +166,7 @@ experimentalLegacy = [ 'html5-legacy', 'legacy' ], newExperimental = [ 'html5', 'html5-legacy' ]; - $.each( [ + [ // Pure legacy: how MW worked before 2017 [ legacy, text, legacyEncoded ], // Transition to a new world: legacy links with HTML5 fallback @@ -179,7 +179,7 @@ [ experimentalLegacy, text, html5Experimental ], // Migration from wgExperimentalHtmlIds to modern HTML5 [ newExperimental, text, html5Encoded ] - ], function ( index, testCase ) { + ].forEach( function ( testCase ) { mw.config.set( 'wgFragmentMode', testCase[ 0 ] ); assert.equal( util.escapeIdForLink( testCase[ 1 ] ), testCase[ 2 ] ); @@ -432,23 +432,23 @@ } ); QUnit.test( 'isIPv6Address', function ( assert ) { - $.each( IPV6_CASES, function ( i, ipCase ) { + IPV6_CASES.forEach( function ( ipCase ) { assert.strictEqual( util.isIPv6Address( ipCase[ 1 ] ), ipCase[ 0 ], ipCase[ 2 ] ); } ); } ); QUnit.test( 'isIPv4Address', function ( assert ) { - $.each( IPV4_CASES, function ( i, ipCase ) { + IPV4_CASES.forEach( function ( ipCase ) { assert.strictEqual( util.isIPv4Address( ipCase[ 1 ] ), ipCase[ 0 ], ipCase[ 2 ] ); } ); } ); QUnit.test( 'isIPAddress', function ( assert ) { - $.each( IPV4_CASES, function ( i, ipCase ) { + IPV4_CASES.forEach( function ( ipCase ) { assert.strictEqual( util.isIPv4Address( ipCase[ 1 ] ), ipCase[ 0 ], ipCase[ 2 ] ); } ); - $.each( IPV6_CASES, function ( i, ipCase ) { + IPV6_CASES.forEach( function ( ipCase ) { assert.strictEqual( util.isIPv6Address( ipCase[ 1 ] ), ipCase[ 0 ], ipCase[ 2 ] ); } ); } ); diff --git a/tests/qunit/suites/resources/startup.test.js b/tests/qunit/suites/resources/startup.test.js index ee1340d69d..0866b9ecee 100644 --- a/tests/qunit/suites/resources/startup.test.js +++ b/tests/qunit/suites/resources/startup.test.js @@ -1,5 +1,5 @@ /* global isCompatible: true */ -( function ( $ ) { +( function () { var testcases = { tested: [ /* Grade A */ @@ -146,14 +146,14 @@ QUnit.module( 'startup', QUnit.newMwEnvironment() ); QUnit.test( 'isCompatible( featureTestable )', function ( assert ) { - $.each( testcases.tested, function ( i, ua ) { + testcases.tested.forEach( function ( ua ) { assert.strictEqual( isCompatible( ua ), true, ua ); } ); } ); QUnit.test( 'isCompatible( blacklisted )', function ( assert ) { - $.each( testcases.blacklisted, function ( i, ua ) { + testcases.blacklisted.forEach( function ( ua ) { assert.strictEqual( isCompatible( ua ), false, ua ); } ); } ); -}( jQuery ) ); +}() );