X-Git-Url: http://git.asbjorn.biz/?a=blobdiff_plain;f=src%2Fcore.js;h=c7d10f3c87712ed0e62609113938a21b2c0d77fb;hb=3e46bce751f062775be95a6bef3cf53096a3ed21;hp=78f2bcd140fbafc161d8a421e0af79773c5d05b1;hpb=7346a476cc1fe20612fb1e9b08faef27e36a962c;p=jquery.git diff --git a/src/core.js b/src/core.js index 78f2bcd..c7d10f3 100644 --- a/src/core.js +++ b/src/core.js @@ -47,20 +47,16 @@ jQuery.fn = jQuery.prototype = { else { var elem = document.getElementById( match[3] ); - // Make sure an element was located - if ( elem ){ - // Handle the case where IE and Opera return items - // by name instead of ID - if ( elem.id != match[3] ) - return jQuery().find( selector ); - - // Otherwise, we inject the element directly into the jQuery object - var ret = jQuery( elem ); - ret.context = document; - ret.selector = selector; - return ret; - } - selector = []; + // Handle the case where IE and Opera return items + // by name instead of ID + if ( elem && elem.id != match[3] ) + return jQuery().find( selector ); + + // Otherwise, we inject the element directly into the jQuery object + var ret = jQuery( elem || [] ); + ret.context = document; + ret.selector = selector; + return ret; } // HANDLE: $(expr, [context]) @@ -79,7 +75,9 @@ jQuery.fn = jQuery.prototype = { this.context = selector.context; } - return this.setArray(jQuery.makeArray(selector)); + return this.setArray(jQuery.isArray( selector ) ? + selector : + jQuery.makeArray(selector)); }, // Start with an empty selector @@ -99,7 +97,7 @@ jQuery.fn = jQuery.prototype = { return num === undefined ? // Return a 'clean' array - jQuery.makeArray( this ) : + Array.prototype.slice.call( this ) : // Return just the object this[ num ]; @@ -206,20 +204,22 @@ jQuery.fn = jQuery.prototype = { }, wrapAll: function( html ) { - if ( this[0] ) + if ( this[0] ) { // The elements to wrap the target around - jQuery( html, this[0].ownerDocument ) - .clone() - .insertBefore( this[0] ) - .map(function(){ - var elem = this; + var wrap = jQuery( html, this[0].ownerDocument ).clone(); + + if ( this[0].parentNode ) + wrap.insertBefore( this[0] ); - while ( elem.firstChild ) - elem = elem.firstChild; + wrap.map(function(){ + var elem = this; - return elem; - }) - .append(this); + while ( elem.firstChild ) + elem = elem.firstChild; + + return elem; + }).append(this); + } return this; }, @@ -266,22 +266,22 @@ jQuery.fn = jQuery.prototype = { return this.prevObject || jQuery( [] ); }, + // For internal use only. + // Behaves like an Array's method, not like a jQuery method. push: [].push, + sort: [].sort, + splice: [].splice, find: function( selector ) { - if ( this.length === 1 && !/,/.test(selector) ) { + if ( this.length === 1 ) { var ret = this.pushStack( [], "find", selector ); ret.length = 0; jQuery.find( selector, this[0], ret ); return ret; } else { - var elems = jQuery.map(this, function(elem){ + return this.pushStack( jQuery.unique(jQuery.map(this, function(elem){ return jQuery.find( selector, elem ); - }); - - return this.pushStack( /[^+>] [^+>]/.test( selector ) ? - jQuery.unique( elems ) : - elems, "find", selector ); + })), "find", selector ); } }, @@ -297,33 +297,37 @@ jQuery.fn = jQuery.prototype = { // attributes in IE that are actually only stored // as properties will not be copied (such as the // the name attribute on an input). - var clone = this.cloneNode(true), - container = document.createElement("div"); - container.appendChild(clone); - return jQuery.clean([container.innerHTML])[0]; + var html = this.outerHTML; + if ( !html ) { + var div = this.ownerDocument.createElement("div"); + div.appendChild( this.cloneNode(true) ); + html = div.innerHTML; + } + + return jQuery.clean([html.replace(/ jQuery\d+="(?:\d+|null)"/g, "").replace(/^\s*/, "")])[0]; } else return this.cloneNode(true); }); - // Need to set the expando to null on the cloned set if it exists - // removeData doesn't work here, IE removes it from the original as well - // this is primarily for IE but the data expando shouldn't be copied over in any browser - var clone = ret.find("*").andSelf().each(function(){ - if ( this[ expando ] !== undefined ) - this[ expando ] = null; - }); - // Copy the events from the original to the clone - if ( events === true ) - this.find("*").andSelf().each(function(i){ - if (this.nodeType == 3) + if ( events === true ) { + var orig = this.find("*").andSelf(), i = 0; + + ret.find("*").andSelf().each(function(){ + if ( this.nodeName !== orig[i].nodeName ) return; - var events = jQuery.data( this, "events" ); - for ( var type in events ) - for ( var handler in events[ type ] ) - jQuery.event.add( clone[ i ], type, events[ type ][ handler ], events[ type ][ handler ].data ); + var events = jQuery.data( orig[i], "events" ); + + for ( var type in events ) { + for ( var handler in events[ type ] ) { + jQuery.event.add( this, type, events[ type ][ handler ], events[ type ][ handler ].data ); + } + } + + i++; }); + } // Return the cloned set return ret; @@ -342,14 +346,18 @@ jQuery.fn = jQuery.prototype = { }, closest: function( selector ) { - var pos = jQuery.expr.match.POS.test( selector ) ? jQuery(selector) : null; + var pos = jQuery.expr.match.POS.test( selector ) ? jQuery(selector) : null, + closer = 0; return this.map(function(){ var cur = this; while ( cur && cur.ownerDocument ) { - if ( pos ? pos.index(cur) > -1 : jQuery(cur).is(selector) ) + if ( pos ? pos.index(cur) > -1 : jQuery(cur).is(selector) ) { + jQuery.data(cur, "closest", closer); return cur; + } cur = cur.parentNode; + closer++; } }); }, @@ -462,7 +470,7 @@ jQuery.fn = jQuery.prototype = { html: function( value ) { return value === undefined ? (this[0] ? - this[0].innerHTML : + this[0].innerHTML.replace(/ jQuery\d+="(?:\d+|null)"/g, "") : null) : this.empty().append( value ); }, @@ -617,15 +625,13 @@ jQuery.extend({ // check if an element is in a (or is an) XML document isXMLDoc: function( elem ) { - return elem.documentElement && !elem.body || - elem.tagName && elem.ownerDocument && !elem.ownerDocument.body; + return elem.nodeType === 9 && elem.documentElement.nodeName !== "HTML" || + !!elem.ownerDocument && jQuery.isXMLDoc( elem.ownerDocument ); }, // Evalulates a script in a global context globalEval: function( data ) { - data = jQuery.trim( data ); - - if ( data ) { + if ( data && /\S/.test(data) ) { // Inspired by code by Andrea Giammarchi // http://webreflection.blogspot.com/2007/08/global-scope-evaluation-and-dom.html var head = document.getElementsByTagName("head")[0] || document.documentElement, @@ -708,7 +714,7 @@ jQuery.extend({ // internal only, use hasClass("class") has: function( elem, className ) { - return jQuery.inArray( className, (elem.className || elem).toString().split(/\s+/) ) > -1; + return elem && jQuery.inArray( className, (elem.className || elem).toString().split(/\s+/) ) > -1; } }, @@ -728,26 +734,32 @@ jQuery.extend({ elem.style[ name ] = old[ name ]; }, - css: function( elem, name, force ) { + css: function( elem, name, force, extra ) { if ( name == "width" || name == "height" ) { var val, props = { position: "absolute", visibility: "hidden", display:"block" }, which = name == "width" ? [ "Left", "Right" ] : [ "Top", "Bottom" ]; function getWH() { val = name == "width" ? elem.offsetWidth : elem.offsetHeight; - var padding = 0, border = 0; + + if ( extra === "border" ) + return; + jQuery.each( which, function() { - padding += parseFloat(jQuery.curCSS( elem, "padding" + this, true)) || 0; - border += parseFloat(jQuery.curCSS( elem, "border" + this + "Width", true)) || 0; + if ( !extra ) + val -= parseFloat(jQuery.curCSS( elem, "padding" + this, true)) || 0; + if ( extra === "margin" ) + val += parseFloat(jQuery.curCSS( elem, "margin" + this, true)) || 0; + else + val -= parseFloat(jQuery.curCSS( elem, "border" + this + "Width", true)) || 0; }); - val -= Math.round(padding + border); } - if ( jQuery(elem).is(":visible") ) + if ( elem.offsetWidth !== 0 ) getWH(); else jQuery.swap( elem, props, getWH ); - return Math.max(0, val); + return Math.max(0, Math.round(val)); } return jQuery.curCSS( elem, name, force ); @@ -853,7 +865,7 @@ jQuery.extend({ }); // Trim whitespace, otherwise indexOf won't work as expected - var tags = jQuery.trim( elem ).toLowerCase(); + var tags = elem.replace(/^\s+/, "").substring(0, 10).toLowerCase(); var wrap = // option or optgroup @@ -893,11 +905,12 @@ jQuery.extend({ if ( !jQuery.support.tbody ) { // String was a , *may* have spurious - var tbody = !tags.indexOf(" or - wrap[1] == "
" && tags.indexOf("" && !hasBody ? div.childNodes : []; @@ -923,7 +936,7 @@ jQuery.extend({ if ( fragment ) { for ( var i = 0; ret[i]; i++ ) { - if ( jQuery.nodeName( ret[i], "script" ) ) { + if ( jQuery.nodeName( ret[i], "script" ) && (!ret[i].type || ret[i].type.toLowerCase() === "text/javascript") ) { scripts.push( ret[i].parentNode ? ret[i].parentNode.removeChild( ret[i] ) : ret[i] ); } else { if ( ret[i].nodeType === 1 ) @@ -959,7 +972,7 @@ jQuery.extend({ // Safari mis-reports the default selected property of a hidden option // Accessing the parent's selectedIndex property fixes it - if ( name == "selected" ) + if ( name == "selected" && elem.parentNode ) elem.parentNode.selectedIndex; // If applicable, access the attribute via the DOM 0 way @@ -982,9 +995,11 @@ jQuery.extend({ var attributeNode = elem.getAttributeNode( "tabIndex" ); return attributeNode && attributeNode.specified ? attributeNode.value - : elem.nodeName.match(/^(a|area|button|input|object|select|textarea)$/i) + : elem.nodeName.match(/(button|input|object|select|textarea)/i) ? 0 - : undefined; + : elem.nodeName.match(/^(a|area)$/i) && elem.href + ? 0 + : undefined; } return elem[ name ]; @@ -1219,7 +1234,7 @@ jQuery.each({ empty: function() { // Remove element nodes and prevent memory leaks - jQuery( ">*", this ).remove(); + jQuery(this).children().remove(); // Remove any remaining nodes while ( this.firstChild )