Skip to content

Commit

Permalink
Implement is_superselector sass function [RFC] [WIP]
Browse files Browse the repository at this point in the history
Based on sass#1064
  • Loading branch information
onedayitwillmake authored and mgreter committed May 12, 2015
1 parent 28ee088 commit 1de0132
Show file tree
Hide file tree
Showing 7 changed files with 51 additions and 0 deletions.
20 changes: 20 additions & 0 deletions ast.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -493,6 +493,26 @@ namespace Sass {
#endif
}

bool Selector_List::is_superselector_of(Sass::Selector_List *rhs) {
// For every selector in RHS, see if it matches /any/ of our selectors
for(size_t rhs_i = 0, rhs_L = rhs->length(); rhs_i < rhs_L; ++rhs_i) {
Complex_Selector* seq1 = (*rhs)[rhs_i];
bool any = false;
for (size_t lhs_i = 0, lhs_L = length(); lhs_i < lhs_L; ++lhs_i) {
Complex_Selector* seq2 = (*this)[lhs_i];
bool is_superselector = seq2->is_superselector_of(seq1);
if( is_superselector ) {
any = true;
break;
}
}
if(!any) {
return false;
}
}
return true;
}

/* not used anymore - remove?
Selector_Placeholder* Selector_List::find_placeholder()
{
Expand Down
1 change: 1 addition & 0 deletions ast.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -2086,6 +2086,7 @@ namespace Sass {
: Selector(pstate), Vectorized<Complex_Selector*>(s), wspace_(0)
{ }
// virtual Selector_Placeholder* find_placeholder();
bool is_superselector_of(Selector_List* other);
virtual unsigned long specificity()
{
unsigned long sum = 0;
Expand Down
2 changes: 2 additions & 0 deletions context.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -539,6 +539,8 @@ namespace Sass {
// Misc Functions
register_function(ctx, inspect_sig, inspect, env);
register_function(ctx, unique_id_sig, unique_id, env);
// Selector functions
register_function(ctx, is_superselector_sig, is_superselector, env);
}

void register_c_functions(Context& ctx, Env* env, Sass_Function_List descrs)
Expand Down
14 changes: 14 additions & 0 deletions functions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1564,6 +1564,20 @@ namespace Sass {
// return v;
}

Signature is_superselector_sig = "is-superselector($super, $sub)";
BUILT_IN(is_superselector)
{
To_String to_string(&ctx, false);
Expression* ex_sub = ARG("$sub", Expression);
Expression* ex_sup = ARG("$super", Expression);
string sub_src = ex_sub->perform(&to_string) + "{";
string sup_src = ex_sup->perform(&to_string) + "{";
Complex_Selector* sel_sub = Parser::parse_complex_selector(sub_src.c_str(), ctx);
Complex_Selector* sel_sup = Parser::parse_complex_selector(sup_src.c_str(), ctx);
bool result = sel_sup->is_superselector_of(sel_sub);
return new (ctx.mem) Boolean(pstate, result);
}

Signature unique_id_sig = "unique-id()";
BUILT_IN(unique_id)
{
Expand Down
2 changes: 2 additions & 0 deletions functions.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@ namespace Sass {
extern Signature keywords_sig;
extern Signature set_nth_sig;
extern Signature unique_id_sig;
extern Signature is_superselector_sig;

BUILT_IN(rgb);
BUILT_IN(rgba_4);
Expand Down Expand Up @@ -175,6 +176,7 @@ namespace Sass {
BUILT_IN(keywords);
BUILT_IN(set_nth);
BUILT_IN(unique_id);
BUILT_IN(is_superselector);

}
}
Expand Down
10 changes: 10 additions & 0 deletions parser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,16 @@ namespace Sass {
return p;
}

Complex_Selector* Parser::parse_complex_selector(const char* src, Context& ctx)
{
Parser p = Parser::from_c_str(src, ctx, ParserState("sel", src, 0));
// if(contextualize.parent) {
// p.block_stack.push_back(contextualize.parent->last_block());
// p.last_media_block = contextualize.parent->media_block();
// }
return p.parse_selector_combination();
}

bool Parser::peek_newline(const char* start)
{
return peek_linefeed(start ? start : position);
Expand Down
2 changes: 2 additions & 0 deletions parser.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,8 @@ namespace Sass {
static Parser from_c_str(const char* src, Context& ctx, ParserState pstate = ParserState("[CSTRING]"));
static Parser from_c_str(const char* beg, const char* end, Context& ctx, ParserState pstate = ParserState("[CSTRING]"));
static Parser from_token(Token t, Context& ctx, ParserState pstate = ParserState("[TOKEN]"));
// special static parsers to convert strings into certain selectors
static Complex_Selector* parse_complex_selector(const char* src, Context& ctx);

#ifdef __clang__

Expand Down

0 comments on commit 1de0132

Please sign in to comment.