Skip to content

Commit

Permalink
Refactoring/back-porting selectors and extend
Browse files Browse the repository at this point in the history
- Ports extend logic back from dart-sass
- Gets rid of old extend and node classes
- Ports superselector logic back from dart-sass
- Overhauls internal selector storage structure
- Refactors media rules to match dart-sass closer
- Simplifies compare operations for selectors a bit
- Adds initial implementation for min/max specificity
- Improves selector schema parsing and evaluation
- Implements custom insertion ordered hash map
- Cleans header includes for files touched
- Tons of other small fixes and improvements
  • Loading branch information
mgreter committed Jun 14, 2019
1 parent 5ebc5d5 commit 1312e93
Show file tree
Hide file tree
Showing 85 changed files with 7,172 additions and 7,678 deletions.
11 changes: 7 additions & 4 deletions Makefile.conf
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,9 @@ SOURCES = \
ast_supports.cpp \
ast_sel_cmp.cpp \
ast_sel_unify.cpp \
ast_sel_super.cpp \
ast_sel_weave.cpp \
ast_selectors.cpp \
node.cpp \
context.cpp \
constants.cpp \
fn_utils.cpp \
Expand All @@ -37,19 +38,22 @@ SOURCES = \
position.cpp \
lexer.cpp \
parser.cpp \
parser_selectors.cpp \
prelexer.cpp \
eval.cpp \
eval_selectors.cpp \
expand.cpp \
listize.cpp \
cssize.cpp \
extend.cpp \
extender.cpp \
extension.cpp \
stylesheet.cpp \
output.cpp \
inspect.cpp \
emitter.cpp \
check_nesting.cpp \
remove_placeholders.cpp \
sass.cpp \
sass_util.cpp \
sass_values.cpp \
sass_context.cpp \
sass_functions.cpp \
Expand All @@ -60,7 +64,6 @@ SOURCES = \
c2ast.cpp \
to_value.cpp \
source_map.cpp \
subset_map.cpp \
error_handling.cpp \
memory/SharedPtr.cpp \
utf8_string.cpp \
Expand Down
3 changes: 2 additions & 1 deletion include/sass/base.h
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,8 @@ enum Sass_Output_Style {
SASS_STYLE_COMPRESSED,
// only used internaly
SASS_STYLE_INSPECT,
SASS_STYLE_TO_SASS
SASS_STYLE_TO_SASS,
SASS_STYLE_TO_CSS
};

// to allocate buffer to be filled
Expand Down
120 changes: 56 additions & 64 deletions src/ast.cpp
Original file line number Diff line number Diff line change
@@ -1,17 +1,8 @@
// sass.hpp must go before all system headers to get the
// __EXTENSIONS__ fix on Solaris.
#include "sass.hpp"

#include "ast.hpp"
#include "context.hpp"
#include "node.hpp"
#include "eval.hpp"
#include "extend.hpp"
#include "emitter.hpp"
#include "color_maps.hpp"
#include "ast_fwd_decl.hpp"
#include <set>
#include <iomanip>
#include <iostream>
#include <algorithm>
#include <functional>
#include <cctype>
#include <locale>

Expand Down Expand Up @@ -80,19 +71,21 @@ namespace Sass {
return i.get_buffer();
}

const std::string AST_Node::to_string() const
const std::string AST_Node::to_css(Sass_Inspect_Options opt) const
{
return to_string({ NESTED, 5 });
opt.output_style = TO_CSS;
Sass_Output_Options out(opt);
Emitter emitter(out);
Inspect i(emitter);
i.in_declaration = true;
// ToDo: inspect should be const
const_cast<AST_Node*>(this)->perform(&i);
return i.get_buffer();
}

/////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////

Expression_Obj Hashed::at(Expression_Obj k) const
const std::string AST_Node::to_string() const
{
if (elements_.count(k))
{ return elements_.at(k); }
else { return {}; }
return to_string({ NESTED, 5 });
}

/////////////////////////////////////////////////////////////////////////
Expand Down Expand Up @@ -128,15 +121,23 @@ namespace Sass {

Block::Block(ParserState pstate, size_t s, bool r)
: Statement(pstate),
Vectorized<Statement_Obj>(s),
Vectorized<Statement_Obj, Block>(s),
is_root_(r)
{ }
Block::Block(const Block* ptr)
: Statement(ptr),
Vectorized<Statement_Obj>(*ptr),
Vectorized<Statement_Obj, Block>(*ptr),
is_root_(ptr->is_root_)
{ }

bool Block::isInvisible() const
{
for (auto item : elements()) {
if (!item->is_invisible()) return false;
}
return true;
}

bool Block::has_content()
{
for (size_t i = 0, L = elements().size(); i < L; ++i) {
Expand All @@ -163,19 +164,20 @@ namespace Sass {
/////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////

Ruleset::Ruleset(ParserState pstate, Selector_List_Obj s, Block_Obj b)
: Has_Block(pstate, b), selector_(s), is_root_(false)
Ruleset::Ruleset(ParserState pstate, SelectorListObj s, Block_Obj b)
: Has_Block(pstate, b), selector_(s), schema_(), is_root_(false)
{ statement_type(RULESET); }
Ruleset::Ruleset(const Ruleset* ptr)
: Has_Block(ptr),
selector_(ptr->selector_),
schema_(ptr->schema_),
is_root_(ptr->is_root_)
{ statement_type(RULESET); }

bool Ruleset::is_invisible() const {
if (Selector_List* sl = Cast<Selector_List>(selector())) {
for (size_t i = 0, L = sl->length(); i < L; ++i)
if (!(*sl)[i]->has_placeholder()) return false;
if (const SelectorList * sl = Cast<SelectorList>(selector())) {
for (size_t i = 0, L = sl->length(); i < L; i += 1)
if (!(*sl)[i]->isInvisible()) return false;
}
return true;
}
Expand Down Expand Up @@ -212,30 +214,7 @@ namespace Sass {
/////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////

Media_Block::Media_Block(ParserState pstate, List_Obj mqs, Block_Obj b)
: Has_Block(pstate, b), media_queries_(mqs)
{ statement_type(MEDIA); }
Media_Block::Media_Block(const Media_Block* ptr)
: Has_Block(ptr), media_queries_(ptr->media_queries_)
{ statement_type(MEDIA); }

bool Media_Block::is_invisible() const {
for (size_t i = 0, L = block()->length(); i < L; ++i) {
Statement_Obj stm = block()->at(i);
if (!stm->is_invisible()) return false;
}
return true;
}

bool Media_Block::bubbles()
{
return true;
}

/////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////

Directive::Directive(ParserState pstate, std::string kwd, Selector_List_Obj sel, Block_Obj b, Expression_Obj val)
Directive::Directive(ParserState pstate, std::string kwd, SelectorListObj sel, Block_Obj b, Expression_Obj val)
: Has_Block(pstate, b), keyword_(kwd), selector_(sel), value_(val) // set value manually if needed
{ statement_type(DIRECTIVE); }
Directive::Directive(const Directive* ptr)
Expand Down Expand Up @@ -450,11 +429,19 @@ namespace Sass {
/////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////

Extension::Extension(ParserState pstate, Selector_List_Obj s)
: Statement(pstate), selector_(s)
ExtendRule::ExtendRule(ParserState pstate, SelectorListObj s)
: Statement(pstate), isOptional_(false), selector_(s), schema_()
{ statement_type(EXTEND); }
Extension::Extension(const Extension* ptr)
: Statement(ptr), selector_(ptr->selector_)
ExtendRule::ExtendRule(ParserState pstate, Selector_Schema_Obj s)
: Statement(pstate), isOptional_(false), selector_(), schema_(s)
{
statement_type(EXTEND);
}
ExtendRule::ExtendRule(const ExtendRule* ptr)
: Statement(ptr),
isOptional_(ptr->isOptional_),
selector_(ptr->selector_),
schema_(ptr->schema_)
{ statement_type(EXTEND); }

/////////////////////////////////////////////////////////////////////////
Expand Down Expand Up @@ -672,14 +659,14 @@ namespace Sass {

Arguments::Arguments(ParserState pstate)
: Expression(pstate),
Vectorized<Argument_Obj>(),
Vectorized<Argument_Obj, Arguments>(),
has_named_arguments_(false),
has_rest_argument_(false),
has_keyword_argument_(false)
{ }
Arguments::Arguments(const Arguments* ptr)
: Expression(ptr),
Vectorized<Argument_Obj>(*ptr),
Vectorized<Argument_Obj, Arguments>(*ptr),
has_named_arguments_(ptr->has_named_arguments_),
has_rest_argument_(ptr->has_rest_argument_),
has_keyword_argument_(ptr->has_keyword_argument_)
Expand Down Expand Up @@ -754,12 +741,12 @@ namespace Sass {
/////////////////////////////////////////////////////////////////////////

Media_Query::Media_Query(ParserState pstate, String_Obj t, size_t s, bool n, bool r)
: Expression(pstate), Vectorized<Media_Query_Expression_Obj>(s),
: Expression(pstate), Vectorized<Media_Query_Expression_Obj, Media_Query>(s),
media_type_(t), is_negated_(n), is_restricted_(r)
{ }
Media_Query::Media_Query(const Media_Query* ptr)
: Expression(ptr),
Vectorized<Media_Query_Expression_Obj>(*ptr),
Vectorized<Media_Query_Expression_Obj, Media_Query>(*ptr),
media_type_(ptr->media_type_),
is_negated_(ptr->is_negated_),
is_restricted_(ptr->is_restricted_)
Expand Down Expand Up @@ -885,13 +872,13 @@ namespace Sass {

Parameters::Parameters(ParserState pstate)
: AST_Node(pstate),
Vectorized<Parameter_Obj>(),
Vectorized<Parameter_Obj, Parameters>(),
has_optional_parameters_(false),
has_rest_parameter_(false)
{ }
Parameters::Parameters(const Parameters* ptr)
: AST_Node(ptr),
Vectorized<Parameter_Obj>(*ptr),
Vectorized<Parameter_Obj, Parameters>(*ptr),
has_optional_parameters_(ptr->has_optional_parameters_),
has_rest_parameter_(ptr->has_rest_parameter_)
{ }
Expand Down Expand Up @@ -923,8 +910,13 @@ namespace Sass {
/////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////

// If you forget to add a class here you will get
// undefined reference to `vtable for Sass::Class'

IMPLEMENT_AST_OPERATORS(Ruleset);
IMPLEMENT_AST_OPERATORS(Media_Block);
IMPLEMENT_AST_OPERATORS(MediaRule);
IMPLEMENT_AST_OPERATORS(CssMediaRule);
IMPLEMENT_AST_OPERATORS(CssMediaQuery);
IMPLEMENT_AST_OPERATORS(Import);
IMPLEMENT_AST_OPERATORS(Import_Stub);
IMPLEMENT_AST_OPERATORS(Directive);
Expand All @@ -934,7 +926,7 @@ namespace Sass {
IMPLEMENT_AST_OPERATORS(For);
IMPLEMENT_AST_OPERATORS(If);
IMPLEMENT_AST_OPERATORS(Mixin_Call);
IMPLEMENT_AST_OPERATORS(Extension);
IMPLEMENT_AST_OPERATORS(ExtendRule);
IMPLEMENT_AST_OPERATORS(Media_Query);
IMPLEMENT_AST_OPERATORS(Media_Query_Expression);
IMPLEMENT_AST_OPERATORS(Debug);
Expand Down
Loading

0 comments on commit 1312e93

Please sign in to comment.