diff --git a/bin/carto b/bin/carto index 05946ae99..3da5f87d4 100755 --- a/bin/carto +++ b/bin/carto @@ -174,11 +174,8 @@ if (ext == '.mml') { } return { id: x, data: fs.readFileSync(path.join(path.dirname(input), x), 'utf8') } }); - compileMML(null,data); - } - else { - console.error("Expecting a Stylesheet property containing an (array of) stylesheet object(s) of the form { id: 'x', 'data': 'y' }."); } + compileMML(null,data); } } else if (ext == '.mss') { compileMSS(null,data); diff --git a/lib/carto/renderer.js b/lib/carto/renderer.js index 0135d28d5..49c4cb8d2 100644 --- a/lib/carto/renderer.js +++ b/lib/carto/renderer.js @@ -78,11 +78,12 @@ carto.Renderer.prototype.render = function render(m) { carto.tree.Reference.setVersion(this.options.mapnik_version); var output = []; + var definitions = []; // Transform stylesheets into definitions. if (_.has(m, 'Stylesheet') && !_.isNil(m.Stylesheet)) { m.Stylesheet = _.castArray(m.Stylesheet); - var definitions = _(m.Stylesheet).chain() + definitions = _(m.Stylesheet).chain() .map(function(s) { if (_.isString(s) || !_.has(s, 'id') || !_.has(s, 'data') || _.isNil(s.id) || _.isNil(s.data)) { var e = new Error("Expecting a stylesheet object of the form { id: 'x', 'data': 'y' } for the Stylesheet property.\n"); @@ -102,11 +103,6 @@ carto.Renderer.prototype.render = function render(m) { .flatten() .value(); } - else { - var e = new Error("Expecting a Stylesheet property containing an (array of) stylesheet object(s) of the form { id: 'x', 'data': 'y' }.\n"); - e.stack = null; // do not show stack trace - throw e; - } function appliesTo(name, classIndex) { return function(definition) { @@ -120,28 +116,31 @@ carto.Renderer.prototype.render = function render(m) { for (var i = 0; i < m.Layer.length; i++) { l = m.Layer[i]; styles = []; - classIndex = {}; - if (env.benchmark) console.warn('processing layer: ' + l.id); - // Classes are given as space-separated alphanumeric strings. - var classes = (l['class'] || '').split(/\s+/g); - for (var j = 0; j < classes.length; j++) { - classIndex[classes[j]] = true; - } - matching = definitions.filter(appliesTo(l.name, classIndex)); - rules = inheritDefinitions(matching, env); - sorted = sortStyles(rules, env); + if (definitions.length > 0) { + classIndex = {}; - for (var k = 0, rule, style_name; k < sorted.length; k++) { - rule = foldStyle(sorted[k]); - style_name = l.name + (rule.attachment !== '__default__' ? '-' + rule.attachment : ''); + if (env.benchmark) console.warn('processing layer: ' + l.id); + // Classes are given as space-separated alphanumeric strings. + var classes = (l['class'] || '').split(/\s+/g); + for (var j = 0; j < classes.length; j++) { + classIndex[classes[j]] = true; + } + matching = definitions.filter(appliesTo(l.name, classIndex)); + rules = inheritDefinitions(matching, env); + sorted = sortStyles(rules, env); - // env.effects can be modified by this call - var styleXML = carto.tree.StyleXML(style_name, rule.attachment, rule, env); + for (var k = 0, rule, style_name; k < sorted.length; k++) { + rule = foldStyle(sorted[k]); + style_name = l.name + (rule.attachment !== '__default__' ? '-' + rule.attachment : ''); - if (styleXML) { - output.push(styleXML); - styles.push(style_name); + // env.effects can be modified by this call + var styleXML = carto.tree.StyleXML(style_name, rule.attachment, rule, env); + + if (styleXML) { + output.push(styleXML); + styles.push(style_name); + } } } diff --git a/test/errorhandling/no_stylesheet.result b/test/errorhandling/no_stylesheet.result deleted file mode 100644 index eecfec1a8..000000000 --- a/test/errorhandling/no_stylesheet.result +++ /dev/null @@ -1 +0,0 @@ -Expecting a Stylesheet property containing an (array of) stylesheet object(s) of the form { id: 'x', 'data': 'y' }. diff --git a/test/rendering.test.js b/test/rendering.test.js index 0cb0de12d..129b10ea0 100644 --- a/test/rendering.test.js +++ b/test/rendering.test.js @@ -1,11 +1,32 @@ var path = require('path'), fs = require('fs'), + assert = require('assert'), semver = require('semver'); var carto = require('../lib/carto'); var helper = require('./support/helper'); describe('Rendering', function() { + + it('should support rendering without Stylesheet (for non-styling/vector tile usage)', function(done) { + + // note: this intentionally does not have a `"Stylesheet":[]` property + // so we should skip trying to validate it + var opts = {"name":"","description":"", + "attribution":"", + "center":[0,0,3], + "format":"pbf", + "minzoom":0, + "maxzoom":6, + "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", + "Layer":[], + "json":"{\"vector_layers\":[]}"}; + var renderer = new carto.Renderer(null).render(opts); + assert.ok(renderer); + done(); + }); + + helper.files('rendering', 'mml', function(file) { var api = null, filename = path.basename(file); diff --git a/test/errorhandling/no_stylesheet.mml b/test/rendering/no_stylesheet.mml similarity index 100% rename from test/errorhandling/no_stylesheet.mml rename to test/rendering/no_stylesheet.mml diff --git a/test/rendering/no_stylesheet.result b/test/rendering/no_stylesheet.result new file mode 100644 index 000000000..6b1862d60 --- /dev/null +++ b/test/rendering/no_stylesheet.result @@ -0,0 +1,10 @@ + + + + + + + + +