diff --git a/lib/OpenLayers/Style.js b/lib/OpenLayers/Style.js index f64a58eb1c..2595f25bcb 100644 --- a/lib/OpenLayers/Style.js +++ b/lib/OpenLayers/Style.js @@ -256,10 +256,11 @@ OpenLayers.Style = OpenLayers.Class({ * {Object} the modified style */ createLiterals: function(style, feature) { - var context = this.context || feature.attributes || feature.data; + var context = OpenLayers.Util.extend({}, feature.attributes || feature.data); + OpenLayers.Util.extend(context, this.context); for (var i in this.propertyStyles) { - style[i] = OpenLayers.Style.createLiteral(style[i], context, feature); + style[i] = OpenLayers.Style.createLiteral(style[i], context, feature, i); } return style; }, @@ -383,17 +384,20 @@ OpenLayers.Style = OpenLayers.Class({ * will be replaced by the value of the "bar" attribute of the passed * feature. * context - {Object} context to take attribute values from - * feature - {OpenLayers.Feature.Vector} The feature that will be passed - * to for evaluating functions in the context. + * feature - {} optional feature to pass to + * for evaluating functions in the + * context. + * property - {String} optional, name of the property for which the literal is + * being created for evaluating functions in the context. * * Returns: * {String} the parsed value. In the example of the value parameter above, the * result would be "foo valueOfBar", assuming that the passed feature has an * attribute named "bar" with the value "valueOfBar". */ -OpenLayers.Style.createLiteral = function(value, context, feature) { +OpenLayers.Style.createLiteral = function(value, context, feature, property) { if (typeof value == "string" && value.indexOf("${") != -1) { - value = OpenLayers.String.format(value, context, [feature]); + value = OpenLayers.String.format(value, context, [feature, property]); value = (isNaN(value) || !value) ? value : parseFloat(value); } return value; diff --git a/tests/Style.html b/tests/Style.html index 5bab4ad56d..23b552ca53 100644 --- a/tests/Style.html +++ b/tests/Style.html @@ -237,7 +237,7 @@ } function test_Style_context(t) { - t.plan(2); + t.plan(4); var rule = new OpenLayers.Rule({ symbolizer: {"Point": {externalGraphic: "${img1}"}}, filter: new OpenLayers.Filter.Comparison({ @@ -265,6 +265,18 @@ } }}); t.eq(style.createSymbolizer(feature).externalGraphic, "foo10.png", "correctly evaluated symbolizer without rule"); + + style = new OpenLayers.Style( + {externalGraphic: "${getExternalGraphic}", + pointRadius: "${size}"}, + {context: { + getExternalGraphic: function(feature) { + return "foo" + feature.attributes.size + ".png"; + } + }}); + t.eq(style.createSymbolizer(feature).externalGraphic, "foo10.png", "correctly evaluated symbolizer from context"); + t.eq(style.createSymbolizer(feature).pointRadius, 10, "correctly evaluated symbolizer from attributes"); + }; function test_Style_findPropertyStyles(t) {