summaryrefslogtreecommitdiff
path: root/lib/dojo/lib/plugins/text.js
blob: 75894a32a324c5c1fb67df3cef17024e25e4d5fd (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
/*
	Copyright (c) 2004-2011, The Dojo Foundation All Rights Reserved.
	Available via Academic Free License >= 2.1 OR the modified BSD license.
	see: http://dojotoolkit.org/license for details
*/


//
// dojo text! plugin
//
// We choose to include our own plugin in hopes of leveraging functionality already contained in dojo
// and thereby reducing the size of the plugin compared to various loader implementations. Naturally, this
// allows AMD loaders to be used without their plugins.

// CAUTION, this module may return improper results if the AMD loader does not support toAbsMid and client
// code passes relative plugin resource module ids. In that case, you should consider using the text! plugin
// that comes with your loader.

define(["dojo", "dojo/cache"], function(dojo){
	var
		cached= {},

		cache= function(cacheId, url, value){
			cached[cacheId]= value;
			dojo.cache({toString:function(){return url;}}, value);
		},

		strip= function(text){
			//note: this function courtesy of James Burke (https://github.com/jrburke/requirejs)
			//Strips <?xml ...?> declarations so that external SVG and XML
			//documents can be added to a document without worry. Also, if the string
			//is an HTML document, only the part inside the body tag is returned.
			if(text){
				text= text.replace(/^\s*<\?xml(\s)+version=[\'\"](\d)*.(\d)*[\'\"](\s)*\?>/im, "");
				var matches= text.match(/<body[^>]*>\s*([\s\S]+)\s*<\/body>/im);
				if(matches){
					text= matches[1];
				}
			}else{
				text = "";
			}
			return text;
		};

	return {
		load:function(id, require, load){
			var match, cacheId, url, parts= id.split("!");
			if(require.toAbsMid){
				match= parts[0].match(/(.+)(\.[^\/]*)$/);
				cacheId= match ? require.toAbsMid(match[1]) + match[2] : require.toAbsMid(parts[0]);
				if(cacheId in cached){
					load(parts[1]=="strip" ? strip(cached[cacheId]) : cached[cacheId]);
					return;
				}
			}
			url= require.toUrl(parts[0]);
			dojo.xhrGet({
				url:url,
				load:function(text){
					cacheId && cache(cacheId, url, text);
					load(parts[1]=="strip" ? strip(text) : text);
				}
			});
		},

		cache:function(cacheId, mid, type, value) {
			cache(cacheId, require.nameToUrl(mid) + type, value);
		}
	};
});