1 define("dojo/text", ["./_base/kernel", "require", "./has", "./_base/xhr"], function(dojo, require, has, xhr){
7 getText= function(url, sync, load){
8 xhr("GET", {url: url, sync:!!sync, load: load, headers: dojo.config.textPluginHeaders || {}});
11 // TODOC: only works for dojo AMD loader
13 getText= require.getText;
15 console.error("dojo/text plugin failed to load because loader does not support getText");
22 strip= function(text){
23 //Strips <?xml ...?> declarations so that external SVG and XML
24 //documents can be added to a document without worry. Also, if the string
25 //is an HTML document, only the part inside the body tag is returned.
27 text= text.replace(/^\s*<\?xml(\s)+version=[\'\"](\d)*.(\d)*[\'\"](\s)*\?>/im, "");
28 var matches= text.match(/<body[^>]*>\s*([\s\S]+)\s*<\/body>/im);
42 dojo.cache = function(/*String||Object*/module, /*String*/url, /*String||Object?*/value){
44 // A getter and setter for storing the string content associated with the
45 // module and url arguments.
47 // If module is a string that contains slashes, then it is interpretted as a fully
48 // resolved path (typically a result returned by require.toUrl), and url should not be
49 // provided. This is the preferred signature. If module is a string that does not
50 // contain slashes, then url must also be provided and module and url are used to
51 // call `dojo.moduleUrl()` to generate a module URL. This signature is deprecated.
52 // If value is specified, the cache value for the moduleUrl will be set to
53 // that value. Otherwise, dojo.cache will fetch the moduleUrl and store it
54 // in its internal cache and return that cached value for the URL. To clear
55 // a cache value pass null for value. Since XMLHttpRequest (XHR) is used to fetch the
56 // the URL contents, only modules on the same domain of the page can use this capability.
57 // The build system can inline the cache values though, to allow for xdomain hosting.
58 // module: String||Object
59 // If a String with slashes, a fully resolved path; if a String without slashes, the
60 // module name to use for the base part of the URL, similar to module argument
61 // to `dojo.moduleUrl`. If an Object, something that has a .toString() method that
62 // generates a valid path for the cache item. For example, a dojo._Url object.
64 // The rest of the path to append to the path derived from the module argument. If
65 // module is an object, then this second argument should be the "value" argument instead.
66 // value: String||Object?
67 // If a String, the value to use in the cache for the module/url combination.
68 // If an Object, it can have two properties: value and sanitize. The value property
69 // should be the value to use in the cache, and sanitize can be set to true or false,
70 // to indicate if XML declarations should be removed from the value and if the HTML
71 // inside a body tag in the value should be extracted as the real value. The value argument
72 // or the value property on the value argument are usually only used by the build system
73 // as it inlines cache content.
75 // To ask dojo.cache to fetch content and store it in the cache (the dojo["cache"] style
76 // of call is used to avoid an issue with the build system erroneously trying to intern
77 // this example. To get the build system to intern your dojo.cache calls, use the
78 // "dojo.cache" style of call):
79 // | //If template.html contains "<h1>Hello</h1>" that will be
80 // | //the value for the text variable.
81 // | var text = dojo["cache"]("my.module", "template.html");
83 // To ask dojo.cache to fetch content and store it in the cache, and sanitize the input
84 // (the dojo["cache"] style of call is used to avoid an issue with the build system
85 // erroneously trying to intern this example. To get the build system to intern your
86 // dojo.cache calls, use the "dojo.cache" style of call):
87 // | //If template.html contains "<html><body><h1>Hello</h1></body></html>", the
88 // | //text variable will contain just "<h1>Hello</h1>".
89 // | var text = dojo["cache"]("my.module", "template.html", {sanitize: true});
91 // Same example as previous, but demonstrates how an object can be passed in as
92 // the first argument, then the value argument can then be the second argument.
93 // | //If template.html contains "<html><body><h1>Hello</h1></body></html>", the
94 // | //text variable will contain just "<h1>Hello</h1>".
95 // | var text = dojo["cache"](new dojo._Url("my/module/template.html"), {sanitize: true});
97 // * (string string [value]) => (module, url, value)
98 // * (object [value]) => (module, value), url defaults to ""
100 // * if module is an object, then it must be convertable to a string
101 // * (module, url) module + (url ? ("/" + url) : "") must be a legal argument to require.toUrl
102 // * value may be a string or an object; if an object then may have the properties "value" and/or "sanitize"
104 if(typeof module=="string"){
105 if(/\//.test(module)){
106 // module is a version 1.7+ resolved path
110 // module is a version 1.6- argument to dojo.moduleUrl
111 key = require.toUrl(module.replace(/\./g, "/") + (url ? ("/" + url) : ""));
118 val = (value != undefined && typeof value != "string") ? value.value : value,
119 sanitize = value && value.sanitize;
121 if(typeof val == "string"){
122 //We have a string, set cache value
124 return sanitize ? strip(val) : val;
125 }else if(val === null){
126 //Remove cached value
127 delete theCache[key];
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){
137 return sanitize ? strip(theCache[key]) : theCache[key];
143 // This module implements the dojo/text! plugin and the dojo.cache API.
145 // We choose to include our own plugin to leverage functionality already contained in dojo
146 // and thereby reduce the size of the plugin compared to various foreign loader implementations.
147 // Also, this allows foreign AMD loaders to be used without their plugins.
149 // CAUTION: this module is designed to optionally function synchronously to support the dojo v1.x synchronous
150 // loader. This feature is outside the scope of the CommonJS plugins specification.
152 // the dojo/text caches it's own resources because of dojo.cache
155 normalize: function(id, toAbsMid){
156 // id is something like (path may be relative):
158 // "path/to/text.html"
159 // "path/to/text.html!strip"
160 var parts= id.split("!"),
162 return (/^\./.test(url) ? toAbsMid(url) : url) + (parts[1] ? "!" + parts[1] : "");
165 load: function(id, require, load){
167 // Path to the resource.
169 // Object that include the function toUrl with given id returns a valid URL from which to load the text.
171 // Callback function which will be called, when the loading finished.
173 // id is something like (path is always absolute):
175 // "path/to/text.html"
176 // "path/to/text.html!strip"
178 parts= id.split("!"),
179 stripFlag= parts.length>1,
181 url = require.toUrl(parts[0]),
182 requireCacheUrl = "url:" + url,
184 finish = function(text){
185 load(stripFlag ? strip(text) : text);
187 if(absMid in theCache){
188 text = theCache[absMid];
189 }else if(requireCacheUrl in require.cache){
190 text = require.cache[requireCacheUrl];
191 }else if(url in theCache){
192 text = theCache[url];
196 pending[url].push(finish);
198 var pendingList = pending[url] = [finish];
199 getText(url, !require.async, function(text){
200 theCache[absMid]= theCache[url]= text;
201 for(var i = 0; i<pendingList.length;){
202 pendingList[i++](text);