| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
define(['module'], function (module) { |
| 11 |
'use strict'; |
| 12 |
|
| 13 |
var text, fs, Cc, Ci, xpcIsWindows, |
| 14 |
progIds = ['Msxml2.XMLHTTP', 'Microsoft.XMLHTTP', 'Msxml2.XMLHTTP.4.0'], |
| 15 |
xmlRegExp = /^\s*<\?xml(\s)+version=[\'\"](\d)*.(\d)*[\'\"](\s)*\?>/im, |
| 16 |
bodyRegExp = /<body[^>]*>\s*([\s\S]+)\s*<\/body>/im, |
| 17 |
hasLocation = typeof location !== 'undefined' && location.href, |
| 18 |
defaultProtocol = hasLocation && location.protocol && location.protocol.replace(/\:/, ''), |
| 19 |
defaultHostName = hasLocation && location.hostname, |
| 20 |
defaultPort = hasLocation && (location.port || undefined), |
| 21 |
buildMap = {}, |
| 22 |
masterConfig = (module.config && module.config()) || {}; |
| 23 |
|
| 24 |
function useDefault(value, defaultValue) { |
| 25 |
return value === undefined || value === '' ? defaultValue : value; |
| 26 |
} |
| 27 |
|
| 28 |
|
| 29 |
function isSamePort(protocol1, port1, protocol2, port2) { |
| 30 |
if (port1 === port2) { |
| 31 |
return true; |
| 32 |
} else if (protocol1 === protocol2) { |
| 33 |
if (protocol1 === 'http') { |
| 34 |
return useDefault(port1, '80') === useDefault(port2, '80'); |
| 35 |
} else if (protocol1 === 'https') { |
| 36 |
return useDefault(port1, '443') === useDefault(port2, '443'); |
| 37 |
} |
| 38 |
} |
| 39 |
return false; |
| 40 |
} |
| 41 |
|
| 42 |
text = { |
| 43 |
version: '2.0.16', |
| 44 |
|
| 45 |
strip: function (content) { |
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
if (content) { |
| 50 |
content = content.replace(xmlRegExp, ""); |
| 51 |
var matches = content.match(bodyRegExp); |
| 52 |
if (matches) { |
| 53 |
content = matches[1]; |
| 54 |
} |
| 55 |
} else { |
| 56 |
content = ""; |
| 57 |
} |
| 58 |
return content; |
| 59 |
}, |
| 60 |
|
| 61 |
jsEscape: function (content) { |
| 62 |
return content.replace(/(['\\])/g, '\\$1') |
| 63 |
.replace(/[\f]/g, "\\f") |
| 64 |
.replace(/[\b]/g, "\\b") |
| 65 |
.replace(/[\n]/g, "\\n") |
| 66 |
.replace(/[\t]/g, "\\t") |
| 67 |
.replace(/[\r]/g, "\\r") |
| 68 |
.replace(/[\u2028]/g, "\\u2028") |
| 69 |
.replace(/[\u2029]/g, "\\u2029"); |
| 70 |
}, |
| 71 |
|
| 72 |
createXhr: masterConfig.createXhr || function () { |
| 73 |
|
| 74 |
var xhr, i, progId; |
| 75 |
if (typeof XMLHttpRequest !== "undefined") { |
| 76 |
return new XMLHttpRequest(); |
| 77 |
} else if (typeof ActiveXObject !== "undefined") { |
| 78 |
for (i = 0; i < 3; i += 1) { |
| 79 |
progId = progIds[i]; |
| 80 |
try { |
| 81 |
xhr = new ActiveXObject(progId); |
| 82 |
} catch (e) {} |
| 83 |
|
| 84 |
if (xhr) { |
| 85 |
progIds = [progId]; |
| 86 |
break; |
| 87 |
} |
| 88 |
} |
| 89 |
} |
| 90 |
|
| 91 |
return xhr; |
| 92 |
}, |
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
parseName: function (name) { |
| 103 |
var modName, ext, temp, |
| 104 |
strip = false, |
| 105 |
index = name.lastIndexOf("."), |
| 106 |
isRelative = name.indexOf('./') === 0 || |
| 107 |
name.indexOf('../') === 0; |
| 108 |
|
| 109 |
if (index !== -1 && (!isRelative || index > 1)) { |
| 110 |
modName = name.substring(0, index); |
| 111 |
ext = name.substring(index + 1); |
| 112 |
} else { |
| 113 |
modName = name; |
| 114 |
} |
| 115 |
|
| 116 |
temp = ext || modName; |
| 117 |
index = temp.indexOf("!"); |
| 118 |
if (index !== -1) { |
| 119 |
|
| 120 |
strip = temp.substring(index + 1) === "strip"; |
| 121 |
temp = temp.substring(0, index); |
| 122 |
if (ext) { |
| 123 |
ext = temp; |
| 124 |
} else { |
| 125 |
modName = temp; |
| 126 |
} |
| 127 |
} |
| 128 |
|
| 129 |
return { |
| 130 |
moduleName: modName, |
| 131 |
ext: ext, |
| 132 |
strip: strip |
| 133 |
}; |
| 134 |
}, |
| 135 |
|
| 136 |
xdRegExp: /^((\w+)\:)?\/\/([^\/\\]+)/, |
| 137 |
|
| 138 |
|
| 139 |
|
| 140 |
|
| 141 |
|
| 142 |
|
| 143 |
|
| 144 |
|
| 145 |
|
| 146 |
useXhr: function (url, protocol, hostname, port) { |
| 147 |
var uProtocol, uHostName, uPort, |
| 148 |
match = text.xdRegExp.exec(url); |
| 149 |
if (!match) { |
| 150 |
return true; |
| 151 |
} |
| 152 |
uProtocol = match[2]; |
| 153 |
uHostName = match[3]; |
| 154 |
|
| 155 |
uHostName = uHostName.split(':'); |
| 156 |
uPort = uHostName[1]; |
| 157 |
uHostName = uHostName[0]; |
| 158 |
|
| 159 |
return (!uProtocol || uProtocol === protocol) && |
| 160 |
(!uHostName || uHostName.toLowerCase() === hostname.toLowerCase()) && |
| 161 |
((!uPort && !uHostName) || isSamePort(uProtocol, uPort, protocol, port)); |
| 162 |
}, |
| 163 |
|
| 164 |
finishLoad: function (name, strip, content, onLoad) { |
| 165 |
content = strip ? text.strip(content) : content; |
| 166 |
if (masterConfig.isBuild) { |
| 167 |
buildMap[name] = content; |
| 168 |
} |
| 169 |
onLoad(content); |
| 170 |
}, |
| 171 |
|
| 172 |
load: function (name, req, onLoad, config) { |
| 173 |
|
| 174 |
|
| 175 |
|
| 176 |
|
| 177 |
|
| 178 |
|
| 179 |
|
| 180 |
|
| 181 |
|
| 182 |
if (config && config.isBuild && !config.inlineText) { |
| 183 |
onLoad(); |
| 184 |
return; |
| 185 |
} |
| 186 |
|
| 187 |
masterConfig.isBuild = config && config.isBuild; |
| 188 |
|
| 189 |
var parsed = text.parseName(name), |
| 190 |
nonStripName = parsed.moduleName + |
| 191 |
(parsed.ext ? '.' + parsed.ext : ''), |
| 192 |
url = req.toUrl(nonStripName), |
| 193 |
useXhr = (masterConfig.useXhr) || |
| 194 |
text.useXhr; |
| 195 |
|
| 196 |
|
| 197 |
if (url.indexOf('empty:') === 0) { |
| 198 |
onLoad(); |
| 199 |
return; |
| 200 |
} |
| 201 |
|
| 202 |
|
| 203 |
if (!hasLocation || useXhr(url, defaultProtocol, defaultHostName, defaultPort)) { |
| 204 |
text.get(url, function (content) { |
| 205 |
text.finishLoad(name, parsed.strip, content, onLoad); |
| 206 |
}, function (err) { |
| 207 |
if (onLoad.error) { |
| 208 |
onLoad.error(err); |
| 209 |
} |
| 210 |
}); |
| 211 |
} else { |
| 212 |
|
| 213 |
|
| 214 |
|
| 215 |
|
| 216 |
req([nonStripName], function (content) { |
| 217 |
text.finishLoad(parsed.moduleName + '.' + parsed.ext, |
| 218 |
parsed.strip, content, onLoad); |
| 219 |
}, function (err) { |
| 220 |
if (onLoad.error) { |
| 221 |
onLoad.error(err); |
| 222 |
} |
| 223 |
}); |
| 224 |
} |
| 225 |
}, |
| 226 |
|
| 227 |
write: function (pluginName, moduleName, write, config) { |
| 228 |
if (buildMap.hasOwnProperty(moduleName)) { |
| 229 |
var content = text.jsEscape(buildMap[moduleName]); |
| 230 |
write.asModule(pluginName + "!" + moduleName, |
| 231 |
"define(function () { return '" + |
| 232 |
content + |
| 233 |
"';});\n"); |
| 234 |
} |
| 235 |
}, |
| 236 |
|
| 237 |
writeFile: function (pluginName, moduleName, req, write, config) { |
| 238 |
var parsed = text.parseName(moduleName), |
| 239 |
extPart = parsed.ext ? '.' + parsed.ext : '', |
| 240 |
nonStripName = parsed.moduleName + extPart, |
| 241 |
|
| 242 |
|
| 243 |
fileName = req.toUrl(parsed.moduleName + extPart) + '.js'; |
| 244 |
|
| 245 |
|
| 246 |
|
| 247 |
|
| 248 |
text.load(nonStripName, req, function (value) { |
| 249 |
|
| 250 |
|
| 251 |
|
| 252 |
var textWrite = function (contents) { |
| 253 |
return write(fileName, contents); |
| 254 |
}; |
| 255 |
textWrite.asModule = function (moduleName, contents) { |
| 256 |
return write.asModule(moduleName, fileName, contents); |
| 257 |
}; |
| 258 |
|
| 259 |
text.write(pluginName, nonStripName, textWrite, config); |
| 260 |
}, config); |
| 261 |
} |
| 262 |
}; |
| 263 |
|
| 264 |
if (masterConfig.env === 'node' || (!masterConfig.env && |
| 265 |
typeof process !== "undefined" && |
| 266 |
process.versions && |
| 267 |
!!process.versions.node && |
| 268 |
!process.versions['node-webkit'] && |
| 269 |
!process.versions['atom-shell'])) { |
| 270 |
|
| 271 |
fs = require.nodeRequire('fs'); |
| 272 |
|
| 273 |
text.get = function (url, callback, errback) { |
| 274 |
try { |
| 275 |
var file = fs.readFileSync(url, 'utf8'); |
| 276 |
|
| 277 |
if (file[0] === '\uFEFF') { |
| 278 |
file = file.substring(1); |
| 279 |
} |
| 280 |
callback(file); |
| 281 |
} catch (e) { |
| 282 |
if (errback) { |
| 283 |
errback(e); |
| 284 |
} |
| 285 |
} |
| 286 |
}; |
| 287 |
} else if (masterConfig.env === 'xhr' || (!masterConfig.env && |
| 288 |
text.createXhr())) { |
| 289 |
text.get = function (url, callback, errback, headers) { |
| 290 |
var xhr = text.createXhr(), header; |
| 291 |
xhr.open('GET', url, true); |
| 292 |
|
| 293 |
|
| 294 |
if (headers) { |
| 295 |
for (header in headers) { |
| 296 |
if (headers.hasOwnProperty(header)) { |
| 297 |
xhr.setRequestHeader(header.toLowerCase(), headers[header]); |
| 298 |
} |
| 299 |
} |
| 300 |
} |
| 301 |
|
| 302 |
|
| 303 |
if (masterConfig.onXhr) { |
| 304 |
masterConfig.onXhr(xhr, url); |
| 305 |
} |
| 306 |
|
| 307 |
xhr.onreadystatechange = function (evt) { |
| 308 |
var status, err; |
| 309 |
|
| 310 |
|
| 311 |
if (xhr.readyState === 4) { |
| 312 |
status = xhr.status || 0; |
| 313 |
if (status > 399 && status < 600) { |
| 314 |
|
| 315 |
err = new Error(url + ' HTTP status: ' + status); |
| 316 |
err.xhr = xhr; |
| 317 |
if (errback) { |
| 318 |
errback(err); |
| 319 |
} |
| 320 |
} else { |
| 321 |
callback(xhr.responseText); |
| 322 |
} |
| 323 |
|
| 324 |
if (masterConfig.onXhrComplete) { |
| 325 |
masterConfig.onXhrComplete(xhr, url); |
| 326 |
} |
| 327 |
} |
| 328 |
}; |
| 329 |
xhr.send(null); |
| 330 |
}; |
| 331 |
} else if (masterConfig.env === 'rhino' || (!masterConfig.env && |
| 332 |
typeof Packages !== 'undefined' && typeof java !== 'undefined')) { |
| 333 |
|
| 334 |
text.get = function (url, callback) { |
| 335 |
var stringBuffer, line, |
| 336 |
encoding = "utf-8", |
| 337 |
file = new java.io.File(url), |
| 338 |
lineSeparator = java.lang.System.getProperty("line.separator"), |
| 339 |
input = new java.io.BufferedReader(new java.io.InputStreamReader(new java.io.FileInputStream(file), encoding)), |
| 340 |
content = ''; |
| 341 |
try { |
| 342 |
stringBuffer = new java.lang.StringBuffer(); |
| 343 |
line = input.readLine(); |
| 344 |
|
| 345 |
|
| 346 |
|
| 347 |
|
| 348 |
|
| 349 |
|
| 350 |
if (line && line.length() && line.charAt(0) === 0xfeff) { |
| 351 |
|
| 352 |
|
| 353 |
|
| 354 |
line = line.substring(1); |
| 355 |
} |
| 356 |
|
| 357 |
if (line !== null) { |
| 358 |
stringBuffer.append(line); |
| 359 |
} |
| 360 |
|
| 361 |
while ((line = input.readLine()) !== null) { |
| 362 |
stringBuffer.append(lineSeparator); |
| 363 |
stringBuffer.append(line); |
| 364 |
} |
| 365 |
|
| 366 |
content = String(stringBuffer.toString()); |
| 367 |
} finally { |
| 368 |
input.close(); |
| 369 |
} |
| 370 |
callback(content); |
| 371 |
}; |
| 372 |
} else if (masterConfig.env === 'xpconnect' || (!masterConfig.env && |
| 373 |
typeof Components !== 'undefined' && Components.classes && |
| 374 |
Components.interfaces)) { |
| 375 |
|
| 376 |
Cc = Components.classes; |
| 377 |
Ci = Components.interfaces; |
| 378 |
Components.utils['import']('resource://gre/modules/FileUtils.jsm'); |
| 379 |
xpcIsWindows = ('@mozilla.org/windows-registry-key;1' in Cc); |
| 380 |
|
| 381 |
text.get = function (url, callback) { |
| 382 |
var inStream, convertStream, fileObj, |
| 383 |
readData = {}; |
| 384 |
|
| 385 |
if (xpcIsWindows) { |
| 386 |
url = url.replace(/\//g, '\\'); |
| 387 |
} |
| 388 |
|
| 389 |
fileObj = new FileUtils.File(url); |
| 390 |
|
| 391 |
|
| 392 |
try { |
| 393 |
inStream = Cc['@mozilla.org/network/file-input-stream;1'] |
| 394 |
.createInstance(Ci.nsIFileInputStream); |
| 395 |
inStream.init(fileObj, 1, 0, false); |
| 396 |
|
| 397 |
convertStream = Cc['@mozilla.org/intl/converter-input-stream;1'] |
| 398 |
.createInstance(Ci.nsIConverterInputStream); |
| 399 |
convertStream.init(inStream, "utf-8", inStream.available(), |
| 400 |
Ci.nsIConverterInputStream.DEFAULT_REPLACEMENT_CHARACTER); |
| 401 |
|
| 402 |
convertStream.readString(inStream.available(), readData); |
| 403 |
convertStream.close(); |
| 404 |
inStream.close(); |
| 405 |
callback(readData.value); |
| 406 |
} catch (e) { |
| 407 |
throw new Error((fileObj && fileObj.path || '') + ': ' + e); |
| 408 |
} |
| 409 |
}; |
| 410 |
} |
| 411 |
return text; |
| 412 |
}); |