Skip to content

Commit 6ec5563

Browse files
committed
Create quoted strings coming via API
Make sure quoted strings coming via API are created as a String_Quoted instance. Adjust string "quoted" property sent via API on output. Trance lifecycle of string objects.
1 parent 067b137 commit 6ec5563

File tree

6 files changed

+66
-8
lines changed

6 files changed

+66
-8
lines changed

ast.hpp

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030

3131
#endif
3232

33+
#include "debug.hpp"
3334
#include "util.hpp"
3435
#include "units.hpp"
3536
#include "context.hpp"
@@ -1367,16 +1368,16 @@ namespace Sass {
13671368
public:
13681369
String_Constant(ParserState pstate, string val)
13691370
: String(pstate), quote_mark_(0), can_compress_whitespace_(false), value_(read_css_string(val)), hash_(0)
1370-
{ }
1371+
{ TRACEINST(this) << "String_Constant created " << this; }
13711372
String_Constant(ParserState pstate, const char* beg)
13721373
: String(pstate), quote_mark_(0), can_compress_whitespace_(false), value_(read_css_string(string(beg))), hash_(0)
1373-
{ }
1374+
{ TRACEINST(this) << "String_Constant created " << this; }
13741375
String_Constant(ParserState pstate, const char* beg, const char* end)
13751376
: String(pstate), quote_mark_(0), can_compress_whitespace_(false), value_(read_css_string(string(beg, end-beg))), hash_(0)
1376-
{ }
1377+
{ TRACEINST(this) << "String_Constant created " << this; }
13771378
String_Constant(ParserState pstate, const Token& tok)
13781379
: String(pstate), quote_mark_(0), can_compress_whitespace_(false), value_(read_css_string(string(tok.begin, tok.end))), hash_(0)
1379-
{ }
1380+
{ TRACEINST(this) << "String_Constant created " << this; }
13801381
string type() { return "string"; }
13811382
static string type_name() { return "string"; }
13821383

@@ -1404,6 +1405,16 @@ namespace Sass {
14041405
static char double_quote() { return '"'; }
14051406
static char single_quote() { return '\''; }
14061407

1408+
friend std::ostream& operator << (std::ostream& os, String_Constant& sq) {
1409+
os << "(" << (sq.quote_mark_ ? sq.quote_mark_ : '0') << ",\"" << sq.value_ << "\")";
1410+
return os;
1411+
}
1412+
1413+
friend std::ostream& operator << (std::ostream& os, String_Constant* sq) {
1414+
os << "(" << (sq->quote_mark_ ? sq->quote_mark_ : '0') << ",\"" << sq->value_ << "\")";
1415+
return os;
1416+
}
1417+
14071418
ATTACH_OPERATIONS();
14081419
};
14091420

@@ -1416,6 +1427,7 @@ namespace Sass {
14161427
: String_Constant(pstate, val)
14171428
{
14181429
value_ = unquote(value_, &quote_mark_);
1430+
TRACEINST(this) << "String_Quoted created " << this;
14191431
}
14201432
ATTACH_OPERATIONS();
14211433
};

eval.cpp

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -734,11 +734,14 @@ namespace Sass {
734734
}
735735
else if (value->concrete_type() == Expression::STRING) {
736736
if (auto str = dynamic_cast<String_Quoted*>(value)) {
737+
TRACEINST(str) << "dynamic_cast to String_Quoted worked " << str;
737738
value = new (ctx.mem) String_Quoted(*str);
738739
} else if (auto str = dynamic_cast<String_Constant*>(value)) {
739740
if (str->quote_mark()) {
741+
TRACEINST(str) << "dynamic_cast to String_Quoted did not work, but we have quote " << str;
740742
value = new (ctx.mem) String_Quoted(str->pstate(), str->perform(&to_string));
741743
} else {
744+
TRACEINST(str) << "dynamic_cast to String_Quoted did not work, we are String_Constant " << str;
742745
value = new (ctx.mem) String_Constant(str->pstate(), unquote(str->value()));
743746
}
744747
}
@@ -856,18 +859,26 @@ namespace Sass {
856859

857860
string Eval::interpolation(Expression* s) {
858861
if (String_Quoted* str_quoted = dynamic_cast<String_Quoted*>(s)) {
862+
TRACEINST(str_quoted) << "dynamic_cast to String_Quoted worked " << str_quoted;
859863
if (str_quoted->quote_mark()) {
860864
if (str_quoted->quote_mark() == '*' || str_quoted->is_delayed()) {
865+
TRACEINST(str_quoted) << "... will do interpolation()";
861866
return interpolation(new (ctx.mem) String_Constant(*str_quoted));
862867
} else {
868+
TRACEINST(str_quoted) << "... will string_escape()";
863869
return string_escape(quote(str_quoted->value(), str_quoted->quote_mark()));
864870
}
865871
} else {
872+
TRACEINST(str_quoted) << "dynamic_cast to String_Quoted failed, will evacuate_escapes()";
866873
return evacuate_escapes(str_quoted->value());
867874
}
868875
} else if (String_Constant* str_constant = dynamic_cast<String_Constant*>(s)) {
876+
TRACEINST(str_constant) << "dynamic_cast to String_Constant worked";
869877
string str = str_constant->value();
870-
if (!str_constant->quote_mark()) str = unquote(str);
878+
if (!str_constant->quote_mark()) {
879+
TRACEINST(str_constant) << "... but still need to unquote()";
880+
str = unquote(str);
881+
}
871882
return evacuate_escapes(str);
872883
} else if (String_Schema* str_schema = dynamic_cast<String_Schema*>(s)) {
873884
string res = "";
@@ -1011,14 +1022,22 @@ namespace Sass {
10111022
Expression* feature = e->feature();
10121023
feature = (feature ? feature->perform(this) : 0);
10131024
if (feature && dynamic_cast<String_Quoted*>(feature)) {
1025+
String_Quoted *qfeature = dynamic_cast<String_Quoted*>(feature);
1026+
TRACEINST(qfeature) << "dynamic_cast to String_Quoted worked " << qfeature;
10141027
feature = new (ctx.mem) String_Constant(feature->pstate(),
10151028
dynamic_cast<String_Quoted*>(feature)->value());
1029+
} else {
1030+
TRACEINST(feature) << "dynamic_cast to String_Quoted did not work for " << feature;
10161031
}
10171032
Expression* value = e->value();
10181033
value = (value ? value->perform(this) : 0);
10191034
if (value && dynamic_cast<String_Quoted*>(value)) {
1035+
String_Quoted *qvalue = dynamic_cast<String_Quoted*>(value);
1036+
TRACEINST(qvalue) << "dynamic_cast to String_Quoted worked " << qvalue;
10201037
value = new (ctx.mem) String_Constant(value->pstate(),
10211038
dynamic_cast<String_Quoted*>(value)->value());
1039+
} else {
1040+
TRACEINST(value) << "dynamic_cast to String_Quoted did not work for " << value;
10221041
}
10231042
return new (ctx.mem) Media_Query_Expression(e->pstate(),
10241043
feature,
@@ -1358,7 +1377,11 @@ namespace Sass {
13581377
e = new (ctx.mem) Color(pstate, sass_color_get_r(v), sass_color_get_g(v), sass_color_get_b(v), sass_color_get_a(v));
13591378
} break;
13601379
case SASS_STRING: {
1361-
e = new (ctx.mem) String_Constant(pstate, sass_string_get_value(v));
1380+
if (sass_string_is_quoted(v))
1381+
e = new (ctx.mem) String_Quoted(pstate, sass_string_get_value(v));
1382+
else {
1383+
e = new (ctx.mem) String_Constant(pstate, sass_string_get_value(v));
1384+
}
13621385
} break;
13631386
case SASS_LIST: {
13641387
List* l = new (ctx.mem) List(pstate, sass_list_get_length(v), sass_list_get_separator(v) == SASS_COMMA ? List::COMMA : List::SPACE);

output.cpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,7 @@ namespace Sass {
128128
string val(valConst->value());
129129
if (dynamic_cast<String_Quoted*>(valConst)) {
130130
if (!valConst->quote_mark() && val.empty()) {
131+
131132
bPrintExpression = false;
132133
}
133134
}
@@ -372,27 +373,35 @@ namespace Sass {
372373

373374
void Output::operator()(String_Quoted* s)
374375
{
376+
TRACEINST(s) << "This should be a quoted string... " << s;
375377
if (s->quote_mark()) {
378+
TRACEINST(s) << "... it even has a quote mark property, sending with quote marks";
376379
append_token(quote(s->value(), s->quote_mark()), s);
377380
} else if (!in_comment) {
381+
TRACEINST(s) << "... no quote mark(?), sending via string_to_output";
378382
append_token(string_to_output(s->value()), s);
379383
} else {
384+
TRACEINST(s) << "... no quote mark(?), sending directly (in comment)";
380385
append_token(s->value(), s);
381386
}
382387
}
383388

384389
void Output::operator()(String_Constant* s)
385390
{
391+
TRACEINST(s) << "This should be a constant string... " << s;
386392
if (String_Quoted* quoted = dynamic_cast<String_Quoted*>(s)) {
393+
TRACEINST(s) << "... but dynamic_cast<String_Quoted*> worked";
387394
return Output::operator()(quoted);
388395
} else {
389396
string value(s->value());
390397
if (s->can_compress_whitespace() && output_style() == COMPRESSED) {
391398
value.erase(std::remove_if(value.begin(), value.end(), ::isspace), value.end());
392399
}
393400
if (!in_comment) {
401+
TRACEINST(s) << "... sending via string_to_output";
394402
append_token(string_to_output(value), s);
395403
} else {
404+
TRACEINST(s) << "... sending directly (in comment)";
396405
append_token(value, s);
397406
}
398407
}

parser.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1381,6 +1381,7 @@ namespace Sass {
13811381

13821382
if (lex< identifier >()) {
13831383
String_Constant* str = new (ctx.mem) String_Quoted(pstate, lexed);
1384+
TRACEINST(str) << "We have just created a new instance " << str;
13841385
// Dont' delay this string if it is a name color. Fixes #652.
13851386
str->is_delayed(ctx.names_to_colors.count(unquote(lexed)) == 0);
13861387
return str;

to_c.cpp

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,19 @@ namespace Sass {
1919
{ return sass_make_color(c->r(), c->g(), c->b(), c->a()); }
2020

2121
Sass_Value* To_C::operator()(String_Constant* s)
22-
{ return sass_make_string(s->value().c_str()); }
22+
{
23+
if (s->quote_mark()) {
24+
TRACEINST(s) << "We got String_Constant, but we convert quoted value to C" << s;
25+
return sass_make_qstring(s->value().c_str());
26+
} else {
27+
TRACEINST(s) << "Converting unquoted value to C" << s;
28+
return sass_make_string(s->value().c_str());
29+
}
30+
}
31+
32+
Sass_Value* To_C::operator()(String_Quoted* s)
33+
{ TRACEINST(s) << "Converting quoted value to C" << s;
34+
return sass_make_qstring(s->value().c_str()); }
2335

2436
Sass_Value* To_C::operator()(List* l)
2537
{

to_c.hpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ namespace Sass {
2929
Sass_Value* operator()(Number*);
3030
Sass_Value* operator()(Color*);
3131
Sass_Value* operator()(String_Constant*);
32+
Sass_Value* operator()(String_Quoted*);
3233
Sass_Value* operator()(List*);
3334
Sass_Value* operator()(Map*);
3435
Sass_Value* operator()(Null*);
@@ -41,4 +42,4 @@ namespace Sass {
4142

4243
}
4344

44-
#endif
45+
#endif

0 commit comments

Comments
 (0)