Commit 2fa3bac7 authored by Michał Gołębiowski's avatar Michał Gołębiowski

Core: Make jQuery objects iterable

Make iterating over jQuery objects possible using ES 2015 for-of:

    for ( node of $( "<div id=narwhal>" ) ) {
        console.log( node.id ); // "narwhal"
    }

(partially cherry-picked from bb026fc12c3c2ad37f47f0919e484bddcdc3d291)

Fixes gh-1693
parent a022da70
...@@ -477,6 +477,16 @@ jQuery.extend({ ...@@ -477,6 +477,16 @@ jQuery.extend({
support: support 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 // Populate the class2type map
jQuery.each("Boolean Number String Function Array Date RegExp Object Error".split(" "), jQuery.each("Boolean Number String Function Array Date RegExp Object Error".split(" "),
function(i, name) { function(i, name) {
......
...@@ -24,6 +24,7 @@ ...@@ -24,6 +24,7 @@
"DOMParser": false, "DOMParser": false,
"JSON": false, "JSON": false,
"Promise": false, "Promise": false,
"Symbol": false,
"QUnit": false, "QUnit": false,
"ok": false, "ok": false,
"equal": false, "equal": false,
......
...@@ -1519,3 +1519,23 @@ testIframeWithCallback( "Don't call window.onready (#14802)", "core/onready.html ...@@ -1519,3 +1519,23 @@ testIframeWithCallback( "Don't call window.onready (#14802)", "core/onready.html
equal( error, false, "no call to user-defined onready" ); 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( "<div></div><span></span><a></a>" );
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" );
}
} );
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment