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,
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 ) );
39 jQuery.each( text || this, function(){
40 jQuery.each( this.childNodes, function(){
41 if ( this.nodeType !== 8 ) {
42 ret += this.nodeType !== 1 ?
44 jQuery.fn.text( [ this ] );
52 wrapAll: function( html ) {
53 if ( jQuery.isFunction( html ) ) {
54 return this.each(function() {
55 jQuery(this).wrapAll( html.apply(this, arguments) );
60 // The elements to wrap the target around
61 var wrap = jQuery( html, this[0].ownerDocument ).eq(0).clone();
63 if ( this[0].parentNode ) {
64 wrap.insertBefore( this[0] );
70 while ( elem.firstChild && elem.firstChild.nodeType === 1 ) {
71 elem = elem.firstChild;
81 wrapInner: function( html ) {
82 return this.each(function(){
83 jQuery( this ).contents().wrapAll( html );
87 wrap: function( html ) {
88 return this.each(function(){
89 jQuery( this ).wrapAll( html );
94 return this.domManip(arguments, true, function(elem){
95 if ( this.nodeType === 1 ) {
96 this.appendChild( elem );
101 prepend: function() {
102 return this.domManip(arguments, true, function(elem){
103 if ( this.nodeType === 1 ) {
104 this.insertBefore( elem, this.firstChild );
110 if ( this[0] && this[0].parentNode ) {
111 return this.domManip(arguments, false, function(elem){
112 this.parentNode.insertBefore( elem, this );
114 } else if ( arguments.length ) {
115 var set = jQuery(arguments[0]);
116 set.push.apply( set, this.toArray() );
117 return this.pushStack( set, "before", arguments );
122 if ( this[0] && this[0].parentNode ) {
123 return this.domManip(arguments, false, function(elem){
124 this.parentNode.insertBefore( elem, this.nextSibling );
126 } else if ( arguments.length ) {
127 var set = this.pushStack( this, "after", arguments );
128 set.push.apply( set, jQuery(arguments[0]).toArray() );
133 clone: function( events ) {
135 var ret = this.map(function(){
136 if ( !jQuery.support.noCloneEvent && !jQuery.isXMLDoc(this) ) {
137 // IE copies events bound via attachEvent when
138 // using cloneNode. Calling detachEvent on the
139 // clone will also remove the events from the orignal
140 // In order to get around this, we use innerHTML.
141 // Unfortunately, this means some modifications to
142 // attributes in IE that are actually only stored
143 // as properties will not be copied (such as the
144 // the name attribute on an input).
145 var html = this.outerHTML, ownerDocument = this.ownerDocument;
147 var div = ownerDocument.createElement("div");
148 div.appendChild( this.cloneNode(true) );
149 html = div.innerHTML;
152 return jQuery.clean([html.replace(rinlinejQuery, "")
153 .replace(rleadingWhitespace, "")], ownerDocument)[0];
155 return this.cloneNode(true);
159 // Copy the events from the original to the clone
160 if ( events === true ) {
161 var orig = this.find("*").andSelf(), i = 0;
163 ret.find("*").andSelf().each(function(){
164 if ( this.nodeName !== orig[i].nodeName ) { return; }
166 var events = jQuery.data( orig[i], "events" );
168 for ( var type in events ) {
169 for ( var handler in events[ type ] ) {
170 jQuery.event.add( this, type, events[ type ][ handler ], events[ type ][ handler ].data );
178 // Return the cloned set
182 html: function( value ) {
183 if ( value === undefined ) {
185 this[0].innerHTML.replace(rinlinejQuery, "") :
188 // See if we can take a shortcut and just use innerHTML
189 } else if ( typeof value === "string" && !/<script/i.test( value ) &&
190 (!jQuery.support.leadingWhitespace || !rleadingWhitespace.test( value )) &&
191 !wrapMap[ (rtagName.exec( value ) || ["", ""])[1].toLowerCase() ] ) {
194 for ( var i = 0, l = this.length; i < l; i++ ) {
195 // Remove element nodes and prevent memory leaks
196 if ( this[i].nodeType === 1 ) {
197 cleanData( this[i].getElementsByTagName("*") );
198 this[i].innerHTML = value;
202 // If using innerHTML throws an exception, use the fallback method
204 this.empty().append( value );
208 this.empty().append( value );
214 replaceWith: function( value ) {
215 if ( this[0] && this[0].parentNode ) {
216 return this.after( value ).remove();
218 return this.pushStack( jQuery(jQuery.isFunction(value) ? value() : value), "replaceWith", value );
222 detach: function( selector ) {
223 return this.remove( selector, true );
226 domManip: function( args, table, callback ) {
227 var results, first, value = args[0], scripts = [];
229 if ( jQuery.isFunction(value) ) {
230 return this.each(function() {
231 args[0] = value.call(this);
232 return jQuery(this).domManip( args, table, callback );
237 // If we're in a fragment, just use that instead of building a new one
238 if ( args[0] && args[0].parentNode && args[0].parentNode.nodeType === 11 ) {
239 results = { fragment: args[0].parentNode };
241 results = buildFragment( args, this, scripts );
244 first = results.fragment.firstChild;
247 table = table && jQuery.nodeName( first, "tr" );
249 for ( var i = 0, l = this.length; i < l; i++ ) {
252 root(this[i], first) :
254 results.cacheable || this.length > 1 || i > 0 ?
255 results.fragment.cloneNode(true) :
262 jQuery.each( scripts, evalScript );
268 function root( elem, cur ) {
269 return jQuery.nodeName(elem, "table") ?
270 (elem.getElementsByTagName("tbody")[0] ||
271 elem.appendChild(elem.ownerDocument.createElement("tbody"))) :
277 function buildFragment(args, nodes, scripts){
278 var fragment, cacheable, cached, cacheresults, doc;
280 if ( args.length === 1 && typeof args[0] === "string" && args[0].length < 512 && args[0].indexOf("<option") < 0 ) {
282 cacheresults = jQuery.fragments[ args[0] ];
283 if ( cacheresults ) {
284 if ( cacheresults !== 1 ) {
285 fragment = cacheresults;
292 doc = (nodes && nodes[0] ? nodes[0].ownerDocument || nodes[0] : document);
293 fragment = doc.createDocumentFragment();
294 jQuery.clean( args, doc, fragment, scripts );
298 jQuery.fragments[ args[0] ] = cacheresults ? fragment : 1;
301 return { fragment: fragment, cacheable: cacheable };
304 jQuery.fragments = {};
308 prependTo: "prepend",
309 insertBefore: "before",
310 insertAfter: "after",
311 replaceAll: "replaceWith"
312 }, function(name, original){
313 jQuery.fn[ name ] = function( selector ) {
314 var ret = [], insert = jQuery( selector );
316 for ( var i = 0, l = insert.length; i < l; i++ ) {
317 var elems = (i > 0 ? this.clone(true) : this).get();
318 jQuery.fn[ original ].apply( jQuery(insert[i]), elems );
319 ret = ret.concat( elems );
321 return this.pushStack( ret, name, insert.selector );
326 // keepData is for internal use only--do not document
327 remove: function( selector, keepData ) {
328 if ( !selector || jQuery.multiFilter( selector, [ this ] ).length ) {
329 if ( !keepData && this.nodeType === 1 ) {
330 cleanData( this.getElementsByTagName("*") );
331 cleanData( [ this ] );
334 if ( this.parentNode ) {
335 this.parentNode.removeChild( this );
341 // Remove element nodes and prevent memory leaks
342 if ( this.nodeType === 1 ) {
343 cleanData( this.getElementsByTagName("*") );
346 // Remove any remaining nodes
347 while ( this.firstChild ) {
348 this.removeChild( this.firstChild );
351 }, function(name, fn){
352 jQuery.fn[ name ] = function(){
353 return this.each( fn, arguments );
358 clean: function( elems, context, fragment, scripts ) {
359 context = context || document;
361 // !context.createElement fails in IE with an error but returns typeof 'object'
362 if ( typeof context.createElement === "undefined" ) {
363 context = context.ownerDocument || context[0] && context[0].ownerDocument || document;
366 var ret = [], div = context.createElement("div");
368 jQuery.each(elems, function(i, elem){
369 if ( typeof elem === "number" ) {
373 if ( !elem ) { return; }
375 // Convert html string into DOM nodes
376 if ( typeof elem === "string" && !rhtml.test( elem ) ) {
377 elem = context.createTextNode( elem );
379 } else if ( typeof elem === "string" ) {
380 // Fix "XHTML"-style tags in all browsers
381 elem = elem.replace(rxhtmlTag, fcloseTag);
383 // Trim whitespace, otherwise indexOf won't work as expected
384 var tag = (rtagName.exec( elem ) || ["", ""])[1].toLowerCase(),
385 wrap = wrapMap[ tag ] || wrapMap._default,
388 // Go to html and back, then peel off extra wrappers
389 div.innerHTML = wrap[1] + elem + wrap[2];
391 // Move to the right depth
396 // Remove IE's autoinserted <tbody> from table fragments
397 if ( !jQuery.support.tbody ) {
399 // String was a <table>, *may* have spurious <tbody>
400 var hasBody = rtbody.test(elem),
401 tbody = tag === "table" && !hasBody ?
402 div.firstChild && div.firstChild.childNodes :
404 // String was a bare <thead> or <tfoot>
405 wrap[1] == "<table>" && !hasBody ?
409 for ( var j = tbody.length - 1; j >= 0 ; --j ) {
410 if ( jQuery.nodeName( tbody[ j ], "tbody" ) && !tbody[ j ].childNodes.length ) {
411 tbody[ j ].parentNode.removeChild( tbody[ j ] );
417 // IE completely kills leading whitespace when innerHTML is used
418 if ( !jQuery.support.leadingWhitespace && rleadingWhitespace.test( elem ) ) {
419 div.insertBefore( context.createTextNode( rleadingWhitespace.exec(elem)[0] ), div.firstChild );
422 elem = jQuery.makeArray( div.childNodes );
425 if ( elem.nodeType ) {
428 ret = jQuery.merge( ret, elem );
434 for ( var i = 0; ret[i]; i++ ) {
435 if ( scripts && jQuery.nodeName( ret[i], "script" ) && (!ret[i].type || ret[i].type.toLowerCase() === "text/javascript") ) {
436 scripts.push( ret[i].parentNode ? ret[i].parentNode.removeChild( ret[i] ) : ret[i] );
438 if ( ret[i].nodeType === 1 ) {
439 ret.splice.apply( ret, [i + 1, 0].concat(jQuery.makeArray(ret[i].getElementsByTagName("script"))) );
441 fragment.appendChild( ret[i] );
450 function cleanData( elems ) {
451 for ( var i = 0, elem, id; (elem = elems[i]) != null; i++ ) {
452 if ( (id = elem[expando]) ) {
453 delete jQuery.cache[ id ];