carto/lib/mess/tree/rule.js

64 lines
1.6 KiB
JavaScript
Raw Normal View History

(function(tree) {
tree.Rule = function(name, value, important, index) {
2010-02-24 02:39:05 +08:00
this.name = name;
this.value = (value instanceof tree.Value) ?
value : new(tree.Value)([value]);
2010-06-28 04:40:12 +08:00
this.important = important ? ' ' + important.trim() : '';
this.index = index;
2010-12-14 11:48:10 +08:00
this.symbolizer = tree.Reference.symbolizer(this.name);
2010-02-24 02:39:05 +08:00
if (name.charAt(0) === '@') {
this.variable = true;
} else { this.variable = false }
};
tree.Rule.prototype.toCSS = function(env) {
if (!tree.Reference.validSelector(this.name)) {
throw {
message: 'Unrecognized selector: ' + this.name,
index: this.index
2011-01-19 03:26:57 +08:00
};
}
if (!tree.Reference.validValue(env, this.name, this.value)) {
2010-12-14 08:10:18 +08:00
throw {
message: 'Invalid value for ' +
2010-12-14 11:48:10 +08:00
this.name +
', a ' +
2010-12-14 11:48:10 +08:00
tree.Reference.selector(this.name).type +
' is expected. ' + require('sys').inspect(this.value) +
' was given.',
2010-12-14 08:10:18 +08:00
index: this.index
2011-01-19 03:26:57 +08:00
};
2010-12-14 08:10:18 +08:00
}
if (this.variable) { return '' }
2010-02-26 06:56:21 +08:00
else {
2011-01-05 07:51:12 +08:00
return tree.Reference.selectorName(this.name) +
'="' +
2010-12-14 08:10:18 +08:00
this.value.toCSS(env, this.name) +
2011-01-05 07:51:12 +08:00
'"';
2010-02-26 06:56:21 +08:00
}
2010-02-24 02:39:05 +08:00
};
tree.Rule.prototype.eval = function(context) {
return new(tree.Rule)(this.name,
this.value.eval(context),
this.important,
this.index);
};
tree.Shorthand = function(a, b) {
this.a = a;
this.b = b;
};
tree.Shorthand.prototype = {
toCSS: function(env) {
return this.a.toCSS(env) + '/' + this.b.toCSS(env);
},
eval: function() { return this }
};
2011-01-06 03:23:28 +08:00
})(require('mess/tree'));