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