Skip to content

Commit 3a534a9

Browse files
committed
Refactored opts-in-data passing, cleaner util method
1 parent 54f7653 commit 3a534a9

File tree

2 files changed

+35
-38
lines changed

2 files changed

+35
-38
lines changed

lib/ejs.js

Lines changed: 11 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -54,15 +54,8 @@ var _DEFAULT_DELIMITER = '%';
5454
var _DEFAULT_LOCALS_NAME = 'locals';
5555
var _NAME = 'ejs';
5656
var _REGEX_STRING = '(<%%|%%>|<%=|<%-|<%_|<%#|<%|%>|-%>|_%>)';
57-
var _OPTS = [ 'cache', 'filename', 'delimiter', 'scope', 'context',
58-
'debug', 'compileDebug', 'client', '_with', 'root', 'rmWhitespace',
59-
'strict', 'localsName'];
60-
var _OPTS_IN_DATA_BLACKLIST = {
61-
cache: true,
62-
filename: true,
63-
root: true,
64-
localsName: true
65-
};
57+
var _OPTS = ['delimiter', 'scope', 'context', 'debug', 'compileDebug',
58+
'client', '_with', 'rmWhitespace', 'strict'];
6659
var _BOM = /^\uFEFF/;
6760

6861
/**
@@ -260,31 +253,6 @@ function rethrow(err, str, filename, lineno){
260253
throw err;
261254
}
262255

263-
/**
264-
* Copy properties in data object that are recognized as options to an
265-
* options object.
266-
*
267-
* This is used for compatibility with earlier versions of EJS and Express.js.
268-
*
269-
* @memberof module:ejs-internal
270-
* @param {Object} data data object
271-
* @param {Options} opts options object
272-
* @static
273-
*/
274-
275-
function cpOptsInData(data, opts) {
276-
_OPTS.forEach(function (p) {
277-
if (typeof data[p] != 'undefined') {
278-
// Disallow passing potentially dangerous opts in the data
279-
// These opts should not be settable via a `render` call
280-
if (_OPTS_IN_DATA_BLACKLIST[p]) {
281-
return;
282-
}
283-
opts[p] = data[p];
284-
}
285-
});
286-
}
287-
288256
function stripSemi(str) {
289257
return str.replace(/;(\s*$)/, '$1');
290258
}
@@ -341,7 +309,7 @@ exports.render = function (template, d, o) {
341309
// No options object -- if there are optiony names
342310
// in the data, copy them to options
343311
if (arguments.length == 2) {
344-
cpOptsInData(data, opts);
312+
utils.shallowCopyFromList(opts, data, _OPTS);
345313
}
346314

347315
return handleCache(opts, template)(data);
@@ -366,21 +334,27 @@ exports.renderFile = function () {
366334
var cb = args.pop();
367335
var data = args.shift() || {};
368336
var opts = args.pop() || {};
337+
var optsKeys =_OPTS.slice();
369338
var result;
370339

371340
// Don't pollute passed in opts obj with new vals
372341
opts = utils.shallowCopy({}, opts);
373342

343+
// We don't allow 'cache' option to be passed in the data obj
344+
// for the normal `render` call, but this is where Expres puts it
345+
// so we make an exception for `renderFile`
346+
optsKeys.push('cache');
347+
374348
// No options object -- if there are optiony names
375349
// in the data, copy them to options
376350
if (arguments.length == 3) {
377351
// Express 4
378352
if (data.settings && data.settings['view options']) {
379-
cpOptsInData(data.settings['view options'], opts);
353+
utils.shallowCopyFromList(opts, data.settings['view options'], optsKeys);
380354
}
381355
// Express 3 and lower
382356
else {
383-
cpOptsInData(data, opts);
357+
utils.shallowCopyFromList(opts, data, optsKeys);
384358
}
385359
}
386360
opts.filename = filename;

lib/utils.js

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,9 @@ exports.escapeXML.toString = function () {
102102
};
103103

104104
/**
105-
* Copy all properties from one object to another, in a shallow fashion.
105+
* Naive copy of properties from one object to another.
106+
* Does not recurse into non-scalar properties
107+
* Does not check to see if the property has a value before copying
106108
*
107109
* @param {Object} to Destination object
108110
* @param {Object} from Source object
@@ -118,6 +120,27 @@ exports.shallowCopy = function (to, from) {
118120
return to;
119121
};
120122

123+
/**
124+
* Naive copy of a list of key names, from one object to another.
125+
* Only copies property if it is actually defined
126+
* Does not recurse into non-scalar properties
127+
*
128+
* @param {Object} to Destination object
129+
* @param {Object} from Source object
130+
* @param {Array} list List of properties to copy
131+
* @return {Object} Destination object
132+
* @static
133+
* @private
134+
*/
135+
exports.shallowCopyFromList = function (to, from, list) {
136+
list.forEach(function (p) {
137+
if (typeof from[p] != 'undefined') {
138+
to[p] = from[p];
139+
}
140+
});
141+
return to;
142+
}
143+
121144
/**
122145
* Simple in-process cache implementation. Does not implement limits of any
123146
* sort.

0 commit comments

Comments
 (0)