First pass at tilemill-friendly validation

This commit is contained in:
Tom MacWright 2011-01-24 12:50:42 -05:00
parent 3a727b1e47
commit 76122df6f2
5 changed files with 30 additions and 14 deletions

View File

@ -284,6 +284,10 @@ mess.Parser = function Parser(env) {
root = new tree.Ruleset([], $(this.parsers.primary));
root.root = true;
root.getLine = function(index) {
return index ? (input.slice(0, index).match(/\n/g) || '').length : null;
},
/**
* Get an array of Ruleset objects, flattened
* and sorted according to specificitySort
@ -293,10 +297,11 @@ mess.Parser = function Parser(env) {
return function(options, variables) {
options = options || {};
options.compress = 'compress' in options ?
options.compress : (env.compress || false);
var returnErrors = 'returnErrors' in options ? options.returnErrors : (env.returnErrors || false);
var returnErrors = 'returnErrors' in options ?
options.returnErrors :
(env.returnErrors || false);
env.errors = returnErrors ? [] : null;
try {

View File

@ -347,7 +347,7 @@ mess.Renderer = function Renderer(env) {
}).forEach(function(r) {
for (var i = 0; i < r.rules.length; i++) {
if (r.rules[i].eval) r.rules[i] = r.rules[i].eval(env);
properties.push(r.rules[i].toString());
properties.push(r.rules[i].toString(env));
}
});
return properties.join('');

View File

@ -71,15 +71,26 @@ tree.Definition.prototype.unique_rules = function() {
tree.Definition.prototype.toXML = function(env) {
if (this._xml) return this._xml;
var sym = this.symbolizers();
if (sym.length !== 1) {
if (!env.returnErrors && sym.length !== 1) {
throw {
message: 'A single symbolizer is expected'
+ 'in definition compilation'
+ 'in definition compilation'
}
} else {
sym = sym[0];
}
if (env.returnErrors) {
env.error = function(e) {
if (env.errors) {
env.errors.push(e);
}
else {
throw e;
}
};
}
var symname = sym.charAt(0).toUpperCase()
+ sym.slice(1).replace(/\-./, function(str) {
return str[1].toUpperCase();
@ -94,8 +105,7 @@ tree.Definition.prototype.toXML = function(env) {
}).join('\n ')
+ '/>';
var filters = this.selector.combinedFilter();
var filters = this.selector.combinedFilter(env);
return this._xml = ' <Rule>\n ' +
filters + '\n' +

View File

@ -9,7 +9,6 @@ tree.Ruleset = function(selectors, rules) {
tree.Ruleset.prototype = {
eval: function(env) {
var ruleset = new tree.Ruleset(this.selectors, this.rules.slice(0));
ruleset.root = this.root;
// push the current ruleset to the frames stack
@ -112,8 +111,6 @@ tree.Ruleset.prototype = {
});
return this._lookups[key] = rules;
},
flatten: function(result, parentSelectors) {
var selectors = [];
for (var i = 0; i < this.selectors.length; i++) {

View File

@ -53,12 +53,16 @@ tree.Selector.prototype.layers = function(env) {
}).join(' ');
};
tree.Selector.prototype.combinedFilter = function() {
var filters = this.zoom.toXML();
tree.Selector.prototype.combinedFilter = function(env) {
var filters = this.zoom ? this.zoom.toXML() : [];
if (this.filters.length) {
filters.push('<Filter>' + this.filters.map(function(f) {
return '(' + f.toXML().trim() + ')';
// TODO: this is required to support selectors
// that haven't been processed in the render() function.
if (f instanceof tree.Filter) {
return '(' + f.toXML(env).trim() + ')';
}
}).join(' and ') + '</Filter>');
}