Skip to content

Commit

Permalink
Merge pull request sass#1431 from xzyfer/fix/better-selector-errors
Browse files Browse the repository at this point in the history
Better mirror Ruby Sass errors for null params to selector functions
  • Loading branch information
xzyfer committed Aug 10, 2015
2 parents 4aa12e9 + 68cd236 commit eadae5b
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 6 deletions.
43 changes: 37 additions & 6 deletions src/functions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,12 @@ namespace Sass {
false, true);
}

std::string function_name(Signature sig)
{
std::string str(sig);
return str.substr(0, str.find('('));
}

namespace Functions {

inline void handle_utf8_error (const ParserState& pstate, Backtrace* backtrace)
Expand Down Expand Up @@ -147,6 +153,12 @@ namespace Sass {
Selector_List* get_arg_sel(const string& argname, Env& env, Signature sig, ParserState pstate, Backtrace* backtrace, Context& ctx) {
To_String to_string(&ctx, false);
Expression* exp = ARG(argname, Expression);
if (exp->concrete_type() == Expression::NULL_VAL) {
stringstream msg;
msg << argname << ": null is not a valid selector: it must be a string,\n";
msg << "a list of strings, or a list of lists of strings for `" << function_name(sig) << "'";
error(msg.str(), pstate);
}
string exp_src = exp->perform(&to_string) + "{";
return Parser::parse_selector(exp_src.c_str(), ctx);
}
Expand All @@ -155,6 +167,12 @@ namespace Sass {
Complex_Selector* get_arg_sel(const string& argname, Env& env, Signature sig, ParserState pstate, Backtrace* backtrace, Context& ctx) {
To_String to_string(&ctx, false);
Expression* exp = ARG(argname, Expression);
if (exp->concrete_type() == Expression::NULL_VAL) {
stringstream msg;
msg << argname << ": null is not a valid selector: it must be a string,\n";
msg << "a list of strings, or a list of lists of strings for `" << function_name(sig) << "'";
error(msg.str(), pstate);
}
string exp_src = exp->perform(&to_string) + "{";
Selector_List* sel_list = Parser::parse_selector(exp_src.c_str(), ctx);
return (sel_list->length() > 0) ? sel_list->first() : 0;
Expand All @@ -164,6 +182,12 @@ namespace Sass {
Compound_Selector* get_arg_sel(const string& argname, Env& env, Signature sig, ParserState pstate, Backtrace* backtrace, Context& ctx) {
To_String to_string(&ctx, false);
Expression* exp = ARG(argname, Expression);
if (exp->concrete_type() == Expression::NULL_VAL) {
stringstream msg;
msg << argname << ": null is not a valid selector: it must be a string,\n";
msg << "a list of strings, or a list of lists of strings for `" << function_name(sig) << "'";
error(msg.str(), pstate);
}
string exp_src = exp->perform(&to_string) + "{";
Selector_List* sel_list = Parser::parse_selector(exp_src.c_str(), ctx);
return (sel_list->length() > 0) ? sel_list->first()->tail()->head() : 0;
Expand Down Expand Up @@ -1601,6 +1625,12 @@ namespace Sass {
vector<Selector_List*> parsedSelectors;
for (size_t i = 0, L = arglist->length(); i < L; ++i) {
Expression* exp = dynamic_cast<Expression*>(arglist->value_at_index(i));
if (exp->concrete_type() == Expression::NULL_VAL) {
stringstream msg;
msg << "$selectors: null is not a valid selector: it must be a string,\n";
msg << "a list of strings, or a list of lists of strings for 'selector-nest'";
error(msg.str(), pstate);
}
string exp_src = exp->perform(&to_string) + "{";
Selector_List* sel = Parser::parse_selector(exp_src.c_str(), ctx);
parsedSelectors.push_back(sel);
Expand Down Expand Up @@ -1655,6 +1685,12 @@ namespace Sass {
vector<Selector_List*> parsedSelectors;
for (size_t i = 0, L = arglist->length(); i < L; ++i) {
Expression* exp = dynamic_cast<Expression*>(arglist->value_at_index(i));
if (exp->concrete_type() == Expression::NULL_VAL) {
stringstream msg;
msg << "$selectors: null is not a valid selector: it must be a string,\n";
msg << "a list of strings, or a list of lists of strings for 'selector-append'";
error(msg.str(), pstate);
}
string exp_src = exp->perform(&to_string) + "{";
Selector_List* sel = Parser::parse_selector(exp_src.c_str(), ctx);
parsedSelectors.push_back(sel);
Expand Down Expand Up @@ -1759,8 +1795,6 @@ namespace Sass {
Signature selector_extend_sig = "selector-extend($selector, $extendee, $extender)";
BUILT_IN(selector_extend)
{
To_String to_string;

Selector_List* selector = ARGSEL("$selector", Selector_List, p_contextualize);
Selector_List* extendee = ARGSEL("$extendee", Selector_List, p_contextualize);
Selector_List* extender = ARGSEL("$extender", Selector_List, p_contextualize);
Expand Down Expand Up @@ -1795,10 +1829,7 @@ namespace Sass {
Signature selector_parse_sig = "selector-parse($selector)";
BUILT_IN(selector_parse)
{
To_String to_string(&ctx, false);
Expression* exp = ARG("$selector", Expression);
string sel_src = exp->perform(&to_string) + "{";
Selector_List* sel = Parser::parse_selector(sel_src.c_str(), ctx);
Selector_List* sel = ARGSEL("$selector", Selector_List, p_contextualize);

Listize listize(ctx);
return sel->perform(&listize);
Expand Down
2 changes: 2 additions & 0 deletions src/functions.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ namespace Sass {
Definition* make_native_function(Signature, Native_Function, Context& ctx);
Definition* make_c_function(Sass_Function_Entry c_func, Context& ctx);

std::string function_name(Signature);

namespace Functions {

extern Signature rgb_sig;
Expand Down

0 comments on commit eadae5b

Please sign in to comment.