Skip to content

Commit

Permalink
Converted a lot of for loops to use jQuery.each() instead.
Browse files Browse the repository at this point in the history
  • Loading branch information
jeresig committed Jan 14, 2007
1 parent 866187f commit 34355cd
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 49 deletions.
10 changes: 6 additions & 4 deletions src/ajax/ajax.js
Expand Up @@ -789,17 +789,19 @@ jQuery.extend({
// of form elements
if ( a.constructor == Array || a.jquery )
// Serialize the form elements
for ( var i = 0; i < a.length; i++ )
s.push( encodeURIComponent(a[i].name) + "=" + encodeURIComponent( a[i].value ) );
jQuery.each( a, function(){
s.push( encodeURIComponent(this.name) + "=" + encodeURIComponent( this.value ) );
});

// Otherwise, assume that it's an object of key/value pairs
else
// Serialize the key/values
for ( var j in a )
// If the value is an array then the key names need to be repeated
if ( a[j].constructor == Array )
for ( var k = 0; k < a[j].length; k++ )
s.push( encodeURIComponent(j) + "=" + encodeURIComponent( a[j][k] ) );
jQuery.each( a[j], function(){
s.push( encodeURIComponent(j) + "=" + encodeURIComponent( this ) );
});
else
s.push( encodeURIComponent(j) + "=" + encodeURIComponent( a[j] ) );

Expand Down
10 changes: 6 additions & 4 deletions src/event/event.js
Expand Up @@ -77,8 +77,9 @@ jQuery.event = {
if ( !element ) {
var g = this.global[type];
if ( g )
for ( var i = 0, gl = g.length; i < gl; i++ )
this.trigger( type, data, g[i] );
jQuery.each( g, function(){
jQuery.event.trigger( type, data, this );
});

// Handle triggering a single element
} else if ( element["on" + type] ) {
Expand Down Expand Up @@ -467,8 +468,9 @@ jQuery.extend({
// If there are functions bound, to execute
if ( jQuery.readyList ) {
// Execute all of them
for ( var i = 0; i < jQuery.readyList.length; i++ )
jQuery.readyList[i].apply( document );
jQuery.each( jQuery.readyList, function(){
this.apply( document );
});

// Reset the list of functions
jQuery.readyList = null;
Expand Down
45 changes: 21 additions & 24 deletions src/jquery/jquery.js
Expand Up @@ -546,18 +546,16 @@ jQuery.fn = jQuery.prototype = {
text: function(e) {
if ( typeof e == "string" )
return this.empty().append( document.createTextNode( e ) );
else {
e = e || this;
var t = "";
for ( var j = 0, el = e.length; j < el; j++ ) {
var r = e[j].childNodes;
for ( var i = 0, rl = r.length; i < rl; i++ )
if ( r[i].nodeType != 8 )
t += r[i].nodeType != 1 ?
r[i].nodeValue : jQuery.fn.text([ r[i] ]);
}
return t;
}

var t = "";
jQuery.each( e || this, function(){
jQuery.each( this.childNodes, function(){
if ( this.nodeType != 8 )
t += this.nodeType != 1 ?
this.nodeValue : jQuery.fn.text([ this ]);
});
});
return t;
},

/**
Expand Down Expand Up @@ -1102,8 +1100,9 @@ jQuery.fn = jQuery.prototype = {
if ( table && this.nodeName.toUpperCase() == "TABLE" && a[0].nodeName.toUpperCase() == "TR" )
obj = this.getElementsByTagName("tbody")[0] || this.appendChild(document.createElement("tbody"));

for ( var i = 0, al = a.length; i < al; i++ )
fn.apply( obj, [ clone ? a[i].cloneNode(true) : a[i] ] );
jQuery.each( a, function(){
fn.apply( obj, [ clone ? this.cloneNode(true) : this ] );
});

});
}
Expand Down Expand Up @@ -1316,10 +1315,10 @@ jQuery.extend({
if ( p == "height" || p == "width" ) {
var old = {}, oHeight, oWidth, d = ["Top","Bottom","Right","Left"];

for ( var i = 0, dl = d.length; i < dl; i++ ) {
old["padding" + d[i]] = 0;
old["border" + d[i] + "Width"] = 0;
}
jQuery.each( d, function(){
old["padding" + this] = 0;
old["border" + this + "Width"] = 0;
});

jQuery.swap( e, old, function() {
if (jQuery.css(e,"display") != "none") {
Expand Down Expand Up @@ -1395,10 +1394,8 @@ jQuery.extend({
clean: function(a) {
var r = [];

for ( var i = 0, al = a.length; i < al; i++ ) {
var arg = a[i];

if ( !arg ) continue;
jQuery.each( a, function(i,arg){
if ( !arg ) return;

if ( arg.constructor == Number )
arg = arg.toString();
Expand Down Expand Up @@ -1453,14 +1450,14 @@ jQuery.extend({
}

if ( arg.length === 0 )
continue;
return;

if ( arg[0] == undefined )
r.push( arg );
else
r = jQuery.merge( r, arg );

}
});

return r;
},
Expand Down
35 changes: 18 additions & 17 deletions src/selector/selector.js
Expand Up @@ -153,10 +153,11 @@ jQuery.extend({

if ( m ) {
// Perform our own iteration and filter
for ( var i = 0, rl = ret.length; i < rl; i++ )
for ( var c = ret[i].firstChild; c; c = c.nextSibling )
jQuery.each( ret, function(){
for ( var c = this.firstChild; c; c = c.nextSibling )
if ( c.nodeType == 1 && ( c.nodeName == m[1].toUpperCase() || m[1] == "*" ) )
r.push( c );
});

ret = r;
t = jQuery.trim( t.replace( re, "" ) );
Expand Down Expand Up @@ -235,20 +236,20 @@ jQuery.extend({
// We need to find all descendant elements, it is more
// efficient to use getAll() when we are already further down
// the tree - we try to recognize that here
for ( var i = 0, rl = ret.length; i < rl; i++ ) {
jQuery.each( ret, function(){
// Grab the tag name being searched for
var tag = m[1] != "" || m[0] == "" ? "*" : m[2];

// Handle IE7 being really dumb about <object>s
if ( ret[i].nodeName.toUpperCase() == "OBJECT" && tag == "*" )
if ( this.nodeName.toUpperCase() == "OBJECT" && tag == "*" )
tag = "param";

jQuery.merge( r,
m[1] != "" && ret.length != 1 ?
jQuery.getAll( ret[i], [], m[1], m[2], rec ) :
ret[i].getElementsByTagName( tag )
jQuery.getAll( this, [], m[1], m[2], rec ) :
this.getElementsByTagName( tag )
);
}
});

// It's faster to filter by class and be done with it
if ( m[1] == "." && ret.length == 1 )
Expand All @@ -263,11 +264,12 @@ jQuery.extend({
r = [];

// Then try to find the element with the ID
for ( var i = 0, tl = tmp.length; i < tl; i++ )
if ( tmp[i].getAttribute("id") == m[2] ) {
r = [ tmp[i] ];
break;
jQuery.each( tmp, function(){
if ( this.getAttribute("id") == m[2] ) {
r = [ this ];
return false;
}
});
}

ret = r;
Expand Down Expand Up @@ -300,14 +302,13 @@ jQuery.extend({
// Look for common filter expressions
while ( t && /^[a-z[({<*:.#]/i.test(t) ) {

var p = jQuery.parse;
var p = jQuery.parse, m;

for ( var i = 0, pl = p.length; i < pl; i++ ) {
jQuery.each( p, function(i,re){

// Look for, and replace, string-like sequences
// and finally build a regexp out of it
var re = p[i];
var m = re.exec( t );
m = re.exec( t );

if ( m ) {
// Remove what we just matched
Expand All @@ -317,9 +318,9 @@ jQuery.extend({
if ( jQuery.expr[ m[1] ]._resort )
m = jQuery.expr[ m[1] ]._resort( m );

break;
return false;
}
}
});

// :not() is a special case that can be optimized by
// keeping it out of the expression list
Expand Down

0 comments on commit 34355cd

Please sign in to comment.