|
| 1 | +/** |
| 2 | + * Copyright © 2015 Magento. All rights reserved. |
| 3 | + * See COPYING.txt for license details. |
| 4 | + */ |
| 5 | +/* inspired by http://github.com/requirejs/text */ |
| 6 | +/*global XMLHttpRequest, XDomainRequest */ |
| 7 | + |
| 8 | +define(['module'], function (module) { |
| 9 | + 'use strict'; |
| 10 | + |
| 11 | + var xmlRegExp = /^\s*<\?xml(\s)+version=[\'\"](\d)*.(\d)*[\'\"](\s)*\?>/im, |
| 12 | + bodyRegExp = /<body[^>]*>\s*([\s\S]+)\s*<\/body>/im, |
| 13 | + stripReg = /!strip$/i, |
| 14 | + defaultConfig = module.config && module.config() || {}; |
| 15 | + |
| 16 | + /** |
| 17 | + * Strips <?xml ...?> declarations so that external SVG and XML documents can be |
| 18 | + * added to a document without worry. |
| 19 | + * Also, if the string is an HTML document, only the part inside the body tag is returned. |
| 20 | + * |
| 21 | + * @param {String} external |
| 22 | + * @returns {String} |
| 23 | + */ |
| 24 | + function stripContent(external) { |
| 25 | + var matches; |
| 26 | + |
| 27 | + if (!external) { |
| 28 | + return ''; |
| 29 | + } |
| 30 | + |
| 31 | + matches = external.match(bodyRegExp); |
| 32 | + external = matches ? |
| 33 | + matches[1] : |
| 34 | + external.replace(xmlRegExp, ''); |
| 35 | + |
| 36 | + return external; |
| 37 | + } |
| 38 | + |
| 39 | + /** |
| 40 | + * Checks that url match current location |
| 41 | + * |
| 42 | + * @param {String} url |
| 43 | + * @returns {Boolean} |
| 44 | + */ |
| 45 | + function sameDomain(url) { |
| 46 | + var uProtocol, uHostName, uPort, |
| 47 | + xdRegExp = /^([\w:]+)?\/\/([^\/\\]+)/i, |
| 48 | + location = window.location, |
| 49 | + match = xdRegExp.exec(url); |
| 50 | + |
| 51 | + if (!match) { |
| 52 | + return true; |
| 53 | + } |
| 54 | + uProtocol = match[1]; |
| 55 | + uHostName = match[2]; |
| 56 | + |
| 57 | + uHostName = uHostName.split(':'); |
| 58 | + uPort = uHostName[1] || ''; |
| 59 | + uHostName = uHostName[0]; |
| 60 | + |
| 61 | + return (!uProtocol || uProtocol === location.protocol) && |
| 62 | + (!uHostName || uHostName.toLowerCase() === location.hostname.toLowerCase()) && |
| 63 | + (!uPort && !uHostName || uPort === location.port); |
| 64 | + } |
| 65 | + |
| 66 | + /** |
| 67 | + * @returns {XMLHttpRequest|XDomainRequest|null} |
| 68 | + */ |
| 69 | + function createRequest(url) { |
| 70 | + var xhr = new XMLHttpRequest(); |
| 71 | + |
| 72 | + if (!sameDomain(url) && typeof XDomainRequest !== 'undefined') { |
| 73 | + xhr = new XDomainRequest(); |
| 74 | + } |
| 75 | + |
| 76 | + return xhr; |
| 77 | + } |
| 78 | + |
| 79 | + /** |
| 80 | + * XHR requester. Returns value to callback. |
| 81 | + * |
| 82 | + * @param {String} url |
| 83 | + * @param {Function} callback |
| 84 | + * @param {Function} fail |
| 85 | + * @param {Object} headers |
| 86 | + */ |
| 87 | + function getContent(url, callback, fail, headers) { |
| 88 | + var xhr = createRequest(url), |
| 89 | + header, |
| 90 | + errorHandler = fail || Function(); |
| 91 | + |
| 92 | + /*eslint-disable max-depth */ |
| 93 | + if ('setRequestHeader' in xhr && headers) { |
| 94 | + for (header in headers) { |
| 95 | + if (headers.hasOwnProperty(header)) { |
| 96 | + xhr.setRequestHeader(header.toLowerCase(), headers[header]); |
| 97 | + } |
| 98 | + } |
| 99 | + } |
| 100 | + |
| 101 | + /*eslint-enable max-depth */ |
| 102 | + |
| 103 | + if (defaultConfig.onXhr) { |
| 104 | + defaultConfig.onXhr(xhr, url); |
| 105 | + } |
| 106 | + |
| 107 | + /** |
| 108 | + * onload handler |
| 109 | + */ |
| 110 | + xhr.onload = function () { |
| 111 | + |
| 112 | + callback(xhr.responseText); |
| 113 | + |
| 114 | + if (defaultConfig.onXhrComplete) { |
| 115 | + defaultConfig.onXhrComplete(xhr, url); |
| 116 | + } |
| 117 | + }; |
| 118 | + |
| 119 | + /** |
| 120 | + * onerror handler |
| 121 | + */ |
| 122 | + xhr.onerror = function (event) { |
| 123 | + errorHandler(event); |
| 124 | + |
| 125 | + if (defaultConfig.onXhrFailure) { |
| 126 | + defaultConfig.onXhrFailure(xhr, url, event); |
| 127 | + } |
| 128 | + }; |
| 129 | + |
| 130 | + xhr.open('GET', url); |
| 131 | + xhr.send(); |
| 132 | + } |
| 133 | + |
| 134 | + /** |
| 135 | + * Main method used by RequireJs. |
| 136 | + * |
| 137 | + * @param {String} name - has format: some.module.filext!strip |
| 138 | + * @param {Function} req |
| 139 | + * @param {Function|undefined} onLoad |
| 140 | + */ |
| 141 | + function loadContent(name, req, onLoad) { |
| 142 | + |
| 143 | + var toStrip = stripReg.test(name), |
| 144 | + url = req.toUrl(name.replace(stripReg, '')), |
| 145 | + headers = defaultConfig.headers; |
| 146 | + |
| 147 | + getContent(url, function (content) { |
| 148 | + content = toStrip ? stripContent(content) : content; |
| 149 | + onLoad(content); |
| 150 | + }, onLoad.error, headers); |
| 151 | + } |
| 152 | + |
| 153 | + return { |
| 154 | + load: loadContent, |
| 155 | + get: getContent |
| 156 | + }; |
| 157 | +}); |
0 commit comments