substitute.js.html revision eb9b6f817df5c660be9f00fc5d27fe33479fdc98
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<title>API: dd-plugin substitute.js (YUI Library)</title>
<link rel="stylesheet" type="text/css" href="assets/api.css">
<script type="text/javascript" src="assets/api-js"></script>
<script type="text/javascript" src="assets/ac-js"></script>
</head>
<body id="yahoo-com">
<div id="doc3" class="yui-t2">
<div id="hd">
<a href="http://developer.yahoo.com/yui/"><h1>Yahoo! UI Library</h1></a>
<h3>dd-plugin&nbsp; <span class="subtitle">3.00PR1</span></h3>
<p>
<a href="/index.html">Yahoo! UI Library</a>
&gt; <a href="/module_dd-plugin.html">dd-plugin</a>
&gt; 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">&quot;substitute&quot;</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">&#39;dump&#39;</span><span class="o">,</span> <span class="nx">SPACE</span><span class="o">=</span><span class="s1">&#39; &#39;</span><span class="o">,</span> <span class="nx">LBRACE</span><span class="o">=</span><span class="s1">&#39;{&#39;</span><span class="o">,</span> <span class="nx">RBRACE</span><span class="o">=</span><span class="s1">&#39;}&#39;</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&#39;s toString() if this has</span>
<span class="c"> * been overridden, otherwise it does a shallow dump of the key/value</span>
<span class="c"> * pairs if Y.dump is available (if dump isn&#39;t available, toString()</span>
<span class="c"> * is used).</span>
<span class="c"> *</span>
<span class="c"> * This method is included in the &#39;substitute&#39; 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">&lt;</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">&gt;=</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">&gt;</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">&quot;&quot;</span><span class="o">;</span>
<span class="c">// look for the keyword &#39;dump&#39;, 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">&gt;</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 &#39;dump&#39; 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">&gt;-</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">&amp;&amp;</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">&quot;~-&quot;</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">&quot;-~&quot;</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">&gt;=</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">&quot;~-&quot;</span> <span class="o">+</span> <span class="nx">i</span> <span class="o">+</span> <span class="s2">&quot;-~&quot;</span><span class="o">),</span> <span class="s2">&quot;{&quot;</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">&quot;}&quot;</span><span class="o">,</span> <span class="s2">&quot;g&quot;</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">&quot;@VERSION@&quot;</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">&#39;dump&#39;</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">
<li class=""><a href="module_anim.html">anim</a></li>
<li class=""><a href="module_anim-color.html">anim-color</a></li>
<li class=""><a href="module_anim-curve.html">anim-curve</a></li>
<li class=""><a href="module_anim-easing.html">anim-easing</a></li>
<li class=""><a href="module_anim-scroll.html">anim-scroll</a></li>
<li class=""><a href="module_anim-xy.html">anim-xy</a></li>
<li class=""><a href="module_attribute.html">attribute</a></li>
<li class=""><a href="module_base.html">base</a></li>
<li class="selected"><a href="module_dd-plugin.html">dd-plugin</a></li>
<li class=""><a href="module_event.html">event</a></li>
<li class=""><a href="module_get.html">get</a></li>
<li class=""><a href="module_io.html">io</a></li>
<li class=""><a href="module_loader.html">loader</a></li>
<li class=""><a href="module_node.html">node</a></li>
<li class=""><a href="module_node-class.html">node-class</a></li>
<li class=""><a href="module_node-region.html">node-region</a></li>
<li class=""><a href="module_node-screen.html">node-screen</a></li>
<li class=""><a href="module_Queue.html">Queue</a></li>
<li class=""><a href="module_yui.html">yui</a></li>
</ul>
</div>
<div class="module">
<h4>Classes</h4>
<ul class="content">
<li class=""><a href="DragPlugin.html">DragPlugin</a></li>
<li class=""><a href="DropPlugin.html">DropPlugin</a></li>
</ul>
</div>
<div class="module">
<h4>Files</h4>
<ul class="content">
<li class=""><a href="dd-drop-plugin.js.html">dd-drop-plugin.js</a></li>
<li class=""><a href="dd-plugin.js.html">dd-plugin.js</a></li>
<li class=""><a href="dump.js.html">dump.js</a></li>
<li class="selected"><a href="substitute.js.html">substitute.js</a></li>
</ul>
</div>
</div>
</div>
</div>
<div id="ft">
<hr />
Copyright &copy; 2008 Yahoo! Inc. All rights reserved.
</div>
</div>
</body>
</html>