From d57006dd976315aa0222fb3f36e0c857baeb5842 Mon Sep 17 00:00:00 2001 From: Tom MacWright Date: Wed, 27 Jun 2012 14:58:56 -0400 Subject: [PATCH] Add advanced field test, fixes for combining fields with colors that otherwise go back to mapnik --- lib/carto/tree/dimension.js | 2 +- lib/carto/tree/literal.js | 3 +++ lib/carto/tree/operation.js | 16 +++++++++++++++- test/rendering/field_advanced.mml | 23 +++++++++++++++++++++++ test/rendering/field_advanced.mss | 6 ++++++ test/rendering/field_advanced.result | 23 +++++++++++++++++++++++ 6 files changed, 71 insertions(+), 2 deletions(-) create mode 100644 test/rendering/field_advanced.mml create mode 100644 test/rendering/field_advanced.mss create mode 100644 test/rendering/field_advanced.result diff --git a/lib/carto/tree/dimension.js b/lib/carto/tree/dimension.js index dfa7de2..0fe8458 100644 --- a/lib/carto/tree/dimension.js +++ b/lib/carto/tree/dimension.js @@ -25,7 +25,7 @@ tree.Dimension.prototype = { return new tree.Color([this.value, this.value, this.value]); }, toString: function() { - return this.value; + return this.value.toString(); }, // In an operation between two Dimensions, diff --git a/lib/carto/tree/literal.js b/lib/carto/tree/literal.js index 5281608..135be6c 100644 --- a/lib/carto/tree/literal.js +++ b/lib/carto/tree/literal.js @@ -1,3 +1,6 @@ +// A literal is a literal string for Mapnik - the +// result of the combination of a `tree.Field` with any +// other type. (function(tree) { tree.Literal = function Field(content) { diff --git a/lib/carto/tree/operation.js b/lib/carto/tree/operation.js index 000971e..627063a 100644 --- a/lib/carto/tree/operation.js +++ b/lib/carto/tree/operation.js @@ -46,9 +46,23 @@ tree.Operation.prototype.eval = function(env) { }; } + // Fields, literals, dimensions, and quoted strings can be combined. if (a instanceof tree.Field || b instanceof tree.Field || a instanceof tree.Literal || b instanceof tree.Literal) { - return new tree.Literal(a.eval(env).toString(true) + this.op + b.eval(env).toString(true)); + if (a.is === 'color' || b.is === 'color') { + env.error({ + message: "Can't subtract, divide, or multiply colors in expressions.", + index: this.index, + type: 'runtime', + filename: this.filename + }); + return { + is: 'undefined', + value: 'undefined' + }; + } else { + return new tree.Literal(a.eval(env).toString(true) + this.op + b.eval(env).toString(true)); + } } return a.operate(this.op, b); diff --git a/test/rendering/field_advanced.mml b/test/rendering/field_advanced.mml new file mode 100644 index 0000000..12153a4 --- /dev/null +++ b/test/rendering/field_advanced.mml @@ -0,0 +1,23 @@ +{ + "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": [ + "field_advanced.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/test_data/shape_demo.zip", + "type": "shape" + } + }, + { + "class": "new", + "name": "countries", + "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/test_data/shape_demo.zip", + "type": "shape" + } + }] +} diff --git a/test/rendering/field_advanced.mss b/test/rendering/field_advanced.mss new file mode 100644 index 0000000..68f41e8 --- /dev/null +++ b/test/rendering/field_advanced.mss @@ -0,0 +1,6 @@ +#world { + text-name: "hello " + [NAME] + " hello" + 2; + text-size: 11; + text-face-name: "Georgia Regular", "Arial Italic"; +} + diff --git a/test/rendering/field_advanced.result b/test/rendering/field_advanced.result new file mode 100644 index 0000000..cf47ac3 --- /dev/null +++ b/test/rendering/field_advanced.result @@ -0,0 +1,23 @@ + + + + + + + + + + + world + + + + + + +