Skip to content

Commit 5e3de8f

Browse files
delapuentexzyfer
authored andcommitted
Modifying emitter to take brackets into account.
First, instead of directly writting the left or right parenthesis characters, now functions are called for left or right delimiters and change in case of parenthesis or brackets. Second, in case of bracketed list, always add the brackets. Also add comma in case it is a one-item comma-separate list. Otherwise it would become a one-item space-separated list. Third, libsass prevent from emitting invisible items. Thus, an empty space / comma separated list won't be printed at all but bracketed list are never invisible, event the empty ones.
1 parent 922704c commit 5e3de8f

File tree

3 files changed

+38
-11
lines changed

3 files changed

+38
-11
lines changed

src/inspect.cpp

Lines changed: 34 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -360,10 +360,19 @@ namespace Sass {
360360
append_string(")");
361361
}
362362

363+
std::string Inspect::lbracket(List_Ptr list) {
364+
return list->is_bracketed() ? "[" : "(";
365+
}
366+
367+
std::string Inspect::rbracket(List_Ptr list) {
368+
return list->is_bracketed() ? "]" : ")";
369+
}
370+
363371
void Inspect::operator()(List_Ptr list)
364372
{
365-
if (output_style() == TO_SASS && list->empty()) {
366-
append_string("()");
373+
if (list->empty() && (output_style() == TO_SASS || list->is_bracketed())) {
374+
append_string(lbracket(list));
375+
append_string(rbracket(list));
367376
return;
368377
}
369378
std::string sep(list->separator() == SASS_SPACE ? " " : ",");
@@ -374,19 +383,24 @@ namespace Sass {
374383

375384
bool was_space_array = in_space_array;
376385
bool was_comma_array = in_comma_array;
386+
// if the list is bracketed, always include the left bracket
387+
if (list->is_bracketed()) {
388+
append_string(lbracket(list));
389+
}
377390
// probably ruby sass eqivalent of element_needs_parens
378-
if (output_style() == TO_SASS &&
391+
else if (output_style() == TO_SASS &&
379392
list->length() == 1 &&
380393
!list->from_selector() &&
381394
!SASS_MEMORY_CAST(List, list->at(0)) &&
382-
!SASS_MEMORY_CAST(Selector_List, list->at(0))) {
383-
append_string("(");
395+
!SASS_MEMORY_CAST(Selector_List, list->at(0))
396+
) {
397+
append_string(lbracket(list));
384398
}
385399
else if (!in_declaration && (list->separator() == SASS_HASH ||
386400
(list->separator() == SASS_SPACE && in_space_array) ||
387401
(list->separator() == SASS_COMMA && in_comma_array)
388402
)) {
389-
append_string("(");
403+
append_string(lbracket(list));
390404
}
391405

392406
if (list->separator() == SASS_SPACE) in_space_array = true;
@@ -415,19 +429,29 @@ namespace Sass {
415429

416430
in_comma_array = was_comma_array;
417431
in_space_array = was_space_array;
432+
433+
// if the list is bracketed, always include the right bracket
434+
if (list->is_bracketed()) {
435+
if (list->separator() == SASS_COMMA && list->size() == 1) {
436+
append_string(",");
437+
}
438+
append_string(rbracket(list));
439+
}
418440
// probably ruby sass eqivalent of element_needs_parens
419-
if (output_style() == TO_SASS &&
441+
else if (output_style() == TO_SASS &&
420442
list->length() == 1 &&
421443
!list->from_selector() &&
422444
!SASS_MEMORY_CAST(List, list->at(0)) &&
423-
!SASS_MEMORY_CAST(Selector_List, list->at(0))) {
424-
append_string(",)");
445+
!SASS_MEMORY_CAST(Selector_List, list->at(0))
446+
) {
447+
append_string(",");
448+
append_string(rbracket(list));
425449
}
426450
else if (!in_declaration && (list->separator() == SASS_HASH ||
427451
(list->separator() == SASS_SPACE && in_space_array) ||
428452
(list->separator() == SASS_COMMA && in_comma_array)
429453
)) {
430-
append_string(")");
454+
append_string(rbracket(list));
431455
}
432456

433457
}

src/inspect.hpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,9 @@ namespace Sass {
9292
virtual void operator()(Complex_Selector_Ptr);
9393
virtual void operator()(Selector_List_Ptr);
9494

95+
virtual std::string lbracket(List_Ptr);
96+
virtual std::string rbracket(List_Ptr);
97+
9598
// template <typename U>
9699
// void fallback(U x) { fallback_impl(reinterpret_cast<AST_Node_Ptr>(x)); }
97100
};

src/output.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,7 @@ namespace Sass {
155155
Expression_Ptr item = &list->at(list_i);
156156
if (!item->is_invisible()) all_invisible = false;
157157
}
158-
if (all_invisible) bPrintExpression = false;
158+
if (all_invisible && !list->is_bracketed()) bPrintExpression = false;
159159
}
160160
}
161161
// Print if OK

0 commit comments

Comments
 (0)