Skip to content

Commit

Permalink
Merge pull request #1390 from eoneill/customFunctionsContext
Browse files Browse the repository at this point in the history
Invoke custom functions with options.context
  • Loading branch information
xzyfer committed Mar 23, 2016
2 parents 5b2862e + 662b98c commit 09bf80c
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 2 deletions.
4 changes: 2 additions & 2 deletions lib/index.js
Expand Up @@ -343,7 +343,7 @@ module.exports.render = function(options, cb) {
bridge.success(data);
}

var result = tryCallback(cb.callback, args.concat(done));
var result = tryCallback(cb.callback.bind(options.context), args.concat(done));

if (result) {
done(result);
Expand Down Expand Up @@ -400,7 +400,7 @@ module.exports.renderSync = function(options) {
var cb = normalizeFunctionSignature(signature, functions[signature]);

options.functions[cb.signature] = function() {
return tryCallback(cb.callback, arguments);
return tryCallback(cb.callback.bind(options.context), arguments);
};
});
}
Expand Down
41 changes: 41 additions & 0 deletions test/api.js
Expand Up @@ -911,6 +911,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() {
it('four booleans', function(done) {
sass.render({
Expand Down Expand Up @@ -1646,6 +1668,25 @@ describe('api', function() {

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() {
Expand Down

0 comments on commit 09bf80c

Please sign in to comment.