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