| 1 |
if (typeof define !== 'function') { var define = (require('../../../amdefine'))(module); } |
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
(function () { |
| 14 |
var 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 |
|
| 23 |
define(function () { |
| 24 |
var text, get, fs; |
| 25 |
|
| 26 |
if (typeof window !== "undefined" && window.navigator && window.document) { |
| 27 |
get = function (url, callback) { |
| 28 |
var xhr = text.createXhr(); |
| 29 |
xhr.open('GET', url, true); |
| 30 |
xhr.onreadystatechange = function (evt) { |
| 31 |
|
| 32 |
|
| 33 |
if (xhr.readyState === 4) { |
| 34 |
callback(xhr.responseText); |
| 35 |
} |
| 36 |
}; |
| 37 |
xhr.send(null); |
| 38 |
}; |
| 39 |
} else if (typeof process !== "undefined" && |
| 40 |
process.versions && |
| 41 |
!!process.versions.node) { |
| 42 |
|
| 43 |
fs = (require.nodeRequire || require)('fs'); |
| 44 |
|
| 45 |
get = function (url, callback) { |
| 46 |
callback(fs.readFileSync(url, 'utf8')); |
| 47 |
}; |
| 48 |
} else if (typeof Packages !== 'undefined') { |
| 49 |
|
| 50 |
get = function (url, callback) { |
| 51 |
var encoding = "utf-8", |
| 52 |
file = new java.io.File(url), |
| 53 |
lineSeparator = java.lang.System.getProperty("line.separator"), |
| 54 |
input = new java.io.BufferedReader(new java.io.InputStreamReader(new java.io.FileInputStream(file), encoding)), |
| 55 |
stringBuffer, line, |
| 56 |
content = ''; |
| 57 |
try { |
| 58 |
stringBuffer = new java.lang.StringBuffer(); |
| 59 |
line = input.readLine(); |
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
if (line && line.length() && line.charAt(0) === 0xfeff) { |
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
line = line.substring(1); |
| 71 |
} |
| 72 |
|
| 73 |
stringBuffer.append(line); |
| 74 |
|
| 75 |
while ((line = input.readLine()) !== null) { |
| 76 |
stringBuffer.append(lineSeparator); |
| 77 |
stringBuffer.append(line); |
| 78 |
} |
| 79 |
|
| 80 |
content = String(stringBuffer.toString()); |
| 81 |
} finally { |
| 82 |
input.close(); |
| 83 |
} |
| 84 |
callback(content); |
| 85 |
}; |
| 86 |
} |
| 87 |
|
| 88 |
text = { |
| 89 |
version: '1.0.2', |
| 90 |
|
| 91 |
strip: function (content) { |
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
if (content) { |
| 96 |
content = content.replace(xmlRegExp, ""); |
| 97 |
var matches = content.match(bodyRegExp); |
| 98 |
if (matches) { |
| 99 |
content = matches[1]; |
| 100 |
} |
| 101 |
} else { |
| 102 |
content = ""; |
| 103 |
} |
| 104 |
return content; |
| 105 |
}, |
| 106 |
|
| 107 |
jsEscape: function (content) { |
| 108 |
return content.replace(/(['\\])/g, '\\$1') |
| 109 |
.replace(/[\f]/g, "\\f") |
| 110 |
.replace(/[\b]/g, "\\b") |
| 111 |
.replace(/[\n]/g, "\\n") |
| 112 |
.replace(/[\t]/g, "\\t") |
| 113 |
.replace(/[\r]/g, "\\r"); |
| 114 |
}, |
| 115 |
|
| 116 |
createXhr: function () { |
| 117 |
|
| 118 |
var xhr, i, progId; |
| 119 |
if (typeof XMLHttpRequest !== "undefined") { |
| 120 |
return new XMLHttpRequest(); |
| 121 |
} else { |
| 122 |
for (i = 0; i < 3; i++) { |
| 123 |
progId = progIds[i]; |
| 124 |
try { |
| 125 |
xhr = new ActiveXObject(progId); |
| 126 |
} catch (e) {} |
| 127 |
|
| 128 |
if (xhr) { |
| 129 |
progIds = [progId]; |
| 130 |
break; |
| 131 |
} |
| 132 |
} |
| 133 |
} |
| 134 |
|
| 135 |
if (!xhr) { |
| 136 |
throw new Error("createXhr(): XMLHttpRequest not available"); |
| 137 |
} |
| 138 |
|
| 139 |
return xhr; |
| 140 |
}, |
| 141 |
|
| 142 |
get: get, |
| 143 |
|
| 144 |
|
| 145 |
|
| 146 |
|
| 147 |
|
| 148 |
|
| 149 |
|
| 150 |
|
| 151 |
|
| 152 |
parseName: function (name) { |
| 153 |
var strip = false, index = name.indexOf("."), |
| 154 |
modName = name.substring(0, index), |
| 155 |
ext = name.substring(index + 1, name.length); |
| 156 |
|
| 157 |
index = ext.indexOf("!"); |
| 158 |
if (index !== -1) { |
| 159 |
|
| 160 |
strip = ext.substring(index + 1, ext.length); |
| 161 |
strip = strip === "strip"; |
| 162 |
ext = ext.substring(0, index); |
| 163 |
} |
| 164 |
|
| 165 |
return { |
| 166 |
moduleName: modName, |
| 167 |
ext: ext, |
| 168 |
strip: strip |
| 169 |
}; |
| 170 |
}, |
| 171 |
|
| 172 |
xdRegExp: /^((\w+)\:)?\/\/([^\/\\]+)/, |
| 173 |
|
| 174 |
|
| 175 |
|
| 176 |
|
| 177 |
|
| 178 |
|
| 179 |
|
| 180 |
|
| 181 |
|
| 182 |
useXhr: function (url, protocol, hostname, port) { |
| 183 |
var match = text.xdRegExp.exec(url), |
| 184 |
uProtocol, uHostName, uPort; |
| 185 |
if (!match) { |
| 186 |
return true; |
| 187 |
} |
| 188 |
uProtocol = match[2]; |
| 189 |
uHostName = match[3]; |
| 190 |
|
| 191 |
uHostName = uHostName.split(':'); |
| 192 |
uPort = uHostName[1]; |
| 193 |
uHostName = uHostName[0]; |
| 194 |
|
| 195 |
return (!uProtocol || uProtocol === protocol) && |
| 196 |
(!uHostName || uHostName === hostname) && |
| 197 |
((!uPort && !uHostName) || uPort === port); |
| 198 |
}, |
| 199 |
|
| 200 |
finishLoad: function (name, strip, content, onLoad, config) { |
| 201 |
content = strip ? text.strip(content) : content; |
| 202 |
if (config.isBuild) { |
| 203 |
buildMap[name] = content; |
| 204 |
} |
| 205 |
onLoad(content); |
| 206 |
}, |
| 207 |
|
| 208 |
load: function (name, req, onLoad, config) { |
| 209 |
|
| 210 |
|
| 211 |
|
| 212 |
|
| 213 |
|
| 214 |
|
| 215 |
|
| 216 |
|
| 217 |
|
| 218 |
if (config.isBuild && !config.inlineText) { |
| 219 |
onLoad(); |
| 220 |
return; |
| 221 |
} |
| 222 |
|
| 223 |
var parsed = text.parseName(name), |
| 224 |
nonStripName = parsed.moduleName + '.' + parsed.ext, |
| 225 |
url = req.toUrl(nonStripName), |
| 226 |
useXhr = (config && config.text && config.text.useXhr) || |
| 227 |
text.useXhr; |
| 228 |
|
| 229 |
|
| 230 |
if (!hasLocation || useXhr(url, defaultProtocol, defaultHostName, defaultPort)) { |
| 231 |
text.get(url, function (content) { |
| 232 |
text.finishLoad(name, parsed.strip, content, onLoad, config); |
| 233 |
}); |
| 234 |
} else { |
| 235 |
|
| 236 |
|
| 237 |
|
| 238 |
|
| 239 |
req([nonStripName], function (content) { |
| 240 |
text.finishLoad(parsed.moduleName + '.' + parsed.ext, |
| 241 |
parsed.strip, content, onLoad, config); |
| 242 |
}); |
| 243 |
} |
| 244 |
}, |
| 245 |
|
| 246 |
write: function (pluginName, moduleName, write, config) { |
| 247 |
if (moduleName in buildMap) { |
| 248 |
var content = text.jsEscape(buildMap[moduleName]); |
| 249 |
write.asModule(pluginName + "!" + moduleName, |
| 250 |
"define(function () { return '" + |
| 251 |
content + |
| 252 |
"';});\n"); |
| 253 |
} |
| 254 |
}, |
| 255 |
|
| 256 |
writeFile: function (pluginName, moduleName, req, write, config) { |
| 257 |
var parsed = text.parseName(moduleName), |
| 258 |
nonStripName = parsed.moduleName + '.' + parsed.ext, |
| 259 |
|
| 260 |
|
| 261 |
fileName = req.toUrl(parsed.moduleName + '.' + |
| 262 |
parsed.ext) + '.js'; |
| 263 |
|
| 264 |
|
| 265 |
|
| 266 |
|
| 267 |
text.load(nonStripName, req, function (value) { |
| 268 |
|
| 269 |
|
| 270 |
|
| 271 |
var textWrite = function (contents) { |
| 272 |
return write(fileName, contents); |
| 273 |
}; |
| 274 |
textWrite.asModule = function (moduleName, contents) { |
| 275 |
return write.asModule(moduleName, fileName, contents); |
| 276 |
}; |
| 277 |
|
| 278 |
text.write(pluginName, nonStripName, textWrite, config); |
| 279 |
}, config); |
| 280 |
} |
| 281 |
}; |
| 282 |
|
| 283 |
return text; |
| 284 |
}); |
| 285 |
}()); |