diff --git a/lib/carto/tree/value.js b/lib/carto/tree/value.js index 7e74e76..e12619b 100644 --- a/lib/carto/tree/value.js +++ b/lib/carto/tree/value.js @@ -36,7 +36,9 @@ tree.Value.prototype = { v = "'" + v + "'"; } else if (val.is === 'field') { // replace [variable] by ctx['variable'] - v = v.replace(/\[(.*)\]/g, "data['$1']"); + v = v.replace(/\[([^\]]*)\]/g, function(matched) { + return matched.replace(/\[(.*)\]/g, "data['$1']"); + }); }else if (val.is === 'call') { v = JSON.stringify({ name: val.name, diff --git a/test/rendering_js.test.js b/test/rendering_js.test.js index e09ea9d..e3e566d 100644 --- a/test/rendering_js.test.js +++ b/test/rendering_js.test.js @@ -4,19 +4,19 @@ var carto = require('../lib/carto'); describe('RenderingJS', function() { var shader; var style = [ - '#world {', - 'line-width: 2;', - 'line-color: #f00;', - '[frame-offset = 1] {', - 'line-width: 3;', - '}', - '[frame-offset = 2] {', - 'line-width: 3;', - '}', - '}', - '', - '#worls[frame-offset = 10] {', - 'line-width: 4;', + '#world {', + 'line-width: 2;', + 'line-color: #f00;', + '[frame-offset = 1] {', + 'line-width: 3;', + '}', + '[frame-offset = 2] {', + 'line-width: 3;', + '}', + '}', + '', + '#worls[frame-offset = 10] {', + 'line-width: 4;', '}' ].join('\n'); @@ -183,6 +183,18 @@ describe('RenderingJS', function() { assert.equal(st.args[2].args[0].value, 10); }); + it("should work with multiple operands", function(){ + var css = [ + '#layer {', + ' marker-width: [value] * [value] * 0.5;', + '}' + ].join('\n'); + var shader = (new carto.RendererJS({ debug: false })).render(css); + var layer = shader.getLayers()[0]; + var width = layer.shader['marker-width'].style({value: 4}, {zoom: 1}); + assert.equal(width, 8); + }); + it("should not throw `ReferenceError` with `=~` operator", function(){ var css = [ '#layer[name=~".*wadus*"] {', @@ -197,7 +209,7 @@ describe('RenderingJS', function() { assert.equal(value, 14); }, ReferenceError); }); - + it("`=~` operator should support numbers", function(){ var css = [ '#layer[value=~"^10"] {',