Commit 0086b382 by Aaron Leung

Cleaning up and correcting the nested-style emitter. Now handles arbitrarily…

Cleaning up and correcting the nested-style emitter. Now handles arbitrarily nested rule-less rulesets. New spec test added and passed.
parent ba8709ad
......@@ -17,7 +17,9 @@ namespace Sass {
break;
}
}
return output.str();
string retval(output.str());
if (!retval.empty()) retval.resize(retval.size()-1);
return retval;
}
}
\ No newline at end of file
......@@ -71,14 +71,35 @@ namespace Sass {
void Node::emit_nested_css(stringstream& buf,
const string& prefix,
size_t depth) {
switch(type) {
case selector:
if (!prefix.empty()) buf << " ";
buf << string(token);
string indentation(2 * depth, ' ');
bool has_rules, has_nested_rulesets;
if (type == ruleset) {
has_rules = !(children[1].children.empty());
has_nested_rulesets = !(children[1].opt_children.empty());
}
switch (type) {
case ruleset:
if (has_rules) {
buf << indentation;
children[0].emit_nested_css(buf, prefix, depth); // selector
buf << " {";
for (int i = 0; i < children[1].children.size(); ++i) {
children[1].children[i].emit_nested_css(buf, "", depth + 1); // rules
}
buf << " }" << endl;
}
if (has_nested_rulesets) {
for (int i = 0; i < children[1].opt_children.size(); ++i) { // do each nested ruleset
children[1].opt_children[i].emit_nested_css(buf, prefix + (prefix.empty() ? "" : " ") + string(children[0].token), depth + (has_rules ? 1 : 0));
}
}
if (depth == 0 && prefix.empty()) buf << endl;
break;
case comment:
buf << string(2 * depth, ' ') << string(token);
if (depth == 0) buf << endl;
case rule:
buf << endl << indentation;
children[0].emit_nested_css(buf, "", depth); // property
children[1].emit_nested_css(buf, "", depth); // values
buf << ";";
break;
case property:
buf << string(token) << ":";
......@@ -88,30 +109,13 @@ namespace Sass {
buf << " " << string(children[i].token);
}
break;
case rule:
buf << string(2 * depth, ' ');
children[0].emit_nested_css(buf, prefix, depth);
children[1].emit_nested_css(buf, prefix, depth);
buf << ";";
break;
case clauses:
if (!children.empty()) {
buf << " {";
for (int i = 0; i < children.size() && children[i].type != Node::null; ++i) {
buf << endl;
children[i].emit_nested_css(buf, prefix, depth + 1);
}
buf << " }" << endl;
}
for (int i = 0; i < opt_children.size(); ++i)
opt_children[i].emit_nested_css(buf, prefix, depth + (children.empty() ? 0 : 1));
case selector:
buf << prefix << (prefix.empty() ? "" : " ") << string(token);
break;
case ruleset:
buf << string(2 * depth, ' ') << prefix;
if (!(children[1].children.empty()))
children[0].emit_nested_css(buf, prefix, depth);
string newprefix(prefix.empty() ? prefix : prefix + " ");
children[1].emit_nested_css(buf, newprefix + string(children[0].token), depth);
case comment:
if (depth != 0) buf << endl;
buf << indentation << string(token);
if (depth == 0) buf << endl;
break;
}
}
......@@ -158,6 +162,7 @@ namespace Sass {
}
string newprefix(prefix.empty() ? prefix : prefix + " ");
children[1].emit_expanded_css(buf, newprefix + string(children[0].token));
if (prefix.empty()) buf << endl;
break;
}
}
......
......@@ -13,11 +13,11 @@ int main(int argc, char* argv[]) {
Document doc(argv[1], 0);
doc.parse_scss();
string output = doc.emit_css(doc.expanded);
cout << output;
cout << endl;
// string output = doc.emit_css(doc.nested);
// string output = doc.emit_css(doc.expanded);
// cout << output;
// cout << endl;
string output = doc.emit_css(doc.nested);
cout << output;
return 0;
}
\ No newline at end of file
div {
span {
color: red;
background: blue;
}
}
div {
color: gray;
empty {
span {
color: red;
background: blue;
}
}
}
empty1 {
empty2 {
div {
blah: blah;
}
}
}
empty1 {
empty2 {
div {
bloo: blee;
empty3 {
span {
blah: blah;
blah: blah;
}
}
}
}
}
div span {
color: red;
background: blue; }
div {
color: gray; }
div empty span {
color: red;
background: blue; }
empty1 empty2 div {
blah: blah; }
empty1 empty2 div {
bloo: blee; }
empty1 empty2 div empty3 span {
blah: blah;
blah: blah; }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment