proper nested selector rendering, wooh.

This commit is contained in:
cloudhead 2010-02-28 01:06:54 -05:00
parent cd764a2d8c
commit 8ac1bdf06c

View File

@ -9,15 +9,29 @@ node.Ruleset.prototype = {
if (r instanceof node.Rule && r.variable === true) { return r } if (r instanceof node.Rule && r.variable === true) { return r }
}); });
}, },
toCSS: function (path, env) { toCSS: function (context, env) {
var css = [], rules = [], rulesets = []; var css = [],
rules = [],
rulesets = [],
paths = [],
selector;
if (! this.root) path.push(this.selectors.map(function (s) { return s.toCSS(env) })); if (! this.root) {
if (context.length === 0) {
paths = this.selectors.map(function (s) { return [s.toCSS(env)] });
} else {
for (var s = 0; s < this.selectors.length; s++) {
for (var c = 0; c < context.length; c++) {
paths.push(context[c].concat([this.selectors[s].toCSS()]));
}
}
}
}
env.frames.unshift(this); env.frames.unshift(this);
for (var i = 0; i < this.rules.length; i++) { for (var i = 0; i < this.rules.length; i++) {
if (this.rules[i] instanceof node.Ruleset) { if (this.rules[i] instanceof node.Ruleset) {
rulesets.push(this.rules[i].toCSS(path, env)); rulesets.push(this.rules[i].toCSS(paths, env));
} else { } else {
if (this.rules[i].toCSS) { if (this.rules[i].toCSS) {
rules.push(this.rules[i].toCSS(env)); rules.push(this.rules[i].toCSS(env));
@ -27,18 +41,22 @@ node.Ruleset.prototype = {
} }
} }
if (rules.length > 0) { rulesets = rulesets.join('');
if (path.length > 0) {
css.push(path.join('').trim(), if (this.root) {
" {\n " + rules.join('\n ') + "\n}\n", css.push(rules.join('\n'));
rulesets.join('')); } else {
} else { if (rules.length > 0) {
css.push(rules.join('\n'), rulesets.join('')); selector = paths.map(function (p) { return p.join('').trim() })
.join(paths.length > 3 ? ',\n' : ', ').trim();
css.push(selector, " {\n " + rules.join('\n ') + "\n}\n");
} }
} }
path.pop(); css.push(rulesets);
env.frames.shift(); env.frames.shift();
for (var p = 0; p < paths.length; p++) { paths[p].pop() }
return css.join(''); return css.join('');
} }
}; };