<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Garrick Cheung &#187; Sly</title>
	<atom:link href="http://www.garrickcheung.com/tag/sly/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.garrickcheung.com</link>
	<description>Sharing what I know and learn about CSS, MooTools, Javascript, PHP and etc.</description>
	<lastBuildDate>Fri, 30 Sep 2011 05:16:10 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=</generator>
		<item>
		<title>Sly, New Selector Engine &amp; How to Use in MooTools</title>
		<link>http://www.garrickcheung.com/javascript/sly-selector-engine/</link>
		<comments>http://www.garrickcheung.com/javascript/sly-selector-engine/#comments</comments>
		<pubDate>Thu, 26 Mar 2009 16:00:44 +0000</pubDate>
		<dc:creator>Garrick</dc:creator>
				<category><![CDATA[Javascript]]></category>
		<category><![CDATA[MooTools]]></category>
		<category><![CDATA[selector]]></category>
		<category><![CDATA[selector engine]]></category>
		<category><![CDATA[Sly]]></category>

		<guid isPermaLink="false">http://www.garrickcheung.com/?p=302</guid>
		<description><![CDATA[You&#8217;ve heard of selector engines right? The engine that parses the HTML to match a string argument to return matching HTML elements? How about Sizzle and Peppy (two fast selector engines) that was released? Or how a couple JS frameworks were implementing Sizzle and the MooTools dev team decided not to follow suit? Now, have [...]]]></description>
			<content:encoded><![CDATA[<p>You&#8217;ve heard of selector engines right? The engine that parses the HTML to match a string argument to return matching HTML elements? How about Sizzle and Peppy (two fast selector engines) that was released? Or how a couple JS frameworks were implementing Sizzle and the <a href="http://mootools.net/blog/2008/12/04/sizzle/" target="_blank">MooTools dev team decided not to follow suit</a>?</p>
<p>Now, have you heard of Sly, the new selector engine on the block? No? That&#8217;s because <a href="http://digitarald.de" target="_blank">Harald Kirschner</a> just released it yesterday, 03/25/09, and I can&#8217;t tell you how awesome it is.. or can I?</p>
<p>I&#8217;ll start you off with a brief description&#8230; and graphs!<span id="more-302"></span></p>
<blockquote><p>Sly is a turbocharged, cross-browser, library-agnostic JavaScript class for querying DOM documents using CSS3 selectors.</p></blockquote>
<p>And turbocharged it sure is, just check out the stats!<br />
<img src="http://www.garrickcheung.com/wp-content/uploads/sly_selector_performance.png" alt="Sly Selector Performance" style="width:455px;" /></p>
<p>The benefits:</p>
<ul>
<li>Pure and powerful <em>JavaScript</em> matching algorithm for <em>fast</em> and <em>accurate</em> queries</li>
<li>Extra optimizations for <em>frequently used selectors</em> and <em>latest browser features</em></li>
<li>Works uniformly in <code>DOM</code> documents, fragments or <code>XML</code> documents</li>
<li>Utility methods for matching and filtering of elements</li>
<li><em>Standalone selector parser</em> to produce <em>JavaScript</em> <code>Object</code> representations</li>
<li><em>Customizable</em> pseudo-classes, attribute operators and combinators</li>
<li>Just <strong>3 kB</strong>! (<a href="http://developer.yahoo.com/yui/compressor/" target="_blank">minified</a> and <a href="http://www.gzip.org/" target="_blank">gzipped</a>, <strong>8 kB</strong> without <em>gzip</em>)</li>
<li>No dependencies on third-party JS libraries, but developers can override internal methods (like <code>getAttribute</code>) for seamless integration.</li>
<li>Code follows the <a href="http://mootools.net" target="_blank">MooTools</a> philosophy, respecting strict standards, throwing no warnings and using meaningful variable names</li>
</ul>
<h3>Use it in MooTools?</h3>
<p>Yes, that&#8217;s right. You can use it in MooTools right now, though I don&#8217;t think it&#8217;s necessary since the MooTools selector engine is very good. But just like how <a href="http://davidwalsh.name/mootools-peppy-sizzle" target="_blank">David Walsh showed us how to use Sizzle and Peppy in MooTools</a>, you can do the following to use Sly:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
</pre></td><td class="code"><pre class="javascript" style="font-family:monospace;">Window.<span style="color: #660066;">implement</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#123;</span>
    $Sly<span style="color: #339933;">:</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>selector<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
        <span style="color: #000066; font-weight: bold;">return</span> <span style="color: #003366; font-weight: bold;">new</span> Sly<span style="color: #009900;">&#40;</span>selector<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #006600; font-style: italic;">// returns an array of all the anchors in the page</span>
$Sly<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'a'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">search</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #006600; font-style: italic;">// returns the first anchor on the page</span>
$Sly<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'a'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">find</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>Now, you may be wondering whether doing this will allow you to use MooTools methods on the elements. Well, the good news is that you can because the MooTools Elements class applies the methods to all elements. Observe:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
</pre></td><td class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #006600; font-style: italic;">// returns the href of the first anchor</span>
$Sly<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'a'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">find</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">get</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'href'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>And lastly, <a href="http://blog.olicio.us/2009/03/19/sly–-the-super-snazzy-selector-engine/" target="_blank">Oskar Krawczyk posted how to replace the $$ function itself (thanks Oskar!)</a>:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
</pre></td><td class="code"><pre class="javascript" style="font-family:monospace;">Window.<span style="color: #660066;">implement</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#123;</span>
    $$<span style="color: #339933;">:</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>selector<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
        <span style="color: #000066; font-weight: bold;">return</span> Sly.<span style="color: #660066;">search</span><span style="color: #009900;">&#40;</span>selector<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>Great job, Harald, great job indeed. Way to represent!</p>
]]></content:encoded>
			<wfw:commentRss>http://www.garrickcheung.com/javascript/sly-selector-engine/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>

