3 var rinlinejQuery = / jQuery\d+="(?:\d+|null)"/g,
4 rleadingWhitespace = /^\s+/,
5 rxhtmlTag = /(<([\w:]+)[^>]*?)\/>/g,
6 rselfClosing = /^(?:area|br|col|embed|hr|img|input|link|meta|param)$/i,
7 rtagName = /<([\w:]+)/,
10 rnocache = /<script|<object|<embed|<option|<style/i,
11 rchecked = /checked\s*(?:[^=]|=\s*.checked.)/i, // checked="checked" or checked (html5)
12 fcloseTag = function( all, front, tag ) {
13 return rselfClosing.test( tag ) ?
15 front + "></" + tag + ">";
18 option: [ 1, "<select multiple='multiple'>", "</select>" ],
19 legend: [ 1, "<fieldset>", "</fieldset>" ],
20 thead: [ 1, "<table>", "</table>" ],
21 tr: [ 2, "<table><tbody>", "</tbody></table>" ],
22 td: [ 3, "<table><tbody><tr>", "</tr></tbody></table>" ],
23 col: [ 2, "<table><tbody></tbody><colgroup>", "</colgroup></table>" ],
24 area: [ 1, "<map>", "</map>" ],
25 _default: [ 0, "", "" ]
28 wrapMap.optgroup = wrapMap.option;
29 wrapMap.tbody = wrapMap.tfoot = wrapMap.colgroup = wrapMap.caption = wrapMap.thead;
30 wrapMap.th = wrapMap.td;
32 // IE can't serialize <link> and <script> tags normally
33 if ( !jQuery.support.htmlSerialize ) {
34 wrapMap._default = [ 1, "div<div>", "</div>" ];
38 text: function( text ) {
39 if ( jQuery.isFunction(text) ) {
40 return this.each(function(i) {
41 var self = jQuery(this);
42 self.text( text.call(this, i, self.text()) );
46 if ( typeof text !== "object" && text !== undefined ) {
47 return this.empty().append( (this[0] && this[0].ownerDocument || document).createTextNode( text ) );
50 return jQuery.text( this );
53 wrapAll: function( html ) {
54 if ( jQuery.isFunction( html ) ) {
55 return this.each(function(i) {
56 jQuery(this).wrapAll( html.call(this, i) );
61 // The elements to wrap the target around
62 var wrap = jQuery( html, this[0].ownerDocument ).eq(0).clone(true);
64 if ( this[0].parentNode ) {
65 wrap.insertBefore( this[0] );
71 while ( elem.firstChild && elem.firstChild.nodeType === 1 ) {
72 elem = elem.firstChild;
82 wrapInner: function( html ) {
83 if ( jQuery.isFunction( html ) ) {
84 return this.each(function(i) {
85 jQuery(this).wrapInner( html.call(this, i) );
89 return this.each(function() {
90 var self = jQuery( this ), contents = self.contents();
92 if ( contents.length ) {
93 contents.wrapAll( html );
101 wrap: function( html ) {
102 return this.each(function() {
103 jQuery( this ).wrapAll( html );
108 return this.parent().each(function() {
109 if ( !jQuery.nodeName( this, "body" ) ) {
110 jQuery( this ).replaceWith( this.childNodes );
116 return this.domManip(arguments, true, function( elem ) {
117 if ( this.nodeType === 1 ) {
118 this.appendChild( elem );
123 prepend: function() {
124 return this.domManip(arguments, true, function( elem ) {
125 if ( this.nodeType === 1 ) {
126 this.insertBefore( elem, this.firstChild );
132 if ( this[0] && this[0].parentNode ) {
133 return this.domManip(arguments, false, function( elem ) {
134 this.parentNode.insertBefore( elem, this );
136 } else if ( arguments.length ) {
137 var set = jQuery(arguments[0]);
138 set.push.apply( set, this.toArray() );
139 return this.pushStack( set, "before", arguments );
144 if ( this[0] && this[0].parentNode ) {
145 return this.domManip(arguments, false, function( elem ) {
146 this.parentNode.insertBefore( elem, this.nextSibling );
148 } else if ( arguments.length ) {
149 var set = this.pushStack( this, "after", arguments );
150 set.push.apply( set, jQuery(arguments[0]).toArray() );
155 // keepData is for internal use only--do not document
156 remove: function( selector, keepData ) {
157 for ( var i = 0, elem; (elem = this[i]) != null; i++ ) {
158 if ( !selector || jQuery.filter( selector, [ elem ] ).length ) {
159 if ( !keepData && elem.nodeType === 1 ) {
160 jQuery.cleanData( elem.getElementsByTagName("*") );
161 jQuery.cleanData( [ elem ] );
164 if ( elem.parentNode ) {
165 elem.parentNode.removeChild( elem );
174 for ( var i = 0, elem; (elem = this[i]) != null; i++ ) {
175 // Remove element nodes and prevent memory leaks
176 if ( elem.nodeType === 1 ) {
177 jQuery.cleanData( elem.getElementsByTagName("*") );
180 // Remove any remaining nodes
181 while ( elem.firstChild ) {
182 elem.removeChild( elem.firstChild );
189 clone: function( events ) {
191 var ret = this.map(function() {
192 if ( !jQuery.support.noCloneEvent && !jQuery.isXMLDoc(this) ) {
193 // IE copies events bound via attachEvent when
194 // using cloneNode. Calling detachEvent on the
195 // clone will also remove the events from the orignal
196 // In order to get around this, we use innerHTML.
197 // Unfortunately, this means some modifications to
198 // attributes in IE that are actually only stored
199 // as properties will not be copied (such as the
200 // the name attribute on an input).
201 var html = this.outerHTML, ownerDocument = this.ownerDocument;
203 var div = ownerDocument.createElement("div");
204 div.appendChild( this.cloneNode(true) );
205 html = div.innerHTML;
208 return jQuery.clean([html.replace(rinlinejQuery, "")
209 // Handle the case in IE 8 where action=/test/> self-closes a tag
210 .replace(/\=([^="'>\s]+\/)>/g, '="$1">')
211 .replace(rleadingWhitespace, "")], ownerDocument)[0];
213 return this.cloneNode(true);
217 // Copy the events from the original to the clone
218 if ( events === true ) {
219 cloneCopyEvent( this, ret );
220 cloneCopyEvent( this.find("*"), ret.find("*") );
223 // Return the cloned set
227 html: function( value ) {
228 if ( value === undefined ) {
229 return this[0] && this[0].nodeType === 1 ?
230 this[0].innerHTML.replace(rinlinejQuery, "") :
233 // See if we can take a shortcut and just use innerHTML
234 } else if ( typeof value === "string" && !rnocache.test( value ) &&
235 (jQuery.support.leadingWhitespace || !rleadingWhitespace.test( value )) &&
236 !wrapMap[ (rtagName.exec( value ) || ["", ""])[1].toLowerCase() ] ) {
238 value = value.replace(rxhtmlTag, fcloseTag);
241 for ( var i = 0, l = this.length; i < l; i++ ) {
242 // Remove element nodes and prevent memory leaks
243 if ( this[i].nodeType === 1 ) {
244 jQuery.cleanData( this[i].getElementsByTagName("*") );
245 this[i].innerHTML = value;
249 // If using innerHTML throws an exception, use the fallback method
251 this.empty().append( value );
254 } else if ( jQuery.isFunction( value ) ) {
255 this.each(function(i){
256 var self = jQuery(this), old = self.html();
257 self.empty().append(function(){
258 return value.call( this, i, old );
263 this.empty().append( value );
269 replaceWith: function( value ) {
270 if ( this[0] && this[0].parentNode ) {
271 // Make sure that the elements are removed from the DOM before they are inserted
272 // this can help fix replacing a parent with child elements
273 if ( jQuery.isFunction( value ) ) {
274 return this.each(function(i) {
275 var self = jQuery(this), old = self.html();
276 self.replaceWith( value.call( this, i, old ) );
280 if ( typeof value !== "string" ) {
281 value = jQuery(value).detach();
284 return this.each(function() {
285 var next = this.nextSibling, parent = this.parentNode;
287 jQuery(this).remove();
290 jQuery(next).before( value );
292 jQuery(parent).append( value );
296 return this.pushStack( jQuery(jQuery.isFunction(value) ? value() : value), "replaceWith", value );
300 detach: function( selector ) {
301 return this.remove( selector, true );
304 domManip: function( args, table, callback ) {
305 var results, first, value = args[0], scripts = [], fragment, parent;
307 // We can't cloneNode fragments that contain checked, in WebKit
308 if ( !jQuery.support.checkClone && arguments.length === 3 && typeof value === "string" && rchecked.test( value ) ) {
309 return this.each(function() {
310 jQuery(this).domManip( args, table, callback, true );
314 if ( jQuery.isFunction(value) ) {
315 return this.each(function(i) {
316 var self = jQuery(this);
317 args[0] = value.call(this, i, table ? self.html() : undefined);
318 self.domManip( args, table, callback );
323 parent = value && value.parentNode;
325 // If we're in a fragment, just use that instead of building a new one
326 if ( jQuery.support.parentNode && parent && parent.nodeType === 11 && parent.childNodes.length === this.length ) {
327 results = { fragment: parent };
330 results = jQuery.buildFragment( args, this, scripts );
333 fragment = results.fragment;
335 if ( fragment.childNodes.length === 1 ) {
336 first = fragment = fragment.firstChild;
338 first = fragment.firstChild;
342 table = table && jQuery.nodeName( first, "tr" );
344 for ( var i = 0, l = this.length; i < l; i++ ) {
347 root(this[i], first) :
349 i > 0 || results.cacheable || this.length > 1 ?
350 fragment.cloneNode(true) :
356 if ( scripts.length ) {
357 jQuery.each( scripts, evalScript );
365 function root( elem, cur ) {
366 return jQuery.nodeName(elem, "table") ?
367 (elem.getElementsByTagName("tbody")[0] ||
368 elem.appendChild(elem.ownerDocument.createElement("tbody"))) :
372 function cloneCopyEvent(orig, ret) {
375 ret.each(function() {
376 if ( this.nodeName !== (orig[i] && orig[i].nodeName) ) {
380 var oldData = jQuery.data( orig[i++] ), curData = jQuery.data( this, oldData ), events = oldData && oldData.events;
383 delete curData.handle;
386 for ( var type in events ) {
387 for ( var handler in events[ type ] ) {
388 jQuery.event.add( this, type, events[ type ][ handler ], events[ type ][ handler ].data );
395 jQuery.buildFragment = function( args, nodes, scripts ) {
396 var fragment, cacheable, cacheresults,
397 doc = (nodes && nodes[0] ? nodes[0].ownerDocument || nodes[0] : document);
399 // Only cache "small" (1/2 KB) strings that are associated with the main document
400 // Cloning options loses the selected state, so don't cache them
401 // IE 6 doesn't like it when you put <object> or <embed> elements in a fragment
402 // Also, WebKit does not clone 'checked' attributes on cloneNode, so don't cache
403 if ( args.length === 1 && typeof args[0] === "string" && args[0].length < 512 && doc === document &&
404 !rnocache.test( args[0] ) && (jQuery.support.checkClone || !rchecked.test( args[0] )) ) {
407 cacheresults = jQuery.fragments[ args[0] ];
408 if ( cacheresults ) {
409 if ( cacheresults !== 1 ) {
410 fragment = cacheresults;
416 fragment = doc.createDocumentFragment();
417 jQuery.clean( args, doc, fragment, scripts );
421 jQuery.fragments[ args[0] ] = cacheresults ? fragment : 1;
424 return { fragment: fragment, cacheable: cacheable };
427 jQuery.fragments = {};
431 prependTo: "prepend",
432 insertBefore: "before",
433 insertAfter: "after",
434 replaceAll: "replaceWith"
435 }, function( name, original ) {
436 jQuery.fn[ name ] = function( selector ) {
437 var ret = [], insert = jQuery( selector ),
438 parent = this.length === 1 && this[0].parentNode;
440 if ( parent && parent.nodeType === 11 && parent.childNodes.length === 1 && insert.length === 1 ) {
441 insert[ original ]( this[0] );
445 for ( var i = 0, l = insert.length; i < l; i++ ) {
446 var elems = (i > 0 ? this.clone(true) : this).get();
447 jQuery( insert[i] )[ original ]( elems );
448 ret = ret.concat( elems );
451 return this.pushStack( ret, name, insert.selector );
457 clean: function( elems, context, fragment, scripts ) {
458 context = context || document;
460 // !context.createElement fails in IE with an error but returns typeof 'object'
461 if ( typeof context.createElement === "undefined" ) {
462 context = context.ownerDocument || context[0] && context[0].ownerDocument || document;
467 for ( var i = 0, elem; (elem = elems[i]) != null; i++ ) {
468 if ( typeof elem === "number" ) {
476 // Convert html string into DOM nodes
477 if ( typeof elem === "string" && !rhtml.test( elem ) ) {
478 elem = context.createTextNode( elem );
480 } else if ( typeof elem === "string" ) {
481 // Fix "XHTML"-style tags in all browsers
482 elem = elem.replace(rxhtmlTag, fcloseTag);
484 // Trim whitespace, otherwise indexOf won't work as expected
485 var tag = (rtagName.exec( elem ) || ["", ""])[1].toLowerCase(),
486 wrap = wrapMap[ tag ] || wrapMap._default,
488 div = context.createElement("div");
490 // Go to html and back, then peel off extra wrappers
491 div.innerHTML = wrap[1] + elem + wrap[2];
493 // Move to the right depth
498 // Remove IE's autoinserted <tbody> from table fragments
499 if ( !jQuery.support.tbody ) {
501 // String was a <table>, *may* have spurious <tbody>
502 var hasBody = rtbody.test(elem),
503 tbody = tag === "table" && !hasBody ?
504 div.firstChild && div.firstChild.childNodes :
506 // String was a bare <thead> or <tfoot>
507 wrap[1] === "<table>" && !hasBody ?
511 for ( var j = tbody.length - 1; j >= 0 ; --j ) {
512 if ( jQuery.nodeName( tbody[ j ], "tbody" ) && !tbody[ j ].childNodes.length ) {
513 tbody[ j ].parentNode.removeChild( tbody[ j ] );
519 // IE completely kills leading whitespace when innerHTML is used
520 if ( !jQuery.support.leadingWhitespace && rleadingWhitespace.test( elem ) ) {
521 div.insertBefore( context.createTextNode( rleadingWhitespace.exec(elem)[0] ), div.firstChild );
524 elem = div.childNodes;
527 if ( elem.nodeType ) {
530 ret = jQuery.merge( ret, elem );
535 for ( i = 0; ret[i]; i++ ) {
536 if ( scripts && jQuery.nodeName( ret[i], "script" ) && (!ret[i].type || ret[i].type.toLowerCase() === "text/javascript") ) {
537 scripts.push( ret[i].parentNode ? ret[i].parentNode.removeChild( ret[i] ) : ret[i] );
540 if ( ret[i].nodeType === 1 ) {
541 ret.splice.apply( ret, [i + 1, 0].concat(jQuery.makeArray(ret[i].getElementsByTagName("script"))) );
543 fragment.appendChild( ret[i] );
551 cleanData: function( elems ) {
552 var data, id, cache = jQuery.cache,
553 special = jQuery.event.special,
554 deleteExpando = jQuery.support.deleteExpando;
556 for ( var i = 0, elem; (elem = elems[i]) != null; i++ ) {
557 if ( elem.nodeName && jQuery.noData[elem.nodeName.toLowerCase()] ) {
561 id = elem[ jQuery.expando ];
566 if ( data && data.events ) {
567 for ( var type in data.events ) {
568 if ( special[ type ] ) {
569 jQuery.event.remove( elem, type );
572 jQuery.removeEvent( elem, type, data.handle );
577 if ( deleteExpando ) {
578 delete elem[ jQuery.expando ];
580 } else if ( elem.removeAttribute ) {
581 elem.removeAttribute( jQuery.expando );
590 function evalScript( i, elem ) {
598 jQuery.globalEval( elem.text || elem.textContent || elem.innerHTML || "" );
601 if ( elem.parentNode ) {
602 elem.parentNode.removeChild( elem );