]>
git.wh0rd.org - tt-rss.git/blob - lib/dojo/text.js.uncompressed.js
1 define("dojo/text", ["./_base/kernel", "require", "./has", "./_base/xhr"], function(dojo
, require
, has
, xhr
){
5 // This module implements the !dojo/text plugin and the dojo.cache API.
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.
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.
16 getText= function(url
, sync
, load
){
17 xhr("GET", {url
:url
, sync
:!!sync
, load
:load
});
20 // TODOC: only works for dojo AMD loader
22 getText
= require
.getText
;
24 console
.error("dojo/text plugin failed to load because loader does not support getText");
31 strip= function(text
){
32 //Strips <?xml ...?> declarations so that external SVG and XML
33 //documents can be added to a document without worry. Also, if the string
34 //is an HTML document, only the part inside the body tag is returned.
36 text
= text
.replace(/^\s*<\?xml(\s)+version=[\'\"](\d)*.(\d)*[\'\"](\s)*\?>/im, "");
37 var matches
= text
.match(/<body[^>]*>\s*([\s\S]+)\s*<\/body>/im);
53 // the dojo/text caches it's own resources because of dojo.cache
56 normalize:function(id
, toAbsMid
){
57 // id is something like (path may be relative):
59 // "path/to/text.html"
60 // "path/to/text.html!strip"
61 var parts
= id
.split("!"),
63 return (/^\./.test(url
) ? toAbsMid(url
) : url
) + (parts
[1] ? "!" + parts
[1] : "");
66 load:function(id
, require
, load
){
67 // id is something like (path is always absolute):
69 // "path/to/text.html"
70 // "path/to/text.html!strip"
73 stripFlag
= parts
.length
>1,
75 url
= require
.toUrl(parts
[0]),
77 finish = function(text
){
78 load(stripFlag
? strip(text
) : text
);
80 if(absMid
in theCache
){
81 text
= theCache
[absMid
];
82 }else if(url
in require
.cache
){
83 text
= require
.cache
[url
];
84 }else if(url
in theCache
){
89 pending
[url
].push(finish
);
91 var pendingList
= pending
[url
] = [finish
];
92 getText(url
, !require
.async
, function(text
){
93 theCache
[absMid
]= theCache
[url
]= text
;
94 for(var i
= 0; i
<pendingList
.length
;){
95 pendingList
[i
++](text
);
106 dojo
.cache= function(/*String||Object*/module
, /*String*/url
, /*String||Object?*/value
){
107 // * (string string [value]) => (module, url, value)
108 // * (object [value]) => (module, value), url defaults to ""
110 // * if module is an object, then it must be convertable to a string
111 // * (module, url) module + (url ? ("/" + url) : "") must be a legal argument to require.toUrl
112 // * value may be a string or an object; if an object then may have the properties "value" and/or "sanitize"
114 if(typeof module
=="string"){
115 if(/\//.test(module
)){
116 // module is a version 1.7+ resolved path
120 // module is a version 1.6- argument to dojo.moduleUrl
121 key
= require
.toUrl(module
.replace(/\./g, "/") + (url
? ("/" + url
) : ""));
128 val
= (value
!= undefined && typeof value
!= "string") ? value
.value
: value
,
129 sanitize
= value
&& value
.sanitize
;
131 if(typeof val
== "string"){
132 //We have a string, set cache value
134 return sanitize
? strip(val
) : val
;
135 }else if(val
=== null){
136 //Remove cached value
137 delete theCache
[key
];
140 //Allow cache values to be empty strings. If key property does
141 //not exist, fetch it.
142 if(!(key
in theCache
)){
143 getText(key
, true, function(text
){
147 return sanitize
? strip(theCache
[key
]) : theCache
[key
];
154 dojo.cache = function(module, url, value){
156 // A getter and setter for storing the string content associated with the
157 // module and url arguments.
159 // If module is a string that contains slashes, then it is interpretted as a fully
160 // resolved path (typically a result returned by require.toUrl), and url should not be
161 // provided. This is the preferred signature. If module is a string that does not
162 // contain slashes, then url must also be provided and module and url are used to
163 // call `dojo.moduleUrl()` to generate a module URL. This signature is deprecated.
164 // If value is specified, the cache value for the moduleUrl will be set to
165 // that value. Otherwise, dojo.cache will fetch the moduleUrl and store it
166 // in its internal cache and return that cached value for the URL. To clear
167 // a cache value pass null for value. Since XMLHttpRequest (XHR) is used to fetch the
168 // the URL contents, only modules on the same domain of the page can use this capability.
169 // The build system can inline the cache values though, to allow for xdomain hosting.
170 // module: String||Object
171 // If a String with slashes, a fully resolved path; if a String without slashes, the
172 // module name to use for the base part of the URL, similar to module argument
173 // to `dojo.moduleUrl`. If an Object, something that has a .toString() method that
174 // generates a valid path for the cache item. For example, a dojo._Url object.
176 // The rest of the path to append to the path derived from the module argument. If
177 // module is an object, then this second argument should be the "value" argument instead.
178 // value: String||Object?
179 // If a String, the value to use in the cache for the module/url combination.
180 // If an Object, it can have two properties: value and sanitize. The value property
181 // should be the value to use in the cache, and sanitize can be set to true or false,
182 // to indicate if XML declarations should be removed from the value and if the HTML
183 // inside a body tag in the value should be extracted as the real value. The value argument
184 // or the value property on the value argument are usually only used by the build system
185 // as it inlines cache content.
187 // To ask dojo.cache to fetch content and store it in the cache (the dojo["cache"] style
188 // of call is used to avoid an issue with the build system erroneously trying to intern
189 // this example. To get the build system to intern your dojo.cache calls, use the
190 // "dojo.cache" style of call):
191 // | //If template.html contains "<h1>Hello</h1>" that will be
192 // | //the value for the text variable.
193 // | var text = dojo["cache"]("my.module", "template.html");
195 // To ask dojo.cache to fetch content and store it in the cache, and sanitize the input
196 // (the dojo["cache"] style of call is used to avoid an issue with the build system
197 // erroneously trying to intern this example. To get the build system to intern your
198 // dojo.cache calls, use the "dojo.cache" style of call):
199 // | //If template.html contains "<html><body><h1>Hello</h1></body></html>", the
200 // | //text variable will contain just "<h1>Hello</h1>".
201 // | var text = dojo["cache"]("my.module", "template.html", {sanitize: true});
203 // Same example as previous, but demostrates how an object can be passed in as
204 // the first argument, then the value argument can then be the second argument.
205 // | //If template.html contains "<html><body><h1>Hello</h1></body></html>", the
206 // | //text variable will contain just "<h1>Hello</h1>".
207 // | var text = dojo["cache"](new dojo._Url("my/module/template.html"), {sanitize: true});