This repository has been archived by the owner on Mar 13, 2021. It is now read-only.
forked from sass/libsass
-
Notifications
You must be signed in to change notification settings - Fork 1
/
contextualize.cpp
137 lines (119 loc) · 4.15 KB
/
contextualize.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
#include "contextualize.hpp"
#include "ast.hpp"
#include "eval.hpp"
#include "backtrace.hpp"
#include "to_string.hpp"
#include "parser.hpp"
namespace Sass {
Contextualize::Contextualize(Context& ctx, Eval* eval, Env* env, Backtrace* bt, Selector* placeholder, Selector* extender)
: ctx(ctx), eval(eval), env(env), parent(0), backtrace(bt), placeholder(placeholder), extender(extender)
{ }
Contextualize::~Contextualize() { }
Selector* Contextualize::fallback_impl(AST_Node* n)
{ return parent; }
Contextualize* Contextualize::with(Selector* s, Env* e, Backtrace* bt, Selector* p, Selector* ex)
{
parent = s;
env = e;
backtrace = bt;
placeholder = p;
extender = ex;
return this;
}
Selector* Contextualize::operator()(Selector_Schema* s)
{
To_String to_string;
string result_str(s->contents()->perform(eval->with(env, backtrace))->perform(&to_string));
result_str += '{'; // the parser looks for a brace to end the selector
Selector* result_sel = Parser::from_c_str(result_str.c_str(), ctx, s->path(), s->line()).parse_selector_group();
return result_sel->perform(this);
}
Selector* Contextualize::operator()(Selector_Group* s)
{
Selector_Group* p = static_cast<Selector_Group*>(parent);
Selector_Group* ss = 0;
if (p) {
ss = new (ctx.mem) Selector_Group(s->path(), s->line(), p->length() * s->length());
for (size_t i = 0, L = p->length(); i < L; ++i) {
for (size_t j = 0, L = s->length(); j < L; ++j) {
parent = (*p)[i];
Selector_Combination* comb = static_cast<Selector_Combination*>((*s)[j]->perform(this));
if (comb) *ss << comb;
}
}
}
else {
ss = new (ctx.mem) Selector_Group(s->path(), s->line(), s->length());
for (size_t j = 0, L = s->length(); j < L; ++j) {
Selector_Combination* comb = static_cast<Selector_Combination*>((*s)[j]->perform(this));
if (comb) *ss << comb;
}
}
return ss->length() ? ss : 0;
}
Selector* Contextualize::operator()(Selector_Combination* s)
{
To_String to_string;
Selector_Combination* ss = new (ctx.mem) Selector_Combination(*s);
if (ss->head()) {
ss->head(static_cast<Simple_Selector_Sequence*>(s->head()->perform(this)));
}
if (ss->tail()) {
ss->tail(static_cast<Selector_Combination*>(s->tail()->perform(this)));
}
if (!ss->head() && ss->combinator() == Selector_Combination::ANCESTOR_OF) {
return ss->tail();
}
else {
return ss;
}
}
Selector* Contextualize::operator()(Simple_Selector_Sequence* s)
{
To_String to_string;
if (placeholder && extender && s->perform(&to_string) == placeholder->perform(&to_string)) {
return extender;
}
Simple_Selector_Sequence* ss = new (ctx.mem) Simple_Selector_Sequence(s->path(), s->line(), s->length());
for (size_t i = 0, L = s->length(); i < L; ++i) {
Simple_Selector* simp = static_cast<Simple_Selector*>((*s)[i]->perform(this));
if (simp) *ss << simp;
}
return ss->length() ? ss : 0;
}
Selector* Contextualize::operator()(Negated_Selector* s)
{
Selector* old_parent = parent;
parent = 0;
Negated_Selector* neg = new (ctx.mem) Negated_Selector(s->path(),
s->line(),
s->selector()->perform(this));
parent = old_parent;
return neg;
}
Selector* Contextualize::operator()(Pseudo_Selector* s)
{ return s; }
Selector* Contextualize::operator()(Attribute_Selector* s)
{ return s; }
Selector* Contextualize::operator()(Selector_Qualifier* s)
{ return s; }
Selector* Contextualize::operator()(Type_Selector* s)
{ return s; }
Selector* Contextualize::operator()(Selector_Placeholder* s)
{
To_String to_string;
if (placeholder && extender && s->perform(&to_string) == placeholder->perform(&to_string)) {
return extender;
}
else {
return s;
}
}
Selector* Contextualize::operator()(Selector_Reference* s)
{
if (!parent) return 0;
Selector_Reference* ss = new (ctx.mem) Selector_Reference(*s);
ss->selector(parent);
return ss;
}
}