From 221aaebd93494039f079d4ee7f0a9111c6c048b6 Mon Sep 17 00:00:00 2001 From: rwldrn Date: Thu, 16 Jun 2011 17:52:38 -0400 Subject: [PATCH] [#583] Fixes Popcorn.forEach() NodeList enumeration bug --- popcorn.js | 8 +++++++- test/popcorn.unit.js | 34 ++++++++++++++++++++++++++++++++-- 2 files changed, 39 insertions(+), 3 deletions(-) diff --git a/popcorn.js b/popcorn.js index efa17bf..0032156 100644 --- a/popcorn.js +++ b/popcorn.js @@ -316,12 +316,18 @@ return obj.forEach( fn, context ); } + if ( toString.call( obj ) === "[object NodeList]" ) { + for ( var key = 0, len = obj.length; key < len; key++ ) { + fn.call( context, obj[ key ], key, obj ); + } + return obj; + } + for ( var key in obj ) { if ( hasOwn.call( obj, key ) ) { fn.call( context, obj[ key ], key, obj ); } } - return obj; }; diff --git a/test/popcorn.unit.js b/test/popcorn.unit.js index 7c4a603..6eb473f 100644 --- a/test/popcorn.unit.js +++ b/test/popcorn.unit.js @@ -70,6 +70,36 @@ test("Popcorn.* Static Methods", function () { }); }); +test("Popcorn.forEach", function() { + + expect(3); + + var count = 0, + nodelist = document.querySelectorAll("div[id^='qunit-']"), + array = [ 1, 2 ], + object = { a: "1", b: "2" }; + + Popcorn.forEach( nodelist, function() { + count++; + }); + + equal(count, nodelist.length, nodelist.length + " elements in NodeList"); + count = 0; + + Popcorn.forEach( array, function() { + count++; + }); + + equal(count, array.length, array.length + " items in Array"); + count = 0; + + Popcorn.forEach( object, function() { + count++; + }); + + equal(count, Popcorn.sizeOf(object), Popcorn.sizeOf(object) + " properties in object"); +}); + test("Popcorn.util.toSeconds" , function () { var framerate = 24, storedStartTime, @@ -433,8 +463,8 @@ test("Popcorn.[addTrackEvent | removeTrackEvent].ref()", function() { var popped = Popcorn("#video"); - // Calling exec() will create tracks and added them to the - // trackreference internally + // Calling exec() will create tracks and added them to the + // trackreference internally popped.exec( 1, function() { /* ... */ }); popped.exec( 3, function() { /* ... */ }); popped.exec( 5, function() { /* ... */ }); -- 2.43.0