text.js 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. define("dojo/text", ["./_base/kernel", "require", "./has", "./_base/xhr"], function(dojo, require, has, xhr){
  2. // module:
  3. // dojo/text
  4. // summary:
  5. // This module implements the !dojo/text plugin and the dojo.cache API.
  6. // description:
  7. // We choose to include our own plugin to leverage functionality already contained in dojo
  8. // and thereby reduce the size of the plugin compared to various foreign loader implementations.
  9. // Also, this allows foreign AMD loaders to be used without their plugins.
  10. //
  11. // CAUTION: this module is designed to optionally function synchronously to support the dojo v1.x synchronous
  12. // loader. This feature is outside the scope of the CommonJS plugins specification.
  13. var getText;
  14. if(1){
  15. getText= function(url, sync, load){
  16. xhr("GET", {url:url, sync:!!sync, load:load});
  17. };
  18. }else{
  19. // TODOC: only works for dojo AMD loader
  20. if(require.getText){
  21. getText= require.getText;
  22. }else{
  23. console.error("dojo/text plugin failed to load because loader does not support getText");
  24. }
  25. }
  26. var
  27. theCache= {},
  28. strip= function(text){
  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(text){
  33. text= text.replace(/^\s*<\?xml(\s)+version=[\'\"](\d)*.(\d)*[\'\"](\s)*\?>/im, "");
  34. var matches= text.match(/<body[^>]*>\s*([\s\S]+)\s*<\/body>/im);
  35. if(matches){
  36. text= matches[1];
  37. }
  38. }else{
  39. text = "";
  40. }
  41. return text;
  42. },
  43. notFound = {},
  44. pending = {},
  45. result= {
  46. dynamic:
  47. // the dojo/text caches it's own resources because of dojo.cache
  48. true,
  49. normalize:function(id, toAbsMid){
  50. // id is something like (path may be relative):
  51. //
  52. // "path/to/text.html"
  53. // "path/to/text.html!strip"
  54. var parts= id.split("!"),
  55. url= parts[0];
  56. return (/^\./.test(url) ? toAbsMid(url) : url) + (parts[1] ? "!" + parts[1] : "");
  57. },
  58. load:function(id, require, load){
  59. // id is something like (path is always absolute):
  60. //
  61. // "path/to/text.html"
  62. // "path/to/text.html!strip"
  63. var
  64. parts= id.split("!"),
  65. stripFlag= parts.length>1,
  66. absMid= parts[0],
  67. url = require.toUrl(parts[0]),
  68. text = notFound,
  69. finish = function(text){
  70. load(stripFlag ? strip(text) : text);
  71. };
  72. if(absMid in theCache){
  73. text = theCache[absMid];
  74. }else if(url in require.cache){
  75. text = require.cache[url];
  76. }else if(url in theCache){
  77. text = theCache[url];
  78. }
  79. if(text===notFound){
  80. if(pending[url]){
  81. pending[url].push(finish);
  82. }else{
  83. var pendingList = pending[url] = [finish];
  84. getText(url, !require.async, function(text){
  85. theCache[absMid]= theCache[url]= text;
  86. for(var i = 0; i<pendingList.length;){
  87. pendingList[i++](text);
  88. }
  89. delete pending[url];
  90. });
  91. }
  92. }else{
  93. finish(text);
  94. }
  95. }
  96. };
  97. dojo.cache= function(/*String||Object*/module, /*String*/url, /*String||Object?*/value){
  98. // * (string string [value]) => (module, url, value)
  99. // * (object [value]) => (module, value), url defaults to ""
  100. //
  101. // * if module is an object, then it must be convertable to a string
  102. // * (module, url) module + (url ? ("/" + url) : "") must be a legal argument to require.toUrl
  103. // * value may be a string or an object; if an object then may have the properties "value" and/or "sanitize"
  104. var key;
  105. if(typeof module=="string"){
  106. if(/\//.test(module)){
  107. // module is a version 1.7+ resolved path
  108. key = module;
  109. value = url;
  110. }else{
  111. // module is a version 1.6- argument to dojo.moduleUrl
  112. key = require.toUrl(module.replace(/\./g, "/") + (url ? ("/" + url) : ""));
  113. }
  114. }else{
  115. key = module + "";
  116. value = url;
  117. }
  118. var
  119. val = (value != undefined && typeof value != "string") ? value.value : value,
  120. sanitize = value && value.sanitize;
  121. if(typeof val == "string"){
  122. //We have a string, set cache value
  123. theCache[key] = val;
  124. return sanitize ? strip(val) : val;
  125. }else if(val === null){
  126. //Remove cached value
  127. delete theCache[key];
  128. return null;
  129. }else{
  130. //Allow cache values to be empty strings. If key property does
  131. //not exist, fetch it.
  132. if(!(key in theCache)){
  133. getText(key, true, function(text){
  134. theCache[key]= text;
  135. });
  136. }
  137. return sanitize ? strip(theCache[key]) : theCache[key];
  138. }
  139. };
  140. return result;
  141. /*=====
  142. dojo.cache = function(module, url, value){
  143. // summary:
  144. // A getter and setter for storing the string content associated with the
  145. // module and url arguments.
  146. // description:
  147. // If module is a string that contains slashes, then it is interpretted as a fully
  148. // resolved path (typically a result returned by require.toUrl), and url should not be
  149. // provided. This is the preferred signature. If module is a string that does not
  150. // contain slashes, then url must also be provided and module and url are used to
  151. // call `dojo.moduleUrl()` to generate a module URL. This signature is deprecated.
  152. // If value is specified, the cache value for the moduleUrl will be set to
  153. // that value. Otherwise, dojo.cache will fetch the moduleUrl and store it
  154. // in its internal cache and return that cached value for the URL. To clear
  155. // a cache value pass null for value. Since XMLHttpRequest (XHR) is used to fetch the
  156. // the URL contents, only modules on the same domain of the page can use this capability.
  157. // The build system can inline the cache values though, to allow for xdomain hosting.
  158. // module: String||Object
  159. // If a String with slashes, a fully resolved path; if a String without slashes, the
  160. // module name to use for the base part of the URL, similar to module argument
  161. // to `dojo.moduleUrl`. If an Object, something that has a .toString() method that
  162. // generates a valid path for the cache item. For example, a dojo._Url object.
  163. // url: String
  164. // The rest of the path to append to the path derived from the module argument. If
  165. // module is an object, then this second argument should be the "value" argument instead.
  166. // value: String||Object?
  167. // If a String, the value to use in the cache for the module/url combination.
  168. // If an Object, it can have two properties: value and sanitize. The value property
  169. // should be the value to use in the cache, and sanitize can be set to true or false,
  170. // to indicate if XML declarations should be removed from the value and if the HTML
  171. // inside a body tag in the value should be extracted as the real value. The value argument
  172. // or the value property on the value argument are usually only used by the build system
  173. // as it inlines cache content.
  174. // example:
  175. // To ask dojo.cache to fetch content and store it in the cache (the dojo["cache"] style
  176. // of call is used to avoid an issue with the build system erroneously trying to intern
  177. // this example. To get the build system to intern your dojo.cache calls, use the
  178. // "dojo.cache" style of call):
  179. // | //If template.html contains "<h1>Hello</h1>" that will be
  180. // | //the value for the text variable.
  181. // | var text = dojo["cache"]("my.module", "template.html");
  182. // example:
  183. // To ask dojo.cache to fetch content and store it in the cache, and sanitize the input
  184. // (the dojo["cache"] style of call is used to avoid an issue with the build system
  185. // erroneously trying to intern this example. To get the build system to intern your
  186. // dojo.cache calls, use the "dojo.cache" style of call):
  187. // | //If template.html contains "<html><body><h1>Hello</h1></body></html>", the
  188. // | //text variable will contain just "<h1>Hello</h1>".
  189. // | var text = dojo["cache"]("my.module", "template.html", {sanitize: true});
  190. // example:
  191. // Same example as previous, but demostrates how an object can be passed in as
  192. // the first argument, then the value argument can then be the second argument.
  193. // | //If template.html contains "<html><body><h1>Hello</h1></body></html>", the
  194. // | //text variable will contain just "<h1>Hello</h1>".
  195. // | var text = dojo["cache"](new dojo._Url("my/module/template.html"), {sanitize: true});
  196. return val; //String
  197. };
  198. =====*/
  199. });