Skip to content

Commit f5f01ae

Browse files
committed
Merge pull request #1509 from mgreter/bugfix/importer-c-api-path-handling
Improve importer C-API and path handling
2 parents fbafcc6 + bbb6e44 commit f5f01ae

File tree

9 files changed

+51
-38
lines changed

9 files changed

+51
-38
lines changed

include/sass_functions.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ ADDAPI void ADDCALL sass_delete_importer (Sass_Importer_Entry cb);
5757
ADDAPI Sass_Import_List ADDCALL sass_make_import_list (size_t length);
5858
// Creator for a single import entry returned by the custom importer inside the list
5959
ADDAPI Sass_Import_Entry ADDCALL sass_make_import_entry (const char* path, char* source, char* srcmap);
60-
ADDAPI Sass_Import_Entry ADDCALL sass_make_import (const char* path, const char* base, char* source, char* srcmap);
60+
ADDAPI Sass_Import_Entry ADDCALL sass_make_import (const char* imp_path, const char* abs_base, char* source, char* srcmap);
6161
// set error message to abort import and to print out a message (path from existing object is used in output)
6262
ADDAPI Sass_Import_Entry ADDCALL sass_import_set_error(Sass_Import_Entry import, const char* message, size_t line, size_t col);
6363

@@ -67,8 +67,8 @@ ADDAPI void ADDCALL sass_import_set_list_entry (Sass_Import_List list, size_t id
6767
ADDAPI Sass_Import_Entry ADDCALL sass_import_get_list_entry (Sass_Import_List list, size_t idx);
6868

6969
// Getters for import entry
70-
ADDAPI const char* ADDCALL sass_import_get_path (Sass_Import_Entry);
71-
ADDAPI const char* ADDCALL sass_import_get_base (Sass_Import_Entry);
70+
ADDAPI const char* ADDCALL sass_import_get_imp_path (Sass_Import_Entry);
71+
ADDAPI const char* ADDCALL sass_import_get_abs_path (Sass_Import_Entry);
7272
ADDAPI const char* ADDCALL sass_import_get_source (Sass_Import_Entry);
7373
ADDAPI const char* ADDCALL sass_import_get_srcmap (Sass_Import_Entry);
7474
// Explicit functions to take ownership of these items

src/ast.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1924,6 +1924,7 @@ namespace Sass {
19241924
// some final cosmetics
19251925
if (res == "-0.0") res.erase(0, 1);
19261926
else if (res == "-0") res.erase(0, 1);
1927+
else if (res == "") res = "0";
19271928

19281929
// add unit now
19291930
res += unit();

src/context.cpp

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -411,15 +411,17 @@ namespace Sass {
411411
}
412412

413413

414-
std::vector<std::string> Context::get_included_files(size_t skip)
414+
// for data context we want to start after "stdin"
415+
// we probably always want to skip the header includes?
416+
std::vector<std::string> Context::get_included_files(bool skip, size_t headers)
415417
{
418+
// create a copy of the vector for manupulations
416419
std::vector<std::string> includes = included_files;
417420
if (includes.size() == 0) return includes;
418-
std::sort( includes.begin() + skip, includes.end() );
419-
includes.erase( includes.begin(), includes.begin() + skip );
421+
if (skip) { includes.erase( includes.begin(), includes.begin() + 1 + headers); }
422+
else { includes.erase( includes.begin() + 1, includes.begin() + 1 + headers); }
420423
includes.erase( std::unique( includes.begin(), includes.end() ), includes.end() );
421-
// the skip solution seems more robust, as we may have real files named stdin
422-
// includes.erase( std::remove( includes.begin(), includes.end(), "stdin" ), includes.end() );
424+
std::sort( includes.begin() + (skip ? 0 : 1), includes.end() );
423425
return includes;
424426
}
425427

src/context.hpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -110,8 +110,8 @@ namespace Sass {
110110
Block* parse_string();
111111
void add_source(std::string, std::string, char*);
112112

113-
std::string add_file(const std::string& file);
114-
std::string add_file(const std::string& base, const std::string& file, ParserState pstate);
113+
std::string add_file(const std::string& imp_path);
114+
std::string add_file(const std::string& imp_path, const std::string& abs_path, ParserState pstate);
115115

116116

117117
// allow to optionally overwrite the input path
@@ -122,7 +122,7 @@ namespace Sass {
122122
char* compile_block(Block* root);
123123
char* generate_source_map();
124124

125-
std::vector<std::string> get_included_files(size_t skip = 0);
125+
std::vector<std::string> get_included_files(bool skip = false, size_t headers = 0);
126126

127127
private:
128128
void collect_plugin_paths(const char* paths_str);

src/functions.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1575,7 +1575,7 @@ namespace Sass {
15751575

15761576
Arguments* args = SASS_MEMORY_NEW(ctx.mem, Arguments, pstate);
15771577
std::string full_name(name + "[f]");
1578-
Definition* def = static_cast<Definition*>((d_env)[full_name]);
1578+
Definition* def = d_env.has(full_name) ? static_cast<Definition*>((d_env)[full_name]) : 0;
15791579
Parameters* params = def ? def->parameters() : 0;
15801580
size_t param_size = params ? params->length() : 0;
15811581
for (size_t i = 0, L = arglist->length(); i < L; ++i) {

src/parser.cpp

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -329,18 +329,25 @@ namespace Sass {
329329

330330
bool Parser::do_import(const std::string& import_path, Import* imp, std::vector<Sass_Importer_Entry> importers, bool only_one)
331331
{
332+
size_t i = 0;
332333
bool has_import = false;
333334
std::string load_path = unquote(import_path);
334-
for (auto importer : importers) {
335+
for (Sass_Importer_Entry& importer : importers) {
335336
// int priority = sass_importer_get_priority(importer);
336337
Sass_Importer_Fn fn = sass_importer_get_function(importer);
337338
if (Sass_Import_List includes =
338339
fn(load_path.c_str(), importer, ctx.c_compiler)
339340
) {
340341
Sass_Import_List list = includes;
341-
while (*includes) {
342+
while (*includes) { ++i;
343+
std::string uniq_path = load_path;
344+
if (!only_one && i) {
345+
std::stringstream pathstrm;
346+
pathstrm << uniq_path << ":" << i;
347+
uniq_path = pathstrm.str();
348+
}
342349
Sass_Import_Entry include = *includes;
343-
const char *file = sass_import_get_path(include);
350+
const char *abs_path = sass_import_get_abs_path(include);
344351
char* source = sass_import_take_source(include);
345352
size_t line = sass_import_get_error_line(include);
346353
size_t column = sass_import_get_error_column(include);
@@ -349,15 +356,15 @@ namespace Sass {
349356
if (line == std::string::npos && column == std::string::npos) error(message, pstate);
350357
else error(message, ParserState(message, source, Position(line, column)));
351358
} else if (source) {
352-
if (file) {
353-
ctx.add_source(load_path, file, source);
354-
imp->files().push_back(load_path);
359+
if (abs_path) {
360+
ctx.add_source(uniq_path, abs_path, source);
361+
imp->files().push_back(uniq_path);
355362
} else {
356-
ctx.add_source(load_path, load_path, source);
357-
imp->files().push_back(load_path);
363+
ctx.add_source(uniq_path, uniq_path, source);
364+
imp->files().push_back(uniq_path);
358365
}
359-
} else if(file) {
360-
import_single_file(imp, file);
366+
} else if(abs_path) {
367+
import_single_file(imp, abs_path);
361368
}
362369
++includes;
363370
}

src/sass_context.cpp

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -511,21 +511,24 @@ extern "C" {
511511

512512
// maybe skip some entries of included files
513513
// we do not include stdin for data contexts
514-
size_t skip = 0;
514+
bool skip = false;
515515

516516
// dispatch to the correct render function
517517
if (c_ctx->type == SASS_CONTEXT_FILE) {
518518
root = cpp_ctx->parse_file();
519519
} else if (c_ctx->type == SASS_CONTEXT_DATA) {
520520
root = cpp_ctx->parse_string();
521-
skip = 1; // skip first entry of includes
521+
skip = true; // skip first entry of includes
522522
}
523523

524-
// skip all prefixed files?
525-
skip += cpp_ctx->head_imports;
524+
// skip all prefixed files? (ToDo: check srcmap)
525+
// IMO source-maps should point to headers already
526+
// therefore don't skip it for now. re-enable or
527+
// remove completely once this is tested
528+
size_t headers = cpp_ctx->head_imports;
526529

527530
// copy the included files on to the context (dont forget to free)
528-
if (root) copy_strings(cpp_ctx->get_included_files(skip), &c_ctx->included_files);
531+
if (root) copy_strings(cpp_ctx->get_included_files(skip, headers), &c_ctx->included_files);
529532

530533
// return parsed block
531534
return root;

src/sass_functions.cpp

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,8 @@ extern "C" {
3838

3939
// External import entry
4040
struct Sass_Import {
41-
char* path;
42-
char* base;
41+
char* imp_path; // path as found in the import statement
42+
char *abs_path; // path after importer has resolved it
4343
char* source;
4444
char* srcmap;
4545
// error handling
@@ -92,12 +92,12 @@ extern "C" {
9292

9393
// Creator for a single import entry returned by the custom importer inside the list
9494
// We take ownership of the memory for source and srcmap (freed when context is destroyd)
95-
Sass_Import_Entry ADDCALL sass_make_import(const char* path, const char* base, char* source, char* srcmap)
95+
Sass_Import_Entry ADDCALL sass_make_import(const char* imp_path, const char* abs_path, char* source, char* srcmap)
9696
{
9797
Sass_Import* v = (Sass_Import*) calloc(1, sizeof(Sass_Import));
9898
if (v == 0) return 0;
99-
v->path = path ? sass_strdup(path) : 0;
100-
v->base = base ? sass_strdup(base) : 0;
99+
v->imp_path = imp_path ? sass_strdup(imp_path) : 0;
100+
v->abs_path = abs_path ? sass_strdup(abs_path) : 0;
101101
v->source = source;
102102
v->srcmap = srcmap;
103103
v->error = 0;
@@ -142,17 +142,17 @@ extern "C" {
142142
// Just in case we have some stray import structs
143143
void ADDCALL sass_delete_import(Sass_Import_Entry import)
144144
{
145-
free(import->path);
146-
free(import->base);
145+
free(import->imp_path);
146+
free(import->abs_path);
147147
free(import->source);
148148
free(import->srcmap);
149149
free(import->error);
150150
free(import);
151151
}
152152

153153
// Getter for import entry
154-
const char* ADDCALL sass_import_get_path(Sass_Import_Entry entry) { return entry->path; }
155-
const char* ADDCALL sass_import_get_base(Sass_Import_Entry entry) { return entry->base; }
154+
const char* ADDCALL sass_import_get_imp_path(Sass_Import_Entry entry) { return entry->imp_path; }
155+
const char* ADDCALL sass_import_get_abs_path(Sass_Import_Entry entry) { return entry->abs_path; }
156156
const char* ADDCALL sass_import_get_source(Sass_Import_Entry entry) { return entry->source; }
157157
const char* ADDCALL sass_import_get_srcmap(Sass_Import_Entry entry) { return entry->srcmap; }
158158

src/sass_interface.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,7 @@ extern "C" {
135135
c_ctx->error_message = 0;
136136
c_ctx->error_status = 0;
137137

138-
copy_strings(cpp_ctx.get_included_files(1), &c_ctx->included_files, 1);
138+
copy_strings(cpp_ctx.get_included_files(true), &c_ctx->included_files, 1);
139139
}
140140
catch (Error_Invalid& e) {
141141
std::stringstream msg_stream;
@@ -227,7 +227,7 @@ extern "C" {
227227
c_ctx->error_message = 0;
228228
c_ctx->error_status = 0;
229229

230-
copy_strings(cpp_ctx.get_included_files(), &c_ctx->included_files);
230+
copy_strings(cpp_ctx.get_included_files(false), &c_ctx->included_files);
231231
}
232232
catch (Error_Invalid& e) {
233233
std::stringstream msg_stream;

0 commit comments

Comments
 (0)