jQuery.parseXML extracted from ajax & added to core, needs unit testing.
[jquery.git] / src / core.js
index d41b9a9..0f9801a 100644 (file)
@@ -546,6 +546,28 @@ jQuery.extend({
                        jQuery.error( "Invalid JSON: " + data );
                }
        },
+       
+       // Cross-browser xml parsing
+       // (xml & tmp used internally)
+       parseXML: function( data , xml , tmp ) {
+
+               if ( window.DOMParser ) { // Standard
+                       tmp = new DOMParser();
+                       xml = tmp.parseFromString( data , "text/xml" );
+               } else { // IE
+                       xml = new ActiveXObject( "Microsoft.XMLDOM" );
+                       xml.async = "false";
+                       xml.loadXML( data );
+               }
+               
+               tmp = xml.documentElement;
+               
+               if ( ! tmp || ! tmp.nodeName || tmp.nodeName === "parsererror" ) {
+                       jQuery.error( "Invalid XML: " + data );
+               }
+               
+               return xml;
+       },
 
        noop: function() {},
 
@@ -800,12 +822,14 @@ jQuery.extend({
                        deferred  = {
                                
                                // then( f1, f2, ...)
-                               then: function() {
+                               then: function () {
                                        
                                        if ( ! cancelled ) {
                                        
                                                var args = arguments,
                                                        i,
+                                                       length,
+                                                       elem,
                                                        type,
                                                        _fired;
                                                        
@@ -814,13 +838,13 @@ jQuery.extend({
                                                        fired = 0;
                                                }
                                                
-                                               for ( i in args ) {
-                                                       i = args[ i ];
-                                                       type = jQuery.type( i );
+                                               for ( i = 0, length = args.length ; i < length ; i++ ) {
+                                                       elem = args[ i ];
+                                                       type = jQuery.type( elem );
                                                        if ( type === "array" ) {
-                                                               this.then.apply( this , i );
+                                                               deferred.then.apply( deferred , elem );
                                                        } else if ( type === "function" ) {
-                                                               callbacks.push( i );
+                                                               callbacks.push( elem );
                                                        }
                                                }