This repository has been archived by the owner on Oct 30, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 214
/
Copy pathassets.common.js
313 lines (256 loc) · 9.85 KB
/
assets.common.js
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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
/*
* Copyright (c) 2011-2013, Yahoo! Inc. All rights reserved.
* Copyrights licensed under the New BSD License.
* See the accompanying LICENSE file for terms.
*/
/*jslint anon:true, sloppy:true, nomen:true*/
/*global YUI,Image*/
/**
* @module ActionContextAddon
*/
YUI.add('mojito-assets-addon', function(Y, NAME) {
var isInline = function(id) {
return YUI._mojito && YUI._mojito._cache &&
YUI._mojito._cache.compiled &&
YUI._mojito._cache.compiled.css &&
YUI._mojito._cache.compiled.css.inline &&
YUI._mojito._cache.compiled.css.inline[id];
};
/**
* <strong>Access point:</strong> <em>ac.assets.*</em>
* Provides methods for adding HTML assets to a page.
* @class Assets.common
*/
function AssetsAcAddon(command, adapter, ac) {
this.assetsRoot = command.instance.assetsRoot;
this.assets = {};
this.added = {}; // content: boolean
this.mojitType = command.instance.type;
this.context = command.context;
// Add "assets" if they are found in the config.
if (command.instance && command.instance.config &&
command.instance.config.assets) {
this.addAssets(command.instance.config.assets);
}
}
AssetsAcAddon.prototype = {
namespace: 'assets',
/**
* Method for adding a CSS file to the page.
* @method addCss
* @param {string} link A URL (./local.css converts to
* /static/mojit_type/assets/local.css).
* @param {string} location Either "top" or "bottom".
*/
addCss: function(link, location) {
this.addAsset('css', (location || 'top'), link);
},
/**
* Method for adding a JS file to the page.
* @method addJs
* @param {string} link A URL (./local.css converts to
* /static/mojit_type/assets/local.css).
* @param {string} location Either "top" or "bottom".
*/
addJs: function(link, location) {
this.addAsset('js', (location || 'bottom'), link);
},
/**
* Method for adding a Blob of data to the page. This can be used
* for adding custom "script" or "style" blocks.
* @method addBlob
* @param {string} content A string of data.
* @param {string} location Either "top" or "bottom".
*/
addBlob: function(content, location) {
this.addAsset('blob', (location || 'bottom'), content);
},
/**
* @method addAsset
* @param {string} type css|js|blob
* @param {string} location Either "top" or "bottom".
* @param {string} content A string of data.
*/
addAsset: function(type, location, content) {
if (!content) {
return;
}
if (content.indexOf('./') === 0) {
content = this.getUrl(content.slice(2));
}
if (this.added[content]) {
return;
}
this.added[content] = true;
// If we have not added the files for this mojit, we should add
// them inline now.
if (('css' === type) && isInline(content)) {
// We can't do this on the server, because YUI._mojito._cache is
// a server-lifetime global, so it "tunnels" between requests.
if ('client' === this.context.runtime) {
if (!YUI._mojito._cache.compiled.css.inline.added) {
YUI._mojito._cache.compiled.css.inline.added = {};
}
if (YUI._mojito._cache.compiled.css.inline.added[content]) {
// Looks like we've already added this to the DOM
return;
}
YUI._mojito._cache.compiled.css.inline.added[
content
] = true;
}
// Y.log('Inlining css for mojitType for the first time' +
// content, 'debug' , NAME);
type = 'blob';
content = '<style type="text/css">\n' +
YUI._mojito._cache.compiled.css.inline[content] +
'</style>\n';
// Beware! "content" changes here. This is the actual CSS and
// not the URI of the CSS resource!!!
if (this.added[content]) {
return;
}
}
if (!this.assets[location]) {
this.assets[location] = {};
}
if (!this.assets[location][type]) {
this.assets[location][type] = [];
}
this.assets[location][type].push(content);
},
/**
* @method addAssets
* @param {object} assets by location (top|bottom) & type (css|js|blob)
*/
addAssets: function(assets) {
var location,
type,
content;
for (location in assets) {
if (assets.hasOwnProperty(location)) {
for (type in assets[location]) {
if (assets[location].hasOwnProperty(type)) {
for (content in assets[location][type]) {
if (assets[location][type].
hasOwnProperty(content)) {
this.addAsset(type, location,
assets[location][type][content]);
}
}
}
}
}
}
},
/**
* @method preLoadImage
* @param {string} url
* @deprecated
*/
preLoadImage: function(url) {
var img;
if (typeof document !== 'undefined') {
img = new Image();
img.src = url;
}
},
/**
* @method preLoadImages
* @param {array.<string>} url
* @deprecated
*/
preLoadImages: function(urls) {
var i;
for (i in urls) {
if (urls.hasOwnProperty(i)) {
this.preLoadImage(urls[i]);
}
}
},
/**
* @method getUrl
* @param {string} path of asset, relative
* @return {string}
*/
getUrl: function(path) {
return this.assetsRoot + '/' + path;
},
/**
* @method mixAssets
* @param {object} to
* @param {object} from
* @return {object}
*/
mixAssets: function(to, from) {
return Y.mojito.util.metaMerge(to, from);
},
/**
* @method getAssets
* @return {object} assets by location (top|bottom) & type (css|js|blob)
*/
getAssets: function() {
return this.assets;
},
/**
* @method renderLocations
* @return {object} hash table with location and the HTML fragments
* for each location with all the assets rendered.
*/
renderLocations: function () {
var fragments = {};
// Attach assets found in the "meta" to the page
Y.Object.each(this.assets, function (types, location) {
if (!fragments[location]) {
fragments[location] = '';
}
Y.Object.each(types, function (list, type) {
var i,
data = '',
url;
if ('js' === type) {
for (i = 0; i < list.length; i += 1) {
// TODO: Fuly escape any HTML chars in the URL to avoid trivial
// attribute injection attacks. See owasp-esapi reference impl.
url = encodeURI(list[i]);
data += '<script type="text/javascript" src="' +
url + '"></script>\n';
}
} else if ('css' === type) {
for (i = 0; i < list.length; i += 1) {
// TODO: Escape any HTML chars in the URL to avoid trivial
// attribute injection attacks. See owasp-esapi reference impl.
url = encodeURI(list[i]);
data += '<link rel="stylesheet" type="text/css" href="' +
url + '"/>\n';
}
} else if ('blob' === type) {
for (i = 0; i < list.length; i += 1) {
// NOTE: Giant security hole...but used by everyone who uses
// Mojito so there's not much we can do except tell authors of
// Mojito applications to _never_ use user input to generate
// blob content or populate config data. Whatever goes in here
// can't be easily encoded without the likelihood of corruption.
data += list[i] + '\n';
}
} else {
Y.log('Unknown asset type "' + type + '". Skipped.', 'warn', NAME);
}
fragments[location] += data;
});
});
return fragments;
},
/**
* @mergeMetaInto
* @private
*/
mergeMetaInto: function(meta) {
this.mixAssets(meta.assets, this.assets);
}
};
Y.namespace('mojito.addons.ac').assets = AssetsAcAddon;
}, '0.1.0', {requires: [
'mojito',
'mojito-util'
]});