{"version":3,"file":"get-css-data.js","sources":["../src/get-urls.js","../src/get-css.js"],"sourcesContent":["/*global XDomainRequest*/\n\n// Functions\n// =============================================================================\n/**\n * Requests one-or-more URLs and returns array of data in order specified.\n * Provides callbacks error and success callbacks for each XMLHttpRequest.\n *\n * @param {array|string} url Single URL or array of URLs to request\n * @param {object} [options] Options object\n * @param {string} [options.mimeType] Overrides MIME type returned by\n * server\n * @param {function} [options.onBeforeSend] Callback before each request is\n * sent. Passes 1) the xhr object, 2) the URL, and 3) the\n * URL index as arguments.\n * @param {function} [options.onSuccess] Callback on xhr success. Passes 1)\n * xhr response text, 2) the URL, and 3) the URL index as\n * arguments.\n * @param {function} [options.onError] Callback on xhr error. Passes 1) the\n * xhr object, 2) the URL, 3) the URL index as arguments.\n * @param {function} [options.onComplete] Callback after all requests have\n * completed. Passes 1) an array of response text for each\n * URL in order provided as an argument.\n */\nfunction getUrls(urls, options = {}) {\n const settings = {\n mimeType : options.mimeType || null,\n onBeforeSend: options.onBeforeSend || Function.prototype,\n onSuccess : options.onSuccess || Function.prototype,\n onError : options.onError || Function.prototype,\n onComplete : options.onComplete || Function.prototype\n };\n const urlArray = Array.isArray(urls) ? urls : [urls];\n const urlQueue = Array.apply(null, Array(urlArray.length)).map(x => null);\n\n // Functions (Private)\n // -------------------------------------------------------------------------\n function isValidCss(text) {\n const isString = typeof text === 'string';\n const isHTML = isString && text.trim().charAt(0) === '<';\n\n return isString && !isHTML;\n }\n\n function onError(xhr, urlIndex) {\n settings.onError(xhr, urlArray[urlIndex], urlIndex);\n }\n\n function onSuccess(responseText, urlIndex) {\n const returnVal = settings.onSuccess(responseText, urlArray[urlIndex], urlIndex);\n\n responseText = returnVal === false ? '' : returnVal || responseText;\n urlQueue[urlIndex] = responseText;\n\n // Complete\n if (urlQueue.indexOf(null) === -1) {\n settings.onComplete(urlQueue);\n }\n }\n\n // Main\n // -------------------------------------------------------------------------\n const parser = document.createElement('a');\n\n urlArray.forEach((url, i) => {\n parser.setAttribute('href', url);\n parser.href = String(parser.href);\n\n const isIElte9 = Boolean(document.all && !window.atob);\n const isIElte9CORS = isIElte9 && parser.host.split(':')[0] !== location.host.split(':')[0];\n\n // IE 9 CORS\n if (isIElte9CORS) {\n const isSameProtocol = parser.protocol === location.protocol;\n\n if (isSameProtocol) {\n const xdr = new XDomainRequest();\n\n // Event handlers must be assigned AFTER xdr.open\n xdr.open('GET', url);\n\n xdr.timeout = 0; // Prevent aborts/timeouts\n xdr.onprogress = Function.prototype; // Prevent aborts/timeouts\n xdr.ontimeout = Function.prototype; // Prevent aborts/timeouts\n xdr.onload = function() {\n const text = xdr.responseText;\n\n if (isValidCss(text)) {\n onSuccess(text, i);\n }\n else {\n onError(xdr, i);\n }\n };\n xdr.onerror = function(err) {\n onError(xdr, i);\n };\n\n // Wrap in setTimeout to fix known issues with XDomainRequest\n // when sending multiple requests\n setTimeout(function() {\n xdr.send();\n }, 0);\n }\n else {\n // eslint-disable-next-line\n console.warn(`Internet Explorer 9 Cross-Origin (CORS) requests must use the same protocol (${url})`);\n onError(null, i);\n }\n }\n // Other\n else {\n const xhr = new XMLHttpRequest();\n\n xhr.open('GET', url);\n\n // overrideMimeType method not available in all browsers\n if (settings.mimeType && xhr.overrideMimeType) {\n xhr.overrideMimeType(settings.mimeType);\n }\n\n settings.onBeforeSend(xhr, url, i);\n\n xhr.onreadystatechange = function() {\n if (xhr.readyState === 4) {\n const text = xhr.responseText;\n\n // Success\n if (xhr.status < 400 && isValidCss(text)) {\n onSuccess(text, i);\n }\n // Success via file protocol (file://)\n else if (xhr.status === 0 && isValidCss(text)) {\n onSuccess(text, i);\n }\n // Error\n else {\n onError(xhr, i);\n }\n }\n };\n\n xhr.send();\n }\n });\n}\n\n\n// Export\n// =============================================================================\nexport default getUrls;\n","// Dependencies\n// =============================================================================\nimport getUrls from './get-urls';\n\n\n// Functions (Public)\n// =============================================================================\n/**\n * Gets CSS data from