From 2fa3bac7eb2021361a1d0c498c278df812ec48b9 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Micha=C5=82=20Go=C5=82=C4=99biowski?= Date: Mon, 1 Jun 2015 23:25:38 +0200 Subject: [PATCH] Core: Make jQuery objects iterable Make iterating over jQuery objects possible using ES 2015 for-of: for ( node of $( "
" ) ) { console.log( node.id ); // "narwhal" } (partially cherry-picked from bb026fc12c3c2ad37f47f0919e484bddcdc3d291) Fixes gh-1693 --- src/core.js | 10 ++++++++++ test/.jshintrc | 1 + test/unit/core.js | 20 ++++++++++++++++++++ 3 files changed, 31 insertions(+) diff --git a/src/core.js b/src/core.js index df70e63b6..70865d3f8 100644 --- a/src/core.js +++ b/src/core.js @@ -477,6 +477,16 @@ jQuery.extend({ support: support }); +// JSHint would error on this code due to the Symbol not being defined in ES5. +// Defining this global in .jshintrc would create a danger of using the global +// unguarded in another place, it seems safer to just disable JSHint for these +// three lines. +/* jshint ignore: start */ +if ( typeof Symbol === "function" ) { + jQuery.fn[ Symbol.iterator ] = deletedIds[ Symbol.iterator ]; +} +/* jshint ignore: end */ + // Populate the class2type map jQuery.each("Boolean Number String Function Array Date RegExp Object Error".split(" "), function(i, name) { diff --git a/test/.jshintrc b/test/.jshintrc index f345337a1..272752e53 100644 --- a/test/.jshintrc +++ b/test/.jshintrc @@ -24,6 +24,7 @@ "DOMParser": false, "JSON": false, "Promise": false, + "Symbol": false, "QUnit": false, "ok": false, "equal": false, diff --git a/test/unit/core.js b/test/unit/core.js index 39e61fcb6..07019e86b 100644 --- a/test/unit/core.js +++ b/test/unit/core.js @@ -1519,3 +1519,23 @@ testIframeWithCallback( "Don't call window.onready (#14802)", "core/onready.html equal( error, false, "no call to user-defined onready" ); } ); + +test( "Iterability of jQuery objects (gh-1693)", function() { + /* jshint unused: false */ + expect( 1 ); + + var i, elem, result; + + if ( typeof Symbol === "function" ) { + + elem = jQuery( "
" ); + result = ""; + + try { + eval( "for ( i of elem ) { result += i.nodeName; }" ); + } catch ( e ) {} + equal( result, "DIVSPANA", "for-of works on jQuery objects" ); + } else { + ok( true, "The browser doesn't support Symbols" ); + } +} ); -- 2.39.5