Compare commits

...

2 Commits

Author SHA1 Message Date
Tom MacWright c0526392dc Adding field
13 years ago
Tom MacWright 8b5ea80e5d refilter mode for carto
13 years ago

@ -52,7 +52,7 @@ var carto = {
[ 'alpha', 'anonymous', 'call', 'color', 'comment', 'definition', 'dimension',
'directive', 'element', 'expression', 'filterset', 'filter',
'keyword', 'layer', 'mixin', 'operation', 'quoted',
'keyword', 'layer', 'mixin', 'operation', 'quoted', 'field',
'reference', 'rule', 'ruleset', 'selector', 'style', 'url', 'value',
'variable', 'zoom', 'invalid', 'fontset'
].forEach(function(n) {

@ -444,6 +444,14 @@ carto.Parser = function Parser(env) {
}
},
field: function() {
var name;
if (input.charAt(i) !== '[') return;
if (name = $(/^\[([^\]]+)\]/)) {
return new tree.Field(name[1]);
}
},
comparison: function() {
var str;
if (str = $(/^=|!=|<=|>=|<|>/)) {
@ -461,7 +469,9 @@ carto.Parser = function Parser(env) {
//
keyword: function() {
var k;
if (k = $(/^[A-Za-z-]+[A-Za-z-0-9_]*/)) { return new tree.Keyword(k) }
if (k = $(/^[A-Za-z-]+[A-Za-z-0-9_]*/)) {
return new tree.Keyword(k);
}
},
//
@ -786,13 +796,14 @@ carto.Parser = function Parser(env) {
}
},
filter: function() {
save();
var key, op, val;
if (! $('[')) return;
if (key = $(/^[a-zA-Z0-9-_]+/) || $(this.entities.quoted) || $(this.entities.variable)) {
if (key = $(this.entities.field) || $(this.entities.literal) || $(this.entities.variable)) {
if ((op = $(this.entities.comparison)) &&
(val = $(this.entities.quoted) || $(this.entities.variable) || $(/^[\w-\.]+/))) {
(val = $(this.entities.field)|| $(this.entities.literal) || $(this.entities.variable))) {
if (! $(']')) return;
return new tree.Filter(key, op, val, memo, env.filename);
}

@ -0,0 +1,13 @@
(function(tree) {
tree.Field = function Keyword(value) {
this.value = value;
this.is = 'field';
};
tree.Field.prototype = {
eval: function() { return this },
toString: function() { return '[' + this.value + ']' }
};
})(require('../tree'));

@ -1,32 +1,19 @@
(function(tree) {
tree.Filter = function Filter(key, op, val, index, filename) {
if (key.is) {
this.key = key.value;
this._key = key;
} else {
this.key = key;
}
this.key = key.value;
this._key = key;
this.val = val.value;
this._val = val;
this.op = op;
this.index = index;
this.filename = filename;
if (val.is) {
this.val = val.value;
this._val = val;
} else {
this.val = val;
}
if (this.op !== '=' && this.op !== '!=') {
this.val = 1 * this.val;
}
this.id = this.key + this.op + this.val;
};
// XML-safe versions of comparators
var opXML = {
'<': '&lt;',
@ -38,19 +25,12 @@ var opXML = {
};
tree.Filter.prototype.toXML = function(env) {
if (this.op !== '=' && this.op !== '!=' && isNaN(this.val)) {
env.error({
message: 'Cannot use operator "' + this.op + '" with value ' + this.val,
index: this.index,
filename: this.filename
});
}
// Handle variables correctly
if (this.val.eval) this._val = this.val.eval(env);
if (this.key.eval) this._key = this.key.eval(env);
if (this._key) var key = this._key.toString(false);
if (this._val) var val = this._val.toString(this._val.is == 'string');
return '[' + (key || this.key) + '] ' + opXML[this.op] + ' ' + (val || this.val);
return this._key.toString(this._key.is === 'string') + ' ' +
opXML[this.op] + ' ' + this._val.toString(this._val.is === 'string');
};
tree.Filter.prototype.toString = function() {

@ -1,5 +1,29 @@
#world[POP2005 > 100.1] {
#world[[POP2005] > 100.1] {
polygon-fill: #FFF;
line-color:#F00;
line-width: 0.5;
}
#world[5 > 100.1] {
polygon-fill: #FFF;
}
#world[[POP2005] > [POP2004]] {
polygon-fill: #FFF;
line-color:#F00;
line-width: 0.5;
}
#world[[POP2005] = "hello"] {
polygon-fill: #FFF;
line-color:#F00;
line-width: 0.5;
}
/*
#world[POP2004 = "test"] {
polygon-fill: #FFF;
line-color:#F00;
line-width: 0.5;
}
*/

Loading…
Cancel
Save