Commit 662b98cb by Eugene ONeill

invoke custom functions with options.context

parent 11c96eee
...@@ -343,7 +343,7 @@ module.exports.render = function(options, cb) { ...@@ -343,7 +343,7 @@ module.exports.render = function(options, cb) {
bridge.success(data); bridge.success(data);
} }
var result = tryCallback(cb.callback, args.concat(done)); var result = tryCallback(cb.callback.bind(options.context), args.concat(done));
if (result) { if (result) {
done(result); done(result);
...@@ -400,7 +400,7 @@ module.exports.renderSync = function(options) { ...@@ -400,7 +400,7 @@ module.exports.renderSync = function(options) {
var cb = normalizeFunctionSignature(signature, functions[signature]); var cb = normalizeFunctionSignature(signature, functions[signature]);
options.functions[cb.signature] = function() { options.functions[cb.signature] = function() {
return tryCallback(cb.callback, arguments); return tryCallback(cb.callback.bind(options.context), arguments);
}; };
}); });
} }
......
...@@ -857,6 +857,28 @@ describe('api', function() { ...@@ -857,6 +857,28 @@ describe('api', function() {
}); });
}); });
it('should call custom functions with correct context', function(done) {
function assertExpected(result) {
assert.equal(result.css.toString().trim(), 'div {\n foo1: 1;\n foo2: 2; }');
}
var options = {
data: 'div { foo1: foo(); foo2: foo(); }',
functions: {
// foo() is stateful and will persist an incrementing counter
'foo()': function() {
assert(this);
this.fooCounter = (this.fooCounter || 0) + 1;
return new sass.types.Number(this.fooCounter);
}
}
};
sass.render(options, function(error, result) {
assertExpected(result);
done();
});
});
describe('should properly bubble up errors from sass color constructor', function() { describe('should properly bubble up errors from sass color constructor', function() {
it('four booleans', function(done) { it('four booleans', function(done) {
sass.render({ sass.render({
...@@ -1592,6 +1614,25 @@ describe('api', function() { ...@@ -1592,6 +1614,25 @@ describe('api', function() {
done(); done();
}); });
it('should call custom functions with correct context', function(done) {
function assertExpected(result) {
assert.equal(result.css.toString().trim(), 'div {\n foo1: 1;\n foo2: 2; }');
}
var options = {
data: 'div { foo1: foo(); foo2: foo(); }',
functions: {
// foo() is stateful and will persist an incrementing counter
'foo()': function() {
assert(this);
this.fooCounter = (this.fooCounter || 0) + 1;
return new sass.types.Number(this.fooCounter);
}
}
};
assertExpected(sass.renderSync(options));
done();
});
}); });
describe('.renderSync({stats: {}})', function() { describe('.renderSync({stats: {}})', function() {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment