yzt
2023-05-26 2f70f6727314edd84d8ec2bfe3ce832803f1ea77
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
define([
    '../json',
    '../_base/kernel',
    '../_base/array',
    '../has',
    '../has!dom?../selector/_loader' // only included for has() qsa tests
], function(JSON, kernel, array, has){
    has.add('activex', typeof ActiveXObject !== 'undefined');
    has.add('dom-parser', function(global){
        return 'DOMParser' in global;
    });
 
    var handleXML;
    if(has('activex')){
        // GUIDs obtained from http://msdn.microsoft.com/en-us/library/ms757837(VS.85).aspx
        var dp = [
            'Msxml2.DOMDocument.6.0',
            'Msxml2.DOMDocument.4.0',
            'MSXML2.DOMDocument.3.0',
            'MSXML.DOMDocument' // 2.0
        ];
        var lastParser;
 
        handleXML = function(response){
            var result = response.data;
            var text = response.text;
 
            if(result && has('dom-qsa2.1') && !result.querySelectorAll && has('dom-parser')){
                // http://bugs.dojotoolkit.org/ticket/15631
                // IE9 supports a CSS3 querySelectorAll implementation, but the DOM implementation
                // returned by IE9 xhr.responseXML does not. Manually create the XML DOM to gain
                // the fuller-featured implementation and avoid bugs caused by the inconsistency
                result = new DOMParser().parseFromString(text, 'application/xml');
            }
 
            function createDocument(p) {
                    try{
                        var dom = new ActiveXObject(p);
                        dom.async = false;
                        dom.loadXML(text);
                        result = dom;
                        lastParser = p;
                    }catch(e){ return false; }
                    return true;
            }
 
            if(!result || !result.documentElement){
                // The creation of an ActiveX object is expensive, so we cache the
                // parser type to avoid trying all parser types each time we handle a
                // document. There is some concern that some parser types might fail
                // depending on the document being parsed. If parsing using the cached
                // parser type fails, we do the more expensive operation of finding one
                // that works for the given document.
                // https://bugs.dojotoolkit.org/ticket/15246
                if(!lastParser || !createDocument(lastParser)) {
                    array.some(dp, createDocument);
                }
            }
 
            return result;
        };
    }
 
    var handleNativeResponse = function(response) {
        if(!has('native-xhr2-blob') && response.options.handleAs === 'blob' && typeof Blob !== 'undefined'){
            return new Blob([ response.xhr.response ], { type: response.xhr.getResponseHeader('Content-Type') });
        }
 
        return response.xhr.response;
    }
 
    var handlers = {
        'javascript': function(response){
            return kernel.eval(response.text || '');
        },
        'json': function(response){
            return JSON.parse(response.text || null);
        },
        'xml': handleXML,
        'blob': handleNativeResponse,
        'arraybuffer': handleNativeResponse,
        'document': handleNativeResponse
    };
 
    function handle(response){
        var handler = handlers[response.options.handleAs];
 
        response.data = handler ? handler(response) : (response.data || response.text);
 
        return response;
    }
 
    handle.register = function(name, handler){
        handlers[name] = handler;
    };
 
    return handle;
});