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 { ...@@ -17,7 +17,9 @@ namespace Sass {
break; 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 { ...@@ -71,14 +71,35 @@ namespace Sass {
void Node::emit_nested_css(stringstream& buf, void Node::emit_nested_css(stringstream& buf,
const string& prefix, const string& prefix,
size_t depth) { size_t depth) {
switch(type) { string indentation(2 * depth, ' ');
case selector: bool has_rules, has_nested_rulesets;
if (!prefix.empty()) buf << " "; if (type == ruleset) {
buf << string(token); 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; break;
case comment: case rule:
buf << string(2 * depth, ' ') << string(token); buf << endl << indentation;
if (depth == 0) buf << endl; children[0].emit_nested_css(buf, "", depth); // property
children[1].emit_nested_css(buf, "", depth); // values
buf << ";";
break; break;
case property: case property:
buf << string(token) << ":"; buf << string(token) << ":";
...@@ -88,30 +109,13 @@ namespace Sass { ...@@ -88,30 +109,13 @@ namespace Sass {
buf << " " << string(children[i].token); buf << " " << string(children[i].token);
} }
break; break;
case rule: case selector:
buf << string(2 * depth, ' '); buf << prefix << (prefix.empty() ? "" : " ") << string(token);
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));
break; break;
case ruleset: case comment:
buf << string(2 * depth, ' ') << prefix; if (depth != 0) buf << endl;
if (!(children[1].children.empty())) buf << indentation << string(token);
children[0].emit_nested_css(buf, prefix, depth); if (depth == 0) buf << endl;
string newprefix(prefix.empty() ? prefix : prefix + " ");
children[1].emit_nested_css(buf, newprefix + string(children[0].token), depth);
break; break;
} }
} }
...@@ -158,6 +162,7 @@ namespace Sass { ...@@ -158,6 +162,7 @@ namespace Sass {
} }
string newprefix(prefix.empty() ? prefix : prefix + " "); string newprefix(prefix.empty() ? prefix : prefix + " ");
children[1].emit_expanded_css(buf, newprefix + string(children[0].token)); children[1].emit_expanded_css(buf, newprefix + string(children[0].token));
if (prefix.empty()) buf << endl;
break; break;
} }
} }
......
...@@ -13,11 +13,11 @@ int main(int argc, char* argv[]) { ...@@ -13,11 +13,11 @@ int main(int argc, char* argv[]) {
Document doc(argv[1], 0); Document doc(argv[1], 0);
doc.parse_scss(); doc.parse_scss();
string output = doc.emit_css(doc.expanded); // string output = doc.emit_css(doc.expanded);
cout << output;
cout << endl;
// string output = doc.emit_css(doc.nested);
// cout << output; // cout << output;
// cout << endl;
string output = doc.emit_css(doc.nested);
cout << output;
return 0; 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