Tweaked formatting of src/manipulation.js.
[jquery.git] / src / manipulation.js
1 jQuery.fn.extend({
2         text: function( text ) {
3                 if ( typeof text !== "object" && text !== undefined )
4                         return this.empty().append( (this[0] && this[0].ownerDocument || document).createTextNode( text ) );
5
6                 var ret = "";
7
8                 jQuery.each( text || this, function(){
9                         jQuery.each( this.childNodes, function(){
10                                 if ( this.nodeType !== 8 ) {
11                                         ret += this.nodeType !== 1 ?
12                                                 this.nodeValue :
13                                                 jQuery.fn.text( [ this ] );
14                                 }
15                         });
16                 });
17
18                 return ret;
19         },
20
21         wrapAll: function( html ) {
22                 if ( jQuery.isFunction( html ) ) {
23                         return this.each(function() {
24                                 jQuery(this).wrapAll( html.apply(this, arguments) );
25                         });
26                 }
27                 
28                 if ( this[0] ) {
29                         // The elements to wrap the target around
30                         var wrap = jQuery( html, this[0].ownerDocument ).eq(0).clone();
31
32                         if ( this[0].parentNode ) {
33                                 wrap.insertBefore( this[0] );
34                         }
35
36                         wrap.map(function(){
37                                 var elem = this;
38
39                                 while ( elem.firstChild && elem.firstChild.nodeType === 1 ) {
40                                         elem = elem.firstChild;
41                                 }
42
43                                 return elem;
44                         }).append(this);
45                 }
46
47                 return this;
48         },
49
50         wrapInner: function( html ) {
51                 return this.each(function(){
52                         jQuery( this ).contents().wrapAll( html );
53                 });
54         },
55
56         wrap: function( html ) {
57                 return this.each(function(){
58                         jQuery( this ).wrapAll( html );
59                 });
60         },
61
62         append: function() {
63                 return this.domManip(arguments, true, function(elem){
64                         if ( this.nodeType === 1 ) {
65                                 this.appendChild( elem );
66                         }
67                 });
68         },
69
70         prepend: function() {
71                 return this.domManip(arguments, true, function(elem){
72                         if ( this.nodeType === 1 ) {
73                                 this.insertBefore( elem, this.firstChild );
74                         }
75                 });
76         },
77
78         before: function() {
79                 return this.domManip(arguments, false, function(elem){
80                         this.parentNode.insertBefore( elem, this );
81                 });
82         },
83
84         after: function() {
85                 return this.domManip(arguments, false, function(elem){
86                         this.parentNode.insertBefore( elem, this.nextSibling );
87                 });
88         },
89
90         clone: function( events ) {
91                 // Do the clone
92                 var ret = this.map(function(){
93                         if ( !jQuery.support.noCloneEvent && !jQuery.isXMLDoc(this) ) {
94                                 // IE copies events bound via attachEvent when
95                                 // using cloneNode. Calling detachEvent on the
96                                 // clone will also remove the events from the orignal
97                                 // In order to get around this, we use innerHTML.
98                                 // Unfortunately, this means some modifications to
99                                 // attributes in IE that are actually only stored
100                                 // as properties will not be copied (such as the
101                                 // the name attribute on an input).
102                                 var html = this.outerHTML, ownerDocument = this.ownerDocument;
103                                 if ( !html ) {
104                                         var div = ownerDocument.createElement("div");
105                                         div.appendChild( this.cloneNode(true) );
106                                         html = div.innerHTML;
107                                 }
108
109                                 return jQuery.clean([html.replace(/ jQuery\d+="(?:\d+|null)"/g, "").replace(/^\s*/, "")], ownerDocument)[0];
110                         } else
111                                 return this.cloneNode(true);
112                 });
113
114                 // Copy the events from the original to the clone
115                 if ( events === true ) {
116                         var orig = this.find("*").andSelf(), i = 0;
117
118                         ret.find("*").andSelf().each(function(){
119                                 if ( this.nodeName !== orig[i].nodeName ) { return; }
120
121                                 var events = jQuery.data( orig[i], "events" );
122
123                                 for ( var type in events ) {
124                                         for ( var handler in events[ type ] ) {
125                                                 jQuery.event.add( this, type, events[ type ][ handler ], events[ type ][ handler ].data );
126                                         }
127                                 }
128
129                                 i++;
130                         });
131                 }
132
133                 // Return the cloned set
134                 return ret;
135         },
136
137         html: function( value ) {
138                 return value === undefined ?
139                         (this[0] ?
140                                 this[0].innerHTML.replace(/ jQuery\d+="(?:\d+|null)"/g, "") :
141                                 null) :
142                         this.empty().append( value );
143         },
144
145         replaceWith: function( value ) {
146                 return this.after( value ).remove();
147         },
148
149         domManip: function( args, table, callback ) {
150                 var fragment, scripts, cacheable, cached, cacheresults, first,
151                         value = args[0];
152
153                 if ( jQuery.isFunction(value) ) {
154                         return this.each(function() {
155                                 args[0] = value.call(this);
156                                 return jQuery(this).domManip( args, table, callback );
157                         });
158                 };
159
160                 if ( this[0] ) {
161                         if ( args.length === 1 && typeof args[0] === "string" && args[0].length < 512 && args[0].indexOf("<option") < 0 ) {
162                                 cacheable = true;
163                                 cacheresults = jQuery.fragments[ args[0] ];
164                                 if ( cacheresults ) {
165                                         if ( cacheresults !== 1 ) {
166                                                 fragment = cacheresults;
167                                         }
168                                         cached = true;
169                                 }
170                         }
171                         
172                         if ( !fragment ) {
173                                 fragment = (this[0].ownerDocument || this[0]).createDocumentFragment();
174                                 scripts = jQuery.clean( args, (this[0].ownerDocument || this[0]), fragment );
175                         }
176
177                         first = fragment.firstChild;
178
179                         if ( first ) {
180                                 table = table && jQuery.nodeName( first, "tr" );
181
182                                 for ( var i = 0, l = this.length; i < l; i++ ) {
183                                         callback.call(
184                                                 table ?
185                                                         root(this[i], first) :
186                                                         this[i],
187                                                 cacheable || this.length > 1 || i > 0 ?
188                                                         fragment.cloneNode(true) :
189                                                         fragment
190                                         );
191                                 }
192                         }
193
194                         if ( scripts ) {
195                                 jQuery.each( scripts, evalScript );
196                         }
197
198                         if ( cacheable ) {
199                                 jQuery.fragments[ args[0] ] = cacheresults ? fragment : 1;
200                         }
201                 }
202
203                 return this;
204
205                 function root( elem, cur ) {
206                         return jQuery.nodeName(elem, "table") ?
207                                 (elem.getElementsByTagName("tbody")[0] ||
208                                 elem.appendChild(elem.ownerDocument.createElement("tbody"))) :
209                                 elem;
210                 }
211         }
212 });
213
214 jQuery.fragments = {};
215
216 jQuery.each({
217         appendTo: "append",
218         prependTo: "prepend",
219         insertBefore: "before",
220         insertAfter: "after",
221         replaceAll: "replaceWith"
222 }, function(name, original){
223         jQuery.fn[ name ] = function( selector ) {
224                 var ret = [], insert = jQuery( selector );
225
226                 for ( var i = 0, l = insert.length; i < l; i++ ) {
227                         var elems = (i > 0 ? this.clone(true) : this).get();
228                         jQuery.fn[ original ].apply( jQuery(insert[i]), elems );
229                         ret = ret.concat( elems );
230                 }
231
232                 return this.pushStack( ret, name, selector );
233         };
234 });
235
236 jQuery.each({
237         remove: function( selector ) {
238                 if ( !selector || jQuery.multiFilter( selector, [ this ] ).length ) {
239                         if ( this.nodeType === 1 ) {
240                                 cleanData( jQuery("*", this).add(this) );
241                         }
242
243                         if ( this.parentNode ) {
244                                 this.parentNode.removeChild( this );
245                         }
246                 }
247         },
248
249         empty: function() {
250                 // Remove element nodes and prevent memory leaks
251                 if ( this.nodeType === 1 ) {
252                         cleanData( jQuery("*", this) );
253                 }
254
255                 // Remove any remaining nodes
256                 while ( this.firstChild ) {
257                         this.removeChild( this.firstChild );
258                 }
259         }
260 }, function(name, fn){
261         jQuery.fn[ name ] = function(){
262                 return this.each( fn, arguments );
263         };
264 });
265
266 jQuery.extend({
267         clean: function( elems, context, fragment ) {
268                 context = context || document;
269
270                 // !context.createElement fails in IE with an error but returns typeof 'object'
271                 if ( typeof context.createElement === "undefined" ) {
272                         context = context.ownerDocument || context[0] && context[0].ownerDocument || document;
273                 }
274
275                 // If a single string is passed in and it's a single tag
276                 // just do a createElement and skip the rest
277                 if ( !fragment && elems.length === 1 && typeof elems[0] === "string" ) {
278                         var match = /^<(\w+)\s*\/?>$/.exec(elems[0]);
279                         if ( match )
280                                 return [ context.createElement( match[1] ) ];
281                 }
282
283                 var ret = [], scripts = [], div = context.createElement("div");
284
285                 jQuery.each(elems, function(i, elem){
286                         if ( typeof elem === "number" ) {
287                                 elem += '';
288                         }
289
290                         if ( !elem ) { return; }
291
292                         // Convert html string into DOM nodes
293                         if ( typeof elem === "string" ) {
294                                 // Fix "XHTML"-style tags in all browsers
295                                 elem = elem.replace(/(<(\w+)[^>]*?)\/>/g, function(all, front, tag){
296                                         return /^(abbr|br|col|img|input|link|meta|param|hr|area|embed)$/i.test(tag) ?
297                                                 all :
298                                                 front + "></" + tag + ">";
299                                 });
300
301                                 // Trim whitespace, otherwise indexOf won't work as expected
302                                 var tags = elem.replace(/^\s+/, "").substring(0, 10).toLowerCase();
303
304                                 var wrap =
305                                         // option or optgroup
306                                         !tags.indexOf("<opt") &&
307                                         [ 1, "<select multiple='multiple'>", "</select>" ] ||
308
309                                         !tags.indexOf("<leg") &&
310                                         [ 1, "<fieldset>", "</fieldset>" ] ||
311
312                                         /^<(thead|tbody|tfoot|colg|cap)/.test(tags) &&
313                                         [ 1, "<table>", "</table>" ] ||
314
315                                         !tags.indexOf("<tr") &&
316                                         [ 2, "<table><tbody>", "</tbody></table>" ] ||
317
318                                         // <thead> matched above
319                                         (!tags.indexOf("<td") || !tags.indexOf("<th")) &&
320                                         [ 3, "<table><tbody><tr>", "</tr></tbody></table>" ] ||
321
322                                         !tags.indexOf("<col") &&
323                                         [ 2, "<table><tbody></tbody><colgroup>", "</colgroup></table>" ] ||
324
325                                         // IE can't serialize <link> and <script> tags normally
326                                         !jQuery.support.htmlSerialize &&
327                                         [ 1, "div<div>", "</div>" ] ||
328
329                                         [ 0, "", "" ];
330
331                                 // Go to html and back, then peel off extra wrappers
332                                 div.innerHTML = wrap[1] + elem + wrap[2];
333
334                                 // Move to the right depth
335                                 while ( wrap[0]-- ) {
336                                         div = div.lastChild;
337                                 }
338
339                                 // Remove IE's autoinserted <tbody> from table fragments
340                                 if ( !jQuery.support.tbody ) {
341
342                                         // String was a <table>, *may* have spurious <tbody>
343                                         var hasBody = /<tbody/i.test(elem),
344                                                 tbody = !tags.indexOf("<table") && !hasBody ?
345                                                         div.firstChild && div.firstChild.childNodes :
346
347                                                         // String was a bare <thead> or <tfoot>
348                                                         wrap[1] == "<table>" && !hasBody ?
349                                                                 div.childNodes :
350                                                                 [];
351
352                                         for ( var j = tbody.length - 1; j >= 0 ; --j ) {
353                                                 if ( jQuery.nodeName( tbody[ j ], "tbody" ) && !tbody[ j ].childNodes.length ) {
354                                                         tbody[ j ].parentNode.removeChild( tbody[ j ] );
355                                                 }
356                                         }
357
358                                 }
359
360                                 // IE completely kills leading whitespace when innerHTML is used
361                                 if ( !jQuery.support.leadingWhitespace && /^\s/.test( elem ) ) {
362                                         div.insertBefore( context.createTextNode( /^\s*/.exec(elem)[0] ), div.firstChild );
363                                 }
364
365                                 elem = jQuery.makeArray( div.childNodes );
366                         }
367
368                         if ( elem.nodeType ) {
369                                 ret.push( elem );
370                         } else {
371                                 ret = jQuery.merge( ret, elem );
372                         }
373
374                 });
375
376                 if ( fragment ) {
377                         for ( var i = 0; ret[i]; i++ ) {
378                                 if ( jQuery.nodeName( ret[i], "script" ) && (!ret[i].type || ret[i].type.toLowerCase() === "text/javascript") ) {
379                                         scripts.push( ret[i].parentNode ? ret[i].parentNode.removeChild( ret[i] ) : ret[i] );
380                                 } else {
381                                         if ( ret[i].nodeType === 1 )
382                                                 ret.splice.apply( ret, [i + 1, 0].concat(jQuery.makeArray(ret[i].getElementsByTagName("script"))) );
383                                         fragment.appendChild( ret[i] );
384                                 }
385                         }
386
387                         return scripts;
388                 }
389
390                 return ret;
391         }
392 });
393
394 function cleanData( elems ) {
395         for ( var i = 0, l = elems.length; i < l; i++ ) {
396                 var id = elems[i][expando];
397                 if ( id ) {
398                         delete jQuery.cache[ id ];
399                 }
400         }
401 }