Compare commits

...

1 Commits

Author SHA1 Message Date
Tom MacWright 880b0e8f0a Another ill-fated attempt at filters, this breaks many tests and does
12 years ago

@ -655,10 +655,17 @@ carto.Parser = function Parser(env) {
save();
var key, op, val;
if (! $('[')) return;
if (key = $(/^[a-zA-Z0-9\-_]+/) || $(this.entities.quoted) || $(this.entities.variable)) {
if (key = $(/^[a-zA-Z0-9\-_]+/) ||
$(this.expression)) {
if ((op = $(this.entities.comparison)) &&
(val = $(this.entities.quoted) || $(this.entities.variable) || $(/^[\w\-\.]+/))) {
(val = $(this.expression))) {
if (! $(']')) return;
// Support legacy-style non-wrapped field names,
// like [NAME = 5]
if (!key.is) {
key = new tree.Field(key);
}
return new tree.Filter(key, op, val, memo, env.filename);
}
}

@ -9,7 +9,10 @@ tree.Field.prototype = {
toString: function() {
return '[' + this.value + ']';
},
'eval': function() {
'eval': function(env) {
if (this.value.eval) {
this.value = this.value.eval(env);
}
return this;
}
};

@ -1,27 +1,12 @@
(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;
}
// TODO: figure out what the f
this.key = key;
this.op = op;
this.index = index;
this.filename = filename;
if (val.is) {
this.val = val.value;
this._val = val;
} else {
this.val = val;
}
if (ops[this.op][1] == 'numeric') {
this.val = 1 * this.val;
}
this.val = val;
this.id = this.key + this.op + this.val;
};
@ -39,23 +24,12 @@ var ops = {
};
tree.Filter.prototype.toXML = function(env) {
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');
if (
(ops[this.op][1] == 'numeric' && isNaN(this.val)) ||
(ops[this.op][1] == 'string' && (val || this.val)[0] != "'")
) {
env.error({
message: 'Cannot use operator "' + this.op + '" with value ' + this.val,
index: this.index,
filename: this.filename
});
}
return '[' + (key || this.key) + ']' + ops[this.op][0] + '' + (val || this.val) + (ops[this.op][2] || '');
return (this.key.eval(env).toString(true)) +
ops[this.op][0] + '' +
(this.val.eval(env).toString(true)) +
// This section is only for operators
// which can wrap, like .match
(ops[this.op][2] || '');
};
tree.Filter.prototype.toString = function() {

@ -43,7 +43,7 @@ Object.defineProperty(tree.Filterset.prototype, 'clone', {
Object.defineProperty(tree.Filterset.prototype, 'cloneWith', {
enumerable: false,
value: function(other) {
var additions;
var additions = [];
for (var id in other) {
var status = this.addable(other[id]);
if (status === false) {
@ -51,14 +51,13 @@ Object.defineProperty(tree.Filterset.prototype, 'cloneWith', {
}
if (status === true) {
// Adding the filter will override another value.
if (!additions) additions = [];
additions.push(other[id]);
}
}
// Adding the other filters doesn't make this filterset invalid, but it
// doesn't add anything to it either.
if (!additions) return null;
if (!additions.length) return null;
// We can successfully add all filters. Now clone the filterset and add the
// new rules.
@ -79,13 +78,11 @@ Object.defineProperty(tree.Filterset.prototype, 'cloneWith', {
}
});
/**
* Returns true when the new filter can be added, false otherwise.
*/
// Returns true when the new filter can be added, false otherwise.
Object.defineProperty(tree.Filterset.prototype, 'addable', {
enumerable: false,
value: function(filter) {
var key = filter.key, value = filter.val;
var key = filter.key.toString(), value = filter.val;
switch (filter.op) {
case '=':
@ -141,13 +138,11 @@ Object.defineProperty(tree.Filterset.prototype, 'addable', {
}
});
/**
* Only call this function for filters that have been cleared by .addable().
*/
// Only call this function for filters that have been cleared by .addable().
Object.defineProperty(tree.Filterset.prototype, 'add', {
enumerable: false,
value: function(filter) {
var key = filter.key;
var key = filter.key.toString();
switch (filter.op) {
case '=':

Loading…
Cancel
Save