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