diff --git a/lib/carto/parser.js b/lib/carto/parser.js index 1994859..316442f 100644 --- a/lib/carto/parser.js +++ b/lib/carto/parser.js @@ -530,12 +530,18 @@ carto.Parser = function Parser(env) { zooms.push(z); conditions++; } else if (f) { - filters.add(f); + var err = filters.add(f); + if (err) { + throw makeError({ + message: err, + index: i - 1 + }); + } conditions++; } else if (attachment) { throw makeError({ - message:'Encountered second attachment name.', - index:i - 1 + message: 'Encountered second attachment name.', + index: i - 1 }); } else { attachment = a; diff --git a/lib/carto/tree/filterset.js b/lib/carto/tree/filterset.js index d1134ee..2bec009 100644 --- a/lib/carto/tree/filterset.js +++ b/lib/carto/tree/filterset.js @@ -155,9 +155,37 @@ tree.Filterset.prototype.addable = function(filter) { } }; +// Does the new filter constitute a conflict? +tree.Filterset.prototype.conflict = function(filter) { + var key = filter.key.toString(), + value = filter.val.toString(); + + if (!isNaN(parseFloat(value))) value = parseFloat(value); + + // if (a=b) && (a=c) + // if (a=b) && (a!=b) + // or (a!=b) && (a=b) + if ((filter.op === '=' && key + '=' in this.filters && + value != this.filters[key + '='].val.toString()) || + (filter.op === '!=' && key + '=' in this.filters && + value == this.filters[key + '='].val.toString()) || + (filter.op === '=' && key + '!=' in this.filters && + value == this.filters[key + '!='].val.toString())) { + return filter.toString() + ' added to ' + this.toString() + ' produces an invalid filter'; + } + + return false; +}; + // Only call this function for filters that have been cleared by .addable(). -tree.Filterset.prototype.add = function(filter) { - var key = filter.key.toString(), id, op = filter.op, numval; +tree.Filterset.prototype.add = function(filter, env) { + var key = filter.key.toString(), + id, + op = filter.op, + conflict = this.conflict(filter), + numval; + + if (conflict) return conflict; if (op === '=') { for (var i in this.filters) { diff --git a/test/errorhandling/contradiction.mml b/test/errorhandling/contradiction.mml new file mode 100644 index 0000000..d6b4416 --- /dev/null +++ b/test/errorhandling/contradiction.mml @@ -0,0 +1,15 @@ +{ + "srs": "+proj=merc +a=6378137 +b=6378137 +lat_ts=0.0 +lon_0=0.0 +x_0=0.0 +y_0=0.0 +k=1.0 +units=m +nadgrids=@null +wktext +no_defs +over", + "Stylesheet": [ + "contradiction.mss" + ], + "Layer": [{ + "name": "world", + "srs": "+proj=merc +a=6378137 +b=6378137 +lat_ts=0.0 +lon_0=0.0 +x_0=0.0 +y_0=0.0 +k=1.0 +units=m +nadgrids=@null +wktext +no_defs +over", + "Datasource": { + "file": "http://tilemill-data.s3.amazonaws.com/district.geojson", + "type": "ogr", + "layer": "OGRGeoJSON" + } + }] +} diff --git a/test/errorhandling/contradiction.mss b/test/errorhandling/contradiction.mss new file mode 100644 index 0000000..648bf1f --- /dev/null +++ b/test/errorhandling/contradiction.mss @@ -0,0 +1,3 @@ +#world[FeatureCla!=""][FeatureCla=""] { + polygon-fill: #fff; +} diff --git a/test/errorhandling/contradiction.result b/test/errorhandling/contradiction.result new file mode 100644 index 0000000..63c7724 --- /dev/null +++ b/test/errorhandling/contradiction.result @@ -0,0 +1 @@ +contradiction.mss:1:37 [[FeatureCla]=] added to [FeatureCla]!= produces an invalid filter diff --git a/test/errorhandling/contradiction_2.mss b/test/errorhandling/contradiction_2.mss new file mode 100644 index 0000000..2aaa907 --- /dev/null +++ b/test/errorhandling/contradiction_2.mss @@ -0,0 +1,3 @@ +#world[FeatureCla=""][FeatureCla!=""] { + polygon-fill: #fff; +} diff --git a/test/errorhandling/contradiction_2.result b/test/errorhandling/contradiction_2.result new file mode 100644 index 0000000..99588ed --- /dev/null +++ b/test/errorhandling/contradiction_2.result @@ -0,0 +1 @@ +contradiction_2.mss:1:37 [[FeatureCla]!=] added to [FeatureCla]= produces an invalid filter diff --git a/test/rendering/partial_overrides.mss b/test/rendering/partial_overrides.mss index ea4a852..7ae9847 100644 --- a/test/rendering/partial_overrides.mss +++ b/test/rendering/partial_overrides.mss @@ -18,10 +18,6 @@ line-color: #f00; } -#world.m_5[NAME="United States"][NAME="Canada"] { - line-color: #f00; -} - #world.m_6[NAME!="Canada"][zoom > 5] { line-opacity: .7; } diff --git a/test/rendering/partial_overrides.result b/test/rendering/partial_overrides.result index 07bec60..f3ac99b 100644 --- a/test/rendering/partial_overrides.result +++ b/test/rendering/partial_overrides.result @@ -1,9 +1,9 @@ - + - - + world - [absolute path] - shape + +