OpenLayers OpenLayers

Ticket #1133: string_format.2.patch

File string_format.2.patch, 3.3 kB (added by ahocevar, 1 year ago)

Fixed tests, are now ok in FF, IE and Opera

  • tests/test_BaseTypes.html

    old new  
    8282        t.eq(OpenLayers.String.camelize(str), "chickenHeadMan", "string with multiple middle hyphens and end hyphen is camelized correctly (end hyphen dropped)"); 
    8383 
    8484 
    85    } 
     85    } 
     86     
     87    function test_String_format(t) { 
     88        var unchanged = [ 
     89            "", "${ ", "${", " ${", "${${", "${}", "${${}}", " ${ ${", 
     90            "}", "${${} }" 
     91        ] 
     92        t.plan(4 + unchanged.length); 
     93 
     94        var format = OpenLayers.String.format; 
     95         
     96        var expected; 
     97        for(var i=0; i<unchanged.length; ++i) { 
     98            expected = unchanged[i]; 
     99            t.eq(format(expected), expected, 
     100                 "'" + expected + "' left unchanged"); 
     101        } 
     102 
     103        t.eq(format("${foo} none"), 
     104             "undefined none", "undefined properties don't bomb"); 
     105 
     106        window.foo = "bar"; 
     107        t.eq(format("${foo} none"), 
     108             "bar none", "window context used if none passed"); 
     109         
     110        var context = {bar: "foo"}; 
     111        t.eq(format("${bar} foo", context), "foo foo", 
     112             "properties accessed from context"); 
     113         
     114        var context = {bar: "foo", foo: "bar"}; 
     115        t.eq(format("a ${bar} is a ${foo}", context), "a foo is a bar", 
     116             "multiple properties replaced correctly"); 
     117 
     118    } 
    86119    
     120    
    87121    function test_06_Number_limitSigDigs(t) { 
    88122        t.plan(9); 
    89123       
  • lib/OpenLayers/BaseTypes.js

    old new  
    8787            camelizedString += s.charAt(0).toUpperCase() + s.substring(1); 
    8888        } 
    8989        return camelizedString; 
     90    }, 
     91 
     92    /** 
     93     * APIFunction: OpenLayers.String.format 
     94     * Given a string with tokens in the form ${token}, return a string 
     95     *     with tokens replaced with properties from the given context 
     96     *     object.  Represent a literal "${" by doubling it, e.g. "${${". 
     97     * 
     98     * Parameters: 
     99     * template - {String} A string with tokens to be replaced.  A template 
     100     *     has the form "literal ${token}" where the token will be replaced 
     101     *     by the value of context["token"]. 
     102     * context - {Object} An optional object with properties corresponding 
     103     *     to the tokens in the format string.  If no context is sent, the 
     104     *     window object will be used. 
     105     * 
     106     * Returns: 
     107     * {String} A string with tokens replaced from the context object. 
     108     */ 
     109    format: function(template, context) { 
     110        if(!context) { 
     111            context = window; 
     112        } 
     113        var tokens = template.split("${"); 
     114        var item, last; 
     115        for(var i=1; i<tokens.length; i++) { 
     116            item = tokens[i]; 
     117            last = item.indexOf("}");  
     118            if(last > 0) {  
     119                tokens[i] = context[item.substring(0, last)] + 
     120                            item.substring(++last);  
     121            } else { 
     122                tokens[i] = "${" + item; 
     123            } 
     124        } 
     125        return tokens.join(""); 
    90126    } 
     127 
    91128}; 
    92129 
    93130if (!String.prototype.startsWith) {