-
Notifications
You must be signed in to change notification settings - Fork 0
/
attributes.js
408 lines (311 loc) · 13.2 KB
/
attributes.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
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
/* eslint max-len: 0 */
var assert = require('assert');
var run = require('./run');
describe('htmlprep attributes', function() {
it('replaces relative css urls with absolute urls', function(done) {
var html = '<html><head><title>title</title><link rel="stylesheet" href="css/styles.css"></head></html>';
run(html, {assetPathPrefix: '//cdnhost/abcd'}, function(err, output) {
if (err) return done(err);
assert.equal(output, '<html><head><title>title</title><link rel="stylesheet" href="//cdnhost/abcd/css/styles.css"/></head></html>');
done();
});
});
it('replaces relative css urls with absolute urls', function(done) {
var html = '<html><body><script src="js/app.js"></script><img src="images/logo.jpg"></html>';
run(html, {assetPathPrefix: '//cdnhost/abc'}, function(err, output) {
if (err) return done(err);
assert.equal(output, '<html><body><script src="//cdnhost/abc/js/app.js"></script><img src="//cdnhost/abc/images/logo.jpg"/></body></html>');
done();
});
});
it('performs attribute processing on tags with build attribute', function(done) {
// var html = '<html><link rel="stylesheet" data-build="debug" href="css/styles.css"></html>';
var html = '<head>' +
'<link data-build="release" rel="stylesheet" href="css/components.min.css">' +
'<link data-build="release" rel="stylesheet" href="css/app.min.css">' +
'<link data-build="debug" rel="stylesheet" href="bower_components/bootstrap.css">' +
'<link data-build="debug" rel="stylesheet" href="tmp/styles.css"></head>';
var options = {
assetPathPrefix: '//cdn.com',
buildType: 'debug'
};
run(html, options, function(err, output) {
if (err) return done(err);
// assert.equal(output, '<html><link rel="stylesheet" href="//cdn.com/css/styles.css"></html>');
assert.equal(output, '<head><link rel="stylesheet" href="//cdn.com/bower_components/bootstrap.css"/><link rel="stylesheet" href="//cdn.com/tmp/styles.css"/></head>');
done();
});
});
it('preserves empty attributes', function(done) {
var html = '<html><div ng-show></div></html>';
run(html, function(err, output) {
if (err) return done(err);
assert.equal(output, html);
done();
});
});
it('does not rewrite URLs of images with data-src-keep attribute', function(done) {
var html = '<html><img src="/media/logo.png" data-src-keep/></html>';
run(html, {assetPathPrefix: 'cdnhost.com'}, function(err, output) {
if (err) return done(err);
assert.equal(output, html);
done();
});
});
it('does not prepend path to embedded images', function(done) {
var html = '<html><img src="data:image/png;base64,VBORw0KGgoAAAANSUhEUgAAAAoAAAAKCAYAAAC"/></html>';
run(html, {assetPathPrefix: 'cdnhost.com'}, function(err, output) {
if (err) return done(err);
assert.equal(output, html);
done();
});
});
it('script tag with no src attribute', function(done) {
var html = '<html><script>function() {}</script></html>';
run(html, {assetPathPrefix: 'cdnhost.com'}, function(err, output) {
if (err) return done(err);
assert.equal(output, html);
done();
});
});
it('strips out tags with data-strip attribute', function(done) {
var html = '<html><head><script data-strip>var __global={};</script></head></html>';
run(html, function(err, output) {
if (err) return done(err);
assert.equal(output, '<html><head></head></html>');
done();
});
});
it('appends nested paths to relative paths', function(done) {
var html = '<html><img src="../images/summer.png"></html>';
run(html, {assetPathPrefix: '//cdnhost.com/site123/v1', pathFromRoot: 'blog'}, function(err, output) {
if (err) return done(err);
assert.equal(output, '<html><img src="//cdnhost.com/site123/v1/blog/../images/summer.png"/></html>');
done();
});
});
it('appends pathFromRoot to same directory relative paths', function(done) {
var html = '<html><img src="images/summer.png"></html>';
run(html, {assetPathPrefix: '//cdnhost.com/site123/v1', pathFromRoot: 'blog'}, function(err, output) {
if (err) return done(err);
assert.equal(output, '<html><img src="//cdnhost.com/site123/v1/blog/images/summer.png"/></html>');
done();
});
});
it('tacks on fingerprint to src attributes with data-fingerprint', function(done) {
var html = '<html><img data-fingerprint src="images/summer.png"></html>';
run(html, {fingerprint: '123'}, function(err, output) {
if (err) return done(err);
assert.equal(output, '<html><img src="images/summer.png?__fp=123"/></html>');
done();
});
});
it('preserves quotes in attributes', function(done) {
var html = '<body class="index" data-languages="[\'angular\',\'template\']"></body>';
run(html, {}, function(err, output) {
if (err) return done(err);
assert.equal(output, html);
done();
});
});
it('preserves entity escaped chars in attributes', function(done) {
var html = '<body data-languages="{"test":"1"}"></body>';
run(html, {}, function(err, output) {
if (err) return done(err);
assert.equal(output, html);
done();
});
});
it('fixes extraneous leading slash', function(done) {
var html = '<body><script src="//js/script.js"></script></body>';
run(html, {assetPathPrefix: '//cdnhost.com'}, function(err, output) {
if (err) return done(err);
assert.equal(output, '<body><script src="//cdnhost.com/js/script.js"></script></body>');
done();
});
});
it('leaves absolute url attributes alone', function(done) {
var html = '<body><link href="https://fonts.google.com/arial.css" rel="stylesheet"/></body>';
run(html, {assetPathPrefix: '//cdnhost.com'}, function(err, output) {
if (err) return done(err);
assert.equal(output, html);
done();
});
});
it('leaves valid scheme-less url alone', function(done) {
var html = '<body><link href="//fonts.google.com/arial.css" rel="stylesheet"/></body>';
run(html, {assetPathPrefix: '//cdnhost.com'}, function(err, output) {
if (err) return done(err);
assert.equal(output, html);
done();
});
});
it('strips leading slashes from anchor href attributes', function(done) {
var html = '<body><a href="//somesite.com">ok</a><a href="//posts/about">about</a></body>';
run(html, {assetPathPrefix: '//cdnhost.com'}, function(err, output) {
if (err) return done(err);
assert.equal(output, '<body><a href="//somesite.com">ok</a><a href="/posts/about">about</a></body>');
done();
});
});
it('replaces site base url placeholder with cdn url', function(done) {
var html = '<body><script src="https://__baseurl__/js/site.js"></script></body>';
var opts = {
assetPathPrefix: '//cdnhost.com',
baseUrlPlaceholder: 'https://__baseurl__'
};
run(html, opts, function(err, output) {
if (err) return done(err);
assert.equal(output, '<body><script src="//cdnhost.com/js/site.js"></script></body>');
done();
});
});
it('substitutes base url placeholder with base url', function(done) {
var html = '<body><a href="https://__baseurl__/post/about">about</a></body>';
var opts = {
baseUrlPlaceholder: 'https://__baseurl__',
baseUrl: 'https://mysite.com'
};
run(html, opts, function(err, output) {
if (err) return done(err);
assert.equal(output, '<body><a href="https://mysite.com/post/about">about</a></body>');
done();
});
});
it('handles double slash when subsituting base url', function(done) {
var html = '<body><a href="https://__baseurl__//post/about">about</a></body>';
var opts = {
baseUrlPlaceholder: 'https://__baseurl__',
baseUrl: 'https://mysite.com'
};
run(html, opts, function(err, output) {
if (err) return done(err);
assert.equal(output, '<body><a href="https://mysite.com/post/about">about</a></body>');
done();
});
});
it('updates inline background image urls', function(done) {
var html = '<body><div style="background-image:url(https://__baseurl__/img/bg.jpg)"></div></body>';
var opts = {
baseUrlPlaceholder: 'https://__baseurl__',
baseUrl: 'https://mysite.com',
assetPathPrefix: '//cdnhost.com'
};
run(html, opts, function(err, output) {
if (err) return done(err);
assert.equal(output, '<body><div style="background-image:url(//cdnhost.com/img/bg.jpg)"></div></body>');
done();
});
});
it('updates baseurl embedded in querystrings', function(done) {
var html = '<body><a href="http://www.twitter.com/share?url=https%3a%2f%2f__baseurl__%2fabout%2f" target="_blank">share</a></body>';
var opts = {
baseUrlPlaceholder: 'https://__baseurl__',
baseUrl: 'https://mysite.com'
};
run(html, opts, function(err, output) {
if (err) return done(err);
assert.equal(output, '<body><a href="http://www.twitter.com/share?url=https%3a%2f%2fmysite.com%2fabout%2f" target="_blank">share</a></body>');
done();
});
});
it('updates rss link attributes', function(done) {
var html = '<html><link href="https://__baseurl__/index.xml" rel="alternate" type="application/rss+xml" title="RSS feed"/></html>';
var opts = {
baseUrlPlaceholder: 'https://__baseurl__',
baseUrl: 'https://mysite.com'
};
run(html, opts, function(err, output) {
if (err) return done(err);
assert.equal(output, '<html><link href="https://mysite.com/index.xml" rel="alternate" type="application/rss+xml" title="RSS feed"/></html>');
done();
});
});
it('does not prepend assets paths based on noAssetPathPrefixes', function(done) {
var html = '<html><div style="background-image:url(/img/bg.jpg)"></div><img src="logo.jpg"/></html>';
var opts = {
noPathPrefixPatterns: ['/img/*.jpg'],
assetPathPrefix: '//cdn.net/'
};
run(html, opts, function(err, output) {
if (err) return done(err);
assert.equal(output, '<html><div style="background-image:url(/img/bg.jpg)"></div><img src="//cdn.net/logo.jpg"/></html>');
done();
});
});
it('noAssetPathPrefixes handles non-leading slash', function(done) {
var html = '<html><img src="img/bg.jpg"/></html>';
var opts = {
noPathPrefixPatterns: ['/img/*.jpg'],
assetPathPrefix: '//cdn.net/'
};
run(html, opts, function(err, output) {
if (err) return done(err);
assert.equal(output, html);
done();
});
});
it('trims leading whitespace from attributes', function(done) {
var html = '<a href=" https://__baseurl__" class="btn btn-default"><i class="fa fa-home"></i> Home</a>';
var opts = {
baseUrlPlaceholder: 'https://__baseurl__',
baseUrl: 'https://domain.net'
};
run(html, opts, function(err, output) {
if (err) return done(err);
assert.equal(output, '<a href="https://domain.net/" class="btn btn-default"><i class="fa fa-home"></i> Home</a>');
done();
});
});
it('it update open graph urls', function(done) {
var html = '<head><meta property="og:url" content="https://__baseurl__/path"></head>';
var opts = {
baseUrlPlaceholder: 'https://__baseurl__',
baseUrl: 'https://domain.net'
};
run(html, opts, function(err, output) {
if (err) return done(err);
assert.equal(output, '<head><meta property="og:url" content="https://domain.net/path"/></head>');
done();
});
});
it('it update open graph urls', function(done) {
var html = '<head><meta name="twitter:image" content="https://__baseurl__/assets/logo.png"/></head>';
var opts = {
baseUrlPlaceholder: 'https://__baseurl__',
baseUrl: 'https://domain.net'
};
run(html, opts, function(err, output) {
if (err) return done(err);
assert.equal(output, '<head><meta name="twitter:image" content="https://domain.net/assets/logo.png"/></head>');
done();
});
});
it('updates baseurl in social sharing links', function(done) {
var html = '<a href="//twitter.com/share?text=share&url=https://__baseurl__/share&via=website">twitter</a>';
var opts = {
baseUrlPlaceholder: 'https://__baseurl__',
baseUrl: 'https://domain.net'
};
run(html, opts, function(err, output) {
if (err) return done(err);
assert.equal(output, '<a href="//twitter.com/share?text=share&url=https://domain.net/share&via=website">twitter</a>');
done();
});
});
it('updates baseurl in onclick attributes', function(done) {
var html = '<a href="//www.reddit.com/submit" onclick="window.location=\'//www.reddit.com/submit?url=\'' +
'encodeURIComponent(\'https://__baseurl__/share\') + \'&title=share\'; return false">reddit</a>';
var opts = {
baseUrlPlaceholder: 'https://__baseurl__',
baseUrl: 'https://domain.net'
};
run(html, opts, function(err, output) {
if (err) return done(err);
var expected = '<a href="//www.reddit.com/submit" onclick="window.location=\'//www.reddit.com/submit?url=\'' +
'encodeURIComponent(\'https://domain.net/share\') + \'&title=share\'; return false">reddit</a>';
assert.equal(expected, output);
done();
});
});
});