X-Git-Url: http://git.asbjorn.biz/?a=blobdiff_plain;f=jquery%2Fjquery.js;h=4a960a902d2b8dd787e83a6624edf9b72cafb62c;hb=0ababc8fea6a6003be1649cfc3817d6c5628f0c7;hp=3b1354aec777c0a21f9780f47cfed0ab4cf1ccd6;hpb=130b8a1c030ae7888934fcd8dee6695eac271fcb;p=jquery.git diff --git a/jquery/jquery.js b/jquery/jquery.js index 3b1354a..4a960a9 100644 --- a/jquery/jquery.js +++ b/jquery/jquery.js @@ -1,7 +1,9 @@ /* - * jQuery (http://jquery.com/) - * By John Resig (http://ejohn.org/) - * Under an Attribution, Share Alike License + * jQuery (jquery.com) + * + * Copyright (c) 2006 John Resig (ejohn.org) + * Licensed under the MIT License: + * http://www.opensource.org/licenses/mit-license.php * * $Date$ * $Rev$ @@ -67,13 +69,14 @@ function $(a,c) { }, css: function(a,b) { - return this.each(function(){ - if ( !b ) - for ( var j in a ) - $.attr(this.style,j,a[j]); - else - $.attr(this.style,a,b); - }); + return a.constructor != String || b ? + this.each(function(){ + if ( !b ) + for ( var j in a ) + $.attr(this.style,j,a[j]); + else + $.attr(this.style,a,b); + }) : $.css( this.get(0), a ); }, toggle: function() { return this.each(function(){ @@ -105,7 +108,9 @@ function $(a,c) { }, removeClass: function(c) { return this.each(function(){ - this.className = c == null ? '' : + if ( c == null ) + this.className = ''; + else this.className.replace( new RegExp('(^|\\s*\\b[^-])'+c+'($|\\b(?=[^-]))', 'g'), ''); }); @@ -141,7 +146,7 @@ function $(a,c) { var clone = this.size() > 1; var a = $.clean(arguments); return this.each(function(){ - for ( var i in a ) + for ( var i = 0; i < a.length; i++ ) this.appendChild( clone ? a[i].cloneNode(true) : a[i] ); }); }, @@ -167,7 +172,7 @@ function $(a,c) { var clone = this.size() > 1; var a = $.clean(arguments); return this.each(function(){ - for ( var i in a ) + for ( var i = 0; i < a.length; i++ ) this.parentNode.insertBefore( clone ? a[i].cloneNode(true) : a[i], this ); }); }, @@ -214,27 +219,23 @@ function $(a,c) { }, parent: function(a) { - this.cur = $.map(this.cur,function(d){ - return d.parentNode; - }); - if ( a ) this.cur = $.filter(a,this.cur).r; - return this; + this.cur = $.map(this.cur,function(d){ + return d.parentNode; + }); + if ( a ) this.cur = $.filter(a,this.cur).r; + return this; }, parents: function(a) { - this.cur = $.map(this.cur,$.parents); - if ( a ) this.cur = $.filter(a,this.cur).r; - return this; + this.cur = $.map(this.cur,$.parents); + if ( a ) this.cur = $.filter(a,this.cur).r; + return this; }, siblings: function(a) { - // Incorrect, need to exclude current element - this.cur = $.map(this.cur,$.sibling); - if ( a ) this.cur = $.filter(a,this.cur).r; - return this; - }, - - parents: function(a) { + // Incorrect, need to exclude current element + this.cur = $.map(this.cur,$.sibling); + if ( a ) this.cur = $.filter(a,this.cur).r; return this; }, @@ -387,7 +388,7 @@ $.g = { checked: "a.checked" }, // TODO: Write getAttribute helper - ".": "$.hasWord(a.className||a.getAttribute('class'),m[2])", + ".": "$.hasWord(a,m[2])", "@": { "=": "$.attr(a,m[3]) == m[4]", "!=": "$.attr(a,m[3]) != m[4]", @@ -515,22 +516,22 @@ $.tag = function(a,b){ }; $.attr = function(o,a,v){ - if ( a && a.constructor == String ) { - var fix = { - 'for': 'htmlFor', - 'text': 'cssText', - 'class': 'className', - 'float': 'cssFloat' - }; - a = (fix[a] && fix[a].replace && fix[a]) || a; - var r = new RegExp("-([a-z])","ig"); - a = a.replace(r,function(z,b){return b.toUpperCase();}); - if ( v != null ) { - o[a] = v; - if ( o.setAttribute ) o.setAttribute(a,v); - } - return o[a] || o.getAttribute(a) || ''; - } else return ''; + if ( a && a.constructor == String ) { + var fix = { + 'for': 'htmlFor', + 'text': 'cssText', + 'class': 'className', + 'float': 'cssFloat' + }; + a = (fix[a] && fix[a].replace && fix[a]) || a; + var r = new RegExp("-([a-z])","ig"); + a = a.replace(r,function(z,b){return b.toUpperCase();}); + if ( v != null ) { + o[a] = v; + if ( o.setAttribute ) o.setAttribute(a,v); + } + return o[a] || o.getAttribute(a) || ''; + } else return ''; }; $.filter = function(t,r,not) { @@ -586,34 +587,36 @@ $.parents = function(a){ return b; }; -$.cleanSpaces = function(t){return t.replace(/^\s+|\s+$/g, '')}; +$.cleanSpaces = function(t){ + return t.replace(/^\s+|\s+$/g, '') +}; $.ofType = function(a,n,e) { - var t = $.grep($.sibling(a),function(b){return b.nodeName == a.nodeName}); - if ( e ) n = t.length - n - 1; - return n != null ? t[n] == a : t.length; + var t = $.grep($.sibling(a),function(b){return b.nodeName == a.nodeName}); + if ( e ) n = t.length - n - 1; + return n != null ? t[n] == a : t.length; }; $.sibling = function(a,n,e) { - var type = []; - var tmp = a.parentNode.childNodes; - for ( var i = 0; i < tmp.length; i++ ) { - if ( tmp[i].nodeType == 1 ) - type[type.length] = tmp[i]; - if ( tmp[i] == a ) - type.n = type.length - 1; - } - if ( e ) n = type.length - n - 1; - type.cur = ( type[n] == a ); - type.prev = ( type.n > 0 ? type[type.n - 1] : null ); - type.next = ( type.n < type.length - 1 ? type[type.n + 1] : null ); - return type; + var type = []; + var tmp = a.parentNode.childNodes; + for ( var i = 0; i < tmp.length; i++ ) { + if ( tmp[i].nodeType == 1 ) + type[type.length] = tmp[i]; + if ( tmp[i] == a ) + type.n = type.length - 1; + } + if ( e ) n = type.length - n - 1; + type.cur = ( type[n] == a ); + type.prev = ( type.n > 0 ? type[type.n - 1] : null ); + type.next = ( type.n < type.length - 1 ? type[type.n + 1] : null ); + return type; }; $.hasWord = function(e,a) { - if ( e == null ) return false; - if ( e.className != null ) e = e.className; - return new RegExp("(^|\\s)" + a + "(\\s|$)").test(e) + if ( e == null ) return false; + if ( e.className != null ) e = e.className; + return new RegExp("(^|\\s)" + a + "(\\s|$)").test(e) }; $.getAll = function(o,r) { @@ -633,36 +636,36 @@ $.merge = function(a,b) { for ( var j = 0; j < b.length; j++ ) d[j] = b[j]; - for ( var i = 0; i < a.length; i++ ) { - var c = true; - for ( var j = 0; j < b.length; j++ ) - if ( a[i] == b[j] ) - c = false; - if ( c ) - d[d.length] = a[i]; - } + for ( var i = 0; i < a.length; i++ ) { + var c = true; + for ( var j = 0; j < b.length; j++ ) + if ( a[i] == b[j] ) + c = false; + if ( c ) + d[d.length] = a[i]; + } return d; }; $.grep = function(a,f,s) { - var r = []; + var r = []; if ( a != null ) for ( var i = 0; i < a.length; i++ ) if ( (!s && f(a[i],i)) || (s && !f(a[i],i)) ) r[r.length] = a[i]; - return r; + return r; }; $.map = function(a,f) { - var r = []; - for ( var i = 0; i < a.length; i++ ) { - var t = f(a[i],i); - if ( t != null ) { - if ( t.constructor != Array ) t = [t]; + var r = []; + for ( var i = 0; i < a.length; i++ ) { + var t = f(a[i],i); + if ( t != null ) { + if ( t.constructor != Array ) t = [t]; r = $.merge( t, r ); } - } - return r; + } + return r; }; // Bind an event to an element @@ -699,9 +702,10 @@ function removeEvent(element, type, handler) { } }; -function triggerEvent(element,type) { - if ( element["on" + type] ) - element["on" + type]({ type: type }); +function triggerEvent(element,type,data) { + data = data || [{ type: type }]; + if ( element && element["on" + type] ) + $.apply( element, element["on" + type], data ); } function handleEvent(event) { @@ -710,7 +714,7 @@ function handleEvent(event) { var handlers = []; for ( var i in this.events[event.type] ) handlers[handlers.length] = this.events[event.type][i]; - for ( var i = 0; i < handlers.length; i++ ) { + for ( var i = 0; i < handlers.length; i++ ) { try { if ( handlers[i].constructor == Function ) { this.$$handleEvent = handlers[i]; @@ -752,6 +756,6 @@ $.fn.text = function(e) { }; setTimeout(function(){ - if ( typeof Prototype != "undefined" && $.g == null && $.clean == null ) - throw "Error: You are overwriting jQuery, please include jQuery last."; + if ( typeof Prototype != "undefined" && $.g == null && $.clean == null ) + throw "Error: You are overwriting jQuery, please include jQuery last."; }, 1000);