rleadingWhitespace = /^\s+/,
rxhtmlTag = /(<(\w+)[^>]*?)\/>/g,
rselfClosing = /^(?:abbr|br|col|img|input|link|meta|param|hr|area|embed)$/i,
- rinsideTable = /^<(thead|tbody|tfoot|colg|cap)/,
+ rtagName = /<(\w+)/,
rtbody = /<tbody/i,
+ rhtml = /</,
fcloseTag = function(all, front, tag){
return rselfClosing.test(tag) ?
all :
front + "></" + tag + ">";
+ },
+ wrapMap = {
+ option: [ 1, "<select multiple='multiple'>", "</select>" ],
+ legend: [ 1, "<fieldset>", "</fieldset>" ],
+ thead: [ 1, "<table>", "</table>" ],
+ tr: [ 2, "<table><tbody>", "</tbody></table>" ],
+ td: [ 3, "<table><tbody><tr>", "</tr></tbody></table>" ],
+ col: [ 2, "<table><tbody></tbody><colgroup>", "</colgroup></table>" ],
+ _default: [ 0, "", "" ]
};
+wrapMap.optgroup = wrapMap.option;
+wrapMap.tbody = wrapMap.tfoot = wrapMap.colgroup = wrapMap.caption = wrapMap.thead;
+wrapMap.th = wrapMap.td;
+
+// IE can't serialize <link> and <script> tags normally
+if ( !jQuery.support.htmlSerialize ) {
+ wrapMap._default = [ 1, "div<div>", "</div>" ];
+}
+
jQuery.fn.extend({
text: function( text ) {
if ( typeof text !== "object" && text !== undefined )
},
before: function() {
- return this.domManip(arguments, false, function(elem){
- this.parentNode.insertBefore( elem, this );
- });
+ if ( this[0] && this[0].parentNode ) {
+ return this.domManip(arguments, false, function(elem){
+ this.parentNode.insertBefore( elem, this );
+ });
+ } else {
+ var set = jQuery.isFunction(arguments[0]) ?
+ jQuery( arguments[0]() ) :
+ jQuery.apply(jQuery, arguments);
+
+ return this.pushStack( set.add( this ), "before", arguments );
+ }
},
after: function() {
- return this.domManip(arguments, false, function(elem){
- this.parentNode.insertBefore( elem, this.nextSibling );
- });
+ if ( this[0] && this[0].parentNode ) {
+ return this.domManip(arguments, false, function(elem){
+ this.parentNode.insertBefore( elem, this.nextSibling );
+ });
+ } else {
+ return jQuery.isFunction(arguments[0]) ?
+ this.add( arguments[0]() ) :
+ this.add.apply( this, arguments );
+ }
},
clone: function( events ) {
},
html: function( value ) {
- return value === undefined ?
- (this[0] ?
+ if ( value === undefined ) {
+ return this[0] ?
this[0].innerHTML.replace(rinlinejQuery, "") :
- null) :
+ null;
+
+ // See if we can take a shortcut and just use innerHTML
+ } else if ( typeof value === "string" && !/<script/i.test( value ) &&
+ (!jQuery.support.leadingWhitespace || !rleadingWhitespace.test( value )) &&
+ !wrapMap[ (rtagName.exec( value ) || ["", ""])[1].toLowerCase() ] ) {
+
+ try {
+ for ( var i = 0, l = this.length; i < l; i++ ) {
+ // Remove element nodes and prevent memory leaks
+ if ( this[i].nodeType === 1 ) {
+ cleanData( this[i].getElementsByTagName("*") );
+ this[i].innerHTML = value;
+ }
+ }
+
+ // If using innerHTML throws an exception, use the fallback method
+ } catch(e) {
+ this.empty().append( value );
+ }
+
+ } else {
this.empty().append( value );
+ }
+
+ return this;
},
replaceWith: function( value ) {
- return this.after( value ).remove();
+ if ( this[0] && this[0].parentNode ) {
+ return this.after( value ).remove();
+ } else {
+ return this.pushStack( jQuery(jQuery.isFunction(value) ? value() : value), "replaceWith", value );
+ }
},
detach: function( selector ) {
if ( args[0] && args[0].parentNode && args[0].parentNode.nodeType === 11 ) {
results = { fragment: args[0].parentNode };
} else {
- results = buildFragment( args, this[0], scripts );
+ results = buildFragment( args, this, scripts );
}
first = results.fragment.firstChild;
jQuery.fn[ original ].apply( jQuery(insert[i]), elems );
ret = ret.concat( elems );
}
-
- return this.pushStack( ret, name, selector );
+ return this.pushStack( ret, name, insert.selector );
};
});
remove: function( selector, keepData ) {
if ( !selector || jQuery.multiFilter( selector, [ this ] ).length ) {
if ( !keepData && this.nodeType === 1 ) {
- cleanData( jQuery("*", this).add(this) );
+ cleanData( this.getElementsByTagName("*") );
+ cleanData( [ this ] );
}
if ( this.parentNode ) {
empty: function() {
// Remove element nodes and prevent memory leaks
if ( this.nodeType === 1 ) {
- cleanData( jQuery("*", this) );
+ cleanData( this.getElementsByTagName("*") );
}
// Remove any remaining nodes
if ( !elem ) { return; }
// Convert html string into DOM nodes
- if ( typeof elem === "string" ) {
+ if ( typeof elem === "string" && !rhtml.test( elem ) ) {
+ elem = context.createTextNode( elem );
+
+ } else if ( typeof elem === "string" ) {
// Fix "XHTML"-style tags in all browsers
elem = elem.replace(rxhtmlTag, fcloseTag);
// Trim whitespace, otherwise indexOf won't work as expected
- var tags = elem.replace(rleadingWhitespace, "")
- .substring(0, 10).toLowerCase();
-
- var wrap =
- // option or optgroup
- !tags.indexOf("<opt") &&
- [ 1, "<select multiple='multiple'>", "</select>" ] ||
-
- !tags.indexOf("<leg") &&
- [ 1, "<fieldset>", "</fieldset>" ] ||
-
- rinsideTable.test(tags) &&
- [ 1, "<table>", "</table>" ] ||
-
- !tags.indexOf("<tr") &&
- [ 2, "<table><tbody>", "</tbody></table>" ] ||
-
- // <thead> matched above
- (!tags.indexOf("<td") || !tags.indexOf("<th")) &&
- [ 3, "<table><tbody><tr>", "</tr></tbody></table>" ] ||
-
- !tags.indexOf("<col") &&
- [ 2, "<table><tbody></tbody><colgroup>", "</colgroup></table>" ] ||
-
- // IE can't serialize <link> and <script> tags normally
- !jQuery.support.htmlSerialize &&
- [ 1, "div<div>", "</div>" ] ||
-
- [ 0, "", "" ];
+ var tag = (rtagName.exec( elem ) || ["", ""])[1].toLowerCase(),
+ wrap = wrapMap[ tag ] || wrapMap._default,
+ depth = wrap[0];
// Go to html and back, then peel off extra wrappers
div.innerHTML = wrap[1] + elem + wrap[2];
// Move to the right depth
- while ( wrap[0]-- ) {
+ while ( depth-- ) {
div = div.lastChild;
}
// String was a <table>, *may* have spurious <tbody>
var hasBody = rtbody.test(elem),
- tbody = !tags.indexOf("<table") && !hasBody ?
+ tbody = tag === "table" && !hasBody ?
div.firstChild && div.firstChild.childNodes :
// String was a bare <thead> or <tfoot>
});
function cleanData( elems ) {
- for ( var i = 0, l = elems.length; i < l; i++ ) {
- var id = elems[i][expando];
- if ( id ) {
+ for ( var i = 0, elem, id; (elem = elems[i]) != null; i++ ) {
+ if ( (id = elem[expando]) ) {
delete jQuery.cache[ id ];
}
}