Permalink
Browse files

hotfix for chaining filters when the first takes a variable as an arg…

…ument
  • Loading branch information...
1 parent 40d18c8 commit 0a995d72f31a379b6c7ee56c2ae42b93fc97921c @paularmstrong committed Oct 3, 2011
Showing with 15 additions and 5 deletions.
  1. +14 −4 lib/helpers.js
  2. +1 −1 tests/filters.test.js
View
@@ -188,17 +188,27 @@ exports.wrapFilter = function (variable, filter) {
if (filters.hasOwnProperty(filter.name)) {
args = [variable];
_.each(filter.args, function (value, key) {
+ var varname;
+ try {
+ varname = '__' + parser.parseVariable(value).name.replace(/\W/g, '_');
+ } catch (e) {
+ args.push(value);
+ return;
+ }
+
if (exports.isValidName(value)) {
- output += exports.setVar('__' + parser.parseVariable(value).name.replace(/\W/g, '_'), parser.parseVariable(value));
- args.push('__' + parser.parseVariable(value).name.replace(/\W/g, '_'));
+ output += exports.setVar(varname, parser.parseVariable(value));
+ args.push(varname);
} else if (_.isArray(value) || typeof value === 'object') {
- args.push(value);
+ args.push(value.replace(/\"/g, ''));
} else {
value += '';
args.push(('\'' + value.replace(/(^\"|\"$)/g, '') + '\''));
}
});
- output += 'return __filters.' + filter.name + '.apply(this, ' + JSON.stringify(args).replace(/\"/g, '').replace(/\\'/g, '') + ')';
+
+ args = JSON.stringify(args).replace(/\"/g, '').replace(/\\/g, '"').replace(/""(\w)/g, '\\\\$1');
+ output += 'return __filters.' + filter.name + '.apply(this, ' + args + ')';
}
return output + '})()';
View
@@ -14,7 +14,7 @@ exports.add = function (test) {
testFilter(test, 'add(2)', { v: 'foo' }, 'foo2', 'string var turns addend into a string');
testFilter(test, 'add("bar")', { v: 'foo' }, 'foobar', 'strings concatenated');
testFilter(test, 'add({ bar: 2, baz: 3 })|join(",")', { v: { foo: 1 }}, '1,2,3', 'add objects from object literal');
- // testFilter(test, 'add(b)|join(",")', { v: { foo: 1 }, b: { bar: 2 }}, '1,2', 'add objects from var');
+ testFilter(test, 'add(b)|join(",")', { v: { foo: 1 }, b: { bar: 2 }}, '1,2', 'add objects from var');
testFilter(test, 'add(b)', { v: 'foo', b: [1, 2] }, 'foo1,2', 'add array to string');
test.done();
};

0 comments on commit 0a995d7

Please sign in to comment.