From 3cc419aadaf96077f401b0a4de8daf652ca57ed7 Mon Sep 17 00:00:00 2001 From: Tim Schaub Date: Wed, 19 Mar 2008 19:23:15 +0000 Subject: [PATCH] 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 --- lib/OpenLayers/Style.js | 2 +- tests/test_Style.html | 41 +++++++++++++++++++++++++++++++++++++++++ 2 files changed, 42 insertions(+), 1 deletion(-) diff --git a/lib/OpenLayers/Style.js b/lib/OpenLayers/Style.js index 4db67448fa..6750ec693d 100644 --- a/lib/OpenLayers/Style.js +++ b/lib/OpenLayers/Style.js @@ -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; } diff --git a/tests/test_Style.html b/tests/test_Style.html index 7714d3b2be..84c181dd5b 100644 --- a/tests/test_Style.html +++ b/tests/test_Style.html @@ -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);