<?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>Web 3.0, 6 Bladed Razors, 7 Minute Abs &#187; Packer</title>
	<atom:link href="http://www.zachleat.com/web/tag/packer/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.zachleat.com/web</link>
	<description></description>
	<lastBuildDate>Mon, 06 Feb 2012 17:09:08 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	
		<item>
		<title>Using Phing to automate JavaScript and CSS Minimization</title>
		<link>http://www.zachleat.com/web/using-phing-to-automate-javascript-and-css-minimization/</link>
		<comments>http://www.zachleat.com/web/using-phing-to-automate-javascript-and-css-minimization/#comments</comments>
		<pubDate>Sat, 11 Aug 2007 06:33:53 +0000</pubDate>
		<dc:creator>Zach Leatherman</dc:creator>
				<category><![CDATA[CSS]]></category>
		<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[csstidy]]></category>
		<category><![CDATA[Packer]]></category>
		<category><![CDATA[Phing]]></category>

		<guid isPermaLink="false">http://www.zachleat.com/web/2007/08/11/using-phing-to-automate-javascript-and-css-minimization/</guid>
		<description><![CDATA[This article may be too advanced for beginner programmers. Unfortunately, I will not support any code that I do not write (in this case), so if you have troubles installing some of the packages required below, please see the authors of the problem code. Thank you. PHING! Party on Wayne. Party on Garth! If you&#8217;ve [...]]]></description>
			<content:encoded><![CDATA[<p><em>This article may be too advanced for beginner programmers.  Unfortunately, I will not support any code that I do not write (in this case), so if you have troubles installing some of the packages required below, please see the authors of the problem code.  Thank you.</em></p>
<p>PHING!  <a href="http://en.wikipedia.org/wiki/Wayne%27s_World">Party on Wayne.  Party on Garth!</a></p>
<p>If you&#8217;ve never used Phing, it&#8217;s an automation tool for PHP that is a port of Java&#8217;s ANT Build tool.  But it&#8217;s not just for building (obviously, that&#8217;d be silly).  You can hook all kinds of tasks into Phing: unit tests for your PHP code (you&#8217;re doing test driven development, aren&#8217;t you?), building your documentation, etc.  But today, let&#8217;s look at automation of my favorite front end development tasks: packing (minimizing) our JavaScript and CSS.</p>
<p>To do this, I&#8217;m going to hook you up with two Filters for Phing that will use the <a href="http://joliclic.free.fr/php/javascript-packer/en/">PHP port of Dean Edwards JavaScript Packer</a>, and the PHP class <a href="http://csstidy.sourceforge.net/">CSSTidy</a>.</p>
<p>Here&#8217;s what you&#8217;ll be responsible for:</p>
<ol>
<li>Download both CSSTidy and the PHP port of Packer using the links above.</li>
<li>Download Phing if you don&#8217;t already have it, and install it.  Note the following change I had to make to my Phing \bin\phing.bat file to get it working: Change <code>set PHP_CLASSPATH="%PHING_HOME%\classes"</code> to remove the quotes: <code>set PHP_CLASSPATH=%PHING_HOME%\classes</code></li>
<li>Download the two Filters I made for Phing: <strong><a href="/Projects/phing/JSPackerFilter.phps">JSPackerFilter.phps</a></strong> and <strong><a href="/Projects/phing/CssTidyFilter.phps">CssTidyFilter.phps</a></strong>, change the extensions to .php and copy into your phing directory \classes\phing\filters\</li>
<li>Change the path in the include_once declaration at the top of each of the files to point to the csstidy and packer libraries you downloaded above:
<pre language="php">
include_once "C:\Lib\packer.php-1.0\class.JavaScriptPacker.php";
</pre>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #b1b100;">include_once</span> <span style="color: #0000ff;">&quot;C:\Lib\csstidy-1.3\class.csstidy.php&quot;</span><span style="color: #339933;">;</span></pre></div></div>

</li>
<li>Get a working build file set up to point the directories your project is using.  Here is a sample I made:

<div class="wp_syntax"><div class="code"><pre class="xml" style="font-family:monospace;"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;project</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;testProject&quot;</span> <span style="color: #000066;">default</span>=<span style="color: #ff0000;">&quot;pack-all&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
	<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;target</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;pack-all&quot;</span> <span style="color: #000066;">depends</span>=<span style="color: #ff0000;">&quot;pack-javascript,pack-css&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
	<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;target</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;pack-javascript&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
		<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;copy</span> <span style="color: #000066;">todir</span>=<span style="color: #ff0000;">&quot;./Web/js&quot;</span> <span style="color: #000066;">overwrite</span>=<span style="color: #ff0000;">&quot;true&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
			<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;fileset</span> <span style="color: #000066;">dir</span>=<span style="color: #ff0000;">&quot;../Web/js&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
				<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;include</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;*.js&quot;</span> <span style="color: #000000; font-weight: bold;">/&gt;</span></span>
			<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/fileset<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
&nbsp;
			<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;filterchain<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
				<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;filterreader</span> <span style="color: #000066;">classname</span>=<span style="color: #ff0000;">&quot;phing.filters.JSPackerFilter&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
				  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;param</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;encoding&quot;</span> <span style="color: #000066;">value</span>=<span style="color: #ff0000;">&quot;62&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span><span style="color: #808080; font-style: italic;">&lt;!-- 0,10,62,95 or 'None', 'Numeric', 'Normal', 'High ASCII'. --&gt;</span>
				  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;param</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;fastDecode&quot;</span> <span style="color: #000066;">value</span>=<span style="color: #ff0000;">&quot;true&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span><span style="color: #808080; font-style: italic;">&lt;!-- include the fast decoder in the packed result --&gt;</span>
				  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;param</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;specialCharacters&quot;</span> <span style="color: #000066;">value</span>=<span style="color: #ff0000;">&quot;false&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span><span style="color: #808080; font-style: italic;">&lt;!-- have you flagged your private and local variables in the script --&gt;</span>
				<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/filterreader<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
			<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/filterchain<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
		<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/copy<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
	<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/target<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
	<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;target</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;pack-css&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
		<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;copy</span> <span style="color: #000066;">todir</span>=<span style="color: #ff0000;">&quot;./Web/css&quot;</span> <span style="color: #000066;">overwrite</span>=<span style="color: #ff0000;">&quot;true&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
			<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;fileset</span> <span style="color: #000066;">dir</span>=<span style="color: #ff0000;">&quot;../Web/css&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
				<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;include</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;*.css&quot;</span> <span style="color: #000000; font-weight: bold;">/&gt;</span></span>
			<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/fileset<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
&nbsp;
			<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;filterchain<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
				<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;filterreader</span> <span style="color: #000066;">classname</span>=<span style="color: #ff0000;">&quot;phing.filters.CssTidyFilter&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
					<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;param</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;remove_last_;&quot;</span> <span style="color: #000066;">value</span>=<span style="color: #ff0000;">&quot;true&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
					<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;param</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;preserveCss&quot;</span> <span style="color: #000066;">value</span>=<span style="color: #ff0000;">&quot;false&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
					<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;param</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;template&quot;</span> <span style="color: #000066;">value</span>=<span style="color: #ff0000;">&quot;high_compression&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
				<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/filterreader<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
			<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/filterchain<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
		<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/copy<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
	<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/target<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/project<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></div></div>

<p>Templates for CssTidy include: low_compression, default, high_compression (declarations are limited to one line apiece), highest_compression (everything is put on one line).</p>
<p>The above build file is set up to work with the following directory structure:</p>
<ul>
<li>Build
<ul>
<li>Web
<ul>
<li>css</li>
<li>js</li>
</ul>
</li>
<li><strong>build.xml</strong></li>
</ul>
</li>
<li>Web
<ul>
<li>css</li>
<li>js</li>
</ul>
</li>
</ul>
<p>Files are copied from the source in /Web to the /Build/Web directory.  I hope that you can see from the build.xml file above that the target directory is specified in the copy tag, todir attribute.</p>

<div class="wp_syntax"><div class="code"><pre class="xml" style="font-family:monospace;"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;copy</span> <span style="color: #000066;">todir</span>=<span style="color: #ff0000;">&quot;./Web/js&quot;</span> <span style="color: #000066;">overwrite</span>=<span style="color: #ff0000;">&quot;true&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span></pre></div></div>

<p>and the source directory is specified in the fileset tag, dir attribute.</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #339933;">&lt;</span>fileset dir<span style="color: #339933;">=</span><span style="color: #3366CC;">&quot;../Web/css&quot;</span><span style="color: #339933;">&gt;</span>
	<span style="color: #339933;">&lt;</span>include <span style="color: #000066;">name</span><span style="color: #339933;">=</span><span style="color: #3366CC;">&quot;*.css&quot;</span> <span style="color: #339933;">/&gt;</span>
<span style="color: #339933;">&lt;/</span>fileset<span style="color: #339933;">&gt;</span></pre></div></div>

</li>
</ol>
<p>And then, all you need to do is navigate to the directory holding your build.xml and run the phing command (if you have phing in your path. If not, you can use an absolute link to phing, for example c:\software\phing\bin\phing).  Your minimized javascript and css will be in the Build directory!</p>
]]></content:encoded>
			<wfw:commentRss>http://www.zachleat.com/web/using-phing-to-automate-javascript-and-css-minimization/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>Using DOM Query Libraries in YUI</title>
		<link>http://www.zachleat.com/web/using-dom-query-libraries-in-yui/</link>
		<comments>http://www.zachleat.com/web/using-dom-query-libraries-in-yui/#comments</comments>
		<pubDate>Mon, 30 Jul 2007 07:37:03 +0000</pubDate>
		<dc:creator>Zach Leatherman</dc:creator>
				<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[CSS Selectors]]></category>
		<category><![CDATA[DED|Chain]]></category>
		<category><![CDATA[ExtJS]]></category>
		<category><![CDATA[jQuery]]></category>
		<category><![CDATA[Packer]]></category>
		<category><![CDATA[YUI]]></category>

		<guid isPermaLink="false">http://www.zachleat.com/web/2007/07/30/using-dom-query-libraries-in-yui/</guid>
		<description><![CDATA[Recently, I posted the top 8 things I thought the YUI Library needed to be a top tier JavaScript library again. One of those things included a CSS Selector DOM Querying class. Use one of these babies for awhile, and you&#8217;ll never be able to code without it again. They&#8217;re amazingly useful and will shorten [...]]]></description>
			<content:encoded><![CDATA[<p>Recently, I posted the top 8 things I thought the YUI Library needed to be a top tier JavaScript library again.  One of those things included a CSS Selector DOM Querying class.  Use one of these babies for awhile, and you&#8217;ll never be able to code without it again.  They&#8217;re amazingly useful and will shorten your code quite beautifully.</p>
<p>So, let&#8217;s plug one of them into YUI!  Let&#8217;s get some of that power under the hood.  And let&#8217;s make it work identically to how we write our code for YUI currently, instead of changing the coding style (a la Dustin Diaz&#8217;s great extension to YUI called <a href="http://dedchain.dustindiaz.com/">DED|Chain</a>).</p>
<p>How are we going to do this?  Let&#8217;s dive into the shallow end of the pool.</p>
<p>The include order of our javascript files is going to be important.  So, first we&#8217;ll do the YUI library core file (either yahoo-dom-event.js or utilities.js depending on your need for animation).  Next, we need to include a CSS Selector DOM Querying class.  For this proof of concept, I&#8217;ve provided examples for my two favorites, the jQuery engine and Jack Slocum&#8217;s DomQuery.  These files are released under the MIT license, so make sure that complies with your project.  It&#8217;s pretty liberal, so you shouldn&#8217;t have any problems.  If you find these querying libraries useful, please donate to these projects!</p>
<p>For code simplicity, I&#8217;ve added a one liner to Jack&#8217;s DomQuery to make it work as a standalone file.</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #000066; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span> <span style="color: #000066; font-weight: bold;">typeof</span> Ext <span style="color: #339933;">==</span> <span style="color: #3366CC;">'undefined'</span> <span style="color: #009900;">&#41;</span> <span style="color: #003366; font-weight: bold;">var</span> Ext <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span><span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span></pre></div></div>

<p>If you want to read more about DomQuery, you can go to <a href="http://www.jackslocum.com/blog/2007/01/11/domquery-css-selector-basic-xpath-implementation-with-benchmarks/">Jack&#8217;s site all about syntax and benchmarks</a>.</p>
<p>Download: <a href="/Projects/Y2/DomQuery.js">Jack Slocum&#8217;s DomQuery Standalone File (24 KB)</a> 1.0 Alpha 3 &#8211; Rev 4<br />
Download: <a href="/Projects/Y2/DomQuery-packer.js">Jack Slocum&#8217;s DomQuery Standalone File Packed (7 KB)</a> 1.0 Alpha 3 &#8211; Rev 4</p>
<p>If you want to use jQuery&#8217;s selector engine, I&#8217;ve stripped out the functions unnecessary to the selector engine in the 1.1.3.1 release and packaged it up as jDomQuery.  There are differences between these two package&#8217;s syntax, so make sure you keep that in mind, especially when looking at the pseudos, like :gt() and :lt() for example.</p>
<p>Download: <a href="/Projects/Y2/jdomquery-1.1.3.1.js">jDomQuery Standalone File (23 KB)</a> 1.1.3.1<br />
Download: <a href="/Projects/Y2/jdomquery-1.1.3.1-packer.js">jDomQuery Standalone File Packed (10 KB)</a> 1.1.3.1</p>
<p>If you want to read about jQuery syntax, <a href="http://docs.jquery.com/Selectors">go over to their documentation</a>.  My current preference is the jQuery syntax, given that it <a href="http://www.zachleat.com/web/2007/07/10/javascript-frameworks-and-jsf/">works with poorly implemented JSF ID attributes</a>.  But that&#8217;s a different argument.</p>
<p>If you want to use the full jQuery library, that should work too, but if you want to do that, you might just want to switch to using jQuery at that point (dare I say it).</p>
<p>Next, we&#8217;ll extend the YAHOO.util.Dom class into a new Y2 namespace.  This will map all of the functions from YAHOO.util.Dom into a new CSS Selector extension.</p>
<h1>Download: <a href="/Projects/Y2/Dom.js">Y2.util.Dom (2 KB)</a></h1>
<p>(pack at your own leisure, you can do it online at <a href="http://dean.edwards.name/packer/">Dean Edward&#8217;s Packer</a>)</p>
<h1>Putting It All Together</h1>

<div class="wp_syntax"><div class="code"><pre class="html" style="font-family:monospace;">&lt;html&gt;
	&lt;body&gt;
		&lt;div&gt;
			&lt;div class=&quot;first&quot;&gt;
				&lt;span&gt;&lt;/span&gt;
			&lt;/div&gt;
			&lt;div class=&quot;second&quot;&gt;
				&lt;b&gt;&lt;/b&gt;
			&lt;/div&gt;
		&lt;/div&gt;
		&lt;script type=&quot;text/javascript&quot; src=&quot;/Lib/yui/build/yahoo-dom-event/yahoo-dom-event.js&quot;&gt;&lt;/script&gt;
		&lt;script type=&quot;text/javascript&quot; src=&quot;jdomquery-1.1.3.1-packer.js&quot;&gt;&lt;/script&gt;
		&lt;!-- &lt;script type=&quot;text/javascript&quot; src=&quot;DomQuery-packer.js&quot;&gt; --&gt;
		&lt;script type=&quot;text/javascript&quot; src=&quot;Dom.js&quot;&gt;&lt;/script&gt;
		&lt;script type=&quot;text/javascript&quot;&gt;
		Y2.util.Dom.addClass( 'div div:last', 'myClass' )
		Y2.util.Dom.hasClass( 'div div:last', 'myClass' ); // returns true
		var b = Y2.util.Dom.get( 'div div:last b' );
		Y2.util.Dom.addClass( b, 'myOtherClass' );
		&lt;/script&gt;
	&lt;/body&gt;
&lt;/html&gt;</pre></div></div>

<p>One important thing to note is that when using the CSS Selector syntax, when only one node is selected, functions will return a scalar instead of an array.  So if you&#8217;re expecting to select multiple nodes in your selector query and only one node results, make sure your code handling the result is prepared for such an instance.</p>
<p>Have fun!</p>
]]></content:encoded>
			<wfw:commentRss>http://www.zachleat.com/web/using-dom-query-libraries-in-yui/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
	</channel>
</rss>

