all files / lib/ parseResponse.js

46.67% Statements 7/15
64.29% Branches 9/14
50% Functions 1/2
46.67% Lines 7/15
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                                                                                   
/**
 * @author jQuery
 * @copyright jQuery foundation
 * @license https://github.com/jquery/jquery/blob/master/LICENSE.txt
 *
 * Parses XML string content
 * @param {string} data - content
 * @returns {xml}
 */
function parseXML(data) {
    "use strict";
    var xml;
 
    // Support: IE9
    try {
        xml = (new window.DOMParser()).parseFromString(data, "text/xml");
    } catch (e) {
        xml = undefined;
    }
 
    if (!xml || xml.getElementsByTagName("parsererror").length) {
        throw new SyntaxError("");
    }
 
    return xml;
}
 
/**
 * @memberof ajax
 * @summary parses the response data based on provided responseType
 * @private
 * @param xhr {object} XMLHttpRequest object
 * @returns {object|string}
 */
module.exports = function parseResponse(xhr) {
    "use strict";
 
    // only try to parse if not already parsed by xhr
    if (typeof xhr.response === "string" && xhr.response !== "") {
        if (xhr.responseType === "json") {
            return JSON.parse(xhr.response);
        } else Iif (xhr.type === "text/xml" || xhr.type === "xml") {
            return parseXML(xhr.response);
        }
    }
 
    return xhr.response;
};