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 jQuery( this ).contents().wrapAll( html );
84 wrap: function( html ) {
85 return this.each(function() {
86 jQuery( this ).wrapAll( html );
91 return this.parent().each(function() {
92 if ( !jQuery.nodeName( this, "body" ) ) {
93 jQuery( this ).replaceWith( this.childNodes );
99 return this.domManip(arguments, true, function( elem ) {
100 if ( this.nodeType === 1 ) {
101 this.appendChild( elem );
106 prepend: function() {
107 return this.domManip(arguments, true, function( elem ) {
108 if ( this.nodeType === 1 ) {
109 this.insertBefore( elem, this.firstChild );
115 if ( this[0] && this[0].parentNode ) {
116 return this.domManip(arguments, false, function( elem ) {
117 this.parentNode.insertBefore( elem, this );
119 } else if ( arguments.length ) {
120 var set = jQuery(arguments[0]);
121 set.push.apply( set, this.toArray() );
122 return this.pushStack( set, "before", arguments );
127 if ( this[0] && this[0].parentNode ) {
128 return this.domManip(arguments, false, function( elem ) {
129 this.parentNode.insertBefore( elem, this.nextSibling );
131 } else if ( arguments.length ) {
132 var set = this.pushStack( this, "after", arguments );
133 set.push.apply( set, jQuery(arguments[0]).toArray() );
138 clone: function( events ) {
140 var ret = this.map(function() {
141 if ( !jQuery.support.noCloneEvent && !jQuery.isXMLDoc(this) ) {
142 // IE copies events bound via attachEvent when
143 // using cloneNode. Calling detachEvent on the
144 // clone will also remove the events from the orignal
145 // In order to get around this, we use innerHTML.
146 // Unfortunately, this means some modifications to
147 // attributes in IE that are actually only stored
148 // as properties will not be copied (such as the
149 // the name attribute on an input).
150 var html = this.outerHTML, ownerDocument = this.ownerDocument;
152 var div = ownerDocument.createElement("div");
153 div.appendChild( this.cloneNode(true) );
154 html = div.innerHTML;
157 return jQuery.clean([html.replace(rinlinejQuery, "")
158 .replace(rleadingWhitespace, "")], ownerDocument)[0];
160 return this.cloneNode(true);
164 // Copy the events from the original to the clone
165 if ( events === true ) {
166 cloneCopyEvent( this, ret );
167 cloneCopyEvent( this.find("*"), ret.find("*") );
170 // Return the cloned set
174 html: function( value ) {
175 if ( value === undefined ) {
176 return this[0] && this[0].nodeType === 1 ?
177 this[0].innerHTML.replace(rinlinejQuery, "") :
180 // See if we can take a shortcut and just use innerHTML
181 } else if ( typeof value === "string" && !/<script/i.test( value ) &&
182 (jQuery.support.leadingWhitespace || !rleadingWhitespace.test( value )) &&
183 !wrapMap[ (rtagName.exec( value ) || ["", ""])[1].toLowerCase() ] ) {
186 for ( var i = 0, l = this.length; i < l; i++ ) {
187 // Remove element nodes and prevent memory leaks
188 if ( this[i].nodeType === 1 ) {
189 cleanData( this[i].getElementsByTagName("*") );
190 this[i].innerHTML = value;
194 // If using innerHTML throws an exception, use the fallback method
196 this.empty().append( value );
199 } else if ( jQuery.isFunction( value ) ) {
200 this.each(function(i){
201 var self = jQuery(this), old = self.html();
202 self.empty().append(function(){
203 return value.call( this, i, old );
208 this.empty().append( value );
214 replaceWith: function( value ) {
215 if ( this[0] && this[0].parentNode ) {
216 return this.each(function() {
217 var next = this.nextSibling, parent = this.parentNode;
219 jQuery(this).remove();
222 jQuery(next).before( value );
224 jQuery(parent).append( value );
228 return this.pushStack( jQuery(jQuery.isFunction(value) ? value() : value), "replaceWith", value );
232 detach: function( selector ) {
233 return this.remove( selector, true );
236 domManip: function( args, table, callback ) {
237 var results, first, value = args[0], scripts = [];
239 if ( jQuery.isFunction(value) ) {
240 return this.each(function(i) {
241 var self = jQuery(this);
242 args[0] = value.call(this, i, table ? self.html() : undefined);
243 return self.domManip( args, table, callback );
248 // If we're in a fragment, just use that instead of building a new one
249 if ( args[0] && args[0].parentNode && args[0].parentNode.nodeType === 11 ) {
250 results = { fragment: args[0].parentNode };
252 results = buildFragment( args, this, scripts );
255 first = results.fragment.firstChild;
258 table = table && jQuery.nodeName( first, "tr" );
260 for ( var i = 0, l = this.length; i < l; i++ ) {
263 root(this[i], first) :
265 results.cacheable || this.length > 1 || i > 0 ?
266 results.fragment.cloneNode(true) :
273 jQuery.each( scripts, evalScript );
279 function root( elem, cur ) {
280 return jQuery.nodeName(elem, "table") ?
281 (elem.getElementsByTagName("tbody")[0] ||
282 elem.appendChild(elem.ownerDocument.createElement("tbody"))) :
288 function cloneCopyEvent(orig, ret) {
291 ret.each(function() {
292 if ( this.nodeName !== (orig[i] && orig[i].nodeName) ) {
296 var oldData = jQuery.data( orig[i++] ), curData = jQuery.data( this, oldData ), events = oldData && oldData.events;
299 delete curData.handle;
302 for ( var type in events ) {
303 for ( var handler in events[ type ] ) {
304 jQuery.event.add( this, type, events[ type ][ handler ], events[ type ][ handler ].data );
311 function buildFragment( args, nodes, scripts ) {
312 var fragment, cacheable, cached, cacheresults, doc;
314 if ( args.length === 1 && typeof args[0] === "string" && args[0].length < 512 && args[0].indexOf("<option") < 0 ) {
316 cacheresults = jQuery.fragments[ args[0] ];
317 if ( cacheresults ) {
318 if ( cacheresults !== 1 ) {
319 fragment = cacheresults;
326 doc = (nodes && nodes[0] ? nodes[0].ownerDocument || nodes[0] : document);
327 fragment = doc.createDocumentFragment();
328 jQuery.clean( args, doc, fragment, scripts );
332 jQuery.fragments[ args[0] ] = cacheresults ? fragment : 1;
335 return { fragment: fragment, cacheable: cacheable };
338 jQuery.fragments = {};
342 prependTo: "prepend",
343 insertBefore: "before",
344 insertAfter: "after",
345 replaceAll: "replaceWith"
346 }, function( name, original ) {
347 jQuery.fn[ name ] = function( selector ) {
348 var ret = [], insert = jQuery( selector );
350 for ( var i = 0, l = insert.length; i < l; i++ ) {
351 var elems = (i > 0 ? this.clone(true) : this).get();
352 jQuery.fn[ original ].apply( jQuery(insert[i]), elems );
353 ret = ret.concat( elems );
355 return this.pushStack( ret, name, insert.selector );
360 // keepData is for internal use only--do not document
361 remove: function( selector, keepData ) {
362 if ( !selector || jQuery.filter( selector, [ this ] ).length ) {
363 if ( !keepData && this.nodeType === 1 ) {
364 cleanData( this.getElementsByTagName("*") );
365 cleanData( [ this ] );
368 if ( this.parentNode ) {
369 this.parentNode.removeChild( this );
375 // Remove element nodes and prevent memory leaks
376 if ( this.nodeType === 1 ) {
377 cleanData( this.getElementsByTagName("*") );
380 // Remove any remaining nodes
381 while ( this.firstChild ) {
382 this.removeChild( this.firstChild );
385 }, function( name, fn ) {
386 jQuery.fn[ name ] = function() {
387 return this.each( fn, arguments );
392 clean: function( elems, context, fragment, scripts ) {
393 context = context || document;
395 // !context.createElement fails in IE with an error but returns typeof 'object'
396 if ( typeof context.createElement === "undefined" ) {
397 context = context.ownerDocument || context[0] && context[0].ownerDocument || document;
402 jQuery.each(elems, function( i, elem ) {
403 if ( typeof elem === "number" ) {
411 // Convert html string into DOM nodes
412 if ( typeof elem === "string" && !rhtml.test( elem ) ) {
413 elem = context.createTextNode( elem );
415 } else if ( typeof elem === "string" ) {
416 // Fix "XHTML"-style tags in all browsers
417 elem = elem.replace(rxhtmlTag, fcloseTag);
419 // Trim whitespace, otherwise indexOf won't work as expected
420 var tag = (rtagName.exec( elem ) || ["", ""])[1].toLowerCase(),
421 wrap = wrapMap[ tag ] || wrapMap._default,
423 div = context.createElement("div");
425 // Go to html and back, then peel off extra wrappers
426 div.innerHTML = wrap[1] + elem + wrap[2];
428 // Move to the right depth
433 // Remove IE's autoinserted <tbody> from table fragments
434 if ( !jQuery.support.tbody ) {
436 // String was a <table>, *may* have spurious <tbody>
437 var hasBody = rtbody.test(elem),
438 tbody = tag === "table" && !hasBody ?
439 div.firstChild && div.firstChild.childNodes :
441 // String was a bare <thead> or <tfoot>
442 wrap[1] === "<table>" && !hasBody ?
446 for ( var j = tbody.length - 1; j >= 0 ; --j ) {
447 if ( jQuery.nodeName( tbody[ j ], "tbody" ) && !tbody[ j ].childNodes.length ) {
448 tbody[ j ].parentNode.removeChild( tbody[ j ] );
454 // IE completely kills leading whitespace when innerHTML is used
455 if ( !jQuery.support.leadingWhitespace && rleadingWhitespace.test( elem ) ) {
456 div.insertBefore( context.createTextNode( rleadingWhitespace.exec(elem)[0] ), div.firstChild );
459 elem = jQuery.makeArray( div.childNodes );
462 if ( elem.nodeType ) {
465 ret = jQuery.merge( ret, elem );
471 for ( var i = 0; ret[i]; i++ ) {
472 if ( scripts && jQuery.nodeName( ret[i], "script" ) && (!ret[i].type || ret[i].type.toLowerCase() === "text/javascript") ) {
473 scripts.push( ret[i].parentNode ? ret[i].parentNode.removeChild( ret[i] ) : ret[i] );
475 if ( ret[i].nodeType === 1 ) {
476 ret.splice.apply( ret, [i + 1, 0].concat(jQuery.makeArray(ret[i].getElementsByTagName("script"))) );
478 fragment.appendChild( ret[i] );
487 function cleanData( elems ) {
488 for ( var i = 0, elem, id; (elem = elems[i]) != null; i++ ) {
489 if ( !jQuery.noData[elem.nodeName.toLowerCase()] && (id = elem[expando]) ) {
490 delete jQuery.cache[ id ];