3 var rinlinejQuery = / jQuery\d+="(?:\d+|null)"/g,
4 rleadingWhitespace = /^\s+/,
5 rxhtmlTag = /<(?!area|br|col|embed|hr|img|input|link|meta|param)(([\w:]+)[^>]*)\/>/ig,
6 rtagName = /<([\w:]+)/,
9 rnocache = /<(?:script|object|embed|option|style)/i,
10 // checked="checked" or checked (html5)
11 rchecked = /checked\s*(?:[^=]|=\s*.checked.)/i,
13 option: [ 1, "<select multiple='multiple'>", "</select>" ],
14 legend: [ 1, "<fieldset>", "</fieldset>" ],
15 thead: [ 1, "<table>", "</table>" ],
16 tr: [ 2, "<table><tbody>", "</tbody></table>" ],
17 td: [ 3, "<table><tbody><tr>", "</tr></tbody></table>" ],
18 col: [ 2, "<table><tbody></tbody><colgroup>", "</colgroup></table>" ],
19 area: [ 1, "<map>", "</map>" ],
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 ( jQuery.isFunction(text) ) {
35 return this.each(function(i) {
36 var self = jQuery( this );
38 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.text( 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 if ( jQuery.isFunction( html ) ) {
80 return this.each(function(i) {
81 jQuery(this).wrapInner( html.call(this, i) );
85 return this.each(function() {
86 var self = jQuery( this ),
87 contents = self.contents();
89 if ( contents.length ) {
90 contents.wrapAll( html );
98 wrap: function( html ) {
99 return this.each(function() {
100 jQuery( this ).wrapAll( html );
105 return this.parent().each(function() {
106 if ( !jQuery.nodeName( this, "body" ) ) {
107 jQuery( this ).replaceWith( this.childNodes );
113 return this.domManip(arguments, true, function( elem ) {
114 if ( this.nodeType === 1 ) {
115 this.appendChild( elem );
120 prepend: function() {
121 return this.domManip(arguments, true, function( elem ) {
122 if ( this.nodeType === 1 ) {
123 this.insertBefore( elem, this.firstChild );
129 if ( this[0] && this[0].parentNode ) {
130 return this.domManip(arguments, false, function( elem ) {
131 this.parentNode.insertBefore( elem, this );
133 } else if ( arguments.length ) {
134 var set = jQuery(arguments[0]);
135 set.push.apply( set, this.toArray() );
136 return this.pushStack( set, "before", arguments );
141 if ( this[0] && this[0].parentNode ) {
142 return this.domManip(arguments, false, function( elem ) {
143 this.parentNode.insertBefore( elem, this.nextSibling );
145 } else if ( arguments.length ) {
146 var set = this.pushStack( this, "after", arguments );
147 set.push.apply( set, jQuery(arguments[0]).toArray() );
152 // keepData is for internal use only--do not document
153 remove: function( selector, keepData ) {
154 for ( var i = 0, elem; (elem = this[i]) != null; i++ ) {
155 if ( !selector || jQuery.filter( selector, [ elem ] ).length ) {
156 if ( !keepData && elem.nodeType === 1 ) {
157 jQuery.cleanData( elem.getElementsByTagName("*") );
158 jQuery.cleanData( [ elem ] );
161 if ( elem.parentNode ) {
162 elem.parentNode.removeChild( elem );
171 for ( var i = 0, elem; (elem = this[i]) != null; i++ ) {
172 // Remove element nodes and prevent memory leaks
173 if ( elem.nodeType === 1 ) {
174 jQuery.cleanData( elem.getElementsByTagName("*") );
177 // Remove any remaining nodes
178 while ( elem.firstChild ) {
179 elem.removeChild( elem.firstChild );
186 clone: function( events ) {
188 var ret = this.map(function() {
189 var clone = this.cloneNode(true);
190 if ( !jQuery.support.noCloneEvent && (this.nodeType === 1 || this.nodeType === 11) && !jQuery.isXMLDoc(this) ) {
191 // IE copies events bound via attachEvent when using cloneNode.
192 // Calling detachEvent on the clone will also remove the events
193 // from the original. In order to get around this, we use some
194 // proprietary methods to clear the events. Thanks to MooTools
195 // guys for this hotness.
196 var srcElements = jQuery(this).find('*').andSelf();
197 jQuery(clone).find('*').andSelf().each(function (i, clone) {
198 // We do not need to do anything for non-Elements
199 if (this.nodeType !== 1) {
203 // clearAttributes removes the attributes, but also
204 // removes the attachEvent events
205 clone.clearAttributes();
207 // mergeAttributes only merges back on the original attributes,
209 clone.mergeAttributes(srcElements[i]);
211 // IE6-8 fail to clone children inside object elements that use
212 // the proprietary classid attribute value (rather than the type
213 // attribute) to identify the type of content to display
214 if (clone.nodeName.toLowerCase() === 'object') {
215 clone.outerHTML = srcElements[i].outerHTML;
223 // Copy the events from the original to the clone
224 if ( events === true ) {
225 cloneCopyEvent( this, ret );
226 cloneCopyEvent( this.find("*"), ret.find("*") );
229 // Return the cloned set
233 html: function( value ) {
234 if ( value === undefined ) {
235 return this[0] && this[0].nodeType === 1 ?
236 this[0].innerHTML.replace(rinlinejQuery, "") :
239 // See if we can take a shortcut and just use innerHTML
240 } else if ( typeof value === "string" && !rnocache.test( value ) &&
241 (jQuery.support.leadingWhitespace || !rleadingWhitespace.test( value )) &&
242 !wrapMap[ (rtagName.exec( value ) || ["", ""])[1].toLowerCase() ] ) {
244 value = value.replace(rxhtmlTag, "<$1></$2>");
247 for ( var i = 0, l = this.length; i < l; i++ ) {
248 // Remove element nodes and prevent memory leaks
249 if ( this[i].nodeType === 1 ) {
250 jQuery.cleanData( this[i].getElementsByTagName("*") );
251 this[i].innerHTML = value;
255 // If using innerHTML throws an exception, use the fallback method
257 this.empty().append( value );
260 } else if ( jQuery.isFunction( value ) ) {
261 this.each(function(i){
262 var self = jQuery( this );
264 self.html( value.call(this, i, self.html()) );
268 this.empty().append( value );
274 replaceWith: function( value ) {
275 if ( this[0] && this[0].parentNode ) {
276 // Make sure that the elements are removed from the DOM before they are inserted
277 // this can help fix replacing a parent with child elements
278 if ( jQuery.isFunction( value ) ) {
279 return this.each(function(i) {
280 var self = jQuery(this), old = self.html();
281 self.replaceWith( value.call( this, i, old ) );
285 if ( typeof value !== "string" ) {
286 value = jQuery( value ).detach();
289 return this.each(function() {
290 var next = this.nextSibling,
291 parent = this.parentNode;
293 jQuery( this ).remove();
296 jQuery(next).before( value );
298 jQuery(parent).append( value );
302 return this.pushStack( jQuery(jQuery.isFunction(value) ? value() : value), "replaceWith", value );
306 detach: function( selector ) {
307 return this.remove( selector, true );
310 domManip: function( args, table, callback ) {
311 var results, first, fragment, parent,
315 // We can't cloneNode fragments that contain checked, in WebKit
316 if ( !jQuery.support.checkClone && arguments.length === 3 && typeof value === "string" && rchecked.test( value ) ) {
317 return this.each(function() {
318 jQuery(this).domManip( args, table, callback, true );
322 if ( jQuery.isFunction(value) ) {
323 return this.each(function(i) {
324 var self = jQuery(this);
325 args[0] = value.call(this, i, table ? self.html() : undefined);
326 self.domManip( args, table, callback );
331 parent = value && value.parentNode;
333 // If we're in a fragment, just use that instead of building a new one
334 if ( jQuery.support.parentNode && parent && parent.nodeType === 11 && parent.childNodes.length === this.length ) {
335 results = { fragment: parent };
338 results = jQuery.buildFragment( args, this, scripts );
341 fragment = results.fragment;
343 if ( fragment.childNodes.length === 1 ) {
344 first = fragment = fragment.firstChild;
346 first = fragment.firstChild;
350 table = table && jQuery.nodeName( first, "tr" );
352 for ( var i = 0, l = this.length; i < l; i++ ) {
355 root(this[i], first) :
357 i > 0 || results.cacheable || this.length > 1 ?
358 fragment.cloneNode(true) :
364 if ( scripts.length ) {
365 jQuery.each( scripts, evalScript );
373 function root( elem, cur ) {
374 return jQuery.nodeName(elem, "table") ?
375 (elem.getElementsByTagName("tbody")[0] ||
376 elem.appendChild(elem.ownerDocument.createElement("tbody"))) :
380 function cloneCopyEvent(orig, ret) {
383 ret.each(function() {
384 if ( this.nodeType !== 1 || this.nodeName !== (orig[i] && orig[i].nodeName) ) {
388 var oldData = jQuery.data( orig[i++] ),
389 curData = jQuery.data( this, oldData ),
390 events = oldData && oldData.events;
393 delete curData.handle;
396 for ( var type in events ) {
397 for ( var handler in events[ type ] ) {
398 jQuery.event.add( this, type, events[ type ][ handler ], events[ type ][ handler ].data );
405 jQuery.buildFragment = function( args, nodes, scripts ) {
406 var fragment, cacheable, cacheresults,
407 doc = (nodes && nodes[0] ? nodes[0].ownerDocument || nodes[0] : document);
409 // Only cache "small" (1/2 KB) strings that are associated with the main document
410 // Cloning options loses the selected state, so don't cache them
411 // IE 6 doesn't like it when you put <object> or <embed> elements in a fragment
412 // Also, WebKit does not clone 'checked' attributes on cloneNode, so don't cache
413 if ( args.length === 1 && typeof args[0] === "string" && args[0].length < 512 && doc === document &&
414 !rnocache.test( args[0] ) && (jQuery.support.checkClone || !rchecked.test( args[0] )) ) {
417 cacheresults = jQuery.fragments[ args[0] ];
418 if ( cacheresults ) {
419 if ( cacheresults !== 1 ) {
420 fragment = cacheresults;
426 fragment = doc.createDocumentFragment();
427 jQuery.clean( args, doc, fragment, scripts );
431 jQuery.fragments[ args[0] ] = cacheresults ? fragment : 1;
434 return { fragment: fragment, cacheable: cacheable };
437 jQuery.fragments = {};
441 prependTo: "prepend",
442 insertBefore: "before",
443 insertAfter: "after",
444 replaceAll: "replaceWith"
445 }, function( name, original ) {
446 jQuery.fn[ name ] = function( selector ) {
448 insert = jQuery( selector ),
449 parent = this.length === 1 && this[0].parentNode;
451 if ( parent && parent.nodeType === 11 && parent.childNodes.length === 1 && insert.length === 1 ) {
452 insert[ original ]( this[0] );
456 for ( var i = 0, l = insert.length; i < l; i++ ) {
457 var elems = (i > 0 ? this.clone(true) : this).get();
458 jQuery( insert[i] )[ original ]( elems );
459 ret = ret.concat( elems );
462 return this.pushStack( ret, name, insert.selector );
468 clean: function( elems, context, fragment, scripts ) {
469 context = context || document;
471 // !context.createElement fails in IE with an error but returns typeof 'object'
472 if ( typeof context.createElement === "undefined" ) {
473 context = context.ownerDocument || context[0] && context[0].ownerDocument || document;
478 for ( var i = 0, elem; (elem = elems[i]) != null; i++ ) {
479 if ( typeof elem === "number" ) {
487 // Convert html string into DOM nodes
488 if ( typeof elem === "string" && !rhtml.test( elem ) ) {
489 elem = context.createTextNode( elem );
491 } else if ( typeof elem === "string" ) {
492 // Fix "XHTML"-style tags in all browsers
493 elem = elem.replace(rxhtmlTag, "<$1></$2>");
495 // Trim whitespace, otherwise indexOf won't work as expected
496 var tag = (rtagName.exec( elem ) || ["", ""])[1].toLowerCase(),
497 wrap = wrapMap[ tag ] || wrapMap._default,
499 div = context.createElement("div");
501 // Go to html and back, then peel off extra wrappers
502 div.innerHTML = wrap[1] + elem + wrap[2];
504 // Move to the right depth
509 // Remove IE's autoinserted <tbody> from table fragments
510 if ( !jQuery.support.tbody ) {
512 // String was a <table>, *may* have spurious <tbody>
513 var hasBody = rtbody.test(elem),
514 tbody = tag === "table" && !hasBody ?
515 div.firstChild && div.firstChild.childNodes :
517 // String was a bare <thead> or <tfoot>
518 wrap[1] === "<table>" && !hasBody ?
522 for ( var j = tbody.length - 1; j >= 0 ; --j ) {
523 if ( jQuery.nodeName( tbody[ j ], "tbody" ) && !tbody[ j ].childNodes.length ) {
524 tbody[ j ].parentNode.removeChild( tbody[ j ] );
530 // IE completely kills leading whitespace when innerHTML is used
531 if ( !jQuery.support.leadingWhitespace && rleadingWhitespace.test( elem ) ) {
532 div.insertBefore( context.createTextNode( rleadingWhitespace.exec(elem)[0] ), div.firstChild );
535 elem = div.childNodes;
538 if ( elem.nodeType ) {
541 ret = jQuery.merge( ret, elem );
546 for ( i = 0; ret[i]; i++ ) {
547 if ( scripts && jQuery.nodeName( ret[i], "script" ) && (!ret[i].type || ret[i].type.toLowerCase() === "text/javascript") ) {
548 scripts.push( ret[i].parentNode ? ret[i].parentNode.removeChild( ret[i] ) : ret[i] );
551 if ( ret[i].nodeType === 1 ) {
552 ret.splice.apply( ret, [i + 1, 0].concat(jQuery.makeArray(ret[i].getElementsByTagName("script"))) );
554 fragment.appendChild( ret[i] );
562 cleanData: function( elems ) {
563 var data, id, cache = jQuery.cache,
564 special = jQuery.event.special,
565 deleteExpando = jQuery.support.deleteExpando;
567 for ( var i = 0, elem; (elem = elems[i]) != null; i++ ) {
568 if ( elem.nodeName && jQuery.noData[elem.nodeName.toLowerCase()] ) {
572 id = elem[ jQuery.expando ];
577 if ( data && data.events ) {
578 for ( var type in data.events ) {
579 if ( special[ type ] ) {
580 jQuery.event.remove( elem, type );
583 jQuery.removeEvent( elem, type, data.handle );
588 if ( deleteExpando ) {
589 delete elem[ jQuery.expando ];
591 } else if ( elem.removeAttribute ) {
592 elem.removeAttribute( jQuery.expando );
601 function evalScript( i, elem ) {
609 jQuery.globalEval( elem.text || elem.textContent || elem.innerHTML || "" );
612 if ( elem.parentNode ) {
613 elem.parentNode.removeChild( elem );