diff --git a/core/server/filters.js b/core/server/filters.js index 393fdaf670..b4601603b7 100644 --- a/core/server/filters.js +++ b/core/server/filters.js @@ -58,7 +58,7 @@ Filters.prototype.deregisterFilter = function (name, priority, fn) { }; // Execute filter functions in priority order -Filters.prototype.doFilter = function (name, args) { +Filters.prototype.doFilter = function (name, args, context) { var callbacks = this.filterCallbacks[name], priorityCallbacks = []; @@ -71,13 +71,20 @@ Filters.prototype.doFilter = function (name, args) { _.times(defaults.maxPriority + 1, function (priority) { // Add a function that runs its priority level callbacks in a pipeline priorityCallbacks.push(function (currentArgs) { + var callables; + // Bug out if no handlers on this priority if (!_.isArray(callbacks[priority])) { return when.resolve(currentArgs); } + callables = _.map(callbacks[priority], function (callback) { + return function (args) { + return callback(args, context); + }; + }); // Call each handler for this priority level, allowing for promises or values - return when.pipeline(callbacks[priority], currentArgs); + return when.pipeline(callables, currentArgs); }); }); diff --git a/core/test/unit/filters_spec.js b/core/test/unit/filters_spec.js index c33ec16473..a7f2238e68 100644 --- a/core/test/unit/filters_spec.js +++ b/core/test/unit/filters_spec.js @@ -128,4 +128,26 @@ describe("Filters", function () { }); }); + it("executes filters with a context", function (done) { + var filterName = 'textContext', + testFilterHandler1 = sinon.spy(function (args, context) { + args.context1 = _.isObject(context); + return args; + }), + testFilterHandler2 = sinon.spy(function (args, context) { + args.context2 = _.isObject(context); + return args; + }); + + filters.registerFilter(filterName, 0, testFilterHandler1); + filters.registerFilter(filterName, 1, testFilterHandler2); + + filters.doFilter(filterName, { test: true }, { context: true }).then(function (newArgs) { + + newArgs.context1.should.equal(true); + newArgs.context2.should.equal(true); + done(); + }); + }); + }); \ No newline at end of file