2010-03-02 04:32:21 +08:00
|
|
|
if (typeof(window) === 'undefined') { var tree = require(require('path').join(__dirname, '..', '..', 'less', 'tree')); }
|
2010-02-24 02:39:05 +08:00
|
|
|
|
2010-03-02 04:32:21 +08:00
|
|
|
tree.Ruleset = function Ruleset(selectors, rules) {
|
2010-02-24 02:39:05 +08:00
|
|
|
this.selectors = selectors;
|
|
|
|
this.rules = rules;
|
|
|
|
};
|
2010-03-02 04:32:21 +08:00
|
|
|
tree.Ruleset.prototype = {
|
2010-02-24 02:39:05 +08:00
|
|
|
variables: function () {
|
|
|
|
return this.rules.filter(function (r) {
|
2010-03-02 04:32:21 +08:00
|
|
|
if (r instanceof tree.Rule && r.variable === true) { return r }
|
2010-02-24 02:39:05 +08:00
|
|
|
});
|
|
|
|
},
|
2010-02-28 14:06:54 +08:00
|
|
|
toCSS: function (context, env) {
|
|
|
|
var css = [],
|
|
|
|
rules = [],
|
|
|
|
rulesets = [],
|
|
|
|
paths = [],
|
|
|
|
selector;
|
2010-02-24 02:39:05 +08:00
|
|
|
|
2010-02-28 14:06:54 +08:00
|
|
|
if (! this.root) {
|
|
|
|
if (context.length === 0) {
|
2010-03-01 01:50:41 +08:00
|
|
|
paths = this.selectors.map(function (s) { return [s] });
|
2010-02-28 14:06:54 +08:00
|
|
|
} else {
|
|
|
|
for (var s = 0; s < this.selectors.length; s++) {
|
|
|
|
for (var c = 0; c < context.length; c++) {
|
2010-03-01 01:50:41 +08:00
|
|
|
paths.push(context[c].concat([this.selectors[s]]));
|
2010-02-28 14:06:54 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2010-02-24 02:39:05 +08:00
|
|
|
env.frames.unshift(this);
|
|
|
|
|
|
|
|
for (var i = 0; i < this.rules.length; i++) {
|
2010-03-02 04:32:21 +08:00
|
|
|
if (this.rules[i] instanceof tree.Ruleset) {
|
2010-02-28 14:06:54 +08:00
|
|
|
rulesets.push(this.rules[i].toCSS(paths, env));
|
2010-02-24 02:39:05 +08:00
|
|
|
} else {
|
2010-02-28 03:13:28 +08:00
|
|
|
if (this.rules[i].toCSS) {
|
|
|
|
rules.push(this.rules[i].toCSS(env));
|
|
|
|
} else if (this.rules[i].value) {
|
2010-02-24 02:39:05 +08:00
|
|
|
rules.push(this.rules[i].value.toString());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2010-02-28 14:06:54 +08:00
|
|
|
|
|
|
|
rulesets = rulesets.join('');
|
2010-02-24 02:39:05 +08:00
|
|
|
|
2010-02-28 14:06:54 +08:00
|
|
|
if (this.root) {
|
|
|
|
css.push(rules.join('\n'));
|
|
|
|
} else {
|
|
|
|
if (rules.length > 0) {
|
2010-03-01 01:50:41 +08:00
|
|
|
selector = paths.map(function (p) {
|
2010-03-02 04:32:21 +08:00
|
|
|
return new(tree.Selector)(p).toCSS().trim();
|
2010-03-01 01:50:41 +08:00
|
|
|
}).join(paths.length > 3 ? ',\n' : ', ');
|
2010-02-28 14:06:54 +08:00
|
|
|
css.push(selector, " {\n " + rules.join('\n ') + "\n}\n");
|
2010-02-24 02:39:05 +08:00
|
|
|
}
|
|
|
|
}
|
2010-02-28 14:06:54 +08:00
|
|
|
css.push(rulesets);
|
2010-02-24 02:39:05 +08:00
|
|
|
env.frames.shift();
|
|
|
|
|
2010-02-28 14:06:54 +08:00
|
|
|
for (var p = 0; p < paths.length; p++) { paths[p].pop() }
|
|
|
|
|
2010-02-24 02:39:05 +08:00
|
|
|
return css.join('');
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|