Prevent TypeError when parsing '=~' operator

This commit is contained in:
Daniel García Aubert 2017-03-03 15:27:42 +01:00
parent 0063ddba7f
commit c780998dc8
2 changed files with 19 additions and 2 deletions

View File

@ -93,7 +93,7 @@ tree.Filterset.prototype.toJS = function(env) {
} }
var attrs = "data"; var attrs = "data";
if (op === '=~') { if (op === '=~') {
return attrs + "['" + filter.key.value + "'].match(" + (val.is === 'string' ? "'" + val.toString().replace(/'/g, "\\'") + "'" : val) + ")"; return "(typeof " + attrs + "['" + filter.key.value + "'] === 'string') && " + 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); return attrs + "['" + filter.key.value + "'] " + op + " " + (val.is === 'string' ? "'" + val.toString().replace(/'/g, "\\'") + "'" : val);
}).join(' && '); }).join(' && ');

View File

@ -183,7 +183,7 @@ describe('RenderingJS', function() {
assert.equal(st.args[2].args[0].value, 10); assert.equal(st.args[2].args[0].value, 10);
}); });
it("should not throw `ReferenceError`", function(){ it("should not throw `ReferenceError` with `=~` operator", function(){
var css = [ var css = [
'#layer[name=~".*Cairo*"] {', '#layer[name=~".*Cairo*"] {',
' marker-width: 14;', ' marker-width: 14;',
@ -196,4 +196,21 @@ describe('RenderingJS', function() {
var value = layer.shader['marker-width'].style({ name: 'wadus' }, { zoom: 1 }); var value = layer.shader['marker-width'].style({ name: 'wadus' }, { zoom: 1 });
}); });
}); });
it("should not throw `TypeError` if left operand is not a `string` with operator `=~`", function(){
var css = [
'#layer[name=~1] {',
' 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: 1 }, { zoom: 1 });
value = layer.shader['marker-width'].style({ name: null }, { zoom: 1 });
value = layer.shader['marker-width'].style({ name: undefined }, { zoom: 1 });
value = layer.shader['marker-width'].style({ name: true }, { zoom: 1 });
});
});
}); });