1 var rinlinejQuery = / jQuery\d+="(?:\d+|null)"/g,
2 rleadingWhitespace = /^\s+/,
3 rxhtmlTag = /(<([\w:]+)[^>]*?)\/>/g,
4 rselfClosing = /^(?:abbr|br|col|img|input|link|meta|param|hr|area|embed)$/i,
5 rtagName = /<([\w:]+)/,
8 fcloseTag = function(all, front, tag){
9 return rselfClosing.test(tag) ?
11 front + "></" + tag + ">";
14 option: [ 1, "<select multiple='multiple'>", "</select>" ],
15 legend: [ 1, "<fieldset>", "</fieldset>" ],
16 thead: [ 1, "<table>", "</table>" ],
17 tr: [ 2, "<table><tbody>", "</tbody></table>" ],
18 td: [ 3, "<table><tbody><tr>", "</tr></tbody></table>" ],
19 col: [ 2, "<table><tbody></tbody><colgroup>", "</colgroup></table>" ],
20 _default: [ 0, "", "" ]
23 wrapMap.optgroup = wrapMap.option;
24 wrapMap.tbody = wrapMap.tfoot = wrapMap.colgroup = wrapMap.caption = wrapMap.thead;
25 wrapMap.th = wrapMap.td;
27 // IE can't serialize <link> and <script> tags normally
28 if ( !jQuery.support.htmlSerialize ) {
29 wrapMap._default = [ 1, "div<div>", "</div>" ];
33 text: function( text ) {
34 if ( typeof text !== "object" && text !== undefined ) {
35 return this.empty().append( (this[0] && this[0].ownerDocument || document).createTextNode( text ) );
40 jQuery.each( this, function() {
41 // Get the text from text nodes and CDATA nodes
42 if ( this.nodeType === 3 || this.nodeType === 4 ) {
43 ret += this.nodeValue;
45 // Traverse everything else, except comment nodes
46 } else if ( this.nodeType !== 8 ) {
47 ret += jQuery.fn.text.call( this.childNodes );
54 wrapAll: function( html ) {
55 if ( jQuery.isFunction( html ) ) {
56 return this.each(function() {
57 jQuery(this).wrapAll( html.apply(this, arguments) );
62 // The elements to wrap the target around
63 var wrap = jQuery( html, this[0].ownerDocument ).eq(0).clone();
65 if ( this[0].parentNode ) {
66 wrap.insertBefore( this[0] );
72 while ( elem.firstChild && elem.firstChild.nodeType === 1 ) {
73 elem = elem.firstChild;
83 wrapInner: function( html ) {
84 return this.each(function(){
85 jQuery( this ).contents().wrapAll( html );
89 wrap: function( html ) {
90 return this.each(function(){
91 jQuery( this ).wrapAll( html );
96 return this.parent().each(function(){
97 if ( !jQuery.nodeName( this, "body" ) ) {
98 jQuery( this ).replaceWith( this.childNodes );
104 return this.domManip(arguments, true, function(elem){
105 if ( this.nodeType === 1 ) {
106 this.appendChild( elem );
111 prepend: function() {
112 return this.domManip(arguments, true, function(elem){
113 if ( this.nodeType === 1 ) {
114 this.insertBefore( elem, this.firstChild );
120 if ( this[0] && this[0].parentNode ) {
121 return this.domManip(arguments, false, function(elem){
122 this.parentNode.insertBefore( elem, this );
124 } else if ( arguments.length ) {
125 var set = jQuery(arguments[0]);
126 set.push.apply( set, this.toArray() );
127 return this.pushStack( set, "before", arguments );
132 if ( this[0] && this[0].parentNode ) {
133 return this.domManip(arguments, false, function(elem){
134 this.parentNode.insertBefore( elem, this.nextSibling );
136 } else if ( arguments.length ) {
137 var set = this.pushStack( this, "after", arguments );
138 set.push.apply( set, jQuery(arguments[0]).toArray() );
143 clone: function( events ) {
145 var ret = this.map(function(){
146 if ( !jQuery.support.noCloneEvent && !jQuery.isXMLDoc(this) ) {
147 // IE copies events bound via attachEvent when
148 // using cloneNode. Calling detachEvent on the
149 // clone will also remove the events from the orignal
150 // In order to get around this, we use innerHTML.
151 // Unfortunately, this means some modifications to
152 // attributes in IE that are actually only stored
153 // as properties will not be copied (such as the
154 // the name attribute on an input).
155 var html = this.outerHTML, ownerDocument = this.ownerDocument;
157 var div = ownerDocument.createElement("div");
158 div.appendChild( this.cloneNode(true) );
159 html = div.innerHTML;
162 return jQuery.clean([html.replace(rinlinejQuery, "")
163 .replace(rleadingWhitespace, "")], ownerDocument)[0];
165 return this.cloneNode(true);
169 // Copy the events from the original to the clone
170 if ( events === true ) {
171 cloneCopyEvent( this, ret );
172 cloneCopyEvent( this.find("*"), ret.find("*") );
175 // Return the cloned set
179 html: function( value ) {
180 if ( value === undefined ) {
182 this[0].innerHTML.replace(rinlinejQuery, "") :
185 // See if we can take a shortcut and just use innerHTML
186 } else if ( typeof value === "string" && !/<script/i.test( value ) &&
187 (jQuery.support.leadingWhitespace || !rleadingWhitespace.test( value )) &&
188 !wrapMap[ (rtagName.exec( value ) || ["", ""])[1].toLowerCase() ] ) {
191 for ( var i = 0, l = this.length; i < l; i++ ) {
192 // Remove element nodes and prevent memory leaks
193 if ( this[i].nodeType === 1 ) {
194 cleanData( this[i].getElementsByTagName("*") );
195 this[i].innerHTML = value;
199 // If using innerHTML throws an exception, use the fallback method
201 this.empty().append( value );
205 this.empty().append( value );
211 replaceWith: function( value ) {
212 if ( this[0] && this[0].parentNode ) {
213 return this.each(function(){
214 var next = this.nextSibling, parent = this.parentNode;
216 jQuery(this).remove();
219 jQuery(next).before( value );
221 jQuery(parent).append( value );
225 return this.pushStack( jQuery(jQuery.isFunction(value) ? value() : value), "replaceWith", value );
229 detach: function( selector ) {
230 return this.remove( selector, true );
233 domManip: function( args, table, callback ) {
234 var results, first, value = args[0], scripts = [];
236 if ( jQuery.isFunction(value) ) {
237 return this.each(function() {
238 args[0] = value.call(this);
239 return jQuery(this).domManip( args, table, callback );
244 // If we're in a fragment, just use that instead of building a new one
245 if ( args[0] && args[0].parentNode && args[0].parentNode.nodeType === 11 ) {
246 results = { fragment: args[0].parentNode };
248 results = buildFragment( args, this, scripts );
251 first = results.fragment.firstChild;
254 table = table && jQuery.nodeName( first, "tr" );
256 for ( var i = 0, l = this.length; i < l; i++ ) {
259 root(this[i], first) :
261 results.cacheable || this.length > 1 || i > 0 ?
262 results.fragment.cloneNode(true) :
269 jQuery.each( scripts, evalScript );
275 function root( elem, cur ) {
276 return jQuery.nodeName(elem, "table") ?
277 (elem.getElementsByTagName("tbody")[0] ||
278 elem.appendChild(elem.ownerDocument.createElement("tbody"))) :
284 function cloneCopyEvent(orig, ret) {
288 if ( this.nodeName !== orig[i].nodeName ) {
292 var events = jQuery.data( orig[i], "events" );
294 for ( var type in events ) {
295 for ( var handler in events[ type ] ) {
296 jQuery.event.add( this, type, events[ type ][ handler ], events[ type ][ handler ].data );
302 function buildFragment(args, nodes, scripts){
303 var fragment, cacheable, cached, cacheresults, doc;
305 if ( args.length === 1 && typeof args[0] === "string" && args[0].length < 512 && args[0].indexOf("<option") < 0 ) {
307 cacheresults = jQuery.fragments[ args[0] ];
308 if ( cacheresults ) {
309 if ( cacheresults !== 1 ) {
310 fragment = cacheresults;
317 doc = (nodes && nodes[0] ? nodes[0].ownerDocument || nodes[0] : document);
318 fragment = doc.createDocumentFragment();
319 jQuery.clean( args, doc, fragment, scripts );
323 jQuery.fragments[ args[0] ] = cacheresults ? fragment : 1;
326 return { fragment: fragment, cacheable: cacheable };
329 jQuery.fragments = {};
333 prependTo: "prepend",
334 insertBefore: "before",
335 insertAfter: "after",
336 replaceAll: "replaceWith"
337 }, function(name, original){
338 jQuery.fn[ name ] = function( selector ) {
339 var ret = [], insert = jQuery( selector );
341 for ( var i = 0, l = insert.length; i < l; i++ ) {
342 var elems = (i > 0 ? this.clone(true) : this).get();
343 jQuery.fn[ original ].apply( jQuery(insert[i]), elems );
344 ret = ret.concat( elems );
346 return this.pushStack( ret, name, insert.selector );
351 // keepData is for internal use only--do not document
352 remove: function( selector, keepData ) {
353 if ( !selector || jQuery.filter( selector, [ this ] ).length ) {
354 if ( !keepData && this.nodeType === 1 ) {
355 cleanData( this.getElementsByTagName("*") );
356 cleanData( [ this ] );
359 if ( this.parentNode ) {
360 this.parentNode.removeChild( this );
366 // Remove element nodes and prevent memory leaks
367 if ( this.nodeType === 1 ) {
368 cleanData( this.getElementsByTagName("*") );
371 // Remove any remaining nodes
372 while ( this.firstChild ) {
373 this.removeChild( this.firstChild );
376 }, function(name, fn){
377 jQuery.fn[ name ] = function(){
378 return this.each( fn, arguments );
383 clean: function( elems, context, fragment, scripts ) {
384 context = context || document;
386 // !context.createElement fails in IE with an error but returns typeof 'object'
387 if ( typeof context.createElement === "undefined" ) {
388 context = context.ownerDocument || context[0] && context[0].ownerDocument || document;
393 jQuery.each(elems, function(i, elem){
394 if ( typeof elem === "number" ) {
398 if ( !elem ) { return; }
400 // Convert html string into DOM nodes
401 if ( typeof elem === "string" && !rhtml.test( elem ) ) {
402 elem = context.createTextNode( elem );
404 } else if ( typeof elem === "string" ) {
405 // Fix "XHTML"-style tags in all browsers
406 elem = elem.replace(rxhtmlTag, fcloseTag);
408 // Trim whitespace, otherwise indexOf won't work as expected
409 var tag = (rtagName.exec( elem ) || ["", ""])[1].toLowerCase(),
410 wrap = wrapMap[ tag ] || wrapMap._default,
412 div = context.createElement("div");
414 // Go to html and back, then peel off extra wrappers
415 div.innerHTML = wrap[1] + elem + wrap[2];
417 // Move to the right depth
422 // Remove IE's autoinserted <tbody> from table fragments
423 if ( !jQuery.support.tbody ) {
425 // String was a <table>, *may* have spurious <tbody>
426 var hasBody = rtbody.test(elem),
427 tbody = tag === "table" && !hasBody ?
428 div.firstChild && div.firstChild.childNodes :
430 // String was a bare <thead> or <tfoot>
431 wrap[1] == "<table>" && !hasBody ?
435 for ( var j = tbody.length - 1; j >= 0 ; --j ) {
436 if ( jQuery.nodeName( tbody[ j ], "tbody" ) && !tbody[ j ].childNodes.length ) {
437 tbody[ j ].parentNode.removeChild( tbody[ j ] );
443 // IE completely kills leading whitespace when innerHTML is used
444 if ( !jQuery.support.leadingWhitespace && rleadingWhitespace.test( elem ) ) {
445 div.insertBefore( context.createTextNode( rleadingWhitespace.exec(elem)[0] ), div.firstChild );
448 elem = jQuery.makeArray( div.childNodes );
451 if ( elem.nodeType ) {
454 ret = jQuery.merge( ret, elem );
460 for ( var i = 0; ret[i]; i++ ) {
461 if ( scripts && jQuery.nodeName( ret[i], "script" ) && (!ret[i].type || ret[i].type.toLowerCase() === "text/javascript") ) {
462 scripts.push( ret[i].parentNode ? ret[i].parentNode.removeChild( ret[i] ) : ret[i] );
464 if ( ret[i].nodeType === 1 ) {
465 ret.splice.apply( ret, [i + 1, 0].concat(jQuery.makeArray(ret[i].getElementsByTagName("script"))) );
467 fragment.appendChild( ret[i] );
476 function cleanData( elems ) {
477 for ( var i = 0, elem, id; (elem = elems[i]) != null; i++ ) {
478 if ( (id = elem[expando]) ) {
479 delete jQuery.cache[ id ];