|
| 1 | +/** |
| 2 | + * @license RequireJS text 2.0.3 Copyright (c) 2010-2012, The Dojo Foundation All Rights Reserved. |
| 3 | + * Available via the MIT or new BSD license. |
| 4 | + * see: http://github.com/requirejs/text for details |
| 5 | + */ |
| 6 | +/*jslint regexp: true */ |
| 7 | +/*global require: false, XMLHttpRequest: false, ActiveXObject: false, |
| 8 | + define: false, window: false, process: false, Packages: false, |
| 9 | + java: false, location: false */ |
| 10 | + |
| 11 | +define(['module'], function (module) { |
| 12 | + 'use strict'; |
| 13 | + |
| 14 | + var text, fs, |
| 15 | + progIds = ['Msxml2.XMLHTTP', 'Microsoft.XMLHTTP', 'Msxml2.XMLHTTP.4.0'], |
| 16 | + xmlRegExp = /^\s*<\?xml(\s)+version=[\'\"](\d)*.(\d)*[\'\"](\s)*\?>/im, |
| 17 | + bodyRegExp = /<body[^>]*>\s*([\s\S]+)\s*<\/body>/im, |
| 18 | + hasLocation = typeof location !== 'undefined' && location.href, |
| 19 | + defaultProtocol = hasLocation && location.protocol && location.protocol.replace(/\:/, ''), |
| 20 | + defaultHostName = hasLocation && location.hostname, |
| 21 | + defaultPort = hasLocation && (location.port || undefined), |
| 22 | + buildMap = [], |
| 23 | + masterConfig = (module.config && module.config()) || {}; |
| 24 | + |
| 25 | + text = { |
| 26 | + version: '2.0.3', |
| 27 | + |
| 28 | + strip: function (content) { |
| 29 | + //Strips <?xml ...?> declarations so that external SVG and XML |
| 30 | + //documents can be added to a document without worry. Also, if the string |
| 31 | + //is an HTML document, only the part inside the body tag is returned. |
| 32 | + if (content) { |
| 33 | + content = content.replace(xmlRegExp, ""); |
| 34 | + var matches = content.match(bodyRegExp); |
| 35 | + if (matches) { |
| 36 | + content = matches[1]; |
| 37 | + } |
| 38 | + } else { |
| 39 | + content = ""; |
| 40 | + } |
| 41 | + return content; |
| 42 | + }, |
| 43 | + |
| 44 | + jsEscape: function (content) { |
| 45 | + return content.replace(/(['\\])/g, '\\$1') |
| 46 | + .replace(/[\f]/g, "\\f") |
| 47 | + .replace(/[\b]/g, "\\b") |
| 48 | + .replace(/[\n]/g, "\\n") |
| 49 | + .replace(/[\t]/g, "\\t") |
| 50 | + .replace(/[\r]/g, "\\r") |
| 51 | + .replace(/[\u2028]/g, "\\u2028") |
| 52 | + .replace(/[\u2029]/g, "\\u2029"); |
| 53 | + }, |
| 54 | + |
| 55 | + createXhr: masterConfig.createXhr || function () { |
| 56 | + //Would love to dump the ActiveX crap in here. Need IE 6 to die first. |
| 57 | + var xhr, i, progId; |
| 58 | + if (typeof XMLHttpRequest !== "undefined") { |
| 59 | + return new XMLHttpRequest(); |
| 60 | + } else if (typeof ActiveXObject !== "undefined") { |
| 61 | + for (i = 0; i < 3; i += 1) { |
| 62 | + progId = progIds[i]; |
| 63 | + try { |
| 64 | + xhr = new ActiveXObject(progId); |
| 65 | + } catch (e) {} |
| 66 | + |
| 67 | + if (xhr) { |
| 68 | + progIds = [progId]; // so faster next time |
| 69 | + break; |
| 70 | + } |
| 71 | + } |
| 72 | + } |
| 73 | + |
| 74 | + return xhr; |
| 75 | + }, |
| 76 | + |
| 77 | + /** |
| 78 | + * Parses a resource name into its component parts. Resource names |
| 79 | + * look like: module/name.ext!strip, where the !strip part is |
| 80 | + * optional. |
| 81 | + * @param {String} name the resource name |
| 82 | + * @returns {Object} with properties "moduleName", "ext" and "strip" |
| 83 | + * where strip is a boolean. |
| 84 | + */ |
| 85 | + parseName: function (name) { |
| 86 | + var strip = false, index = name.indexOf("."), |
| 87 | + modName = name.substring(0, index), |
| 88 | + ext = name.substring(index + 1, name.length); |
| 89 | + |
| 90 | + index = ext.indexOf("!"); |
| 91 | + if (index !== -1) { |
| 92 | + //Pull off the strip arg. |
| 93 | + strip = ext.substring(index + 1, ext.length); |
| 94 | + strip = strip === "strip"; |
| 95 | + ext = ext.substring(0, index); |
| 96 | + } |
| 97 | + |
| 98 | + return { |
| 99 | + moduleName: modName, |
| 100 | + ext: ext, |
| 101 | + strip: strip |
| 102 | + }; |
| 103 | + }, |
| 104 | + |
| 105 | + xdRegExp: /^((\w+)\:)?\/\/([^\/\\]+)/, |
| 106 | + |
| 107 | + /** |
| 108 | + * Is an URL on another domain. Only works for browser use, returns |
| 109 | + * false in non-browser environments. Only used to know if an |
| 110 | + * optimized .js version of a text resource should be loaded |
| 111 | + * instead. |
| 112 | + * @param {String} url |
| 113 | + * @returns Boolean |
| 114 | + */ |
| 115 | + useXhr: function (url, protocol, hostname, port) { |
| 116 | + var uProtocol, uHostName, uPort, |
| 117 | + match = text.xdRegExp.exec(url); |
| 118 | + if (!match) { |
| 119 | + return true; |
| 120 | + } |
| 121 | + uProtocol = match[2]; |
| 122 | + uHostName = match[3]; |
| 123 | + |
| 124 | + uHostName = uHostName.split(':'); |
| 125 | + uPort = uHostName[1]; |
| 126 | + uHostName = uHostName[0]; |
| 127 | + |
| 128 | + return (!uProtocol || uProtocol === protocol) && |
| 129 | + (!uHostName || uHostName.toLowerCase() === hostname.toLowerCase()) && |
| 130 | + ((!uPort && !uHostName) || uPort === port); |
| 131 | + }, |
| 132 | + |
| 133 | + finishLoad: function (name, strip, content, onLoad) { |
| 134 | + content = strip ? text.strip(content) : content; |
| 135 | + if (masterConfig.isBuild) { |
| 136 | + buildMap[name] = content; |
| 137 | + } |
| 138 | + onLoad(content); |
| 139 | + }, |
| 140 | + |
| 141 | + load: function (name, req, onLoad, config) { |
| 142 | + //Name has format: some.module.filext!strip |
| 143 | + //The strip part is optional. |
| 144 | + //if strip is present, then that means only get the string contents |
| 145 | + //inside a body tag in an HTML string. For XML/SVG content it means |
| 146 | + //removing the <?xml ...?> declarations so the content can be inserted |
| 147 | + //into the current doc without problems. |
| 148 | + |
| 149 | + // Do not bother with the work if a build and text will |
| 150 | + // not be inlined. |
| 151 | + if (config.isBuild && !config.inlineText) { |
| 152 | + onLoad(); |
| 153 | + return; |
| 154 | + } |
| 155 | + |
| 156 | + masterConfig.isBuild = config.isBuild; |
| 157 | + |
| 158 | + var parsed = text.parseName(name), |
| 159 | + nonStripName = parsed.moduleName + '.' + parsed.ext, |
| 160 | + url = req.toUrl(nonStripName), |
| 161 | + useXhr = (masterConfig.useXhr) || |
| 162 | + text.useXhr; |
| 163 | + |
| 164 | + //Load the text. Use XHR if possible and in a browser. |
| 165 | + if (!hasLocation || useXhr(url, defaultProtocol, defaultHostName, defaultPort)) { |
| 166 | + text.get(url, function (content) { |
| 167 | + text.finishLoad(name, parsed.strip, content, onLoad); |
| 168 | + }, function (err) { |
| 169 | + if (onLoad.error) { |
| 170 | + onLoad.error(err); |
| 171 | + } |
| 172 | + }); |
| 173 | + } else { |
| 174 | + //Need to fetch the resource across domains. Assume |
| 175 | + //the resource has been optimized into a JS module. Fetch |
| 176 | + //by the module name + extension, but do not include the |
| 177 | + //!strip part to avoid file system issues. |
| 178 | + req([nonStripName], function (content) { |
| 179 | + text.finishLoad(parsed.moduleName + '.' + parsed.ext, |
| 180 | + parsed.strip, content, onLoad); |
| 181 | + }); |
| 182 | + } |
| 183 | + }, |
| 184 | + |
| 185 | + write: function (pluginName, moduleName, write, config) { |
| 186 | + if (buildMap.hasOwnProperty(moduleName)) { |
| 187 | + var content = text.jsEscape(buildMap[moduleName]); |
| 188 | + write.asModule(pluginName + "!" + moduleName, |
| 189 | + "define(function () { return '" + |
| 190 | + content + |
| 191 | + "';});\n"); |
| 192 | + } |
| 193 | + }, |
| 194 | + |
| 195 | + writeFile: function (pluginName, moduleName, req, write, config) { |
| 196 | + var parsed = text.parseName(moduleName), |
| 197 | + nonStripName = parsed.moduleName + '.' + parsed.ext, |
| 198 | + //Use a '.js' file name so that it indicates it is a |
| 199 | + //script that can be loaded across domains. |
| 200 | + fileName = req.toUrl(parsed.moduleName + '.' + |
| 201 | + parsed.ext) + '.js'; |
| 202 | + |
| 203 | + //Leverage own load() method to load plugin value, but only |
| 204 | + //write out values that do not have the strip argument, |
| 205 | + //to avoid any potential issues with ! in file names. |
| 206 | + text.load(nonStripName, req, function (value) { |
| 207 | + //Use own write() method to construct full module value. |
| 208 | + //But need to create shell that translates writeFile's |
| 209 | + //write() to the right interface. |
| 210 | + var textWrite = function (contents) { |
| 211 | + return write(fileName, contents); |
| 212 | + }; |
| 213 | + textWrite.asModule = function (moduleName, contents) { |
| 214 | + return write.asModule(moduleName, fileName, contents); |
| 215 | + }; |
| 216 | + |
| 217 | + text.write(pluginName, nonStripName, textWrite, config); |
| 218 | + }, config); |
| 219 | + } |
| 220 | + }; |
| 221 | + |
| 222 | + if (masterConfig.env === 'node' || (!masterConfig.env && |
| 223 | + typeof process !== "undefined" && |
| 224 | + process.versions && |
| 225 | + !!process.versions.node)) { |
| 226 | + //Using special require.nodeRequire, something added by r.js. |
| 227 | + fs = require.nodeRequire('fs'); |
| 228 | + |
| 229 | + text.get = function (url, callback) { |
| 230 | + var file = fs.readFileSync(url, 'utf8'); |
| 231 | + //Remove BOM (Byte Mark Order) from utf8 files if it is there. |
| 232 | + if (file.indexOf('\uFEFF') === 0) { |
| 233 | + file = file.substring(1); |
| 234 | + } |
| 235 | + callback(file); |
| 236 | + }; |
| 237 | + } else if (masterConfig.env === 'xhr' || (!masterConfig.env && |
| 238 | + text.createXhr())) { |
| 239 | + text.get = function (url, callback, errback) { |
| 240 | + var xhr = text.createXhr(); |
| 241 | + xhr.open('GET', url, true); |
| 242 | + |
| 243 | + //Allow overrides specified in config |
| 244 | + if (masterConfig.onXhr) { |
| 245 | + masterConfig.onXhr(xhr, url); |
| 246 | + } |
| 247 | + |
| 248 | + xhr.onreadystatechange = function (evt) { |
| 249 | + var status, err; |
| 250 | + //Do not explicitly handle errors, those should be |
| 251 | + //visible via console output in the browser. |
| 252 | + if (xhr.readyState === 4) { |
| 253 | + status = xhr.status; |
| 254 | + if (status > 399 && status < 600) { |
| 255 | + //An http 4xx or 5xx error. Signal an error. |
| 256 | + err = new Error(url + ' HTTP status: ' + status); |
| 257 | + err.xhr = xhr; |
| 258 | + errback(err); |
| 259 | + } else { |
| 260 | + callback(xhr.responseText); |
| 261 | + } |
| 262 | + } |
| 263 | + }; |
| 264 | + xhr.send(null); |
| 265 | + }; |
| 266 | + } else if (masterConfig.env === 'rhino' || (!masterConfig.env && |
| 267 | + typeof Packages !== 'undefined' && typeof java !== 'undefined')) { |
| 268 | + //Why Java, why is this so awkward? |
| 269 | + text.get = function (url, callback) { |
| 270 | + var stringBuffer, line, |
| 271 | + encoding = "utf-8", |
| 272 | + file = new java.io.File(url), |
| 273 | + lineSeparator = java.lang.System.getProperty("line.separator"), |
| 274 | + input = new java.io.BufferedReader(new java.io.InputStreamReader(new java.io.FileInputStream(file), encoding)), |
| 275 | + content = ''; |
| 276 | + try { |
| 277 | + stringBuffer = new java.lang.StringBuffer(); |
| 278 | + line = input.readLine(); |
| 279 | + |
| 280 | + // Byte Order Mark (BOM) - The Unicode Standard, version 3.0, page 324 |
| 281 | + // http://www.unicode.org/faq/utf_bom.html |
| 282 | + |
| 283 | + // Note that when we use utf-8, the BOM should appear as "EF BB BF", but it doesn't due to this bug in the JDK: |
| 284 | + // http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=4508058 |
| 285 | + if (line && line.length() && line.charAt(0) === 0xfeff) { |
| 286 | + // Eat the BOM, since we've already found the encoding on this file, |
| 287 | + // and we plan to concatenating this buffer with others; the BOM should |
| 288 | + // only appear at the top of a file. |
| 289 | + line = line.substring(1); |
| 290 | + } |
| 291 | + |
| 292 | + stringBuffer.append(line); |
| 293 | + |
| 294 | + while ((line = input.readLine()) !== null) { |
| 295 | + stringBuffer.append(lineSeparator); |
| 296 | + stringBuffer.append(line); |
| 297 | + } |
| 298 | + //Make sure we return a JavaScript string and not a Java string. |
| 299 | + content = String(stringBuffer.toString()); //String |
| 300 | + } finally { |
| 301 | + input.close(); |
| 302 | + } |
| 303 | + callback(content); |
| 304 | + }; |
| 305 | + } |
| 306 | + |
| 307 | + return text; |
| 308 | +}); |
0 commit comments