1/** 2 * Copyright © Magento, Inc. All rights reserved. 3 * See COPYING.txt for license details. 4 */ 5/* inspired by http://github.com/requirejs/text */ 6/*global XDomainRequest */ 7 8define(['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 91 xhr.open('GET', url); 92 93 /*eslint-disable max-depth */ 94 if ('setRequestHeader' in xhr && headers) { 95 for (header in headers) { 96 if (headers.hasOwnProperty(header)) { 97 xhr.setRequestHeader(header.toLowerCase(), headers[header]); 98 } 99 } 100 } 101 102 /** 103 * @inheritdoc 104 */ 105 xhr.onreadystatechange = function () { 106 var status, err; 107 108 //Do not explicitly handle errors, those should be 109 //visible via console output in the browser. 110 if (xhr.readyState === 4) { 111 status = xhr.status || 0; 112 113 if (status > 399 && status < 600) { 114 //An http 4xx or 5xx error. Signal an error. 115 err = new Error(url + ' HTTP status: ' + status); 116 err.xhr = xhr; 117 118 if (fail) { 119 fail(err); 120 } 121 } else { 122 callback(xhr.responseText); 123 124 if (defaultConfig.onXhrComplete) { 125 defaultConfig.onXhrComplete(xhr, url); 126 } 127 } 128 } 129 }; 130 131 /*eslint-enable max-depth */ 132 133 if (defaultConfig.onXhr) { 134 defaultConfig.onXhr(xhr, url); 135 } 136 137 xhr.send(); 138 } 139 140 /** 141 * Main method used by RequireJs. 142 * 143 * @param {String} name - has format: some.module.filext!strip 144 * @param {Function} req 145 * @param {Function|undefined} onLoad 146 */ 147 function loadContent(name, req, onLoad) { 148 149 var toStrip = stripReg.test(name), 150 url = req.toUrl(name.replace(stripReg, '')), 151 headers = defaultConfig.headers; 152 153 getContent(url, function (content) { 154 content = toStrip ? stripContent(content) : content; 155 onLoad(content); 156 }, onLoad.error, headers); 157 } 158 159 return { 160 load: loadContent, 161 get: getContent 162 }; 163});
Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.