Commit 574d19b5 by Brett Wilkins

Adding renderSync function, updating tests and docs

parent 4de3ba00
......@@ -17,9 +17,11 @@ Find it on npm: <https://npmjs.org/package/node-sass>
```javascript
var sass = require('node-sass');
sass.render(scss_content, callback [, options]);
// OR
var css = sass.renderSync(scss_content);
```
Especially, the options argument is optional. It support two attribute: `includePaths` and `outputStyle`, both of them are optional.
Especially, the options argument is optional. It support two attributes: `includePaths` and `outputStyle`, both of which are optional.
`includePaths` is an `Array`, you can add a sass import path.
......@@ -33,6 +35,8 @@ var sass = require('node-sass');
sass.render('body{background:blue; a{color:black;}}', function(err, css){
console.log(css)
}/*, { includePaths: [ 'lib/', 'mod/' ], outputStyle: 'compressed' }*/);
// OR
console.log(sass.renderSync('body{background:blue; a{color:black;}}'));
```
## Connect/Express middleware
......
......@@ -69,11 +69,36 @@ Handle<Value> Render(const Arguments& args) {
int status = uv_queue_work(uv_default_loop(), &ctx_w->request, WorkOnContext, (uv_after_work_cb)MakeCallback);
assert(status == 0);
return Undefined();
return scope.Close(Undefined());
}
Handle<Value> RenderSync(const Arguments& args) {
HandleScope scope;
TryCatch try_catch;
sass_context* ctx = sass_new_context();
char *source;
String::AsciiValue astr(args[0]);
String::AsciiValue bstr(args[1]);
source = new char[strlen(*astr)+1];
strcpy(source, *astr);
ctx->source_string = source;
ctx->options.include_paths = new char[strlen(*bstr)+1];
strcpy(ctx->options.include_paths, *bstr);
ctx->options.output_style = args[2]->Int32Value();
sass_compile(ctx);
if (ctx->error_status == 0) {
return scope.Close(Local<Value>::New(String::New(ctx->output_string)));
} else {
ThrowException(Exception::Error(String::New("Input does not appear to be valid SCSS.")));
}
return scope.Close(Undefined());
}
void RegisterModule(v8::Handle<v8::Object> target) {
NODE_SET_METHOD(target, "render", Render);
NODE_SET_METHOD(target, "renderSync", RenderSync);
}
NODE_MODULE(binding, RegisterModule);
This diff was suppressed by a .gitattributes entry.
......@@ -29,4 +29,12 @@ exports.render = function(css, callback, options) {
return binding.render(css, callback, paths.join(':'), style);
};
exports.renderSync = function(css, options) {
var paths, style;
options = typeof options !== 'object' ? {} : options;
paths = options.include_paths || options.includePaths || [];
style = SASS_OUTPUT_STYLE[options.output_style || options.outputStyle] || 0;
return binding.renderSync(css, paths.join(':'), style);
};
exports.middleware = require('./lib/middleware');
......@@ -12,6 +12,10 @@ var scssStr = '#navbar {\
a {\
font-weight: bold; }}';
// Note that the bad
var badInput = '#navbar \n\
width: 80%';
var expectedRender = '#navbar {\n\
width: 80%;\n\
height: 23px; }\n\
......@@ -41,4 +45,28 @@ describe("compile scss", function() {
}
});
});
});
\ No newline at end of file
it("should execute asynchronously", function(done) {
var inside, outside;
inside = outside = false;
sass.render(scssStr, function (err, css) {
inside = Date.now();
});
outside = Date.now();
done(assert.ok(!inside) && assert.notEqual(outside, false));
});
it("should execute synchronously", function(done) {
var output = sass.renderSync(scssStr);
done(assert.ok(output));
});
it("should throw an exception for bad input", function(done) {
done(assert.throws(function() {
sass.renderSync(badInput);
}));
});
});
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