~launchpad-pqm/lazr-js/toolchain

« back to all changes in this revision

Viewing changes to src-js/lazrjs/yui/substitute/substitute-debug.js

  • Committer: Sidnei da Silva
  • Date: 2009-11-16 00:51:29 UTC
  • mto: This revision was merged to the branch mainline in revision 154.
  • Revision ID: sidnei.da.silva@canonical.com-20091116005129-8ibwjlboa38glaw5
- Improved generation of skin modules and revamped combo service to make it more twisty.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/*
2
 
Copyright (c) 2010, Yahoo! Inc. All rights reserved.
3
 
Code licensed under the BSD License:
4
 
http://developer.yahoo.com/yui/license.html
5
 
version: 3.2.0
6
 
build: 2676
7
 
*/
8
 
YUI.add('substitute', function(Y) {
9
 
 
10
 
/**
11
 
 * String variable substitution and string formatting.
12
 
 * If included, the substitute method is added to the YUI instance.
13
 
 *
14
 
 * @module substitute
15
 
 */
16
 
 
17
 
    var L = Y.Lang, DUMP='dump', SPACE=' ', LBRACE='{', RBRACE='}',
18
 
 
19
 
    /**
20
 
     * The following methods are added to the YUI instance
21
 
     * @class YUI~substitute
22
 
     */
23
 
 
24
 
    /**
25
 
     * Does variable substitution on a string. It scans through the string 
26
 
     * looking for expressions enclosed in { } braces. If an expression 
27
 
     * is found, it is used a key on the object.  If there is a space in
28
 
     * the key, the first word is used for the key and the rest is provided
29
 
     * to an optional function to be used to programatically determine the
30
 
     * value (the extra information might be used for this decision). If 
31
 
     * the value for the key in the object, or what is returned from the
32
 
     * function has a string value, number value, or object value, it is 
33
 
     * substituted for the bracket expression and it repeats.  If this
34
 
     * value is an object, it uses the Object's toString() if this has
35
 
     * been overridden, otherwise it does a shallow dump of the key/value
36
 
     * pairs if Y.dump is available (if dump isn't available, toString()
37
 
     * is used).
38
 
     *
39
 
     * This method is included in the 'substitute' module.  It is not included
40
 
     * in the YUI module.
41
 
     *
42
 
     * @method substitute
43
 
     * @param s {string} The string that will be modified.
44
 
     * @param o An object containing the replacement values
45
 
     * @param f {function} An optional function that can be used to
46
 
     *                     process each match.  It receives the key,
47
 
     *                     value, and any extra metadata included with
48
 
     *                     the key inside of the braces.
49
 
     * @return {string} the substituted string
50
 
     */
51
 
 
52
 
    substitute = function (s, o, f, recurse) {
53
 
        var i, j, k, key, v, meta, saved=[], token, dump,
54
 
            lidx = s.length;
55
 
 
56
 
        for (;;) {
57
 
            i = s.lastIndexOf(LBRACE, lidx);
58
 
            if (i < 0) {
59
 
                break;
60
 
            }
61
 
            j = s.indexOf(RBRACE, i);
62
 
            if (i + 1 >= j) {
63
 
                break;
64
 
            }
65
 
 
66
 
            //Extract key and meta info 
67
 
            token = s.substring(i + 1, j);
68
 
            key = token;
69
 
            meta = null;
70
 
            k = key.indexOf(SPACE);
71
 
            if (k > -1) {
72
 
                meta = key.substring(k + 1);
73
 
                key = key.substring(0, k);
74
 
            }
75
 
 
76
 
            // lookup the value
77
 
            v = o[key];
78
 
 
79
 
            // if a substitution function was provided, execute it
80
 
            if (f) {
81
 
                v = f(key, v, meta);
82
 
            }
83
 
 
84
 
            if (L.isObject(v)) {
85
 
                if (!Y.dump) {
86
 
                    v = v.toString();
87
 
                } else {
88
 
                    if (L.isArray(v)) {
89
 
                        v = Y.dump(v, parseInt(meta, 10));
90
 
                    } else {
91
 
                        meta = meta || "";
92
 
 
93
 
                        // look for the keyword 'dump', if found force obj dump
94
 
                        dump = meta.indexOf(DUMP);
95
 
                        if (dump > -1) {
96
 
                            meta = meta.substring(4);
97
 
                        }
98
 
 
99
 
                        // use the toString if it is not the Object toString 
100
 
                        // and the 'dump' meta info was not found
101
 
                        if (v.toString===Object.prototype.toString||dump>-1) {
102
 
                            v = Y.dump(v, parseInt(meta, 10));
103
 
                        } else {
104
 
                            v = v.toString();
105
 
                        }
106
 
                    }
107
 
                }
108
 
            } else if (!L.isString(v) && !L.isNumber(v)) {
109
 
                // This {block} has no replace string. Save it for later.
110
 
                v = "~-" + saved.length + "-~";
111
 
                saved[saved.length] = token;
112
 
 
113
 
                // break;
114
 
            }
115
 
 
116
 
            s = s.substring(0, i) + v + s.substring(j + 1);
117
 
 
118
 
            if (!recurse) {
119
 
                lidx = i-1;
120
 
            }
121
 
 
122
 
        }
123
 
 
124
 
        // restore saved {block}s
125
 
        for (i=saved.length-1; i>=0; i=i-1) {
126
 
            s = s.replace(new RegExp("~-" + i + "-~"), LBRACE  + saved[i] + RBRACE, "g");
127
 
        }
128
 
 
129
 
        return s;
130
 
 
131
 
    };
132
 
 
133
 
    Y.substitute = substitute;
134
 
    L.substitute = substitute;
135
 
 
136
 
 
137
 
 
138
 
}, '3.2.0' ,{optional:['dump']});