From f41312597c71c5c2871442cb0861c85807f84cfe Mon Sep 17 00:00:00 2001 From: Tom MacWright Date: Tue, 18 Dec 2012 17:16:40 -0500 Subject: [PATCH] Move .is to prototype, start testing quoteds --- lib/carto/renderer.js | 4 ++-- lib/carto/tree/call.js | 3 +-- lib/carto/tree/color.js | 2 +- lib/carto/tree/definition.js | 14 +++++++++----- lib/carto/tree/dimension.js | 2 +- lib/carto/tree/expression.js | 2 +- lib/carto/tree/field.js | 2 +- lib/carto/tree/imagefilter.js | 5 +---- lib/carto/tree/invalid.js | 3 ++- lib/carto/tree/operation.js | 5 ++++- lib/carto/tree/quoted.js | 3 ++- lib/carto/tree/reference.js | 22 ++++++++-------------- lib/carto/tree/rule.js | 7 ++++--- lib/carto/tree/ruleset.js | 1 + package.json | 21 +++++++++++++-------- test/quoted.test.js | 23 +++++++++++++++++++++++ 16 files changed, 74 insertions(+), 45 deletions(-) create mode 100644 test/quoted.test.js diff --git a/lib/carto/renderer.js b/lib/carto/renderer.js index 6af4be7..cd17d72 100644 --- a/lib/carto/renderer.js +++ b/lib/carto/renderer.js @@ -172,8 +172,8 @@ function addRules(current, definition, existing) { k++; } } - } else if (updatedFilters === null && - current[k].zoom === definition.zoom) { + } else if (updatedFilters === null) { // && + // current[k].zoom === definition.zoom) { // Filters can be added, but they don't change the // filters. This means we don't have to split the // definition. diff --git a/lib/carto/tree/call.js b/lib/carto/tree/call.js index 8ba29d6..fcefd28 100644 --- a/lib/carto/tree/call.js +++ b/lib/carto/tree/call.js @@ -2,14 +2,13 @@ var _ = require('underscore'); tree.Call = function Call(name, args, index) { - this.is = 'call'; - this.name = name; this.args = args; this.index = index; }; tree.Call.prototype = { + is: 'call', // // When evaluating a function call, // we either find the function in `tree.functions` [1], diff --git a/lib/carto/tree/color.js b/lib/carto/tree/color.js index 213b2bc..24b94de 100644 --- a/lib/carto/tree/color.js +++ b/lib/carto/tree/color.js @@ -20,11 +20,11 @@ tree.Color = function Color(rgb, a) { return parseInt(c + c, 16); }); } - this.is = 'color'; this.alpha = typeof(a) === 'number' ? a : 1; }; tree.Color.prototype = { + is: 'color', eval: function() { return this; }, // If we have some transparency, the only way to represent it diff --git a/lib/carto/tree/definition.js b/lib/carto/tree/definition.js index 446d678..9b6f2db 100644 --- a/lib/carto/tree/definition.js +++ b/lib/carto/tree/definition.js @@ -1,6 +1,12 @@ (function(tree) { var assert = require('assert'); +// A definition is the combination of a selector and rules, like +// #foo { +// polygon-opacity:1.0; +// } +// +// The selector can have filters tree.Definition = function Definition(selector, rules) { this.elements = selector.elements; assert.ok(selector.filters instanceof tree.Filterset); @@ -50,11 +56,9 @@ tree.Definition.prototype.addRules = function(rules) { return added; }; -/** - * Determine whether this selector matches a given id - * and array of classes, by determining whether - * all elements it contains match. - */ +// Determine whether this selector matches a given id +// and array of classes, by determining whether +// all elements it contains match. tree.Definition.prototype.appliesTo = function(id, classes) { for (var i = 0; i < this.elements.length; i++) { if (!this.elements[i].matches(id, classes)) { diff --git a/lib/carto/tree/dimension.js b/lib/carto/tree/dimension.js index cfd3621..4d061da 100644 --- a/lib/carto/tree/dimension.js +++ b/lib/carto/tree/dimension.js @@ -6,11 +6,11 @@ tree.Dimension = function Dimension(value, unit, index) { this.value = parseFloat(value); this.unit = unit || null; - this.is = 'float'; this.index = index; }; tree.Dimension.prototype = { + is: 'float', eval: function (env) { if (this.unit && ['px', '%'].indexOf(this.unit) === -1) { env.error({ diff --git a/lib/carto/tree/expression.js b/lib/carto/tree/expression.js index 1bf7108..b02fd63 100644 --- a/lib/carto/tree/expression.js +++ b/lib/carto/tree/expression.js @@ -2,10 +2,10 @@ tree.Expression = function Expression(value) { this.value = value; - this.is = 'expression'; }; tree.Expression.prototype = { + is: 'expression', eval: function(env) { if (this.value.length > 1) { return new tree.Expression(this.value.map(function(e) { diff --git a/lib/carto/tree/field.js b/lib/carto/tree/field.js index f6ebe8d..645cc60 100644 --- a/lib/carto/tree/field.js +++ b/lib/carto/tree/field.js @@ -2,10 +2,10 @@ tree.Field = function Field(content) { this.value = content || ''; - this.is = 'field'; }; tree.Field.prototype = { + is: 'field', toString: function() { return '[' + this.value + ']'; }, diff --git a/lib/carto/tree/imagefilter.js b/lib/carto/tree/imagefilter.js index 9fcd2cc..6adfe0b 100644 --- a/lib/carto/tree/imagefilter.js +++ b/lib/carto/tree/imagefilter.js @@ -1,13 +1,10 @@ (function(tree) { -// -// RGB Colors - #ff0014, #eee -// tree.ImageFilter = function ImageFilter(filter, args) { - this.is = 'imagefilter'; this.filter = filter; this.args = args || null; }; tree.ImageFilter.prototype = { + is: 'imagefilter', eval: function() { return this; }, toString: function() { diff --git a/lib/carto/tree/invalid.js b/lib/carto/tree/invalid.js index 70ace6d..cdc90c3 100644 --- a/lib/carto/tree/invalid.js +++ b/lib/carto/tree/invalid.js @@ -4,9 +4,10 @@ tree.Invalid = function Invalid(chunk, index, message) { this.index = index; this.type = 'syntax'; this.message = message || "Invalid code: " + this.chunk; - this.is = 'invalid'; }; +tree.Invalid.prototype.is = 'invalid'; + tree.Invalid.prototype.eval = function(env) { env.error({ chunk: this.chunk, diff --git a/lib/carto/tree/operation.js b/lib/carto/tree/operation.js index 627063a..95a1822 100644 --- a/lib/carto/tree/operation.js +++ b/lib/carto/tree/operation.js @@ -1,12 +1,15 @@ +// An operation is an expression with an op in between two operands, +// like 2 + 1. (function(tree) { tree.Operation = function Operation(op, operands, index) { this.op = op.trim(); this.operands = operands; this.index = index; - this.is = 'operation'; }; +tree.Operation.prototype.is = 'operation'; + tree.Operation.prototype.eval = function(env) { var a = this.operands[0].eval(env), b = this.operands[1].eval(env), diff --git a/lib/carto/tree/quoted.js b/lib/carto/tree/quoted.js index 51dddb5..180822b 100644 --- a/lib/carto/tree/quoted.js +++ b/lib/carto/tree/quoted.js @@ -2,10 +2,11 @@ tree.Quoted = function Quoted(content) { this.value = content || ''; - this.is = 'string'; }; tree.Quoted.prototype = { + is: 'string', + toString: function(quotes) { var xmlvalue = this.value.replace(/\'/g, '''); return (quotes === true) ? "'" + xmlvalue + "'" : this.value; diff --git a/lib/carto/tree/reference.js b/lib/carto/tree/reference.js index 8db1777..2f76bc5 100644 --- a/lib/carto/tree/reference.js +++ b/lib/carto/tree/reference.js @@ -1,9 +1,7 @@ -/* - * Carto pulls in a reference from the `mapnik-reference` - * module. This file builds indexes from that file for its various - * options, and provides validation methods for property: value - * combinations. - */ +// Carto pulls in a reference from the `mapnik-reference` +// module. This file builds indexes from that file for its various +// options, and provides validation methods for property: value +// combinations. (function(tree) { var _ = require('underscore'); @@ -72,10 +70,8 @@ tree.Reference.symbolizer = function(selector) { } }; -/* - * For transform properties and image-filters, - * mapnik has its own functions. - */ +// For transform properties and image-filters, +// mapnik has its own functions. tree.Reference.mapnikFunctions = function() { var functions = []; for (var i in tree.Reference.data.symbolizers) { @@ -88,10 +84,8 @@ tree.Reference.mapnikFunctions = function() { return functions; }; -/* - * For transform properties and image-filters, - * mapnik has its own functions. - */ +// For transform properties and image-filters, +// mapnik has its own functions. tree.Reference.mapnikFunction = function(name) { return _.find(this.mapnikFunctions(), function(f) { return f[0] === name; diff --git a/lib/carto/tree/rule.js b/lib/carto/tree/rule.js index 6f50e61..5fd25f6 100644 --- a/lib/carto/tree/rule.js +++ b/lib/carto/tree/rule.js @@ -1,4 +1,7 @@ (function(tree) { +// a rule is a single property and value combination, or variable +// name and value combination, like +// polygon-opacity: 1.0; or @opacity: 1.0; tree.Rule = function Rule(name, value, index, filename) { var parts = name.split('/'); this.name = parts.pop(); @@ -105,9 +108,7 @@ tree.Rule.prototype.toXML = function(env, content, sep, format) { } }; -/** - * TODO: Rule eval chain should add fontsets to env.frames - */ +// TODO: Rule eval chain should add fontsets to env.frames tree.Rule.prototype['eval'] = function(context) { return new tree.Rule(this.name, this.value['eval'](context), diff --git a/lib/carto/tree/ruleset.js b/lib/carto/tree/ruleset.js index 2bc6d4a..f1b00a4 100644 --- a/lib/carto/tree/ruleset.js +++ b/lib/carto/tree/ruleset.js @@ -8,6 +8,7 @@ tree.Ruleset = function Ruleset(selectors, rules) { this.is = 'ruleset'; }; tree.Ruleset.prototype = { + is: 'ruleset', 'eval': function(env) { var i, ruleset = new tree.Ruleset(this.selectors, this.rules.slice(0)); diff --git a/package.json b/package.json index 7cbcf19..4f5fe67 100644 --- a/package.json +++ b/package.json @@ -3,10 +3,12 @@ "version": "0.9.4", "description": "Mapnik Stylesheet Compiler", "url": "https://github.com/mapbox/carto", - "repositories": [{ - "type": "git", - "url": "http://github.com/mapbox/carto.git" - }], + "repositories": [ + { + "type": "git", + "url": "http://github.com/mapbox/carto.git" + } + ], "author": { "name": "MapBox", "url": "http://mapbox.com/", @@ -23,9 +25,11 @@ "Konstantin Käfer", "Alexis Sellier " ], - "licenses": [{ - "type": "Apache" - }], + "licenses": [ + { + "type": "Apache" + } + ], "bin": { "carto": "./bin/carto", "mml2json.js": "./bin/mml2json.js" @@ -37,7 +41,8 @@ "dependencies": { "underscore": "~1.3.3", "mapnik-reference": "~5.0.0", - "xml2js": "~0.1.13" + "xml2js": "~0.1.13", + "lodash": "~1.0.0-rc.3" }, "devDependencies": { "mocha": "1.3.x", diff --git a/test/quoted.test.js b/test/quoted.test.js new file mode 100644 index 0000000..10c6b67 --- /dev/null +++ b/test/quoted.test.js @@ -0,0 +1,23 @@ +var assert = require('assert'); +var tree = require('../lib/carto/tree.js'); +require('../lib/carto/tree/quoted'); + +describe('Quoted', function() { + describe('basic functionality', function() { + it('should be constructed', function() { + var f = new tree.Quoted("Tom's quoted"); + assert.ok(f); + assert.equal(f.is, 'string'); + }); + it('should produce normal output', function() { + var f = new tree.Quoted("Tom's quoted"); + assert.ok(f); + assert.equal(f.toString(), "Tom's quoted"); + }); + it('should produce xml-friendly output', function() { + var f = new tree.Quoted("Tom's quoted"); + assert.ok(f); + assert.equal(f.toString(true), "'Tom's quoted'"); + }); + }); +});