Move .is to prototype, start testing quoteds
This commit is contained in:
parent
4181df378f
commit
f41312597c
@ -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.
|
||||
|
@ -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],
|
||||
|
@ -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
|
||||
|
@ -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)) {
|
||||
|
@ -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({
|
||||
|
@ -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) {
|
||||
|
@ -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 + ']';
|
||||
},
|
||||
|
@ -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() {
|
||||
|
@ -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,
|
||||
|
@ -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),
|
||||
|
@ -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;
|
||||
|
@ -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;
|
||||
|
@ -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),
|
||||
|
@ -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));
|
||||
|
21
package.json
21
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 <self@cloudhead.net>"
|
||||
],
|
||||
"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",
|
||||
|
23
test/quoted.test.js
Normal file
23
test/quoted.test.js
Normal file
@ -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'");
|
||||
});
|
||||
});
|
||||
});
|
Loading…
Reference in New Issue
Block a user