diff --git a/lib/carto/tree/filterset.js b/lib/carto/tree/filterset.js index 563bf3d..2866959 100644 --- a/lib/carto/tree/filterset.js +++ b/lib/carto/tree/filterset.js @@ -92,6 +92,9 @@ tree.Filterset.prototype.toJS = function(env) { val = filter._val.toString(true); } var attrs = "data"; + if (op === '=~') { + return "(" + attrs + "['" + filter.key.value + "'] + '').match(" + (val.is === 'string' ? "'" + val.toString().replace(/'/g, "\\'") + "'" : val) + ")"; + } return attrs + "['" + filter.key.value + "'] " + op + " " + (val.is === 'string' ? "'" + val.toString().replace(/'/g, "\\'") + "'" : val); }).join(' && '); }; diff --git a/test/rendering_js.test.js b/test/rendering_js.test.js index b716c63..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'); @@ -195,4 +195,33 @@ describe('RenderingJS', function() { assert.equal(width, 8); }); + it("should not throw `ReferenceError` with `=~` operator", function(){ + var css = [ + '#layer[name=~".*wadus*"] {', + ' marker-width: 14;', + '}' + ].join('\n'); + + assert.doesNotThrow(function () { + var shader = (new carto.RendererJS({})).render(css); + var layer = shader.getLayers()[0]; + var value = layer.shader['marker-width'].style({ name: 'wadus' }, { zoom: 1 }); + assert.equal(value, 14); + }, ReferenceError); + }); + + it("`=~` operator should support numbers", function(){ + var css = [ + '#layer[value=~"^10"] {', + ' marker-width: 14;', + '}' + ].join('\n'); + + assert.doesNotThrow(function () { + var shader = (new carto.RendererJS({})).render(css); + var layer = shader.getLayers()[0]; + var value = layer.shader['marker-width'].style({ value: 10 }, { zoom: 1 }); + assert.equal(value, 14); + }, Error); + }); });