Fixing OpenLayers.Style.createLiteral to work with all strings. r=ahocevar (closes #1439)

git-svn-id: http://svn.openlayers.org/trunk/openlayers@6553 dc9f47b5-9b13-0410-9fdd-eb0c1a62fdaf
This commit is contained in:
Tim Schaub
2008-03-19 19:23:15 +00:00
parent 410cd22976
commit 3cc419aada
2 changed files with 42 additions and 1 deletions

View File

@@ -331,7 +331,7 @@ OpenLayers.Style = OpenLayers.Class({
OpenLayers.Style.createLiteral = function(value, context, feature) {
if (typeof value == "string" && value.indexOf("${") != -1) {
value = OpenLayers.String.format(value, context, [feature]);
value = isNaN(value) ? value : parseFloat(value);
value = (isNaN(value) || !value) ? value : parseFloat(value);
}
return value;
}

View File

@@ -155,6 +155,47 @@
t.ok(propertyStyles.strokeColor, "detected strokeColor from style correctly");
t.eq(typeof propertyStyles.pointRadius, "undefined", "correctly detected pointRadius as non-property style");
}
function test_createLiteral(t) {
t.plan(6);
var value, context, feature, result, expected;
var func = OpenLayers.Style.createLiteral;
// without templates
value = "foo";
expected = value;
result = func(value);
t.eq(result, expected, "(no template) preserves literal");
// with templates
value = "${foo}"
expected = "bar";
context = {foo: expected};
result = func(value, context);
t.eq(result, expected, "(template) preserves literal");
expected = "";
context = {foo: expected};
result = func(value, context);
t.eq(result, expected, "(template) preserves empty string");
expected = "16/03/2008";
context = {foo: expected};
result = func(value, context);
t.eq(result, expected, "(template) preserves string with numbers");
expected = 16;
context = {foo: expected + ""};
result = func(value, context);
t.eq(result, expected, "(template) casts integer in a string");
expected = 16;
context = {foo: " " + expected + " "};
result = func(value, context);
t.eq(result, expected, "(template) casts integer in a space padded string");
}
function test_Style_destroy(t) {
t.plan(1);