Skip to content

Commit 05c103e

Browse files
author
Mick Ryan
committed
Merge pull request #71 from mickr/modify-cdnify-for-webdav
BIG-24959 - Cleaned up the webdav helper a little
2 parents 8793927 + 41f21c6 commit 05c103e

2 files changed

Lines changed: 45 additions & 13 deletions

File tree

index.js

Lines changed: 23 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ var _ = require('lodash'),
1212
Helpers = [];
1313

1414
// Load Helpers
15-
Fs.readdirSync(Path.join(__dirname, 'helpers')).forEach(function(file) {
15+
Fs.readdirSync(Path.join(__dirname, 'helpers')).forEach(function (file) {
1616
Helpers.push(require('./helpers/' + file));
1717
});
1818

@@ -33,7 +33,7 @@ module.exports = function (assembler) {
3333
self.helpers = [];
3434
self.decorators = [];
3535

36-
_.each(Helpers, function(Helper) {
36+
_.each(Helpers, function (Helper) {
3737
self.helpers.push(new Helper(self.handlebars));
3838
});
3939

@@ -57,10 +57,10 @@ module.exports = function (assembler) {
5757
* @param {Object} templates
5858
* @param {Function} callback
5959
*/
60-
self.loadTemplates = function(path, callback) {
60+
self.loadTemplates = function (path, callback) {
6161
var processor = self.getTemplateProcessor();
6262

63-
assembler.getTemplates(path, processor, function(error, templates) {
63+
assembler.getTemplates(path, processor, function (error, templates) {
6464
if (error) {
6565
return callback(error);
6666
}
@@ -77,7 +77,7 @@ module.exports = function (assembler) {
7777
});
7878
};
7979

80-
self.getTemplateProcessor = function() {
80+
self.getTemplateProcessor = function () {
8181
return function (templates) {
8282
var precompiledTemplates = {};
8383

@@ -94,7 +94,7 @@ module.exports = function (assembler) {
9494
* @param {Object} templates
9595
* @return {Object}
9696
*/
97-
self.loadTemplatesSync = function(templates) {
97+
self.loadTemplatesSync = function (templates) {
9898
_.each(templates, function (content, fileName) {
9999
self.handlebars.templates[fileName] = self.handlebars.compile(content, self.options);
100100
});
@@ -113,7 +113,7 @@ module.exports = function (assembler) {
113113
return callback(error);
114114
}
115115
// Make translations available to the helpers
116-
self.translate = Localizer(acceptLanguage, translations);
116+
self.translate = Localizer(acceptLanguage, translations);
117117

118118
callback();
119119
});
@@ -129,6 +129,7 @@ module.exports = function (assembler) {
129129
var cdnUrl = settings['cdn_url'] || '';
130130
var versionId = settings['theme_version_id'];
131131
var configId = settings['theme_config_id'];
132+
var protocolMatch = /(.*!?:)/;
132133

133134
if (!path) {
134135
return '';
@@ -138,14 +139,23 @@ module.exports = function (assembler) {
138139
return path;
139140
}
140141

141-
if (path.substr(0, 7) === 'webdav:') {
142-
path = path.slice(7, path.length);
142+
if (protocolMatch.test(path)) {
143+
var match = path.match(protocolMatch);
144+
path = path.slice(match[0].length, path.length);
143145

144146
if (path[0] === '/') {
145147
path = path.slice(1, path.length);
146148
}
147149

148-
return [cdnUrl, 'content', path].join('/');
150+
if (match[0] === 'webdav:') {
151+
return [cdnUrl, 'content', path].join('/');
152+
}
153+
154+
if (path[0] !== '/') {
155+
path = '/' + path;
156+
}
157+
158+
return path;
149159
}
150160

151161
if (path[0] !== '/') {
@@ -168,7 +178,7 @@ module.exports = function (assembler) {
168178
*/
169179
self.addDecorator = function (decorator) {
170180
self.decorators.push(decorator);
171-
}
181+
};
172182

173183
/**
174184
* @param {String} path
@@ -178,7 +188,7 @@ module.exports = function (assembler) {
178188
self.render = function (path, context) {
179189
var output;
180190

181-
_.each(self.helpers, function(helper) {
191+
_.each(self.helpers, function (helper) {
182192
helper.register(context, self);
183193
});
184194

@@ -192,4 +202,4 @@ module.exports = function (assembler) {
192202

193203
return output;
194204
};
195-
}
205+
};

test/helpers/cdn.js

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,4 +81,26 @@ describe('cdn helper', function() {
8181

8282
done();
8383
});
84+
85+
it('should not return a webDav asset if webdav protocol is not correct', function(done) {
86+
87+
expect(c('{{cdn "webbav:img/image.jpg"}}', context))
88+
.to.be.equal('/img/image.jpg');
89+
90+
expect(c('{{cdn "webbav:/img/image.jpg"}}', context))
91+
.to.be.equal('/img/image.jpg');
92+
93+
done();
94+
});
95+
96+
it('should return basic asset URL if protocol is not correct', function(done) {
97+
98+
expect(c('{{cdn "randomProtocol::img/image.jpg"}}', context))
99+
.to.be.equal('/img/image.jpg');
100+
101+
expect(c('{{cdn "randomProtocol:/img/image.jpg"}}', context))
102+
.to.be.equal('/img/image.jpg');
103+
104+
done();
105+
});
84106
});

0 commit comments

Comments
 (0)