Skip to content

Commit a285128

Browse files
author
Aaron Leung
committed
Merge branch 'master' into headless-block-fix
2 parents f8a06ff + edff617 commit a285128

16 files changed

+130
-26
lines changed

.gitmodules

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
[submodule "sass2scss"]
2+
path = sass2scss
3+
url = https://github.com/mgreter/sass2scss.git

Makefile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ SOURCES = \
3131
prelexer.cpp \
3232
sass.cpp \
3333
sass_interface.cpp \
34+
sass2scss/sass2scss.cpp \
3435
source_map.cpp \
3536
to_c.cpp \
3637
to_string.cpp \

ast.hpp

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -409,6 +409,7 @@ namespace Sass {
409409
ADD_PROPERTY(Type, type);
410410
ADD_PROPERTY(Native_Function, native_function);
411411
ADD_PROPERTY(Sass_C_Function, c_function);
412+
ADD_PROPERTY(void*, cookie);
412413
ADD_PROPERTY(bool, is_overload_stub);
413414
ADD_PROPERTY(Signature, signature);
414415
public:
@@ -425,6 +426,7 @@ namespace Sass {
425426
type_(t),
426427
native_function_(0),
427428
c_function_(0),
429+
cookie_(0),
428430
is_overload_stub_(false),
429431
signature_(0)
430432
{ }
@@ -442,6 +444,7 @@ namespace Sass {
442444
type_(FUNCTION),
443445
native_function_(func_ptr),
444446
c_function_(0),
447+
cookie_(0),
445448
is_overload_stub_(overload_stub),
446449
signature_(sig)
447450
{ }
@@ -451,6 +454,7 @@ namespace Sass {
451454
string n,
452455
Parameters* params,
453456
Sass_C_Function func_ptr,
457+
void* cookie,
454458
bool whatever,
455459
bool whatever2)
456460
: Has_Block(path, position, 0),
@@ -460,6 +464,7 @@ namespace Sass {
460464
type_(FUNCTION),
461465
native_function_(0),
462466
c_function_(func_ptr),
467+
cookie_(cookie),
463468
is_overload_stub_(false),
464469
signature_(sig)
465470
{ }
@@ -597,9 +602,13 @@ namespace Sass {
597602
class Function_Call : public Expression {
598603
ADD_PROPERTY(string, name);
599604
ADD_PROPERTY(Arguments*, arguments);
605+
ADD_PROPERTY(void*, cookie);
600606
public:
607+
Function_Call(string path, Position position, string n, Arguments* args, void* cookie)
608+
: Expression(path, position), name_(n), arguments_(args), cookie_(cookie)
609+
{ concrete_type(STRING); }
601610
Function_Call(string path, Position position, string n, Arguments* args)
602-
: Expression(path, position), name_(n), arguments_(args)
611+
: Expression(path, position), name_(n), arguments_(args), cookie_(0)
603612
{ concrete_type(STRING); }
604613
ATTACH_OPERATIONS();
605614
};

constants.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@ namespace Sass {
6666
extern const char even_kwd[] = "even";
6767
extern const char odd_kwd[] = "odd";
6868
extern const char progid_kwd[] = "progid";
69+
extern const char expression_kwd[] = "expression";
6970
extern const char calc_kwd[] = "calc(";
7071

7172
// css attribute-matching operators

constants.hpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@ namespace Sass {
6666
extern const char even_kwd[];
6767
extern const char odd_kwd[];
6868
extern const char progid_kwd[];
69+
extern const char expression_kwd[];
6970
extern const char calc_kwd[];
7071

7172
// css attribute-matching operators

context.cpp

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ namespace Sass {
4545
: mem(Memory_Manager<AST_Node>()),
4646
source_c_str (initializers.source_c_str()),
4747
sources (vector<const char*>()),
48+
c_functions (vector<Sass_C_Function_Descriptor>()),
4849
include_paths (initializers.include_paths()),
4950
queue (vector<pair<string, const char*> >()),
5051
style_sheets (map<string, Block*>()),
@@ -212,6 +213,9 @@ namespace Sass {
212213
Env tge;
213214
Backtrace backtrace(0, "", Position(), "");
214215
register_built_in_functions(*this, &tge);
216+
for (size_t i = 0, S = c_functions.size(); i < S; ++i) {
217+
register_c_function(*this, &tge, c_functions[i]);
218+
}
215219
Eval eval(*this, &tge, &backtrace);
216220
Contextualize contextualize(*this, &eval, &tge, &backtrace);
217221
Expand expand(*this, &eval, &contextualize, &tge, &backtrace);
@@ -281,6 +285,9 @@ namespace Sass {
281285
char wd[wd_len];
282286
string cwd = getcwd(wd, wd_len);
283287
if (cwd[cwd.length() - 1] != '/') cwd += '/';
288+
#ifdef _WIN32
289+
replace(cwd.begin(), cwd.end(), '\\', '/'); //convert Windows backslashes to URL forward slashes
290+
#endif
284291
return cwd;
285292
}
286293

@@ -317,7 +324,7 @@ namespace Sass {
317324
{
318325
using namespace Functions;
319326
// RGB Functions
320-
register_function(ctx, rgb_sig, rgb, env);
327+
register_function(ctx, rgb_sig, rgb, env);
321328
register_overload_stub(ctx, "rgba", env);
322329
register_function(ctx, rgba_4_sig, rgba_4, 4, env);
323330
register_function(ctx, rgba_2_sig, rgba_2, 2, env);
@@ -391,7 +398,7 @@ namespace Sass {
391398
}
392399
void register_c_function(Context& ctx, Env* env, Sass_C_Function_Descriptor descr)
393400
{
394-
Definition* def = make_c_function(descr.signature, descr.function, ctx);
401+
Definition* def = make_c_function(descr.signature, descr.function, descr.cookie, ctx);
395402
def->environment(env);
396403
(*env)[def->name() + "[f]"] = def;
397404
}

context.hpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@
2121
#include "subset_map.hpp"
2222
#endif
2323

24+
struct Sass_C_Function_Descriptor;
25+
2426
namespace Sass {
2527
using namespace std;
2628
class AST_Node;
@@ -44,6 +46,7 @@ namespace Sass {
4446
vector<pair<string, const char*> > queue; // queue of files to be parsed
4547
map<string, Block*> style_sheets; // map of paths to ASTs
4648
SourceMap source_map;
49+
vector<Sass_C_Function_Descriptor> c_functions;
4750

4851
string image_path; // for the image-url Sass function
4952
bool source_comments;

eval.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -340,7 +340,7 @@ namespace Sass {
340340
backtrace = &here;
341341

342342
To_C to_c;
343-
Sass_Value c_val = c_func(args->perform(&to_c));
343+
Sass_Value c_val = c_func(args->perform(&to_c), def->cookie());
344344
if (c_val.unknown.tag == SASS_ERROR) {
345345
error("error in C function " + c->name() + ": " + c_val.error.message, c->path(), c->position(), backtrace);
346346
}

file.cpp

Lines changed: 37 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
#include <sys/stat.h>
1010
#include "file.hpp"
1111
#include "context.hpp"
12+
#include "sass2scss/sass2scss.h"
1213

1314
namespace Sass {
1415
namespace File {
@@ -134,13 +135,32 @@ namespace Sass {
134135
real_path = dir + _base_scss;
135136
// if the file isn't found with '_' + filename + ".scss" ...
136137
if (!(contents = read_file(real_path))) {
137-
string base_scss(base + ".scss");
138-
// try filename + ".scss" as the last resort
139-
real_path = dir + base_scss;
140-
contents = read_file(real_path);
138+
string _base_sass(_base + ".sass");
139+
real_path = dir + _base_sass;
140+
// if the file isn't found with '_' + filename + ".sass" ...
141+
if (!(contents = read_file(real_path))) {
142+
string base_scss(base + ".scss");
143+
real_path = dir + base_scss;
144+
// if the file isn't found with filename + ".scss" ...
145+
if (!(contents = read_file(real_path))) {
146+
string base_sass(base + ".sass");
147+
real_path = dir + base_sass;
148+
// if the file isn't found with filename + ".sass" ...
149+
if (!(contents = read_file(real_path))) {
150+
// default back to scss version
151+
real_path = dir + base_scss;
152+
}
153+
}
154+
}
141155
}
142156
}
143157
}
158+
string extension;
159+
if (real_path.length() > 5) {
160+
extension = real_path.substr(real_path.length() - 5, 5);
161+
}
162+
for(size_t i=0; i<extension.size();++i)
163+
extension[i] = tolower(extension[i]);
144164
return contents;
145165
}
146166

@@ -149,6 +169,10 @@ namespace Sass {
149169
struct stat st;
150170
if (stat(path.c_str(), &st) == -1 || S_ISDIR(st.st_mode)) return 0;
151171
ifstream file(path.c_str(), ios::in | ios::binary | ios::ate);
172+
string extension;
173+
if (path.length() > 5) {
174+
extension = path.substr(path.length() - 5, 5);
175+
}
152176
char* contents = 0;
153177
if (file.is_open()) {
154178
size_t size = file.tellg();
@@ -158,7 +182,15 @@ namespace Sass {
158182
contents[size] = '\0';
159183
file.close();
160184
}
161-
return contents;
185+
for(size_t i=0; i<extension.size();++i)
186+
extension[i] = tolower(extension[i]);
187+
if (extension == ".sass" && contents != 0) {
188+
char * converted = sass2scss(contents, SASS2SCSS_PRETTIFY_1);
189+
delete[] contents; // free the indented contents
190+
return converted; // should be freed by caller
191+
} else {
192+
return contents;
193+
}
162194
}
163195

164196
}

functions.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ namespace Sass {
3636
false);
3737
}
3838

39-
Definition* make_c_function(Signature sig, Sass_C_Function f, Context& ctx)
39+
Definition* make_c_function(Signature sig, Sass_C_Function f, void* cookie, Context& ctx)
4040
{
4141
Parser sig_parser = Parser::from_c_str(sig, ctx, "[c function]");
4242
sig_parser.lex<Prelexer::identifier>();
@@ -48,6 +48,7 @@ namespace Sass {
4848
name,
4949
params,
5050
f,
51+
cookie,
5152
false, true);
5253
}
5354

functions.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ namespace Sass {
2828
typedef Expression* (*Native_Function)(Env&, Context&, Signature, const string&, Position, Backtrace*);
2929

3030
Definition* make_native_function(Signature, Native_Function, Context&);
31-
Definition* make_c_function(Signature sig, Sass_C_Function f, Context& ctx);
31+
Definition* make_c_function(Signature sig, Sass_C_Function f, void* cookie, Context& ctx);
3232

3333
namespace Functions {
3434

prelexer.cpp

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -426,13 +426,16 @@ namespace Sass {
426426
return exactly<lte>(src);
427427
}
428428

429-
// match the IE syntax
430-
const char* progid(const char* src) {
431-
return exactly<progid_kwd>(src);
429+
// match specific IE syntax
430+
const char* ie_progid(const char* src) {
431+
return sequence < exactly<progid_kwd>, exactly<':'>, alternatives< identifier_schema, identifier >, one_plus< sequence< exactly<'.'>, alternatives< identifier_schema, identifier > > > >(src);
432432
}
433-
433+
const char* ie_expression(const char* src) {
434+
return exactly<expression_kwd>(src);
435+
}
436+
// match any IE syntax
434437
const char* ie_stuff(const char* src) {
435-
return sequence< progid, exactly<':'>, alternatives< identifier_schema, identifier >, one_plus< sequence< exactly<'.'>, alternatives< identifier_schema, identifier > > >, delimited_by<'(', ';', true> >(src);
438+
return sequence< alternatives < ie_expression, ie_progid >, delimited_by<'(', ';', true> >(src);
436439
}
437440

438441
// const char* ie_args(const char* src) {

sass.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -119,11 +119,12 @@ union Sass_Value make_sass_list (size_t len, enum Sass_Separator sep);
119119
union Sass_Value make_sass_null ();
120120
union Sass_Value make_sass_error (const char* msg);
121121

122-
typedef union Sass_Value(*Sass_C_Function)(union Sass_Value);
122+
typedef union Sass_Value(*Sass_C_Function)(union Sass_Value, void *cookie);
123123

124124
struct Sass_C_Function_Descriptor {
125125
const char* signature;
126126
Sass_C_Function function;
127+
void *cookie;
127128
};
128129

129130
#ifdef __cplusplus

sass2scss

Submodule sass2scss added at 443a0ce

sass_interface.cpp

Lines changed: 41 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ extern "C" {
3838
void sass_free_context(sass_context* ctx)
3939
{
4040
if (ctx->output_string) free(ctx->output_string);
41+
if (ctx->source_map_string) free(ctx->source_map_string);
4142
if (ctx->error_message) free(ctx->error_message);
4243

4344
free_string_array(ctx->included_files, ctx->num_included_files);
@@ -86,19 +87,40 @@ extern "C" {
8687
{
8788
using namespace Sass;
8889
try {
90+
bool source_maps = false;
91+
string source_map_file = "";
92+
if (c_ctx->source_map_file && (c_ctx->options.source_comments == SASS_SOURCE_COMMENTS_MAP)) {
93+
source_maps = true;
94+
source_map_file = c_ctx->source_map_file;
95+
}
96+
string output_path = c_ctx->output_path ? c_ctx->output_path : "";
8997
Context cpp_ctx(
9098
Context::Data().source_c_str(c_ctx->source_string)
91-
.entry_point("")
99+
.entry_point(c_ctx->input_path ?
100+
c_ctx->input_path :
101+
"")
102+
.output_path(output_path)
92103
.output_style((Output_Style) c_ctx->options.output_style)
93104
.source_comments(c_ctx->options.source_comments == SASS_SOURCE_COMMENTS_DEFAULT)
94-
.source_maps(false) // Only supported for files.
95-
.image_path(c_ctx->options.image_path)
105+
.source_maps(source_maps)
106+
.source_map_file(source_map_file)
107+
.image_path(c_ctx->options.image_path ?
108+
c_ctx->options.image_path :
109+
"")
96110
.include_paths_c_str(c_ctx->options.include_paths)
97111
.include_paths_array(0)
98112
.include_paths(vector<string>())
99113
.precision(c_ctx->options.precision ? c_ctx->options.precision : 5)
100114
);
115+
if (c_ctx->c_functions) {
116+
struct Sass_C_Function_Descriptor* this_func_data = c_ctx->c_functions;
117+
while (this_func_data->signature && this_func_data->function) {
118+
cpp_ctx.c_functions.push_back(*this_func_data);
119+
++this_func_data;
120+
}
121+
}
101122
c_ctx->output_string = cpp_ctx.compile_string();
123+
c_ctx->source_map_string = cpp_ctx.generate_source_map();
102124
c_ctx->error_message = 0;
103125
c_ctx->error_status = 0;
104126

@@ -110,13 +132,15 @@ extern "C" {
110132
c_ctx->error_message = strdup(msg_stream.str().c_str());
111133
c_ctx->error_status = 1;
112134
c_ctx->output_string = 0;
135+
c_ctx->source_map_string = 0;
113136
}
114137
catch(bad_alloc& ba) {
115138
stringstream msg_stream;
116139
msg_stream << "Unable to allocate memory: " << ba.what() << endl;
117140
c_ctx->error_message = strdup(msg_stream.str().c_str());
118141
c_ctx->error_status = 1;
119142
c_ctx->output_string = 0;
143+
c_ctx->source_map_string = 0;
120144
}
121145
// TO DO: CATCH EVERYTHING ELSE
122146
return 0;
@@ -134,18 +158,29 @@ extern "C" {
134158
}
135159
string output_path = c_ctx->output_path ? c_ctx->output_path : "";
136160
Context cpp_ctx(
137-
Context::Data().entry_point(c_ctx->input_path)
138-
.output_path(output_path)
161+
Context::Data().entry_point(c_ctx->input_path ?
162+
c_ctx->input_path :
163+
"")
164+
.output_path(output_path)
139165
.output_style((Output_Style) c_ctx->options.output_style)
140166
.source_comments(c_ctx->options.source_comments == SASS_SOURCE_COMMENTS_DEFAULT)
141167
.source_maps(source_maps)
142168
.source_map_file(source_map_file)
143-
.image_path(c_ctx->options.image_path)
169+
.image_path(c_ctx->options.image_path ?
170+
c_ctx->options.image_path :
171+
"")
144172
.include_paths_c_str(c_ctx->options.include_paths)
145173
.include_paths_array(0)
146174
.include_paths(vector<string>())
147175
.precision(c_ctx->options.precision ? c_ctx->options.precision : 5)
148176
);
177+
if (c_ctx->c_functions) {
178+
struct Sass_C_Function_Descriptor* this_func_data = c_ctx->c_functions;
179+
while (this_func_data->signature && this_func_data->function) {
180+
cpp_ctx.c_functions.push_back(*this_func_data);
181+
++this_func_data;
182+
}
183+
}
149184
c_ctx->output_string = cpp_ctx.compile_file();
150185
c_ctx->source_map_string = cpp_ctx.generate_source_map();
151186
c_ctx->error_message = 0;

0 commit comments

Comments
 (0)