OpenLayers OpenLayers

Ticket #1439: createLiteral.patch

File createLiteral.patch, 2.4 kB (added by tschaub, 10 months ago)

preserve empty string and strings starting with numbers

  • tests/test_Style.html

    old new  
    155155        t.ok(propertyStyles.strokeColor, "detected strokeColor from style correctly"); 
    156156        t.eq(typeof propertyStyles.pointRadius, "undefined", "correctly detected pointRadius as non-property style"); 
    157157    } 
     158     
     159    function test_createLiteral(t) { 
     160        t.plan(6); 
     161         
     162        var value, context, feature, result, expected; 
     163        var func = OpenLayers.Style.createLiteral; 
    158164 
     165        // without templates 
     166        value = "foo"; 
     167        expected = value; 
     168        result = func(value); 
     169        t.eq(result, expected, "(no template) preserves literal"); 
     170         
     171        // with templates 
     172        value = "${foo}" 
     173        expected = "bar"; 
     174        context = {foo: expected}; 
     175        result = func(value, context); 
     176        t.eq(result, expected, "(template) preserves literal"); 
     177         
     178        expected = ""; 
     179        context = {foo: expected}; 
     180        result = func(value, context); 
     181        t.eq(result, expected, "(template) preserves empty string"); 
     182         
     183        expected = "16/03/2008"; 
     184        context = {foo: expected}; 
     185        result = func(value, context); 
     186        t.eq(result, expected, "(template) preserves string with numbers"); 
     187         
     188        expected = 16; 
     189        context = {foo: expected + ""}; 
     190        result = func(value, context); 
     191        t.eq(result, expected, "(template) casts integer in a string"); 
     192 
     193        expected = 16; 
     194        context = {foo: " " + expected + " "}; 
     195        result = func(value, context); 
     196        t.eq(result, expected, "(template) casts integer in a space padded string"); 
     197 
     198    } 
     199 
    159200    function test_Style_destroy(t) { 
    160201        t.plan(1); 
    161202         
  • lib/OpenLayers/Style.js

    old new  
    331331OpenLayers.Style.createLiteral = function(value, context, feature) { 
    332332    if (typeof value == "string" && value.indexOf("${") != -1) { 
    333333        value = OpenLayers.String.format(value, context, [feature]); 
    334         value = isNaN(value) ? value : parseFloat(value); 
     334        value = (isNaN(value) || !value) ? value : parseFloat(value); 
    335335    } 
    336336    return value; 
    337337}