carto/lib/mess/tree/element.js
2011-01-05 14:23:28 -05:00

36 lines
984 B
JavaScript

(function (tree) {
tree.Element = function (combinator, value) {
this.combinator = combinator instanceof tree.Combinator ?
combinator : new(tree.Combinator)(combinator);
this.value = value.toCSS ? value.toCSS() : value.trim();
};
tree.Element.prototype.toCSS = function (env) {
// make names XML-safe in a dreadfully simple manner
return this.combinator.toCSS(env || {}) + this.value
.replace('#', 'id-')
.replace('.', 'class-');
};
tree.Combinator = function (value) {
if (value === ' ') {
this.value = ' ';
} else {
this.value = value ? value.trim() : "";
}
};
tree.Combinator.prototype.toCSS = function (env) {
return {
'' : '',
' ' : ' ',
'&' : '',
':' : ' :',
'::': '::',
'+' : env.compress ? '+' : ' + ',
'~' : env.compress ? '~' : ' ~ ',
'>' : env.compress ? '>' : ' > '
}[this.value];
};
})(require('mess/tree'));