diff --git a/src/binding.cpp b/src/binding.cpp index 89e3469e8..7dd76c724 100644 --- a/src/binding.cpp +++ b/src/binding.cpp @@ -12,13 +12,62 @@ char* CreateString(Local value) { return str; } +struct Sass_Import** sass_importer(const char* file, void* cookie) +{ + NanScope(); + + Handle argv[] = { + NanNew(file) + }; + + Local returned_value = NanNew(((NanCallback*)cookie)->Call(2, argv)); + + if(returned_value->IsArray()) { + Handle array = Handle::Cast(returned_value); + + struct Sass_Import** incs = sass_make_import_list(array->Length()); + + for(size_t i = 0; i < array->Length(); ++i) { + Local value = array->Get(i); + + if(!value->IsObject()) + continue; + + Local object = Local::Cast(value); + char* path = CreateString(object->Get(String::New("path"))); + char* contents = CreateString(object->Get(String::New("contents"))); + + incs[i] = sass_make_import_entry(path, (!contents || contents[0] == '\0') ? 0 : strdup(contents), 0); + } + + return incs; + } else if(returned_value->IsObject()) { + struct Sass_Import** incs = sass_make_import_list(1); + Local object = Local::Cast(returned_value); + char* path = CreateString(object->Get(String::New("path"))); + char* contents = CreateString(object->Get(String::New("contents"))); + + incs[0] = sass_make_import_entry(path, (!contents || contents[0] == '\0') ? 0 : strdup(contents), 0); + + return incs; + } + + struct Sass_Import** incs = sass_make_import_list(1); + + incs[0] = sass_make_import_entry(file, 0, 0); + + return incs; +} + void ExtractOptions(Local options, void* cptr, sass_context_wrapper* ctx_w, bool isFile) { if (ctx_w) { NanAssignPersistent(ctx_w->stats, options->Get(NanNew("stats"))->ToObject()); // async (callback) style Local callback = Local::Cast(options->Get(NanNew("success"))); - Local errorCallback = Local::Cast(options->Get(NanNew("error"))); + Local error_callback = Local::Cast(options->Get(NanNew("error"))); + Local importer_callback = Local::Cast(options->Get(NanNew("importer"))); + if (isFile) { ctx_w->fctx = (struct Sass_File_Context*) cptr; } else { @@ -26,7 +75,8 @@ void ExtractOptions(Local options, void* cptr, sass_context_wrapper* ctx } ctx_w->request.data = ctx_w; ctx_w->callback = new NanCallback(callback); - ctx_w->errorCallback = new NanCallback(errorCallback); + ctx_w->error_callback = new NanCallback(error_callback); + ctx_w->importer_callback = new NanCallback(importer_callback); } struct Sass_Context* ctx; @@ -50,6 +100,7 @@ void ExtractOptions(Local options, void* cptr, sass_context_wrapper* ctx sass_option_set_source_map_file(sass_options, CreateString(options->Get(NanNew("sourceMap")))); sass_option_set_include_path(sass_options, CreateString(options->Get(NanNew("paths")))); sass_option_set_precision(sass_options, options->Get(NanNew("precision"))->Int32Value()); + sass_option_set_importer(sass_options, sass_make_importer(sass_importer, ctx_w->importer_callback)); } void FillStatsObj(Handle stats, Sass_Context* ctx) { @@ -112,7 +163,7 @@ void MakeCallback(uv_work_t* req) { NanNew(err), NanNew(error_status) }; - ctx_w->errorCallback->Call(2, argv); + ctx_w->error_callback->Call(2, argv); } if (try_catch.HasCaught()) { node::FatalException(try_catch); diff --git a/src/sass_context_wrapper.cpp b/src/sass_context_wrapper.cpp index 5f29b0d1c..4a6acefa5 100644 --- a/src/sass_context_wrapper.cpp +++ b/src/sass_context_wrapper.cpp @@ -34,7 +34,8 @@ extern "C" { NanDisposePersistent(ctx_w->stats); delete ctx_w->callback; - delete ctx_w->errorCallback; + delete ctx_w->error_callback; + delete ctx_w->importer_callback; free(ctx_w); } diff --git a/src/sass_context_wrapper.h b/src/sass_context_wrapper.h index a4c106a6b..5612dcec8 100644 --- a/src/sass_context_wrapper.h +++ b/src/sass_context_wrapper.h @@ -17,7 +17,8 @@ struct sass_context_wrapper { Persistent stats; uv_work_t request; NanCallback* callback; - NanCallback* errorCallback; + NanCallback* error_callback; + NanCallback* importer_callback; }; struct sass_context_wrapper* sass_make_context_wrapper(void);