substitute.js.html revision eb9b6f817df5c660be9f00fc5d27fe33479fdc98
<html>
<head>
</head>
<body id="yahoo-com">
<div id="doc3" class="yui-t2">
<div id="hd">
<h3>dd-plugin <span class="subtitle">3.00PR1</span></h3>
<p>
> substitute.js (source view)
</p>
</div>
<div id="bd">
<div id="yui-main">
<div class="yui-b">
<form name="yui-classopts-form">
<span id="classopts"><input type="checkbox" name="showprivate" id="showprivate" /> Show Private</span>
<span id="classopts"><input type="checkbox" name="showprotected" id="showprotected" /> Show Protected</span>
<span id="classopts"><input type="checkbox" name="showdeprecated" id="showdeprecated" /> Show Deprecated</span>
</form>
<div id="srcout">
<style>
#doc3 #classopts { display:none; }
</style>
<div class="highlight" ><pre><span class="nx">YUI</span><span class="o">.</span><span class="nx">add</span><span class="o">(</span><span class="s2">"substitute"</span><span class="o">,</span> <span class="k">function</span><span class="o">(</span><span class="nx">Y</span><span class="o">)</span> <span class="o">{</span>
<span class="k">var</span> <span class="nx">L</span> <span class="o">=</span> <span class="nx">Y</span><span class="o">.</span><span class="nx">Lang</span><span class="o">,</span> <span class="nx">DUMP</span><span class="o">=</span><span class="s1">'dump'</span><span class="o">,</span> <span class="nx">SPACE</span><span class="o">=</span><span class="s1">' '</span><span class="o">,</span> <span class="nx">LBRACE</span><span class="o">=</span><span class="s1">'{'</span><span class="o">,</span> <span class="nx">RBRACE</span><span class="o">=</span><span class="s1">'}'</span><span class="o">,</span>
<span class="c">/**</span>
<span class="c"> * Does variable substitution on a string. It scans through the string </span>
<span class="c"> * looking for expressions enclosed in { } braces. If an expression </span>
<span class="c"> * is found, it is used a key on the object. If there is a space in</span>
<span class="c"> * the key, the first word is used for the key and the rest is provided</span>
<span class="c"> * to an optional function to be used to programatically determine the</span>
<span class="c"> * value (the extra information might be used for this decision). If </span>
<span class="c"> * the value for the key in the object, or what is returned from the</span>
<span class="c"> * function has a string value, number value, or object value, it is </span>
<span class="c"> * substituted for the bracket expression and it repeats. If this</span>
<span class="c"> * value is an object, it uses the Object's toString() if this has</span>
<span class="c"> * is used).</span>
<span class="c"> *</span>
<span class="c"> * This method is included in the 'substitute' module. It is not included</span>
<span class="c"> * in the YUI module.</span>
<span class="c"> *</span>
<span class="c"> * @method substitute</span>
<span class="c"> * @param s {String} The string that will be modified.</span>
<span class="c"> * @param o {Object} An object containing the replacement values</span>
<span class="c"> * @param f {Function} An optional function that can be used to</span>
<span class="c"> * process each match. It receives the key,</span>
<span class="c"> * value, and any extra metadata included with</span>
<span class="c"> * the key inside of the braces.</span>
<span class="c"> * @return {String} the substituted string</span>
<span class="c"> */</span>
<span class="nx">substitute</span> <span class="o">=</span> <span class="k">function</span> <span class="o">(</span><span class="nx">s</span><span class="o">,</span> <span class="nx">o</span><span class="o">,</span> <span class="nx">f</span><span class="o">)</span> <span class="o">{</span>
<span class="k">var</span> <span class="nx">i</span><span class="o">,</span> <span class="nx">j</span><span class="o">,</span> <span class="nx">k</span><span class="o">,</span> <span class="nx">key</span><span class="o">,</span> <span class="nx">v</span><span class="o">,</span> <span class="nx">meta</span><span class="o">,</span> <span class="nx">saved</span><span class="o">=[],</span> <span class="nx">token</span><span class="o">;</span>
<span class="k">for</span> <span class="o">(;;)</span> <span class="o">{</span>
<span class="nx">i</span> <span class="o">=</span> <span class="nx">s</span><span class="o">.</span><span class="nx">lastIndexOf</span><span class="o">(</span><span class="nx">LBRACE</span><span class="o">);</span>
<span class="k">if</span> <span class="o">(</span><span class="nx">i</span> <span class="o"><</span> <span class="m">0</span><span class="o">)</span> <span class="o">{</span>
<span class="k">break</span><span class="o">;</span>
<span class="o">}</span>
<span class="nx">j</span> <span class="o">=</span> <span class="nx">s</span><span class="o">.</span><span class="nx">indexOf</span><span class="o">(</span><span class="nx">RBRACE</span><span class="o">,</span> <span class="nx">i</span><span class="o">);</span>
<span class="k">if</span> <span class="o">(</span><span class="nx">i</span> <span class="o">+</span> <span class="m">1</span> <span class="o">>=</span> <span class="nx">j</span><span class="o">)</span> <span class="o">{</span>
<span class="k">break</span><span class="o">;</span>
<span class="o">}</span>
<span class="c">//Extract key and meta info </span>
<span class="c"></span> <span class="nx">token</span> <span class="o">=</span> <span class="nx">s</span><span class="o">.</span><span class="nx">substring</span><span class="o">(</span><span class="nx">i</span> <span class="o">+</span> <span class="m">1</span><span class="o">,</span> <span class="nx">j</span><span class="o">);</span>
<span class="nx">key</span> <span class="o">=</span> <span class="nx">token</span><span class="o">;</span>
<span class="nx">meta</span> <span class="o">=</span> <span class="kc">null</span><span class="o">;</span>
<span class="nx">k</span> <span class="o">=</span> <span class="nx">key</span><span class="o">.</span><span class="nx">indexOf</span><span class="o">(</span><span class="nx">SPACE</span><span class="o">);</span>
<span class="k">if</span> <span class="o">(</span><span class="nx">k</span> <span class="o">></span> <span class="o">-</span><span class="m">1</span><span class="o">)</span> <span class="o">{</span>
<span class="nx">meta</span> <span class="o">=</span> <span class="nx">key</span><span class="o">.</span><span class="nx">substring</span><span class="o">(</span><span class="nx">k</span> <span class="o">+</span> <span class="m">1</span><span class="o">);</span>
<span class="nx">key</span> <span class="o">=</span> <span class="nx">key</span><span class="o">.</span><span class="nx">substring</span><span class="o">(</span><span class="m">0</span><span class="o">,</span> <span class="nx">k</span><span class="o">);</span>
<span class="o">}</span>
<span class="c">// lookup the value</span>
<span class="c"></span> <span class="nx">v</span> <span class="o">=</span> <span class="nx">o</span><span class="o">[</span><span class="nx">key</span><span class="o">];</span>
<span class="c">// if a substitution function was provided, execute it</span>
<span class="c"></span> <span class="k">if</span> <span class="o">(</span><span class="nx">f</span><span class="o">)</span> <span class="o">{</span>
<span class="nx">v</span> <span class="o">=</span> <span class="nx">f</span><span class="o">(</span><span class="nx">key</span><span class="o">,</span> <span class="nx">v</span><span class="o">,</span> <span class="nx">meta</span><span class="o">);</span>
<span class="o">}</span>
<span class="k">if</span> <span class="o">(</span><span class="nx">L</span><span class="o">.</span><span class="nx">isObject</span><span class="o">(</span><span class="nx">v</span><span class="o">))</span> <span class="o">{</span>
<span class="k">if</span> <span class="o">(!</span><span class="nx">Y</span><span class="o">.</span><span class="nx">dump</span><span class="o">)</span> <span class="o">{</span>
<span class="nx">v</span> <span class="o">=</span> <span class="nx">v</span><span class="o">.</span><span class="nx">toString</span><span class="o">();</span>
<span class="o">}</span> <span class="k">else</span> <span class="o">{</span>
<span class="k">if</span> <span class="o">(</span><span class="nx">L</span><span class="o">.</span><span class="nx">isArray</span><span class="o">(</span><span class="nx">v</span><span class="o">))</span> <span class="o">{</span>
<span class="nx">v</span> <span class="o">=</span> <span class="nx">Y</span><span class="o">.</span><span class="nx">dump</span><span class="o">(</span><span class="nx">v</span><span class="o">,</span> <span class="nb">parseInt</span><span class="o">(</span><span class="nx">meta</span><span class="o">,</span> <span class="m">10</span><span class="o">));</span>
<span class="o">}</span> <span class="k">else</span> <span class="o">{</span>
<span class="nx">meta</span> <span class="o">=</span> <span class="nx">meta</span> <span class="o">||</span> <span class="s2">""</span><span class="o">;</span>
<span class="c">// look for the keyword 'dump', if found force obj dump</span>
<span class="c"></span> <span class="k">var</span> <span class="nx">dump</span> <span class="o">=</span> <span class="nx">meta</span><span class="o">.</span><span class="nx">indexOf</span><span class="o">(</span><span class="nx">DUMP</span><span class="o">);</span>
<span class="k">if</span> <span class="o">(</span><span class="nx">dump</span> <span class="o">></span> <span class="o">-</span><span class="m">1</span><span class="o">)</span> <span class="o">{</span>
<span class="nx">meta</span> <span class="o">=</span> <span class="nx">meta</span><span class="o">.</span><span class="nx">substring</span><span class="o">(</span><span class="m">4</span><span class="o">);</span>
<span class="o">}</span>
<span class="c">// use the toString if it is not the Object toString </span>
<span class="c"></span> <span class="c">// and the 'dump' meta info was not found</span>
<span class="c"></span> <span class="k">if</span> <span class="o">(</span><span class="nx">v</span><span class="o">.</span><span class="nx">toString</span><span class="o">===</span><span class="nb">Object</span><span class="o">.</span><span class="nx">prototype</span><span class="o">.</span><span class="nx">toString</span><span class="o">||</span><span class="nx">dump</span><span class="o">>-</span><span class="m">1</span><span class="o">)</span> <span class="o">{</span>
<span class="nx">v</span> <span class="o">=</span> <span class="nx">Y</span><span class="o">.</span><span class="nx">dump</span><span class="o">(</span><span class="nx">v</span><span class="o">,</span> <span class="nb">parseInt</span><span class="o">(</span><span class="nx">meta</span><span class="o">,</span> <span class="m">10</span><span class="o">));</span>
<span class="o">}</span> <span class="k">else</span> <span class="o">{</span>
<span class="nx">v</span> <span class="o">=</span> <span class="nx">v</span><span class="o">.</span><span class="nx">toString</span><span class="o">();</span>
<span class="o">}</span>
<span class="o">}</span>
<span class="o">}</span>
<span class="o">}</span> <span class="k">else</span> <span class="k">if</span> <span class="o">(!</span><span class="nx">L</span><span class="o">.</span><span class="nx">isString</span><span class="o">(</span><span class="nx">v</span><span class="o">)</span> <span class="o">&&</span> <span class="o">!</span><span class="nx">L</span><span class="o">.</span><span class="nx">isNumber</span><span class="o">(</span><span class="nx">v</span><span class="o">))</span> <span class="o">{</span>
<span class="c">// This {block} has no replace string. Save it for later.</span>
<span class="c"></span> <span class="nx">v</span> <span class="o">=</span> <span class="s2">"~-"</span> <span class="o">+</span> <span class="nx">saved</span><span class="o">.</span><span class="nx">length</span> <span class="o">+</span> <span class="s2">"-~"</span><span class="o">;</span>
<span class="nx">saved</span><span class="o">[</span><span class="nx">saved</span><span class="o">.</span><span class="nx">length</span><span class="o">]</span> <span class="o">=</span> <span class="nx">token</span><span class="o">;</span>
<span class="c">// break;</span>
<span class="c"></span> <span class="o">}</span>
<span class="nx">s</span> <span class="o">=</span> <span class="nx">s</span><span class="o">.</span><span class="nx">substring</span><span class="o">(</span><span class="m">0</span><span class="o">,</span> <span class="nx">i</span><span class="o">)</span> <span class="o">+</span> <span class="nx">v</span> <span class="o">+</span> <span class="nx">s</span><span class="o">.</span><span class="nx">substring</span><span class="o">(</span><span class="nx">j</span> <span class="o">+</span> <span class="m">1</span><span class="o">);</span>
<span class="o">}</span>
<span class="c">// restore saved {block}s</span>
<span class="c"></span> <span class="k">for</span> <span class="o">(</span><span class="nx">i</span><span class="o">=</span><span class="nx">saved</span><span class="o">.</span><span class="nx">length</span><span class="o">-</span><span class="m">1</span><span class="o">;</span> <span class="nx">i</span><span class="o">>=</span><span class="m">0</span><span class="o">;</span> <span class="nx">i</span><span class="o">=</span><span class="nx">i</span><span class="o">-</span><span class="m">1</span><span class="o">)</span> <span class="o">{</span>
<span class="nx">s</span> <span class="o">=</span> <span class="nx">s</span><span class="o">.</span><span class="nx">replace</span><span class="o">(</span><span class="k">new</span> <span class="nb">RegExp</span><span class="o">(</span><span class="s2">"~-"</span> <span class="o">+</span> <span class="nx">i</span> <span class="o">+</span> <span class="s2">"-~"</span><span class="o">),</span> <span class="s2">"{"</span> <span class="o">+</span> <span class="nx">saved</span><span class="o">[</span><span class="nx">i</span><span class="o">]</span> <span class="o">+</span> <span class="s2">"}"</span><span class="o">,</span> <span class="s2">"g"</span><span class="o">);</span>
<span class="o">}</span>
<span class="k">return</span> <span class="nx">s</span><span class="o">;</span>
<span class="o">};</span>
<span class="nx">Y</span><span class="o">.</span><span class="nx">substitute</span> <span class="o">=</span> <span class="nx">substitute</span><span class="o">;</span>
<span class="nx">L</span><span class="o">.</span><span class="nx">substitute</span> <span class="o">=</span> <span class="nx">substitute</span><span class="o">;</span>
<span class="o">},</span> <span class="s2">"@VERSION@"</span><span class="o">,</span> <span class="o">{</span>
<span class="nx">optional</span><span class="o">:</span> <span class="o">[</span><span class="s1">'dump'</span><span class="o">]</span>
<span class="o">});</span>
</pre></div>
</div>
</div>
</div>
<div class="yui-b">
<div class="nav">
<div class="module">
<h4>Modules</h4>
<ul class="content">
</ul>
</div>
<div class="module">
<h4>Classes</h4>
<ul class="content">
</ul>
</div>
<div class="module">
<h4>Files</h4>
<ul class="content">
</ul>
</div>
</div>
</div>
</div>
<div id="ft">
<hr />
Copyright © 2008 Yahoo! Inc. All rights reserved.
</div>
</div>
</body>
</html>