Skip to content

Commit 8116200

Browse files
authored
Implement content-exists function (#2362)
Fixes #2266 Spec sass/sass-spec#1100
1 parent 4462a55 commit 8116200

File tree

7 files changed

+19
-3
lines changed

7 files changed

+19
-3
lines changed

src/context.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -831,6 +831,7 @@ namespace Sass {
831831
register_function(ctx, mixin_exists_sig, mixin_exists, env);
832832
register_function(ctx, feature_exists_sig, feature_exists, env);
833833
register_function(ctx, call_sig, call, env);
834+
register_function(ctx, content_exists_sig, content_exists, env);
834835
// Boolean Functions
835836
register_function(ctx, not_sig, sass_not, env);
836837
register_function(ctx, if_sig, sass_if, env);

src/expand.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -679,7 +679,6 @@ namespace Sass {
679679

680680
Statement_Ptr Expand::operator()(Mixin_Call_Ptr c)
681681
{
682-
683682
if (recursions > maxRecursion) {
684683
throw Exception::StackError(*c);
685684
}
@@ -730,13 +729,14 @@ namespace Sass {
730729
Block_Obj trace_block = SASS_MEMORY_NEW(Block, c->pstate());
731730
Trace_Obj trace = SASS_MEMORY_NEW(Trace, c->pstate(), c->name(), trace_block);
732731

733-
732+
env->set_global("is_in_mixin", bool_true);
734733
block_stack.push_back(trace_block);
735734
for (auto bb : body->elements()) {
736735
Statement_Obj ith = bb->perform(this);
737736
if (ith) trace->block()->append(ith);
738737
}
739738
block_stack.pop_back();
739+
env->del_global("is_in_mixin");
740740

741741
env_stack.pop_back();
742742
backtrace_stack.pop_back();

src/expand.hpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,8 @@ namespace Sass {
3737
std::vector<Media_Block_Ptr> media_block_stack;
3838
std::vector<Backtrace*> backtrace_stack;
3939

40+
Boolean_Obj bool_true;
41+
4042
Statement_Ptr fallback_impl(AST_Node_Ptr n);
4143

4244
private:

src/functions.cpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2000,5 +2000,14 @@ namespace Sass {
20002000
List_Obj list = Cast<List>(value);
20012001
return SASS_MEMORY_NEW(Boolean, pstate, list && list->is_bracketed());
20022002
}
2003+
2004+
Signature content_exists_sig = "content-exists()";
2005+
BUILT_IN(content_exists)
2006+
{
2007+
if (!d_env.has_global("is_in_mixin")) {
2008+
error("Cannot call content-exists() except within a mixin.", pstate, backtrace);
2009+
}
2010+
return SASS_MEMORY_NEW(Boolean, pstate, d_env.has_lexical("@content[m]"));
2011+
}
20032012
}
20042013
}

src/functions.hpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,7 @@ namespace Sass {
106106
extern Signature simple_selectors_sig;
107107
extern Signature selector_parse_sig;
108108
extern Signature is_bracketed_sig;
109+
extern Signature content_exists_sig;
109110

110111
BUILT_IN(rgb);
111112
BUILT_IN(rgba_4);
@@ -188,6 +189,7 @@ namespace Sass {
188189
BUILT_IN(simple_selectors);
189190
BUILT_IN(selector_parse);
190191
BUILT_IN(is_bracketed);
192+
BUILT_IN(content_exists);
191193
}
192194
}
193195

src/parser.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2025,6 +2025,9 @@ namespace Sass {
20252025
lex< identifier >();
20262026
std::string name(lexed);
20272027

2028+
if (Util::normalize_underscores(name) == "content-exists" && stack.back() != Scope::Mixin)
2029+
{ error("Cannot call content-exists() except within a mixin.", pstate); }
2030+
20282031
ParserState call_pos = pstate;
20292032
Arguments_Obj args = parse_arguments();
20302033
return SASS_MEMORY_NEW(Function_Call, call_pos, name, args);

src/parser.hpp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,6 @@ namespace Sass {
3737
ParserState pstate;
3838
int indentation;
3939

40-
4140
Token lexed;
4241

4342
Parser(Context& ctx, const ParserState& pstate)

0 commit comments

Comments
 (0)