1 var rinlinejQuery = / jQuery\d+="(?:\d+|null)"/g,
2 rleadingWhitespace = /^\s+/,
3 rxhtmlTag = /(<([\w:]+)[^>]*?)\/>/g,
4 rselfClosing = /^(?:area|br|col|embed|hr|img|input|link|meta|param)$/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 area: [ 1, "<map>", "</map>" ],
21 _default: [ 0, "", "" ]
24 wrapMap.optgroup = wrapMap.option;
25 wrapMap.tbody = wrapMap.tfoot = wrapMap.colgroup = wrapMap.caption = wrapMap.thead;
26 wrapMap.th = wrapMap.td;
28 // IE can't serialize <link> and <script> tags normally
29 if ( !jQuery.support.htmlSerialize ) {
30 wrapMap._default = [ 1, "div<div>", "</div>" ];
34 text: function( text ) {
35 if ( jQuery.isFunction(text) ) {
36 return this.each(function(i) {
37 var self = jQuery(this);
38 return self.text( text.call(this, i, self.text()) );
42 if ( typeof text !== "object" && text !== undefined ) {
43 return this.empty().append( (this[0] && this[0].ownerDocument || document).createTextNode( text ) );
46 return jQuery.getText( this );
49 wrapAll: function( html ) {
50 if ( jQuery.isFunction( html ) ) {
51 return this.each(function(i) {
52 jQuery(this).wrapAll( html.call(this, i) );
57 // The elements to wrap the target around
58 var wrap = jQuery( html, this[0].ownerDocument ).eq(0).clone(true);
60 if ( this[0].parentNode ) {
61 wrap.insertBefore( this[0] );
67 while ( elem.firstChild && elem.firstChild.nodeType === 1 ) {
68 elem = elem.firstChild;
78 wrapInner: function( html ) {
79 return this.each(function() {
80 var self = jQuery( this ), contents = self.contents();
82 if ( contents.length ) {
83 contents.wrapAll( html );
91 wrap: function( html ) {
92 return this.each(function() {
93 jQuery( this ).wrapAll( html );
98 return this.parent().each(function() {
99 if ( !jQuery.nodeName( this, "body" ) ) {
100 jQuery( this ).replaceWith( this.childNodes );
106 return this.domManip(arguments, true, function( elem ) {
107 if ( this.nodeType === 1 ) {
108 this.appendChild( elem );
113 prepend: function() {
114 return this.domManip(arguments, true, function( elem ) {
115 if ( this.nodeType === 1 ) {
116 this.insertBefore( elem, this.firstChild );
122 if ( this[0] && this[0].parentNode ) {
123 return this.domManip(arguments, false, function( elem ) {
124 this.parentNode.insertBefore( elem, this );
126 } else if ( arguments.length ) {
127 var set = jQuery(arguments[0]);
128 set.push.apply( set, this.toArray() );
129 return this.pushStack( set, "before", arguments );
134 if ( this[0] && this[0].parentNode ) {
135 return this.domManip(arguments, false, function( elem ) {
136 this.parentNode.insertBefore( elem, this.nextSibling );
138 } else if ( arguments.length ) {
139 var set = this.pushStack( this, "after", arguments );
140 set.push.apply( set, jQuery(arguments[0]).toArray() );
145 clone: function( events ) {
147 var ret = this.map(function() {
148 if ( !jQuery.support.noCloneEvent && !jQuery.isXMLDoc(this) ) {
149 // IE copies events bound via attachEvent when
150 // using cloneNode. Calling detachEvent on the
151 // clone will also remove the events from the orignal
152 // In order to get around this, we use innerHTML.
153 // Unfortunately, this means some modifications to
154 // attributes in IE that are actually only stored
155 // as properties will not be copied (such as the
156 // the name attribute on an input).
157 var html = this.outerHTML, ownerDocument = this.ownerDocument;
159 var div = ownerDocument.createElement("div");
160 div.appendChild( this.cloneNode(true) );
161 html = div.innerHTML;
164 return jQuery.clean([html.replace(rinlinejQuery, "")
165 .replace(rleadingWhitespace, "")], ownerDocument)[0];
167 return this.cloneNode(true);
171 // Copy the events from the original to the clone
172 if ( events === true ) {
173 cloneCopyEvent( this, ret );
174 cloneCopyEvent( this.find("*"), ret.find("*") );
177 // Return the cloned set
181 html: function( value ) {
182 if ( value === undefined ) {
183 return this[0] && this[0].nodeType === 1 ?
184 this[0].innerHTML.replace(rinlinejQuery, "") :
187 // See if we can take a shortcut and just use innerHTML
188 } else if ( typeof value === "string" && !/<script/i.test( value ) &&
189 (jQuery.support.leadingWhitespace || !rleadingWhitespace.test( value )) &&
190 !wrapMap[ (rtagName.exec( value ) || ["", ""])[1].toLowerCase() ] ) {
193 for ( var i = 0, l = this.length; i < l; i++ ) {
194 // Remove element nodes and prevent memory leaks
195 if ( this[i].nodeType === 1 ) {
196 cleanData( this[i].getElementsByTagName("*") );
197 this[i].innerHTML = value;
201 // If using innerHTML throws an exception, use the fallback method
203 this.empty().append( value );
206 } else if ( jQuery.isFunction( value ) ) {
207 this.each(function(i){
208 var self = jQuery(this), old = self.html();
209 self.empty().append(function(){
210 return value.call( this, i, old );
215 this.empty().append( value );
221 replaceWith: function( value ) {
222 if ( this[0] && this[0].parentNode ) {
223 // Make sure that the elements are removed from the DOM before they are inserted
224 // this can help fix replacing a parent with child elements
225 if ( !jQuery.isFunction( value ) ) {
226 value = jQuery( value ).detach();
229 return this.each(function() {
230 var next = this.nextSibling, parent = this.parentNode;
232 jQuery(this).remove();
235 jQuery(next).before( value );
237 jQuery(parent).append( value );
241 return this.pushStack( jQuery(jQuery.isFunction(value) ? value() : value), "replaceWith", value );
245 detach: function( selector ) {
246 return this.remove( selector, true );
249 domManip: function( args, table, callback ) {
250 var results, first, value = args[0], scripts = [];
252 if ( jQuery.isFunction(value) ) {
253 return this.each(function(i) {
254 var self = jQuery(this);
255 args[0] = value.call(this, i, table ? self.html() : undefined);
256 return self.domManip( args, table, callback );
261 // If we're in a fragment, just use that instead of building a new one
262 if ( args[0] && args[0].parentNode && args[0].parentNode.nodeType === 11 ) {
263 results = { fragment: args[0].parentNode };
265 results = buildFragment( args, this, scripts );
268 first = results.fragment.firstChild;
271 table = table && jQuery.nodeName( first, "tr" );
273 for ( var i = 0, l = this.length; i < l; i++ ) {
276 root(this[i], first) :
278 results.cacheable || this.length > 1 || i > 0 ?
279 results.fragment.cloneNode(true) :
286 jQuery.each( scripts, evalScript );
292 function root( elem, cur ) {
293 return jQuery.nodeName(elem, "table") ?
294 (elem.getElementsByTagName("tbody")[0] ||
295 elem.appendChild(elem.ownerDocument.createElement("tbody"))) :
301 function cloneCopyEvent(orig, ret) {
304 ret.each(function() {
305 if ( this.nodeName !== (orig[i] && orig[i].nodeName) ) {
309 var oldData = jQuery.data( orig[i++] ), curData = jQuery.data( this, oldData ), events = oldData && oldData.events;
312 delete curData.handle;
315 for ( var type in events ) {
316 for ( var handler in events[ type ] ) {
317 jQuery.event.add( this, type, events[ type ][ handler ], events[ type ][ handler ].data );
324 function buildFragment( args, nodes, scripts ) {
325 var fragment, cacheable, cached, cacheresults, doc;
327 if ( args.length === 1 && typeof args[0] === "string" && args[0].length < 512 && args[0].indexOf("<option") < 0 ) {
329 cacheresults = jQuery.fragments[ args[0] ];
330 if ( cacheresults ) {
331 if ( cacheresults !== 1 ) {
332 fragment = cacheresults;
339 doc = (nodes && nodes[0] ? nodes[0].ownerDocument || nodes[0] : document);
340 fragment = doc.createDocumentFragment();
341 jQuery.clean( args, doc, fragment, scripts );
345 jQuery.fragments[ args[0] ] = cacheresults ? fragment : 1;
348 return { fragment: fragment, cacheable: cacheable };
351 jQuery.fragments = {};
355 prependTo: "prepend",
356 insertBefore: "before",
357 insertAfter: "after",
358 replaceAll: "replaceWith"
359 }, function( name, original ) {
360 jQuery.fn[ name ] = function( selector ) {
361 var ret = [], insert = jQuery( selector );
363 for ( var i = 0, l = insert.length; i < l; i++ ) {
364 var elems = (i > 0 ? this.clone(true) : this).get();
365 jQuery.fn[ original ].apply( jQuery(insert[i]), elems );
366 ret = ret.concat( elems );
368 return this.pushStack( ret, name, insert.selector );
373 // keepData is for internal use only--do not document
374 remove: function( selector, keepData ) {
375 if ( !selector || jQuery.filter( selector, [ this ] ).length ) {
376 if ( !keepData && this.nodeType === 1 ) {
377 cleanData( this.getElementsByTagName("*") );
378 cleanData( [ this ] );
381 if ( this.parentNode ) {
382 this.parentNode.removeChild( this );
388 // Remove element nodes and prevent memory leaks
389 if ( this.nodeType === 1 ) {
390 cleanData( this.getElementsByTagName("*") );
393 // Remove any remaining nodes
394 while ( this.firstChild ) {
395 this.removeChild( this.firstChild );
398 }, function( name, fn ) {
399 jQuery.fn[ name ] = function() {
400 return this.each( fn, arguments );
405 clean: function( elems, context, fragment, scripts ) {
406 context = context || document;
408 // !context.createElement fails in IE with an error but returns typeof 'object'
409 if ( typeof context.createElement === "undefined" ) {
410 context = context.ownerDocument || context[0] && context[0].ownerDocument || document;
415 jQuery.each(elems, function( i, elem ) {
416 if ( typeof elem === "number" ) {
424 // Convert html string into DOM nodes
425 if ( typeof elem === "string" && !rhtml.test( elem ) ) {
426 elem = context.createTextNode( elem );
428 } else if ( typeof elem === "string" ) {
429 // Fix "XHTML"-style tags in all browsers
430 elem = elem.replace(rxhtmlTag, fcloseTag);
432 // Trim whitespace, otherwise indexOf won't work as expected
433 var tag = (rtagName.exec( elem ) || ["", ""])[1].toLowerCase(),
434 wrap = wrapMap[ tag ] || wrapMap._default,
436 div = context.createElement("div");
438 // Go to html and back, then peel off extra wrappers
439 div.innerHTML = wrap[1] + elem + wrap[2];
441 // Move to the right depth
446 // Remove IE's autoinserted <tbody> from table fragments
447 if ( !jQuery.support.tbody ) {
449 // String was a <table>, *may* have spurious <tbody>
450 var hasBody = rtbody.test(elem),
451 tbody = tag === "table" && !hasBody ?
452 div.firstChild && div.firstChild.childNodes :
454 // String was a bare <thead> or <tfoot>
455 wrap[1] === "<table>" && !hasBody ?
459 for ( var j = tbody.length - 1; j >= 0 ; --j ) {
460 if ( jQuery.nodeName( tbody[ j ], "tbody" ) && !tbody[ j ].childNodes.length ) {
461 tbody[ j ].parentNode.removeChild( tbody[ j ] );
467 // IE completely kills leading whitespace when innerHTML is used
468 if ( !jQuery.support.leadingWhitespace && rleadingWhitespace.test( elem ) ) {
469 div.insertBefore( context.createTextNode( rleadingWhitespace.exec(elem)[0] ), div.firstChild );
472 elem = jQuery.makeArray( div.childNodes );
475 if ( elem.nodeType ) {
478 ret = jQuery.merge( ret, elem );
484 for ( var i = 0; ret[i]; i++ ) {
485 if ( scripts && jQuery.nodeName( ret[i], "script" ) && (!ret[i].type || ret[i].type.toLowerCase() === "text/javascript") ) {
486 scripts.push( ret[i].parentNode ? ret[i].parentNode.removeChild( ret[i] ) : ret[i] );
488 if ( ret[i].nodeType === 1 ) {
489 ret.splice.apply( ret, [i + 1, 0].concat(jQuery.makeArray(ret[i].getElementsByTagName("script"))) );
491 fragment.appendChild( ret[i] );
500 function cleanData( elems ) {
501 for ( var i = 0, elem, id; (elem = elems[i]) != null; i++ ) {
502 if ( !jQuery.noData[elem.nodeName.toLowerCase()] && (id = elem[expando]) ) {
503 delete jQuery.cache[ id ];